Skip to content

Commit

Permalink
Upgrade to xUnit v3 and simplify all tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
fgimian committed Jan 14, 2025
1 parent 6d1070d commit 386e7ff
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 500 deletions.
34 changes: 19 additions & 15 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ tasks:
- restore

test:
desc: Test the application and produce a coverage report.
cmds:
- >-
dotnet test
--configuration {{.CONFIGURATION}}
--logger xunit
--verbosity normal
--no-build
--collect:"XPlat Code Coverage"
- >-
dotnet reportgenerator
-reports:src/**/coverage.cobertura.xml
-targetdir:.coverage
"-reporttypes:Cobertura;lcov;Html"
-filefilters:-*.g.cs
desc: Run the unit tests.
cmd: >-
dotnet test
--configuration {{.CONFIGURATION}}
--logger xunit
--verbosity normal
--no-build
--collect:"XPlat Code Coverage"
deps:
- build

cover:
desc: Produce a coverage report for unit tests.
cmd: >-
dotnet reportgenerator
-reports:src/**/coverage.cobertura.xml
-targetdir:.coverage
"-reporttypes:Cobertura;lcov;Html"
-filefilters:-*.g.cs
deps:
- test

publish:
desc: Publish the application ready for distribution.
cmd: >-
Expand Down
18 changes: 1 addition & 17 deletions src/TotalMixVC.Tests/ConfigConverters/IPAddressConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ public class IPAddressConverterTests
[InlineData("192.168.0.1")]
public void Read_Valid_ConvertsWithoutError(string address)
{
// Arrange
var json = $$"""{"Address": "{{address}}"}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(address, model?.Address.ToString());
}

Expand All @@ -31,26 +26,15 @@ public void Read_Valid_ConvertsWithoutError(string address)
[InlineData("osc.example.com")]
public void Read_Invalid_ThrowsException(string address)
{
// Arrange
var json = $$"""{"Address": "{{address}}"}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Fact]
public void Write_Valid_ConvertsWithoutError()
{
// Arrange
var model = new Model() { Address = IPAddress.Loopback };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
/*lang=json,strict*/
Assert.Equal("""{"Address":"127.0.0.1"}""", json);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ public class NonNegativeDoubleConverterTests
[InlineData(10.0)]
public void Read_Valid_ConvertsWithoutError(double value)
{
// Arrange
var json = $$"""{"Value": {{value}}}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(value, model?.Value);
}

Expand All @@ -31,14 +26,8 @@ public void Read_Valid_ConvertsWithoutError(double value)
[InlineData(-10.5)]
public void Read_Invalid_ThrowsException(double value)
{
// Arrange
var json = $$"""{"Value": {{value}}}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Theory]
Expand All @@ -48,13 +37,8 @@ public void Read_Invalid_ThrowsException(double value)
[InlineData(10.0)]
public void Write_Valid_ConvertsWithoutError(double value)
{
// Arrange
var model = new Model() { Value = value };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
Assert.Equal($$"""{"Value":{{value}}}""", json);
}

Expand All @@ -65,14 +49,8 @@ public void Write_Valid_ConvertsWithoutError(double value)
[InlineData(-10.5)]
public void Write_Invalid_ThrowsException(double value)
{
// Arrange
var model = new Model() { Value = value };

// Act
Action action = () => JsonSerializer.Serialize(model);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Serialize(model));
}

internal sealed record Model
Expand Down
26 changes: 2 additions & 24 deletions src/TotalMixVC.Tests/ConfigConverters/PortIntegerConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,8 @@ public class PortIntegerConverterTests
[InlineData(7000)]
public void Read_Valid_ConvertsWithoutError(int port)
{
// Arrange
var json = $$"""{"Port": {{port}}}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(port, model?.Port);
}

Expand All @@ -32,14 +27,8 @@ public void Read_Valid_ConvertsWithoutError(int port)
[InlineData(-50)]
public void Read_Invalid_ThrowsException(int port)
{
// Arrange
var json = $$"""{"Port": {{port}}}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Theory]
Expand All @@ -49,13 +38,8 @@ public void Read_Invalid_ThrowsException(int port)
[InlineData(7000)]
public void Write_Valid_ConvertsWithoutError(int port)
{
// Arrange
var model = new Model() { Port = port };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
Assert.Equal($$"""{"Port":{{port}}}""", json);
}

