Skip to content

Commit 5287ded

Browse files
committed
Added generic class for serialization, that can be used in JSON conversion tests.
1 parent c9bb513 commit 5287ded

File tree

5 files changed

+37
-53
lines changed

5 files changed

+37
-53
lines changed

Enum.Ext/Enum.Ext.NewtonsoftJson.Tests/ConvertTests.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class ConvertTests
1111
[Test]
1212
public void Test_ConvertToJsonWithSerializerOptions()
1313
{
14-
var tempClass = new ClassToSerialize
14+
var tempClass = new ClassToSerialize<Weekday>
1515
{
16-
Weekday = Weekday.Monday
16+
Item = Weekday.Monday
1717
};
1818

1919
var serializerOptions = new JsonSerializerSettings
@@ -23,7 +23,7 @@ public void Test_ConvertToJsonWithSerializerOptions()
2323

2424
var json = JsonConvert.SerializeObject(tempClass, serializerOptions);
2525

26-
json.Should().Be("{\"Weekday\":1}");
26+
json.Should().Be("{\"Item\":1}");
2727
}
2828

2929
[Test]
@@ -34,61 +34,51 @@ public void Test_ConvertFromJsonWithSerializerOptions()
3434
Converters = { new JsonTypeSafeEnumConverter() }
3535
};
3636

37-
var tempClass = JsonConvert.DeserializeObject<ClassToSerialize>("{\"Weekday\":1}", serializerOptions);
37+
var tempClass = JsonConvert.DeserializeObject<ClassToSerialize<Weekday>>("{\"Item\":1}", serializerOptions);
3838

39-
tempClass.Weekday.Should().Be(Weekday.Monday);
39+
tempClass.Item.Should().Be(Weekday.Monday);
4040
}
4141

4242
[Test]
4343
public void Test_ConvertToJsonWithAnnotation()
4444
{
45-
var tempClass = new ClassToSerializeWithAnnotation
45+
var tempClass = new ClassToSerialize<WeekdayWithAnnotation>
4646
{
47-
Weekday = WeekdayWithAnnotation.Monday
47+
Item = WeekdayWithAnnotation.Monday
4848
};
4949

5050
var json = JsonConvert.SerializeObject(tempClass);
5151

52-
json.Should().Be("{\"Weekday\":1}");
52+
json.Should().Be("{\"Item\":1}");
5353
}
5454

5555
[Test]
5656
public void Test_ConvertFromJsonWithAnnotation()
5757
{
58-
var tempClass = JsonConvert.DeserializeObject<ClassToSerializeWithAnnotation>("{\"Weekday\":1}");
58+
var tempClass = JsonConvert.DeserializeObject<ClassToSerialize<WeekdayWithAnnotation>>("{\"Item\":1}");
5959

60-
tempClass.Weekday.Should().Be(WeekdayWithAnnotation.Monday);
60+
tempClass.Item.Should().Be(WeekdayWithAnnotation.Monday);
6161
}
6262

6363
[Test]
6464
public void Test_ConvertNullToJsonWithAnnotation()
6565
{
66-
var tempClass = new ClassToSerializeWithAnnotation
66+
var tempClass = new ClassToSerialize<WeekdayWithAnnotation>
6767
{
68-
Weekday = null
68+
Item = null
6969
};
7070

7171
var json = JsonConvert.SerializeObject(tempClass);
7272

73-
json.Should().Be("{\"Weekday\":null}");
73+
json.Should().Be("{\"Item\":null}");
7474
}
7575

7676
[Test]
7777
public void Test_ConvertToNullFromJsonValueWithAnnotation()
7878
{
79-
var tempClass = JsonConvert.DeserializeObject<ClassToSerializeWithAnnotation>("{\"Weekday\":null}");
79+
var tempClass = JsonConvert.DeserializeObject<ClassToSerialize<WeekdayWithAnnotation>>("{\"Item\":null}");
8080

81-
tempClass.Weekday.Should().Be(null);
82-
}
83-
84-
public class ClassToSerialize
85-
{
86-
public Weekday Weekday { get; set; }
87-
}
88-
89-
public class ClassToSerializeWithAnnotation
90-
{
91-
public WeekdayWithAnnotation? Weekday { get; set; }
81+
tempClass.Item.Should().Be(null);
9282
}
9383
}
9484
}

