Skip to content

Commit 3fbd395

Browse files
committed
Add StringEnumTests
1 parent 91ac442 commit 3fbd395

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using ChartJs.Blazor.ChartJS.Common.Enums;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Globalization;
5+
using Xunit;
6+
7+
namespace ChartJs.Blazor.Tests
8+
{
9+
public partial class StringEnumTests
10+
{
11+
[Theory]
12+
[InlineData("\"foo\"", "foo")]
13+
[InlineData("\"Hello World!\"", "Hello World!")]
14+
[InlineData("\"\\\"That's what!\\\", she said.\"", "\"That's what!\", she said.")]
15+
[InlineData("\"¨öä$ü¨^'{}][\\\\|/-.,+-/*\"", "¨öä$ü¨^'{}][\\|/-.,+-/*")]
16+
public void Deserialize_StringEnum_FromRoot(string json, string expectedValue)
17+
{
18+
// Act
19+
TestStringEnum stringEnum = JsonConvert.DeserializeObject<TestStringEnum>(json);
20+
21+
// Assert
22+
Assert.True(stringEnum.Equals(expectedValue)); // expects all the equality stuff to be correct
23+
}
24+
25+
[Fact]
26+
public void Deserialize_Int_ThrowsNotSupported()
27+
{
28+
// Arrange
29+
const string json = "0";
30+
31+
// Act & Assert
32+
Assert.Throws<NotSupportedException>(() => JsonConvert.DeserializeObject<TestStringEnum>(json));
33+
}
34+
35+
[Fact]
36+
public void Deserialize_Double_ThrowsNotSupported()
37+
{
38+
// Arrange
39+
const string json = "0.0";
40+
41+
// Act & Assert
42+
Assert.Throws<NotSupportedException>(() => JsonConvert.DeserializeObject<TestStringEnum>(json));
43+
}
44+
45+
[Fact]
46+
public void Deserialize_JsonArray_ThrowsNotSupported()
47+
{
48+
// Arrange
49+
const string json = "[1,2,3]";
50+
51+
// Act & Assert
52+
Assert.Throws<NotSupportedException>(() => JsonConvert.DeserializeObject<TestStringEnum>(json));
53+
}
54+
55+
[Fact]
56+
public void Deserialize_JsonObject_ThrowsNotSupported()
57+
{
58+
// Arrange
59+
string json = "{}";
60+
61+
// Act & Assert
62+
Assert.Throws<NotSupportedException>(() => JsonConvert.DeserializeObject<TestStringEnum>(json));
63+
}
64+
65+
[Fact]
66+
public void Deserialize_Null_ReturnsNull()
67+
{
68+
// Arrange
69+
string json = "null";
70+
71+
// Act
72+
TestStringEnum stringEnum = JsonConvert.DeserializeObject<TestStringEnum>(json);
73+
74+
// Assert
75+
Assert.Null(stringEnum);
76+
}
77+
78+
[Fact]
79+
public void Deserialize_Undefined_ReturnsNull()
80+
{
81+
// Arrange
82+
string json = "undefined";
83+
84+
// Act
85+
TestStringEnum stringEnum = JsonConvert.DeserializeObject<TestStringEnum>(json);
86+
87+
// Assert
88+
Assert.Null(stringEnum);
89+
}
90+
}
91+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using ChartJs.Blazor.ChartJS.Common.Enums;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Globalization;
5+
using Xunit;
6+
7+
namespace ChartJs.Blazor.Tests
8+
{
9+
public partial class StringEnumTests
10+
{
11+
[Fact]
12+
public void Equals_StringEnumAndStringEnum_ReturnsTrue()
13+
{
14+
// Arrange
15+
var a = TestStringEnum.Auto;
16+
var b = TestStringEnum.Auto; // different instance, same inner value
17+
18+
// Act
19+
bool equal = a.Equals(b);
20+
21+
// Assert
22+
Assert.True(equal);
23+
}
24+
25+
[Fact]
26+
public void Equals_StringEnumAndString_ReturnsTrue()
27+
{
28+
// Arrange
29+
const string ExampleStringValue = "abcdefg";
30+
var a = TestStringEnum.Custom(ExampleStringValue);
31+
32+
// Act
33+
bool equal = a.Equals(ExampleStringValue);
34+
35+
// Assert
36+
Assert.True(equal);
37+
}
38+
39+
[Fact]
40+
public void Equals_EnumAndNull_ReturnsFalse()
41+
{
42+
// Arrange
43+
var a = TestStringEnum.Custom(string.Empty);
44+
45+
// Act
46+
bool equal = a.Equals(null);
47+
48+
// Assert
49+
Assert.False(equal);
50+
}
51+
52+
[Fact]
53+
public void Equals_SameReference_ReturnsTrue()
54+
{
55+
// Arrange
56+
var a = TestStringEnum.Custom(string.Empty);
57+
var b = a;
58+
59+
// Act
60+
bool equal = a.Equals(b);
61+
62+
// Assert
63+
Assert.True(equal);
64+
}
65+
66+
[Fact]
67+
public void EqualityOperator_StringEnumAndStringEnum_ReturnsTrue()
68+
{
69+
// Arrange
70+
var a = TestStringEnum.Auto;
71+
var b = TestStringEnum.Auto; // different instance, same inner value
72+
73+
// Act
74+
bool equal = a == b;
75+
76+
// Assert
77+
Assert.True(equal);
78+
}
79+
80+
[Fact]
81+
public void EqualityOperator_StringEnumAndNull_ReturnsFalse()
82+
{
83+
// Arrange
84+
var a = TestStringEnum.Custom(string.Empty);
85+
86+
// Act
87+
bool equal = a == null;
88+
89+
// Assert
90+
Assert.False(equal);
91+
}
92+
93+
[Fact]
94+
public void EqualityOperator_NullAndNull_ReturnsTrue()
95+
{
96+
// Arrange
97+
TestStringEnum a = null;
98+
99+
// Act
100+
bool equal = a == null;
101+
102+
// Assert
103+
Assert.True(equal);
104+
}
105+
106+
[Fact]
107+
public void EqualityOperator_SameReference_ReturnsTrue()
108+
{
109+
// Arrange
110+
var a = TestStringEnum.Custom(string.Empty);
111+
var b = a;
112+
113+
// Act
114+
bool equal = a == b;
115+
116+
// Assert
117+
Assert.True(equal);
118+
}
119+
120+
[Fact]
121+
public void GetHashCode_InnerValueAndEnum_Equals()
122+
{
123+
// Arrange
124+
const string ExampleString = "asdf";
125+
TestStringEnum stringEnum = TestStringEnum.Custom(ExampleString);
126+
127+
// Act
128+
int hashCodeValue = ExampleString.GetHashCode();
129+
int hashCodeEnum = stringEnum.GetHashCode();
130+
131+
// Assert
132+
Assert.Equal(hashCodeValue, hashCodeEnum);
133+
}
134+
135+
[Fact]
136+
public void GetHashCode_EnumAndEnum_Equals()
137+
{
138+
// Arrange
139+
var a = TestStringEnum.Auto;
140+
var b = TestStringEnum.Auto; // different instance, same inner value
141+
142+
// Act
143+
int hashA = a.GetHashCode();
144+
int hashB = b.GetHashCode();
145+
146+
// Assert
147+
Assert.Equal(hashA, hashB);
148+
}
149+
}
150+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using ChartJs.Blazor.ChartJS.Common.Enums;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Globalization;
5+
using Xunit;
6+
7+
namespace ChartJs.Blazor.Tests
8+
{
9+
public partial class StringEnumTests
10+
{
11+
[Fact]
12+
public void Construct_NullValue_ThrowsArgumentNullException()
13+
{
14+
Assert.Throws<ArgumentNullException>(() => TestStringEnum.Custom(null));
15+
}
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using ChartJs.Blazor.ChartJS.Common.Enums;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Globalization;
5+
using Xunit;
6+
7+
namespace ChartJs.Blazor.Tests
8+
{
9+
public partial class StringEnumTests
10+
{
11+
[Theory]
12+
[InlineData("foo")]
13+
[InlineData("bar")]
14+
[InlineData("1234567890")]
15+
[InlineData("")]
16+
[InlineData("whomst'd've'ly'yaint'nt'ed'ies's'y'es")]
17+
[InlineData("\"That's what!\", she said.")]
18+
[InlineData("\uD83D\uDE42 emoji shenanigans")]
19+
[InlineData("¨öä$ü¨^'{}][\\|/-.,+-/*")]
20+
public void Serialize_StringEnum_AsRoot(string value)
21+
{
22+
// Arrange
23+
TestStringEnum objEnum = TestStringEnum.Custom(value);
24+
string escapedValue = JsonConvert.ToString(value);
25+
26+
// Act
27+
string serialized = JsonConvert.SerializeObject(objEnum);
28+
29+
// Assert
30+
Assert.Equal(escapedValue, serialized);
31+
}
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using ChartJs.Blazor.ChartJS.Common.Enums;
2+
using Newtonsoft.Json;
3+
using System;
4+
using System.Globalization;
5+
using Xunit;
6+
7+
namespace ChartJs.Blazor.Tests
8+
{
9+
public partial class StringEnumTests
10+
{
11+
private class TestStringEnum : StringEnum
12+
{
13+
public static TestStringEnum Auto => new TestStringEnum("auto");
14+
public static TestStringEnum Custom(string value) => new TestStringEnum(value);
15+
16+
private TestStringEnum(string stringRep) : base(stringRep) { }
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)