-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Serializing dictionary with ', ", . in key.
Fixes issue where GetTopLevelAndSubKeys splits a key when serializing. Adds test to handle new cases. Does not test cases with mixed " and ' due to issue explained in #57
- Loading branch information
Showing
3 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
using System.Collections.Generic; | ||
using Tomlet.Tests.TestModelClasses; | ||
using Xunit; | ||
|
||
namespace Tomlet.Tests | ||
{ | ||
public class SerializeSpecialKeysTests | ||
{ | ||
void AssertEqual(Dictionary<string, string> dictionary, Dictionary<string, string> other) | ||
{ | ||
Assert.Equal(dictionary.Count, other.Count); | ||
foreach (var (key, value) in dictionary) | ||
{ | ||
var otherValue = Assert.Contains(key, (IDictionary<string, string>)other); | ||
Assert.Equal(value, otherValue); | ||
} | ||
} | ||
|
||
|
||
[Fact] | ||
public void NoSpecialKeys() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "SomeKey", "SomeValue" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom(dict); | ||
var otherDict = TomletMain.To<Dictionary<string, string>>(tomlString); | ||
AssertEqual(dict, otherDict); | ||
} | ||
|
||
[Fact] | ||
public void DottedKey() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "Some.Key", "Some.Value" }, | ||
{ "Some.", "Some." }, | ||
{ ".Key", ".Value" }, | ||
{ ".", "." }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom(dict); | ||
var otherDict = TomletMain.To<Dictionary<string, string>>(tomlString); | ||
AssertEqual(dict, otherDict); | ||
} | ||
|
||
[Fact] | ||
public void QuotedKey() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "'SomeKey'", "'SomeValue'" }, | ||
{ "\"SomeKey\"", "\"SomeValue\"" }, | ||
{ "\"", "\"" }, | ||
{ "'", "'" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom(dict); | ||
var otherDict = TomletMain.To<Dictionary<string, string>>(tomlString); | ||
AssertEqual(dict, otherDict); | ||
} | ||
|
||
[Fact] | ||
public void QuotedDottedKey() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "'Some.Key'", "'Some.Value'" }, | ||
{ "\"Some.Key\"", "\"Some.Value\"" }, | ||
{ "'Some'.Key", "'Some'.Value" }, | ||
{ "\"Some\".Key", "\"Some\".Value" }, | ||
{ "Some.'Key'", "Some.'Value'" }, | ||
{ "Some.\"Key\"", "Some.\"Value\"" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom(dict); | ||
var otherDict = TomletMain.To<Dictionary<string, string>>(tomlString); | ||
AssertEqual(dict, otherDict); | ||
} | ||
|
||
[Fact] | ||
public void Brackets() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "[SomeKey]", "[SomeValue]" }, | ||
{ "[SomeKey\"", "[SomeValue\"" }, | ||
{ "[SomeKey", "[SomeValue" }, | ||
{ "SomeKey]", "SomeValue]" }, | ||
{ "[", "]" }, | ||
{ "]", "]" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom(dict); | ||
var otherDict = TomletMain.To<Dictionary<string, string>>(tomlString); | ||
AssertEqual(dict, otherDict); | ||
} | ||
|
||
[Fact] | ||
public void NoSpecialKeysWithClass() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "SomeKey", "SomeValue" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom( | ||
new ClassWithDictionary | ||
{ | ||
GenericDictionary = dict, | ||
} | ||
); | ||
var otherClass = TomletMain.To<ClassWithDictionary>(tomlString); | ||
AssertEqual(dict, otherClass.GenericDictionary); | ||
} | ||
|
||
[Fact] | ||
public void DottedKeyWithClass() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "Some.Key", "Some.Value" }, | ||
{ "Some.", "Some." }, | ||
{ ".Key", ".Value" }, | ||
{ ".", "." }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom( | ||
new ClassWithDictionary | ||
{ | ||
GenericDictionary = dict, | ||
} | ||
); | ||
var otherClass = TomletMain.To<ClassWithDictionary>(tomlString); | ||
AssertEqual(dict, otherClass.GenericDictionary); | ||
} | ||
|
||
[Fact] | ||
public void QuotedKeyWithClass() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "'SomeKey'", "'SomeValue'" }, | ||
{ "\"SomeKey\"", "\"SomeValue\"" }, | ||
{ "\"", "\"" }, | ||
{ "'", "'" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom( | ||
new ClassWithDictionary | ||
{ | ||
GenericDictionary = dict, | ||
} | ||
); | ||
var otherClass = TomletMain.To<ClassWithDictionary>(tomlString); | ||
AssertEqual(dict, otherClass.GenericDictionary); | ||
} | ||
|
||
[Fact] | ||
public void QuotedDottedKeyWithClass() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "'Some.Key'", "'Some.Value'" }, | ||
{ "\"Some.Key\"", "\"Some.Value\"" }, | ||
{ "'Some'.Key", "'Some'.Value" }, | ||
{ "\"Some\".Key", "\"Some\".Value" }, | ||
{ "Some.'Key'", "Some.'Value'" }, | ||
{ "Some.\"Key\"", "Some.\"Value\"" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom( | ||
new ClassWithDictionary | ||
{ | ||
GenericDictionary = dict, | ||
} | ||
); | ||
var otherClass = TomletMain.To<ClassWithDictionary>(tomlString); | ||
AssertEqual(dict, otherClass.GenericDictionary); | ||
} | ||
|
||
[Fact] | ||
public void BracketsWithClass() | ||
{ | ||
var dict = new Dictionary<string, string> | ||
{ | ||
{ "[SomeKey]", "[SomeValue]" }, | ||
{ "[SomeKey\"", "[SomeValue\"" }, | ||
{ "[SomeKey", "[SomeValue" }, | ||
{ "SomeKey]", "SomeValue]" }, | ||
{ "[", "]" }, | ||
{ "]", "]" }, | ||
}; | ||
var tomlString = TomletMain.TomlStringFrom( | ||
new ClassWithDictionary | ||
{ | ||
GenericDictionary = dict, | ||
} | ||
); | ||
var otherClass = TomletMain.To<ClassWithDictionary>(tomlString); | ||
AssertEqual(dict, otherClass.GenericDictionary); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters