Skip to content

Commit d40db0c

Browse files
committed
Merge branch 'development'
* development: Bump version to 2.2.2 Cleaned up docs Marked AddKey(string, KeyData) method obsolete; use AddKey(KeyData) Add tests to prevent similar bugs as found in #83 when adding keys Fixes #83
2 parents 39a3336 + 352c3e0 commit d40db0c

File tree

6 files changed

+125
-56
lines changed

6 files changed

+125
-56
lines changed

src/IniFileParser.Tests/INIFileParser.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="..\IniFileParser\Properties\AssemblyInfo.cs">
8383
<Link>Properties\AssemblyInfo.cs</Link>
8484
</Compile>
85+
<Compile Include="Unit\Model\KeyDataCollectionTests.cs" />
8586
</ItemGroup>
8687
<ItemGroup>
8788
<ProjectReference Include="..\IniFileParser\INIFileParser.csproj">
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using IniParser.Model;
4+
using NUnit.Framework;
5+
6+
namespace IniFileParser.Tests.Unit.Model
7+
{
8+
[TestFixture, Category("Test of data structures used to hold information retrieved for an INI file")]
9+
public class KeyDataCollectionTests
10+
{
11+
[Test]
12+
public void test()
13+
{
14+
var col = new KeyDataCollection();
15+
col.AddKey("key1");
16+
17+
Assert.That(col["key1"], Is.Empty);
18+
19+
20+
col.AddKey("key2", "value2");
21+
22+
Assert.That(col["key2"], Is.EqualTo("value2"));
23+
24+
var keyData = new KeyData("key3");
25+
keyData.Value = "value3";
26+
col.AddKey(keyData);
27+
28+
Assert.That(col["key3"], Is.EqualTo("value3"));
29+
}
30+
}
31+
32+
}

src/IniFileParser.Tests/Unit/Model/SectionDataCollectionTests.cs

+20
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,25 @@ public void remove_all_keys_in_section_without_deleting_the_section()
7979
Assert.That(data["test2"].ContainsKey("key4"), Is.False);
8080

8181
}
82+
83+
[Test]
84+
public void check_adding_sections_to_collection()
85+
{
86+
var col = new SectionDataCollection();
87+
88+
var exampleSection = new SectionData("section1");
89+
exampleSection.Keys.AddKey("examplekey");
90+
exampleSection.Keys["examplekey"] = "examplevalue";
91+
92+
col.Add(exampleSection);
93+
94+
Assert.That(col["section1"], Is.Not.Null);
95+
96+
// Add sections directly to the collection
97+
Assert.That(col.AddSection("section2"), Is.True);
98+
Assert.That(col.AddSection("section2"), Is.False);
99+
100+
Assert.That(col["section2"], Is.Not.Null);
101+
}
82102
}
83103
}

src/IniFileParser/Model/KeyDataCollection.cs