Enum.Ext/Enum.Ext.SystemTextJson.Tests/ConvertTests.cs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class ConvertTests
1212
[Test]
1313
public void Test_ConvertToJsonWithSerializerOptions()
1414
{
15-
var tempClass = new ClassToSerialize
15+
var tempClass = new ClassToSerialize<Weekday>
1616
{
17-
Weekday = Weekday.Monday
17+
Item = Weekday.Monday
1818
};
1919

2020
var serializerOptions = new JsonSerializerOptions
@@ -24,7 +24,7 @@ public void Test_ConvertToJsonWithSerializerOptions()
2424

2525
var json = JsonSerializer.Serialize(tempClass, serializerOptions);
2626

27-
json.Should().Be("{\"Weekday\":1}");
27+
json.Should().Be("{\"Item\":1}");
2828
}
2929

3030
[Test]
@@ -35,61 +35,51 @@ public void Test_ConvertFromJsonWithSerializerOptions()
3535
Converters = { new JsonTypeSafeEnumConverterFactory() }
3636
};
3737

38-
var tempClass = JsonSerializer.Deserialize<ClassToSerialize>("{\"Weekday\":1}", serializerOptions);
38+
var tempClass = JsonSerializer.Deserialize<ClassToSerialize<Weekday>>("{\"Item\":1}", serializerOptions);
3939

40-
tempClass.Weekday.Should().Be(Weekday.Monday);
40+
tempClass.Item.Should().Be(Weekday.Monday);
4141
}
4242

4343
[Test]
4444
public void Test_ConvertToJsonWithAnnotation()
4545
{
46-
var tempClass = new ClassToSerializeWithAnnotation
46+
var tempClass = new ClassToSerialize<WeekdayWithAnnotation>
4747
{
48-
Weekday = WeekdayWithAnnotation.Monday
48+
Item = WeekdayWithAnnotation.Monday
4949
};
5050

5151
var json = JsonSerializer.Serialize(tempClass);
5252

53-
json.Should().Be("{\"Weekday\":1}");
53+
json.Should().Be("{\"Item\":1}");
5454
}
5555

5656
[Test]
5757
public void Test_ConvertFromJsonWithAnnotation()
5858
{
59-
var tempClass = JsonSerializer.Deserialize<ClassToSerializeWithAnnotation>("{\"Weekday\":1}");
59+
var tempClass = JsonSerializer.Deserialize<ClassToSerialize<WeekdayWithAnnotation>>("{\"Item\":1}");
6060

61-
tempClass.Weekday.Should().Be(WeekdayWithAnnotation.Monday);
61+
tempClass.Item.Should().Be(WeekdayWithAnnotation.Monday);
6262
}
6363

6464
[Test]
6565
public void Test_ConvertNullToJsonWithAnnotation()
6666
{
67-
var tempClass = new ClassToSerializeWithAnnotation
67+
var tempClass = new ClassToSerialize<Weekday>
6868
{
69-
Weekday = null
69+
Item = null
7070
};
7171

7272
var json = JsonSerializer.Serialize(tempClass);
7373

74-
json.Should().Be("{\"Weekday\":null}");
74+
json.Should().Be("{\"Item\":null}");
7575
}
7676

7777
[Test]
7878
public void Test_ConvertToNullFromJsonValueWithAnnotation()
7979
{
80-
var tempClass = JsonSerializer.Deserialize<ClassToSerializeWithAnnotation>("{\"Weekday\":null}");
80+
var tempClass = JsonSerializer.Deserialize<ClassToSerialize<WeekdayWithAnnotation>>("{\"Item\":null}");
8181

82-
tempClass.Weekday.Should().Be(null);
83-
}
84-
85-
public class ClassToSerialize
86-
{
87-
public Weekday Weekday { get; set; }
88-
}
89-
90-
public class ClassToSerializeWithAnnotation
91-
{
92-
public WeekdayWithAnnotation Weekday { get; set; }
82+
tempClass.Item.Should().Be(null);
9383
}
9484
}
9585
}

Enum.Ext/Enum.Ext.SystemTextJson.Tests/WeekdayWithAnnotation.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Enum.Ext.SystemTextJson;
2-
using Enum.Ext.Tests.Shared;
32

43
using System.Text.Json.Serialization;
54

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Enum.Ext.Tests.Shared
2+
{
3+
public class ClassToSerialize<T> where T : TypeSafeEnum<T, int>
4+
{
5+
public T Item { get; set; }
6+
}
7+
}

Enum.Ext/Enum.Ext.Tests/YearlyPriceTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using FluentAssertions;
22
using NUnit.Framework;
33
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
64

75
namespace Enum.Ext.Tests
86
{

0 commit comments

Comments
 (0)