|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization.Metadata; |
| 3 | +using Scarlet.System.Text.Json.DateTimeConverter.Tests.Model; |
| 4 | + |
| 5 | +namespace Scarlet.System.Text.Json.DateTimeConverter.Tests; |
| 6 | + |
| 7 | +public class DateTimeConverterResolverTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void DateTimeConverterResolver_ParameterlessConstructor_CanBeCreated() |
| 11 | + { |
| 12 | + // Arrange & Act |
| 13 | + var resolver = new DateTimeConverterResolver(); |
| 14 | + |
| 15 | + // Assert |
| 16 | + Assert.NotNull(resolver); |
| 17 | + } |
| 18 | + |
| 19 | + [Fact] |
| 20 | + public void DateTimeConverterResolver_WithJsonSerializerOptionsConstructor_CanBeCreated() |
| 21 | + { |
| 22 | + // Arrange |
| 23 | + var options = new JsonSerializerOptions(); |
| 24 | + |
| 25 | + // Act |
| 26 | + var resolver = new DateTimeConverterResolver(options); |
| 27 | + |
| 28 | + // Assert |
| 29 | + Assert.NotNull(resolver); |
| 30 | + } |
| 31 | + |
| 32 | + [Fact] |
| 33 | + public void DateTimeConverterResolver_ParameterlessConstructor_GetTypeInfo_ReturnsNull() |
| 34 | + { |
| 35 | + // Arrange |
| 36 | + var resolver = new DateTimeConverterResolver(); |
| 37 | + |
| 38 | + // Act |
| 39 | + // This exercises the parameterless constructor and the fallback path in GetTypeInfo(Type) |
| 40 | + // since _source is null, it calls GetTypeInfo(type, Options), which returns null |
| 41 | + var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute)); |
| 42 | + |
| 43 | + // Assert |
| 44 | + // Without a source resolver, GetTypeInfo should return null |
| 45 | + Assert.Null(typeInfo); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void DateTimeConverterResolver_WithOptionsConstructor_GetTypeInfo_UsesFallbackPath_ReturnsNull() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + var options = new JsonSerializerOptions |
| 53 | + { |
| 54 | + TypeInfoResolver = ResolverModelJsonSerializerContext.Default |
| 55 | + }; |
| 56 | + // Create a resolver with options - this exercises the JsonSerializerOptions constructor |
| 57 | + var resolver = new DateTimeConverterResolver(options); |
| 58 | + |
| 59 | + // Act |
| 60 | + // Call the single-parameter GetTypeInfo |
| 61 | + // This will use the fallback path: GetTypeInfo(type, Options) on line 59 |
| 62 | + // since _source is null (not a JsonSerializerContext) |
| 63 | + // However, GetTypeInfo(Type, JsonSerializerOptions) needs _source to work, so it returns null |
| 64 | + var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute)); |
| 65 | + |
| 66 | + // Assert |
| 67 | + // Without _source set, the resolver can't resolve type info |
| 68 | + Assert.Null(typeInfo); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void DateTimeConverterResolver_GetTypeInfo_WithJsonSerializerContextSource_UsesContextOptions() |
| 73 | + { |
| 74 | + // Arrange |
| 75 | + var sourceContext = ResolverModelJsonSerializerContext.Default; |
| 76 | + var resolver = new DateTimeConverterResolver(sourceContext); |
| 77 | + |
| 78 | + // Act |
| 79 | + // This should use the JsonSerializerContext path in GetTypeInfo(Type) |
| 80 | + // where it detects _source is JsonSerializerContext and uses its Options |
| 81 | + var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute)); |
| 82 | + |
| 83 | + // Assert |
| 84 | + Assert.NotNull(typeInfo); |
| 85 | + Assert.Equal(typeof(SourceGeneratorModelWithAttribute), typeInfo.Type); |
| 86 | + } |
| 87 | + |
| 88 | + [Fact] |
| 89 | + public void DateTimeConverterResolver_WithParameterlessConstructor_CanSerializeWithSourceContext() |
| 90 | + { |
| 91 | + // Arrange |
| 92 | + var resolver = new DateTimeConverterResolver(); |
| 93 | + var options = new JsonSerializerOptions |
| 94 | + { |
| 95 | + TypeInfoResolver = JsonTypeInfoResolver.Combine(resolver, ResolverModelJsonSerializerContext.Default), |
| 96 | + WriteIndented = true |
| 97 | + }; |
| 98 | + var model = new SourceGeneratorModelWithAttribute |
| 99 | + { |
| 100 | + DateTimeProperty = new DateTime(2023, 10, 1, 12, 0, 0, DateTimeKind.Utc), |
| 101 | + DateOnlyProperty = new DateOnly(2023, 10, 1) |
| 102 | + }; |
| 103 | + |
| 104 | + // Act |
| 105 | + var json = JsonSerializer.Serialize(model, options); |
| 106 | + |
| 107 | + // Assert |
| 108 | + // Should use default formats since resolver has no source with attributes |
| 109 | + Assert.Contains("\"DateTimeProperty\": \"2023-10-01T12:00:00Z\"", json); |
| 110 | + Assert.Contains("\"DateOnlyProperty\": \"2023-10-01\"", json); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void DateTimeConverterResolver_WithOptionsConstructor_StoresOptionsForBaseClass() |
| 115 | + { |
| 116 | + // Arrange |
| 117 | + var options = new JsonSerializerOptions |
| 118 | + { |
| 119 | + WriteIndented = true, |
| 120 | + TypeInfoResolver = ResolverModelJsonSerializerContext.Default |
| 121 | + }; |
| 122 | + // This tests the constructor with JsonSerializerOptions parameter |
| 123 | + // The options are passed to the base class JsonSerializerContext |
| 124 | + var resolver = new DateTimeConverterResolver(options); |
| 125 | + |
| 126 | + // Act & Assert |
| 127 | + // The resolver is successfully created with the options |
| 128 | + // The constructor is used when the resolver is part of framework integration |
| 129 | + Assert.NotNull(resolver); |
| 130 | + } |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void DateTimeConverterResolver_WithIJsonTypeInfoResolverSource_UsesSourceForGetTypeInfo() |
| 134 | + { |
| 135 | + // Arrange |
| 136 | + var sourceResolver = ResolverModelJsonSerializerContext.Default; |
| 137 | + var resolver = new DateTimeConverterResolver(sourceResolver); |
| 138 | + var options = new JsonSerializerOptions(); |
| 139 | + |
| 140 | + // Act |
| 141 | + // Call GetTypeInfo with explicit options - this tests the two-parameter GetTypeInfo |
| 142 | + var typeInfo = resolver.GetTypeInfo(typeof(SourceGeneratorModelWithAttribute), options); |
| 143 | + |
| 144 | + // Assert |
| 145 | + // Should get typeInfo from the source resolver |
| 146 | + Assert.NotNull(typeInfo); |
| 147 | + Assert.Equal(typeof(SourceGeneratorModelWithAttribute), typeInfo.Type); |
| 148 | + } |
| 149 | +} |
| 150 | + |
0 commit comments