Expand All @@ -66,14 +50,8 @@ public void Write_Valid_ConvertsWithoutError(int port)
[InlineData(-50)]
public void Write_Invalid_ThrowsException(int port)
{
// Arrange
var model = new Model() { Port = port };

// Act
Action action = () => JsonSerializer.Serialize(model);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Serialize(model));
}

internal sealed record Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ public class PositiveDoubleConverterTests
[InlineData(10.0)]
public void Read_Valid_ConvertsWithoutError(double value)
{
// Arrange
var json = $$"""{"Value": {{value}}}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(value, model?.Value);
}

Expand All @@ -31,14 +26,8 @@ public void Read_Valid_ConvertsWithoutError(double value)
[InlineData(-10.5)]
public void Read_Invalid_ThrowsException(double value)
{
// Arrange
var json = $$"""{"Value": {{value}}}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Theory]
Expand All @@ -48,13 +37,8 @@ public void Read_Invalid_ThrowsException(double value)
[InlineData(10.0)]
public void Write_Valid_ConvertsWithoutError(double value)
{
// Arrange
var model = new Model() { Value = value };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
Assert.Equal($$"""{"Value":{{value}}}""", json);
}

Expand All @@ -65,14 +49,8 @@ public void Write_Valid_ConvertsWithoutError(double value)
[InlineData(-10.5)]
public void Write_Invalid_ThrowsException(double value)
{
// Arrange
var model = new Model() { Value = value };

// Act
Action action = () => JsonSerializer.Serialize(model);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Serialize(model));
}

internal sealed record Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ public class SolidColorBrushConverterTests
[InlineData("BLUE", "#FF0000FF")]
public void Read_Valid_ConvertsWithoutError(string color, string expected)
{
// Arrange
var json = $$"""{"Color": "{{color}}"}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(expected, model?.Color.ToString(CultureInfo.InvariantCulture));
}

Expand All @@ -33,14 +28,8 @@ public void Read_Valid_ConvertsWithoutError(string color, string expected)
[InlineData("#yyxxzz")]
public void Read_Invalid_ThrowsException(string color)
{
// Arrange
var json = $$"""{"Color": "{{color}}"}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Theory]
Expand All @@ -49,14 +38,10 @@ public void Read_Invalid_ThrowsException(string color)
[InlineData("#fff", "#ffffff")]
public void Write_Valid_ConvertsWithoutError(string color, string expected)
{
// Arrange
var converter = new BrushConverter();
var model = new Model() { Color = (SolidColorBrush)converter.ConvertFromString(color)! };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
Assert.Equal($$"""{"Color":"{{expected}}"}""", json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,8 @@ public class VolumeFineIncrementFloatConverterTests
[InlineData(0.05)]
public void Read_Valid_ConvertsWithoutError(float volumeFineIncrement)
{
// Arrange
var json = $$"""{"VolumeFineIncrement": {{volumeFineIncrement}}}""";

// Act
var model = JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Equal(volumeFineIncrement, model?.VolumeFineIncrement);
}

Expand All @@ -30,14 +25,8 @@ public void Read_Valid_ConvertsWithoutError(float volumeFineIncrement)
[InlineData(-1.0)]
public void Read_Invalid_ThrowsException(float volumeFineIncrement)
{
// Arrange
var json = $$"""{"VolumeFineIncrement": {{volumeFineIncrement}}}""";

// Act
Action action = () => JsonSerializer.Deserialize<Model>(json);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Model>(json));
}

[Theory]
Expand All @@ -46,13 +35,8 @@ public void Read_Invalid_ThrowsException(float volumeFineIncrement)
[InlineData(0.05)]
public void Write_Valid_ConvertsWithoutError(float volumeFineIncrement)
{
// Arrange
var model = new Model() { VolumeFineIncrement = volumeFineIncrement };

// Act
var json = JsonSerializer.Serialize(model);

// Assert
Assert.Equal($$"""{"VolumeFineIncrement":{{volumeFineIncrement}}}""", json);
}

Expand All @@ -63,14 +47,8 @@ public void Write_Valid_ConvertsWithoutError(float volumeFineIncrement)
[InlineData(-1.0)]
public void Write_Invalid_ThrowsException(float volumeFineIncrement)
{
// Arrange
var model = new Model() { VolumeFineIncrement = volumeFineIncrement };

// Act
Action action = () => JsonSerializer.Serialize(model);

// Assert
Assert.Throws<JsonException>(action);
Assert.Throws<JsonException>(() => JsonSerializer.Serialize(model));
}

internal sealed record Model
Expand Down
Loading

0 comments on commit 386e7ff

Please sign in to comment.