Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b75e0f7

Browse files
committed
Add more serialization tests
1 parent fef7891 commit b75e0f7

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/tests/UnitTests/Primitives/SerializationTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@ namespace UnitTests.Primitives
1111
[TestFixture]
1212
class SerializationTests
1313
{
14+
[TestCase("2018-05-01T12:04:29.1234567-02:00", "2018-05-01T14:04:29.123+00:00")]
15+
[TestCase("2018-05-01T12:04:29.123456-02:00", "2018-05-01T14:04:29.123+00:00")]
16+
[TestCase("2018-05-01T12:04:29.12345-02:00", "2018-05-01T14:04:29.123+00:00")]
17+
[TestCase("2018-05-01T12:04:29.1234-02:00", "2018-05-01T14:04:29.123+00:00")]
18+
[TestCase("2018-05-01T12:04:29.123-02:00", "2018-05-01T14:04:29.123+00:00")]
19+
[TestCase("2018-05-01T12:04:29.12-02:00", "2018-05-01T14:04:29.120+00:00")]
20+
[TestCase("2018-05-01T12:04:29.1-02:00", "2018-05-01T14:04:29.100+00:00")]
21+
[TestCase("2018-05-01T12:04:29-02:00", "2018-05-01T14:04:29.000+00:00")]
22+
[TestCase("2018-05-01T12:04:29.1234567Z", "2018-05-01T12:04:29.123+00:00")]
23+
[TestCase("2018-05-01T12:04:29.123456Z", "2018-05-01T12:04:29.123+00:00")]
24+
[TestCase("2018-05-01T12:04:29.12345Z", "2018-05-01T12:04:29.123+00:00")]
25+
[TestCase("2018-05-01T12:04:29.1234Z", "2018-05-01T12:04:29.123+00:00")]
26+
[TestCase("2018-05-01T12:04:29.123Z", "2018-05-01T12:04:29.123+00:00")]
27+
[TestCase("2018-05-01T12:04:29.12Z", "2018-05-01T12:04:29.120+00:00")]
28+
[TestCase("2018-05-01T12:04:29.1Z", "2018-05-01T12:04:29.100+00:00")]
29+
[TestCase("2018-05-01T12:04:29Z", "2018-05-01T12:04:29.000+00:00")]
30+
public void FromLocalStringToUniversalDateTimeOffset(string input, string expected)
31+
{
32+
var dtInput = DateTimeOffset.ParseExact(input, Constants.Iso8601Formats, CultureInfo.InvariantCulture, Constants.DateTimeStyle);
33+
var output = dtInput.ToUniversalTime().ToString(Constants.Iso8601Format);
34+
Assert.AreEqual(expected, output);
35+
36+
var json = $@"{{""date"":""{input}""}}";
37+
Assert.DoesNotThrow(() => json.FromJson<ADateTimeOffset>(lowerCase: true));
38+
}
39+
40+
[Test]
41+
public void JsonSerializationUsesKnownFormat()
42+
{
43+
var now = DateTimeOffset.Now;
44+
var output = new ADateTimeOffset { Date = now };
45+
var json = output.ToJson(lowerCase: true);
46+
Assert.AreEqual($@"{{""date"":""{ now.ToUniversalTime().ToString(Constants.Iso8601Format, CultureInfo.InvariantCulture) }""}}", json);
47+
}
48+
1449
[Test]
1550
public void DateTimeSerializationRoundTrip()
1651
{
@@ -53,6 +88,11 @@ public void DateTimeSerializationRoundTripFormatPointZ()
5388
Assert.AreEqual(dt1, dt2);
5489
}
5590

91+
class ADateTimeOffset
92+
{
93+
public DateTimeOffset Date;
94+
}
95+
5696
class TestData
5797
{
5898
public List<string> Things { get; set; } = new List<string>();

0 commit comments

Comments
 (0)