-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeConverterResolverTests.cs
More file actions
150 lines (126 loc) · 5.38 KB
/
DateTimeConverterResolverTests.cs
File metadata and controls
150 lines (126 loc) · 5.38 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Scarlet.System.Text.Json.DateTimeConverter.Tests.Model;
namespace Scarlet.System.Text.Json.DateTimeConverter.Tests;
public class DateTimeConverterResolverTests
{
[Fact]
public void DateTimeConverterResolver_ParameterlessConstructor_CanBeCreated()
{
// Arrange & Act
var resolver = new DateTimeConverterResolver();
// Assert
Assert.NotNull(resolver);
}
[Fact]
public void DateTimeConverterResolver_WithJsonSerializerOptionsConstructor_CanBeCreated()
{
// Arrange
var options = new JsonSerializerOptions();
// Act
var resolver = new DateTimeConverterResolver(options);
// Assert
Assert.NotNull(resolver);
}
[Fact]
public void DateTimeConverterResolver_ParameterlessConstructor_GetTypeInfo_ReturnsNull()
{
// Arrange
var resolver = new DateTimeConverterResolver();
// Act
// This exercises the parameterless constructor and the fallback path in GetTypeInfo(Type)
// since _source is null, it calls GetTypeInfo(type, Options), which returns null
var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute));
// Assert
// Without a source resolver, GetTypeInfo should return null
Assert.Null(typeInfo);
}
[Fact]
public void DateTimeConverterResolver_WithOptionsConstructor_GetTypeInfo_UsesFallbackPath_ReturnsNull()
{
// Arrange
var options = new JsonSerializerOptions
{
TypeInfoResolver = ResolverModelJsonSerializerContext.Default
};
// Create a resolver with options - this exercises the JsonSerializerOptions constructor
var resolver = new DateTimeConverterResolver(options);
// Act
// Call the single-parameter GetTypeInfo
// This will use the fallback path: GetTypeInfo(type, Options) on line 59
// since _source is null (not a JsonSerializerContext)
// However, GetTypeInfo(Type, JsonSerializerOptions) needs _source to work, so it returns null
var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute));
// Assert
// Without _source set, the resolver can't resolve type info
Assert.Null(typeInfo);
}
[Fact]
public void DateTimeConverterResolver_GetTypeInfo_WithJsonSerializerContextSource_UsesContextOptions()
{
// Arrange
var sourceContext = ResolverModelJsonSerializerContext.Default;
var resolver = new DateTimeConverterResolver(sourceContext);
// Act
// This should use the JsonSerializerContext path in GetTypeInfo(Type)
// where it detects _source is JsonSerializerContext and uses its Options
var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute));
// Assert
Assert.NotNull(typeInfo);
Assert.Equal(typeof(SourceGeneratorModelWithAttribute), typeInfo.Type);
}
[Fact]
public void DateTimeConverterResolver_WithParameterlessConstructor_CanSerializeWithSourceContext()
{
// Arrange
var resolver = new DateTimeConverterResolver();
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(resolver, ResolverModelJsonSerializerContext.Default),
WriteIndented = true
};
var model = new SourceGeneratorModelWithAttribute
{
DateTimeProperty = new DateTime(2023, 10, 1, 12, 0, 0, DateTimeKind.Utc),
DateOnlyProperty = new DateOnly(2023, 10, 1)
};
// Act
var json = JsonSerializer.Serialize(model, options);
// Assert
// Should use default formats since resolver has no source with attributes
Assert.Contains("\"DateTimeProperty\": \"2023-10-01T12:00:00Z\"", json);
Assert.Contains("\"DateOnlyProperty\": \"2023-10-01\"", json);
}
[Fact]
public void DateTimeConverterResolver_WithOptionsConstructor_StoresOptionsForBaseClass()
{
// Arrange
var options = new JsonSerializerOptions
{
WriteIndented = true,
TypeInfoResolver = ResolverModelJsonSerializerContext.Default
};
// This tests the constructor with JsonSerializerOptions parameter
// The options are passed to the base class JsonSerializerContext
var resolver = new DateTimeConverterResolver(options);
// Act & Assert
// The resolver is successfully created with the options
// The constructor is used when the resolver is part of framework integration
Assert.NotNull(resolver);
}
[Fact]
public void DateTimeConverterResolver_WithIJsonTypeInfoResolverSource_UsesSourceForGetTypeInfo()
{
// Arrange
var sourceResolver = ResolverModelJsonSerializerContext.Default;
var resolver = new DateTimeConverterResolver(sourceResolver);
var options = new JsonSerializerOptions();
// Act
// Call GetTypeInfo with explicit options - this tests the two-parameter GetTypeInfo
var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute), options);
// Assert
// Should get typeInfo from the source resolver
Assert.NotNull(typeInfo);
Assert.Equal(typeof(SourceGeneratorModelWithAttribute), typeInfo.Type);
}
}