2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
- using CommunityToolkit . Datasync . Client . Serialization ;
6
5
using System . Text . Json ;
6
+ using System . Text . RegularExpressions ;
7
7
8
8
namespace CommunityToolkit . Datasync . Client . Test . Serialization ;
9
9
10
10
[ ExcludeFromCodeCoverage ]
11
- public class DateTimeOffsetConverter_Tests
11
+ public class DateTimeOffsetConverter_Tests : SerializerTests
12
12
{
13
- private readonly JsonSerializerOptions serializerOptions ;
13
+ [ Theory ]
14
+ [ MemberData ( nameof ( Locales ) ) ]
15
+ public void Converter_ReadsJson ( string culture )
16
+ {
17
+ const string json = "{\" updatedAt\" :\" 2021-08-21T12:30:15.123+00:00\" }" ;
18
+ DateTimeOffset value = new ( 2021 , 8 , 21 , 12 , 30 , 15 , 123 , TimeSpan . Zero ) ;
19
+
20
+ TestWithCulture ( culture , ( ) =>
21
+ {
22
+ Entity entity = JsonSerializer . Deserialize < Entity > ( json , SerializerOptions ) ;
23
+ entity . UpdatedAt . Should ( ) . Be ( value ) ;
24
+ } ) ;
25
+ }
14
26
15
- public DateTimeOffsetConverter_Tests ( )
27
+ [ Theory ]
28
+ [ MemberData ( nameof ( Locales ) ) ]
29
+ public void Converter_WritesJson ( string culture )
16
30
{
17
- this . serializerOptions = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
18
- this . serializerOptions . Converters . Add ( new DateTimeOffsetConverter ( ) ) ;
31
+ const string json = "{\" updatedAt\" :\" 2021-08-21T12:30:15.123Z\" }" ;
32
+ DateTimeOffset value = new ( 2021 , 8 , 21 , 12 , 30 , 15 , 123 , 456 , TimeSpan . Zero ) ;
33
+
34
+ TestWithCulture ( culture , ( ) =>
35
+ {
36
+ Entity entity = new ( ) { UpdatedAt = value } ;
37
+ string actual = JsonSerializer . Serialize ( entity , SerializerOptions ) ;
38
+ Assert . Equal ( json , actual ) ;
39
+ } ) ;
19
40
}
20
41
21
- [ Fact ]
22
- public void Read_Null_Works ( )
42
+ [ Theory ]
43
+ [ MemberData ( nameof ( Locales ) ) ]
44
+ public void Converter_WritesJson_WithTimeZone ( string culture )
23
45
{
24
- string json = """{"dt":null}""" ;
25
- SUT actual = JsonSerializer . Deserialize < SUT > ( json , this . serializerOptions ) ;
26
- actual . dt . Should ( ) . Be ( DateTimeOffset . MinValue ) ;
46
+ const string json = "{\" updatedAt\" :\" 2021-08-21T12:30:15.123Z\" }" ;
47
+ DateTimeOffset value = new ( 2021 , 8 , 21 , 20 , 30 , 15 , 123 , 456 , TimeSpan . FromHours ( 8 ) ) ;
48
+
49
+ TestWithCulture ( culture , ( ) =>
50
+ {
51
+ Entity entity = new ( ) { UpdatedAt = value } ;
52
+ string actual = JsonSerializer . Serialize ( entity , SerializerOptions ) ;
53
+ Assert . Equal ( json , actual ) ;
54
+ } ) ;
27
55
}
28
56
29
57
[ Fact ]
30
- public void Read_Int_Throws ( )
58
+ public void Converter_ThrowsOnBadDateInInput ( )
31
59
{
32
- string json = """{"dt":42}""" ;
33
- Action act = ( ) => _ = JsonSerializer . Deserialize < SUT > ( json , this . serializerOptions ) ;
34
- act . Should ( ) . Throw < JsonException > ( ) ;
60
+ const string json = "{\" updatedAt\" :\" foo\" }" ;
61
+ Action act = ( ) => _ = JsonSerializer . Deserialize < Entity > ( json , SerializerOptions ) ;
62
+ act . Should ( ) . Throw < Exception > ( ) ;
63
+ }
64
+
65
+ [ Theory ]
66
+ [ MemberData ( nameof ( Locales ) ) ]
67
+ public void Converter_HandlesNullDateInInput ( string culture )
68
+ {
69
+ const string json = """{"updatedAt":null}""" ;
70
+ DateTimeOffset value = DateTimeOffset . MinValue ;
71
+
72
+ TestWithCulture ( culture , ( ) =>
73
+ {
74
+ Entity entity = JsonSerializer . Deserialize < Entity > ( json , SerializerOptions ) ;
75
+ entity . UpdatedAt . Should ( ) . Be ( value ) ;
76
+ } ) ;
35
77
}
36
78
37
- class SUT
79
+ #region Models
80
+ public class Entity
38
81
{
39
- public DateTimeOffset dt { get ; set ; }
82
+ public DateTimeOffset UpdatedAt { get ; set ; }
40
83
}
41
- }
84
+ #endregion
85
+ }
0 commit comments