-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionBasedTests.JsonDateTimeFormat.cs
More file actions
107 lines (100 loc) · 5.66 KB
/
ReflectionBasedTests.JsonDateTimeFormat.cs
File metadata and controls
107 lines (100 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Text.Json;
using Scarlet.System.Text.Json.DateTimeConverter.Tests.Model;
namespace Scarlet.System.Text.Json.DateTimeConverter.Tests;
public partial class ReflectionBasedTests
{
[Fact]
public void ReflectionBased_WithoutResolver_WithFormatAttribute_DoesNotApplyFormat()
{
// Arrange
var options = new JsonSerializerOptions
{
WriteIndented = true
};
var originalModel = new SourceGeneratorModelWithAttribute
{
DateTimeProperty = new DateTime(2023, 10, 1, 12, 0, 0, DateTimeKind.Utc),
NullableDateTimeProperty = new DateTime(2023, 10, 1, 12, 0, 0, DateTimeKind.Utc),
DateTimeOffsetProperty = new DateTimeOffset(2023, 10, 1, 12, 0, 0, TimeSpan.Zero),
NullableDateTimeOffsetProperty = new DateTimeOffset(2023, 10, 1, 12, 0, 0, TimeSpan.Zero),
DateOnlyProperty = new DateOnly(2023, 10, 1),
NullableDateOnlyProperty = new DateOnly(2023, 10, 1),
TimeOnlyProperty = new TimeOnly(14, 30, 45),
NullableTimeOnlyProperty = new TimeOnly(14, 30, 45)
};
// Expected JSON with DEFAULT formats (not the custom formats from JsonDateTimeFormat attribute)
const string expectedJson = """
{
"DateTimeProperty": "2023-10-01T12:00:00Z",
"NullableDateTimeProperty": "2023-10-01T12:00:00Z",
"DateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
"NullableDateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
"DateOnlyProperty": "2023-10-01",
"NullableDateOnlyProperty": "2023-10-01",
"TimeOnlyProperty": "14:30:45",
"NullableTimeOnlyProperty": "14:30:45"
}
""";
// Act
var json = JsonSerializer.Serialize(originalModel, options);
var deserializedModel = JsonSerializer.Deserialize<SourceGeneratorModelWithAttribute>(json, options);
// Assert
Assert.NotNull(deserializedModel);
Assert.Equal(originalModel.DateTimeProperty, deserializedModel.DateTimeProperty);
Assert.Equal(originalModel.NullableDateTimeProperty, deserializedModel.NullableDateTimeProperty);
Assert.Equal(originalModel.DateTimeOffsetProperty, deserializedModel.DateTimeOffsetProperty);
Assert.Equal(originalModel.NullableDateTimeOffsetProperty, deserializedModel.NullableDateTimeOffsetProperty);
Assert.Equal(originalModel.DateOnlyProperty, deserializedModel.DateOnlyProperty);
Assert.Equal(originalModel.NullableDateOnlyProperty, deserializedModel.NullableDateOnlyProperty);
Assert.Equal(originalModel.TimeOnlyProperty, deserializedModel.TimeOnlyProperty);
Assert.Equal(originalModel.NullableTimeOnlyProperty, deserializedModel.NullableTimeOnlyProperty);
Assert.Equal(expectedJson, json);
}
[Fact]
public void ReflectionBased_WithoutResolver_WithFormatAttribute_WithNullValues_DoesNotApplyFormat()
{
// Arrange
var options = new JsonSerializerOptions
{
WriteIndented = true
};
var originalModel = new SourceGeneratorModelWithAttribute
{
DateTimeProperty = new DateTime(2023, 10, 1, 12, 0, 0, DateTimeKind.Utc),
NullableDateTimeProperty = null,
DateTimeOffsetProperty = new DateTimeOffset(2023, 10, 1, 12, 0, 0, TimeSpan.Zero),
NullableDateTimeOffsetProperty = null,
DateOnlyProperty = new DateOnly(2023, 10, 1),
NullableDateOnlyProperty = null,
TimeOnlyProperty = new TimeOnly(14, 30, 45),
NullableTimeOnlyProperty = null
};
// Expected JSON with DEFAULT formats (not the custom formats from JsonDateTimeFormat attribute)
const string expectedJson = """
{
"DateTimeProperty": "2023-10-01T12:00:00Z",
"NullableDateTimeProperty": null,
"DateTimeOffsetProperty": "2023-10-01T12:00:00+00:00",
"NullableDateTimeOffsetProperty": null,
"DateOnlyProperty": "2023-10-01",
"NullableDateOnlyProperty": null,
"TimeOnlyProperty": "14:30:45",
"NullableTimeOnlyProperty": null
}
""";
// Act
var json = JsonSerializer.Serialize(originalModel, options);
var deserializedModel = JsonSerializer.Deserialize<SourceGeneratorModelWithAttribute>(json, options);
// Assert
Assert.NotNull(deserializedModel);
Assert.Equal(originalModel.DateTimeProperty, deserializedModel.DateTimeProperty);
Assert.Null(deserializedModel.NullableDateTimeProperty);
Assert.Equal(originalModel.DateTimeOffsetProperty, deserializedModel.DateTimeOffsetProperty);
Assert.Null(deserializedModel.NullableDateTimeOffsetProperty);
Assert.Equal(originalModel.DateOnlyProperty, deserializedModel.DateOnlyProperty);
Assert.Null(deserializedModel.NullableDateOnlyProperty);
Assert.Equal(originalModel.TimeOnlyProperty, deserializedModel.TimeOnlyProperty);
Assert.Null(deserializedModel.NullableTimeOnlyProperty);
Assert.Equal(expectedJson, json);
}
}