+67-54
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,44 @@
55
namespace IniParser.Model
66
{
77
/// <summary>
8-
/// <para>Represents a collection of Keydata.</para>
8+
/// Represents a collection of Keydata.
99
/// </summary>
1010
public class KeyDataCollection : ICloneable, IEnumerable<KeyData>
1111
{
1212
IEqualityComparer<string> _searchComparer;
1313
#region Initialization
1414

1515
/// <summary>
16-
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
16+
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
1717
/// </summary>
1818
public KeyDataCollection()
1919
:this(EqualityComparer<string>.Default)
2020
{}
2121

2222
/// <summary>
23-
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
23+
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class with a given
24+
/// search comparer
2425
/// </summary>
26+
/// <param name="searchComparer">
27+
/// Search comparer used to find the key by name in the collection
28+
/// </param>
2529
public KeyDataCollection(IEqualityComparer<string> searchComparer)
2630
{
2731
_searchComparer = searchComparer;
2832
_keyData = new Dictionary<string, KeyData>(_searchComparer);
2933
}
3034

3135
/// <summary>
32-
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class
33-
/// from a previous instance of <see cref="KeyDataCollection"/>.
36+
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class
37+
/// from a previous instance of <see cref="KeyDataCollection"/>.
3438
/// </summary>
3539
/// <remarks>
36-
/// Data is deeply copied
40+
/// Data from the original KeyDataCollection instance is deeply copied
3741
/// </remarks>
3842
/// <param name="ori">
39-
/// The instance of the <see cref="KeyDataCollection"/> class
40-
/// used to create the new instance.</param>
43+
/// The instance of the <see cref="KeyDataCollection"/> class
44+
/// used to create the new instance.
45+
/// </param>
4146
public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> searchComparer)
4247
: this(searchComparer)
4348
{
@@ -57,17 +62,19 @@ public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> search
5762
#endregion
5863

5964
#region Properties
60-
/// <summary>
61-
/// Gets or sets the value of a concrete key.
65+
66+
/// <summary>
67+
/// Gets or sets the value of a concrete key.
6268
/// </summary>
6369
/// <remarks>
64-
/// If we try to assign the value of a key which doesn't exists,
65-
/// a new key is added with the name and the value is assigned to it.
70+
/// If we try to assign the value of a key which doesn't exists,
71+
/// a new key is added with the name and the value is assigned to it.
6672
/// </remarks>
67-
/// <param name="keyName">Name of the key</param>
73+
/// <param name="keyName">
74+
/// Name of the key
75+
/// </param>
6876
/// <returns>
69-
/// The string with key's value or null
70-
/// if the key was not found.
77+
/// The string with key's value or null if the key was not found.
7178
/// </returns>
7279
public string this[string keyName]
7380
{
@@ -92,9 +99,8 @@ public string this[string keyName]
9299
}
93100

94101
/// <summary>
95-
/// Return the number of keys in the collection
102+
/// Return the number of keys in the collection
96103
/// </summary>
97-
/// <value>An integer with the number of keys in the collection.</value>
98104
public int Count
99105
{
100106
get { return _keyData.Count; }
@@ -105,17 +111,14 @@ public int Count
105111
#region Operations
106112

107113
/// <summary>
108-
/// Adds a new key with the specified name and empty value and comments
114+
/// Adds a new key with the specified name and empty value and comments
109115
/// </summary>
110-
/// <remarks>
111-
/// A valid key name is a string with NO blank spaces.
112-
/// </remarks>
113-
/// <param name="keyName">New key to be added.</param>
114-
/// <returns>
115-
/// <c>true</c> if a new empty key was added
116-
/// <c>false</c> otherwise.
116+
/// <param name="keyName">
117+
/// New key to be added.
118+
/// </param>
119+
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
120+
/// in the collection
117121
/// </returns>
118-
/// <exception cref="ArgumentException">If the key name is not valid.</exception>
119122
public bool AddKey(string keyName)
120123
{
121124
if ( !_keyData.ContainsKey(keyName) )
@@ -127,21 +130,12 @@ public bool AddKey(string keyName)
127130
return false;
128131
}
129132

130-
/// <summary>
131-
/// Adds a new key with the specified name and value and comments
132-
/// </summary>
133-
/// <remarks>
134-
/// A valid key name is a string with NO blank spaces.
135-
/// </remarks>
136-
/// <param name="keyName">New key to be added.</param>
137-
/// <param name="keyData">KeyData instance.</param>
138-
/// <returns>
139-
/// <c>true</c> if a new empty key was added
140-
/// <c>false</c> otherwise.
141-
/// </returns>
142-
/// <exception cref="ArgumentException">If the key name is not valid.</exception>
133+
[Obsolete("Pottentially buggy method! Use AddKey(KeyData keyData) instead (See comments in code for an explanation of the bug)")]
143134
public bool AddKey(string keyName, KeyData keyData)
144135
{
136+
// BUG: this actually can allow you to add the keyData having
137+
// keyData.KeyName different from the argument 'keyName' in this method
138+
// which doesn't make any sense
145139
if (AddKey(keyName))
146140
{
147141
_keyData[keyName] = keyData;
@@ -153,18 +147,38 @@ public bool AddKey(string keyName, KeyData keyData)
153147
}
154148

155149
/// <summary>
156-
/// Adds a new key with the specified name and value and comments
150+
/// Adds a new key to the collection
157151
/// </summary>
158-
/// <remarks>
159-
/// A valid key name is a string with NO blank spaces.
160-
/// </remarks>
161-
/// <param name="keyName">New key to be added.</param>
162-
/// <param name="keyValue">Value associated to the kyy.</param>
152+
/// <param name="keyData">
153+
/// KeyData instance.
154+
/// </param>
163155
/// <returns>
164-
/// <c>true</c> if a new empty key was added
165-
/// <c>false</c> otherwise.
156+
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
157+
/// in the collection
158+
/// </returns>
159+
public bool AddKey(KeyData keyData)
160+
{
161+
if (AddKey(keyData.KeyName))
162+
{
163+
_keyData[keyData.KeyName] = keyData;
164+
return true;
165+
}
166+
167+
return false;
168+
}
169+
/// <summary>
170+
/// Adds a new key with the specified name and value to the collection
171+
/// </summary>
172+
/// <param name="keyName">
173+
/// Name of the new key to be added.
174+
/// </param>
175+
/// <param name="keyValue">
176+
/// Value associated to the key.
177+
/// </param>
178+
/// <returns>
179+
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
180+
/// in the collection.
166181
/// </returns>
167-
/// <exception cref="ArgumentException">If the key name is not valid.</exception>
168182
public bool AddKey(string keyName, string keyValue)
169183
{
170184
if (AddKey(keyName))
@@ -251,13 +265,12 @@ public bool RemoveKey(string keyName)
251265
/// <param name="data">The new <see cref="KeyData"/> for the key.</param>
252266
public void SetKeyData(KeyData data)
253267
{
254-
if (data != null)
255-
{
256-
if (_keyData.ContainsKey(data.KeyName))
257-
RemoveKey(data.KeyName);
268+
if (data == null) return;
258269

259-
AddKey(data.KeyName, data);
260-
}
270+
if (_keyData.ContainsKey(data.KeyName))
271+
RemoveKey(data.KeyName);
272+
273+
AddKey(data);
261274
}
262275

263276
#endregion

src/IniFileParser/Model/SectionData.cs

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public SectionData(string sectionName, IEqualityComparer<string> searchComparer)
4949
/// </param>
5050
public SectionData(SectionData ori, IEqualityComparer<string> searchComparer = null)
5151
{
52+
SectionName = ori.SectionName;
53+
54+
_searchComparer = searchComparer;
5255
_leadingComments = new List<string>(ori._leadingComments);
5356
_keyDataCollection = new KeyDataCollection(ori._keyDataCollection, searchComparer ?? ori._searchComparer);
5457
}

src/IniFileParser/Properties/AssemblyInfo.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@
3030
//
3131
// You can specify all the values or you can default the Revision and Build Numbers
3232
// by using the '*' as shown below:
33-
[assembly: AssemblyVersion("2.2.1")]
34-
[assembly: AssemblyFileVersion("2.2.1")]
33+
[assembly: AssemblyVersion("2.2.2")]
34+
[assembly: AssemblyFileVersion("2.2.2")]

0 commit comments

Comments
 (0)