Skip to content

Commit 997c186

Browse files
author
Doug Ferris
committed
Adding json tests
1 parent cd1e41a commit 997c186

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

src/LibYear/Output/Json/DateTimeConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace LibYear.Output.Json;
66

7-
internal sealed class DateTimeConverter : JsonConverter<DateTime>
7+
public sealed class DateTimeConverter : JsonConverter<DateTime>
88
{
99
public override DateTime Read(
1010
ref Utf8JsonReader reader,

src/LibYear/Output/Json/DisplayVersion.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ internal sealed record DisplayVersion
99
public string VersionNumber { get; init; } = string.Empty;
1010
[JsonPropertyName("releaseDate")]
1111
public DateTime ReleaseDate { get; init; }
12+
public Release Release { get; init; }
1213
public DisplayVersion(Release release)
1314
{
15+
Release = release;
1416
VersionNumber = release.Version.ToString();
1517
ReleaseDate = release.Date;
1618
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using LibYear.Output.Json;
2+
using System.Text.Json;
3+
using Xunit;
4+
5+
namespace LibYear.Tests.Output.Json;
6+
7+
public class DateTimeConverterTests
8+
{
9+
private static TestObject DateTimeObject = new()
10+
{
11+
Test = new DateTime(2020, 01, 01)
12+
};
13+
private static string ExpectedJson = @"{""Test"":""2020-01-01""}";
14+
private static JsonSerializerOptions Options = new JsonSerializerOptions
15+
{
16+
Converters =
17+
{
18+
new DateTimeConverter()
19+
}
20+
};
21+
22+
[Fact]
23+
public void ShouldSerializeProperly()
24+
{
25+
var serialized = JsonSerializer.Serialize(DateTimeObject, Options);
26+
Assert.Equal(ExpectedJson, serialized);
27+
}
28+
29+
[Fact]
30+
public void ShouldDeserializeProperly()
31+
{
32+
var deserialized = JsonSerializer.Deserialize<TestObject>(ExpectedJson, Options);
33+
Assert.Equal(DateTimeObject, deserialized);
34+
}
35+
36+
private sealed record TestObject
37+
{
38+
public DateTime Test { get; set; }
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Text.Json;
2+
using LibYear.Output.Json;
3+
using Xunit;
4+
5+
namespace LibYear.Tests.Output.Json;
6+
7+
public class DoubleConverterTests
8+
{
9+
private static TestObject DateTimeObject = new()
10+
{
11+
Test = 15
12+
};
13+
private static string ExpectedJson = @"{""Test"":15}";
14+
private static JsonSerializerOptions Options = new JsonSerializerOptions
15+
{
16+
Converters =
17+
{
18+
new DateTimeConverter()
19+
}
20+
};
21+
22+
[Fact]
23+
public void ShouldSerializeProperly()
24+
{
25+
var serialized = JsonSerializer.Serialize(DateTimeObject, Options);
26+
Assert.Equal(ExpectedJson, serialized);
27+
}
28+
29+
[Fact]
30+
public void ShouldDeserializeProperly()
31+
{
32+
var deserialized = JsonSerializer.Deserialize<TestObject>(ExpectedJson, Options);
33+
Assert.Equal(DateTimeObject, deserialized);
34+
}
35+
36+
private sealed record TestObject
37+
{
38+
public double Test { get; set; }
39+
}
40+
}

test/LibYear.Tests/Output/Json/JsonOutputTests.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ public void NoResultsProducesNoOutput()
1515
{
1616
//arrange
1717
var console = Substitute.For<IAnsiConsole>();
18-
// Act
18+
19+
// act
1920
var output = new JsonOutput(console);
2021
var result = new SolutionResult(Array.Empty<ProjectResult>());
2122
output.DisplayAllResults(result, false);
2223

23-
// Assert
24+
// assert
2425
console.DidNotReceive().WriteLine();
2526
}
2627

@@ -37,11 +38,11 @@ public void ResultsShouldPrintToConsole(bool quietMode)
3738
new ProjectResult(projectFile1, new[] { new Result("test1", new Release(new PackageVersion(1, 2, 3), DateTime.Today), new Release(new PackageVersion(1, 2, 3), DateTime.Today)) }),
3839
});
3940

40-
// Act
41+
// act
4142
var sut = new JsonOutput(console);
4243
sut.DisplayAllResults(solutionResults, quietMode);
4344

44-
// Assert
45+
// assert
4546
Assert.NotEmpty(console.Output);
4647
}
4748

@@ -55,10 +56,10 @@ public void QuietModeResultInSingleLineOutput()
5556
new ProjectResult(projectFile1, new[] { new Result("test1", new Release(new PackageVersion(1, 2, 3), DateTime.Today), new Release(new PackageVersion(1, 2, 3), DateTime.Today)) }),
5657
});
5758

58-
// Act
59+
// act
5960
var result = JsonOutput.FormatOutput(solutionResults, true);
6061

61-
// Assert
62+
// assert
6263
var expectedJsonOutput = @"{""YearsBehind"":0,""DaysBehind"":0,""Projects"":[{""Project"":""test project 1"",""YearsBehind"":0,""Packages"":[{""PackageName"":""test1"",""CurrentVersion"":{""versionNumber"":""1.2.3"",""releaseDate"":""2024-05-29""},""LatestVersion"":{""versionNumber"":""1.2.3"",""releaseDate"":""2024-05-29""},""YearsBehind"":0}]}]}";
6364
Assert.Equal(expectedJsonOutput, result);
6465
}
@@ -74,10 +75,10 @@ public void NonQuietModeShouldResultInMultiLineOutput()
7475
new ProjectResult(projectFile1, new[] { new Result("test1", new Release(new PackageVersion(1, 2, 3), DateTime.Today), new Release(new PackageVersion(1, 2, 3), DateTime.Today)) }),
7576
});
7677

77-
// Act
78+
// act
7879
var result = JsonOutput.FormatOutput(results, false);
7980

80-
// Assert
81+
// assert
8182
var expectedOutput = """
8283
{
8384
"YearsBehind": 0,

0 commit comments

Comments
 (0)