public void NullValues() { var str = Union <string, Version> .Create((string)null); var ver = Union <string, Version> .Create((Version)null); Assert.True(str.Equals(null)); Assert.True(ver.Equals(null)); Assert.Equal(null, str.ToString()); Assert.Equal(null, ver.ToString()); Assert.Equal(0, str.GetHashCode()); Assert.Equal(0, ver.GetHashCode()); Assert.Equal(typeof(string), str.Type); Assert.Equal(typeof(Version), ver.Type); Assert.Null(str.Value); Assert.Null(ver.Value); Assert.Equal( Union <int, string> .Create(null), Union <int, string> .Create(null)); }
public void ExplicitCastToValue() { Assert.Equal(1, (int)Union <int, double> .Create(1)); Assert.Equal(1.0, (double)Union <int, double> .Create(1.0)); Assert.Throws <InvalidCastException>(() => (double)Union <int, double> .Create(1)); Assert.Throws <InvalidCastException>(() => (int)Union <int, double> .Create(1.0)); }
public void Create() { Union <int, double> .Create(123).Match( i => Assert.Equal(123, i), d => { throw new Exception("Value should not be a double"); }); Union <int, double> .Create(123.0).Match( i => { throw new Exception("Value should not be an int"); }, d => Assert.Equal(123.0, d)); }
public void SupportsNestedUnion() { TestNested(Union <int, double> .Create(123), packer => packer.PackArrayHeader(2).Pack("Int32").Pack(123)); TestNested(Union <int, double> .Create(123.0), packer => packer.PackArrayHeader(2).Pack("Double").Pack(123.0)); TestNested(Union <int, string> .Create("Hello"), packer => packer.PackArrayHeader(2).Pack("String").Pack("Hello")); TestNested(Union <int, string> .Create(null), packer => packer.PackArrayHeader(2).Pack("String").PackNull()); TestNested((Union <int, string>)null, packer => packer.PackNull()); TestNested(Union <TaggedA, TaggedB> .Create(new TaggedA(123)), packer => packer.PackArrayHeader(2).Pack("A").PackMapHeader(1).Pack("Number").Pack(123)); TestNested(Union <TaggedA, TaggedB> .Create(new TaggedB(123)), packer => packer.PackArrayHeader(2).Pack("B").PackMapHeader(1).Pack("Number").Pack(123)); }
public void Equals() { Assert.Equal(1, (Union <int, double>) 1); Assert.Equal(1.0, (Union <int, double>) 1.0); Assert.Equal( Union <int, double> .Create(1), Union <int, double> .Create(1)); Assert.Equal( Union <int, string> .Create(null), Union <int, string> .Create(null)); }
public void SupportsTopLevelUnion() { // Top level unions are allowed, so long as each type within the union meets the requirements of a top-level type TestTopLevel( Union <UserScore, UserScoreStruct> .Create(new UserScore("Bob", 123)), packer => packer.PackArrayHeader(2).Pack("Dasher.Tests.UserScore").PackMapHeader(2).Pack("Name").Pack("Bob").Pack("Score").Pack(123)); TestTopLevel( Union <UserScore, UserScoreStruct> .Create(new UserScoreStruct("Bob", 123)), packer => packer.PackArrayHeader(2).Pack("Dasher.Tests.UserScoreStruct").PackMapHeader(2).Pack("Name").Pack("Bob").Pack("Score").Pack(123)); TestTopLevel( Union <UserScore, UserScoreStruct> .Create(null), packer => packer.PackArrayHeader(2).Pack("Dasher.Tests.UserScore").PackNull()); TestTopLevel((Union <UserScore, UserScoreStruct>)null, packer => packer.PackNull()); TestTopLevel(Union <TaggedA, TaggedB> .Create(new TaggedA(123)), packer => packer.PackArrayHeader(2).Pack("A").PackMapHeader(1).Pack("Number").Pack(123)); TestTopLevel(Union <TaggedA, TaggedB> .Create(new TaggedB(123)), packer => packer.PackArrayHeader(2).Pack("B").PackMapHeader(1).Pack("Number").Pack(123)); }
public void Type() { Assert.Equal(typeof(int), Union <int, double> .Create(1).Type); Assert.Equal(typeof(double), Union <int, double> .Create(1.0).Type); }
public void GetHashCodeTest() { Assert.Equal(1.GetHashCode(), Union <int, double> .Create(1).GetHashCode()); Assert.Equal(1.0.GetHashCode(), Union <int, double> .Create(1.0).GetHashCode()); }
public void ToStringTest() { Assert.Equal("1", Union <int, string> .Create(1).ToString()); Assert.Equal("Hello", Union <int, string> .Create("Hello").ToString()); }