-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix enums used as table keys throwing exception (#53)
- Loading branch information
Showing
8 changed files
with
158 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using Tomlet.Tests.TestModelClasses; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Tomlet.Tests; | ||
|
||
public class EnumTests | ||
{ | ||
private readonly ITestOutputHelper _testOutputHelper; | ||
|
||
public EnumTests(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
public void CanSerializeEnum() | ||
{ | ||
var toml = TomletMain.TomlStringFrom(new {EnumValue = TestEnum.Value1}).Trim(); | ||
Assert.Equal("EnumValue = \"Value1\"", toml); | ||
} | ||
|
||
[Fact] | ||
public void SerializingAnUndefinedEnumValueThrows() | ||
{ | ||
var testObj = new {EnumValue = (TestEnum)4}; | ||
Assert.Throws<ArgumentException>(() => TomletMain.TomlStringFrom(testObj)); | ||
} | ||
|
||
[Fact] | ||
public void CanSerializeDictWithEnumValues() | ||
{ | ||
var testObj = new Dictionary<TestEnum, int> {{TestEnum.Value1, 1}, {TestEnum.Value2, 2}, {TestEnum.Value3, 3}}; | ||
var toml = TomletMain.TomlStringFrom(testObj); | ||
Assert.Equal("Value1 = 1\nValue2 = 2\nValue3 = 3\n", toml); | ||
} | ||
|
||
[Fact] | ||
public void CanSerializeNonInlinedClass() | ||
{ | ||
var testObj = new Dictionary<TestEnum, ClassWithDoNotInlineMembers> | ||
{ | ||
{ | ||
TestEnum.Value1, new ClassWithDoNotInlineMembers | ||
{ | ||
ShouldNotBeInlinedField = new Dictionary<string, string> {{"Key", "Value1"}} | ||
} | ||
}, | ||
}; | ||
var toml = TomletMain.TomlStringFrom(testObj); | ||
var expected = "[Value1]\nShouldBeInlined = { }\n[Value1.ShouldNotBeInlinedField]\nKey = \"Value1\"\n\n[Value1.ShouldNotBeInlinedProp]\n\n\n"; | ||
Assert.Equal(expected, toml); | ||
} | ||
|
||
[Fact] | ||
public void CanDeserializeEnumDictionary() | ||
{ | ||
var toml = "Value1 = 1\nValue2 = 2\nValue3 = 3\n"; | ||
var result = TomletMain.To<Dictionary<TestEnum, int>>(toml); | ||
var expected = new Dictionary<TestEnum, int> {{TestEnum.Value1, 1}, {TestEnum.Value2, 2}, {TestEnum.Value3, 3}}; | ||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void CanDeserializeEnumDictionaryWithFields() | ||
{ | ||
var toml = @" | ||
[Value1] | ||
a = 'A' | ||
b = 'B' | ||
"; | ||
var result = TomletMain.To<Dictionary<TestEnum, Subname>>(toml); | ||
var expected = new Dictionary<TestEnum, Subname> | ||
{ | ||
{TestEnum.Value1, new Subname {a = "A", b = "B"}}, | ||
}; | ||
|
||
Assert.Equal(expected, result); | ||
} | ||
|
||
[Fact] | ||
public void CanDeserializeEnumDictionaryInClass() | ||
{ | ||
var toml = @" | ||
[Subnames] | ||
[Subnames.Value1] | ||
a = 'A' | ||
b = 'B' | ||
"; | ||
var result = TomletMain.To<TomlTestClassWithEnumDict>(toml); | ||
var expected = new TomlTestClassWithEnumDict | ||
{ | ||
Subnames = new Dictionary<TestEnum, Subname> {{TestEnum.Value1, new Subname {a = "A", b = "B"}}}, | ||
}; | ||
|
||
Assert.Equal(expected.Subnames, result.Subnames); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
using System; | ||
|
||
namespace Tomlet.Tests.TestModelClasses; | ||
|
||
public class Subname | ||
{ | ||
public string a; | ||
public string b; | ||
|
||
protected bool Equals(Subname other) | ||
{ | ||
return a == other.a && b == other.b; | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
if (obj is null) return false; | ||
if (ReferenceEquals(this, obj)) return true; | ||
if (obj.GetType() != GetType()) return false; | ||
return Equals((Subname)obj); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return HashCode.Combine(a, b); | ||
} | ||
} |
File renamed without changes.
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,9 @@ | ||
using System.Collections.Generic; | ||
using Tomlet.Tests.TestModelClasses; | ||
|
||
namespace Tomlet.Tests; | ||
|
||
public class TomlTestClassWithEnumDict | ||
{ | ||
public Dictionary<TestEnum, Subname> Subnames; | ||
} |
File renamed without changes.
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