Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rickyah committed Jun 13, 2015
2 parents 39a3336 + 352c3e0 commit d40db0c
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 56 deletions.
1 change: 1 addition & 0 deletions src/IniFileParser.Tests/INIFileParser.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="..\IniFileParser\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="Unit\Model\KeyDataCollectionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IniFileParser\INIFileParser.csproj">
Expand Down
32 changes: 32 additions & 0 deletions src/IniFileParser.Tests/Unit/Model/KeyDataCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using IniParser.Model;
using NUnit.Framework;

namespace IniFileParser.Tests.Unit.Model
{
[TestFixture, Category("Test of data structures used to hold information retrieved for an INI file")]
public class KeyDataCollectionTests
{
[Test]
public void test()
{
var col = new KeyDataCollection();
col.AddKey("key1");

Assert.That(col["key1"], Is.Empty);


col.AddKey("key2", "value2");

Assert.That(col["key2"], Is.EqualTo("value2"));

var keyData = new KeyData("key3");
keyData.Value = "value3";
col.AddKey(keyData);

Assert.That(col["key3"], Is.EqualTo("value3"));
}
}

}
20 changes: 20 additions & 0 deletions src/IniFileParser.Tests/Unit/Model/SectionDataCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,25 @@ public void remove_all_keys_in_section_without_deleting_the_section()
Assert.That(data["test2"].ContainsKey("key4"), Is.False);

}

[Test]
public void check_adding_sections_to_collection()
{
var col = new SectionDataCollection();

var exampleSection = new SectionData("section1");
exampleSection.Keys.AddKey("examplekey");
exampleSection.Keys["examplekey"] = "examplevalue";

col.Add(exampleSection);

Assert.That(col["section1"], Is.Not.Null);

// Add sections directly to the collection
Assert.That(col.AddSection("section2"), Is.True);
Assert.That(col.AddSection("section2"), Is.False);

Assert.That(col["section2"], Is.Not.Null);
}
}
}
121 changes: 67 additions & 54 deletions src/IniFileParser/Model/KeyDataCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,44 @@
namespace IniParser.Model
{
/// <summary>
/// <para>Represents a collection of Keydata.</para>
/// Represents a collection of Keydata.
/// </summary>
public class KeyDataCollection : ICloneable, IEnumerable<KeyData>
{
IEqualityComparer<string> _searchComparer;
#region Initialization

/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
/// </summary>
public KeyDataCollection()
:this(EqualityComparer<string>.Default)
{}

/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class.
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class with a given
/// search comparer
/// </summary>
/// <param name="searchComparer">
/// Search comparer used to find the key by name in the collection
/// </param>
public KeyDataCollection(IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer;
_keyData = new Dictionary<string, KeyData>(_searchComparer);
}

/// <summary>
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class
/// from a previous instance of <see cref="KeyDataCollection"/>.
/// Initializes a new instance of the <see cref="KeyDataCollection"/> class
/// from a previous instance of <see cref="KeyDataCollection"/>.
/// </summary>
/// <remarks>
/// Data is deeply copied
/// Data from the original KeyDataCollection instance is deeply copied
/// </remarks>
/// <param name="ori">
/// The instance of the <see cref="KeyDataCollection"/> class
/// used to create the new instance.</param>
/// The instance of the <see cref="KeyDataCollection"/> class
/// used to create the new instance.
/// </param>
public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> searchComparer)
: this(searchComparer)
{
Expand All @@ -57,17 +62,19 @@ public KeyDataCollection(KeyDataCollection ori, IEqualityComparer<string> search
#endregion

#region Properties
/// <summary>
/// Gets or sets the value of a concrete key.

/// <summary>
/// Gets or sets the value of a concrete key.
/// </summary>
/// <remarks>
/// If we try to assign the value of a key which doesn't exists,
/// a new key is added with the name and the value is assigned to it.
/// If we try to assign the value of a key which doesn't exists,
/// a new key is added with the name and the value is assigned to it.
/// </remarks>
/// <param name="keyName">Name of the key</param>
/// <param name="keyName">
/// Name of the key
/// </param>
/// <returns>
/// The string with key's value or null
/// if the key was not found.
/// The string with key's value or null if the key was not found.
/// </returns>
public string this[string keyName]
{
Expand All @@ -92,9 +99,8 @@ public string this[string keyName]
}

/// <summary>
/// Return the number of keys in the collection
/// Return the number of keys in the collection
/// </summary>
/// <value>An integer with the number of keys in the collection.</value>
public int Count
{
get { return _keyData.Count; }
Expand All @@ -105,17 +111,14 @@ public int Count
#region Operations

/// <summary>
/// Adds a new key with the specified name and empty value and comments
/// Adds a new key with the specified name and empty value and comments
/// </summary>
/// <remarks>
/// A valid key name is a string with NO blank spaces.
/// </remarks>
/// <param name="keyName">New key to be added.</param>
/// <returns>
/// <c>true</c> if a new empty key was added
/// <c>false</c> otherwise.
/// <param name="keyName">
/// New key to be added.
/// </param>
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection
/// </returns>
/// <exception cref="ArgumentException">If the key name is not valid.</exception>
public bool AddKey(string keyName)
{
if ( !_keyData.ContainsKey(keyName) )
Expand All @@ -127,21 +130,12 @@ public bool AddKey(string keyName)
return false;
}

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

/// <summary>
/// Adds a new key with the specified name and value and comments
/// Adds a new key to the collection
/// </summary>
/// <remarks>
/// A valid key name is a string with NO blank spaces.
/// </remarks>
/// <param name="keyName">New key to be added.</param>
/// <param name="keyValue">Value associated to the kyy.</param>
/// <param name="keyData">
/// KeyData instance.
/// </param>
/// <returns>
/// <c>true</c> if a new empty key was added
/// <c>false</c> otherwise.
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection
/// </returns>
public bool AddKey(KeyData keyData)
{
if (AddKey(keyData.KeyName))
{
_keyData[keyData.KeyName] = keyData;
return true;
}

return false;
}
/// <summary>
/// Adds a new key with the specified name and value to the collection
/// </summary>
/// <param name="keyName">
/// Name of the new key to be added.
/// </param>
/// <param name="keyValue">
/// Value associated to the key.
/// </param>
/// <returns>
/// <c>true</c> if the key was added <c>false</c> if a key with the same name already exist
/// in the collection.
/// </returns>
/// <exception cref="ArgumentException">If the key name is not valid.</exception>
public bool AddKey(string keyName, string keyValue)
{
if (AddKey(keyName))
Expand Down Expand Up @@ -251,13 +265,12 @@ public bool RemoveKey(string keyName)
/// <param name="data">The new <see cref="KeyData"/> for the key.</param>
public void SetKeyData(KeyData data)
{
if (data != null)
{
if (_keyData.ContainsKey(data.KeyName))
RemoveKey(data.KeyName);
if (data == null) return;

AddKey(data.KeyName, data);
}
if (_keyData.ContainsKey(data.KeyName))
RemoveKey(data.KeyName);

AddKey(data);
}

#endregion
Expand Down
3 changes: 3 additions & 0 deletions src/IniFileParser/Model/SectionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ public SectionData(string sectionName, IEqualityComparer<string> searchComparer)
/// </param>
public SectionData(SectionData ori, IEqualityComparer<string> searchComparer = null)
{
SectionName = ori.SectionName;

_searchComparer = searchComparer;
_leadingComments = new List<string>(ori._leadingComments);
_keyDataCollection = new KeyDataCollection(ori._keyDataCollection, searchComparer ?? ori._searchComparer);
}
Expand Down
4 changes: 2 additions & 2 deletions src/IniFileParser/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@
//
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("2.2.1")]
[assembly: AssemblyFileVersion("2.2.1")]
[assembly: AssemblyVersion("2.2.2")]
[assembly: AssemblyFileVersion("2.2.2")]

0 comments on commit d40db0c

Please sign in to comment.