Skip to content

Commit

Permalink
Fix JSON converters
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Nov 22, 2022
1 parent a9fc3b6 commit 403d8a6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ packages/
.vs/
.vscode/
*.user
*.lutconfig
launchSettings.json
123 changes: 123 additions & 0 deletions UnitTests/Automation_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: GPL-3.0-only

using System.Net;
using System.Text.Json;
using Usbipd.Automation;

namespace UnitTests;
Expand Down Expand Up @@ -210,4 +211,126 @@ public void Device_ClientWslInstance()
Assert.AreEqual(TestClientWslInstance, device.ClientWslInstance);
Assert.IsTrue(device.IsWslAttached);
}

[TestMethod]
public void NullableBusIdJsonConverter_Read_Valid()
{
var reader = new Utf8JsonReader("\"1-42\""u8);
reader.Read();

var converter = new NullableBusIdJsonConverter();
var busId = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
Assert.AreEqual(new BusId(1, 42), busId);
}

[TestMethod]
public void NullableBusIdJsonConverter_Read_Invalid()
{
var converter = new NullableBusIdJsonConverter();
Assert.ThrowsException<FormatException>(() =>
{
var reader = new Utf8JsonReader("\"xxx\""u8);
reader.Read();

_ = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
});
}

[TestMethod]
public void NullableBusIdJsonConverter_Read_Null()
{
var reader = new Utf8JsonReader("null"u8);
reader.Read();

var converter = new NullableBusIdJsonConverter();
var busId = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
Assert.IsNull(busId);
}

[TestMethod]
public void NullableBusIdJsonConverter_Write_Valid()
{
using var memoryStream = new MemoryStream();
var writer = new Utf8JsonWriter(memoryStream, new() { SkipValidation = true });
{
var converter = new NullableBusIdJsonConverter();
converter.Write(writer, new(1, 42), JsonSerializerOptions.Default);
}
writer.Flush();
CollectionAssert.AreEqual("\"1-42\""u8.ToArray(), memoryStream.ToArray());
}

[TestMethod]
public void NullableBusIdJsonConverter_Write_Null()
{
using var memoryStream = new MemoryStream();
var writer = new Utf8JsonWriter(memoryStream, new() { SkipValidation = true });
{
var converter = new NullableBusIdJsonConverter();
converter.Write(writer, null, JsonSerializerOptions.Default);
}
writer.Flush();
CollectionAssert.AreEqual("null"u8.ToArray(), memoryStream.ToArray());
}

[TestMethod]
public void NullableIPAddressJsonConverter_Read_Valid()
{
var reader = new Utf8JsonReader("\"1.2.3.4\""u8);
reader.Read();

var converter = new NullableIPAddressJsonConverter();
var address = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
Assert.AreEqual(IPAddress.Parse("1.2.3.4"), address);
}

[TestMethod]
public void NullableIPAddressJsonConverter_Read_Invalid()
{
var converter = new NullableIPAddressJsonConverter();
Assert.ThrowsException<FormatException>(() =>
{
var reader = new Utf8JsonReader("\"xxx\""u8);
reader.Read();

_ = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
});
}

[TestMethod]
public void NullableIPAddressJsonConverter_Read_Null()
{
var reader = new Utf8JsonReader("null"u8);
reader.Read();

var converter = new NullableIPAddressJsonConverter();
var address = converter.Read(ref reader, typeof(string), JsonSerializerOptions.Default);
Assert.IsNull(address);
}

[TestMethod]
public void NullableIPAddressJsonConverter_Write_Valid()
{
using var memoryStream = new MemoryStream();
var writer = new Utf8JsonWriter(memoryStream, new() { SkipValidation = true });
{
var converter = new NullableIPAddressJsonConverter();
converter.Write(writer, IPAddress.Parse("1.2.3.4"), JsonSerializerOptions.Default);
}
writer.Flush();
CollectionAssert.AreEqual("\"1.2.3.4\""u8.ToArray(), memoryStream.ToArray());
}

[TestMethod]
public void NullableIPAddressJsonConverter_Write_Null()
{
using var memoryStream = new MemoryStream();
var writer = new Utf8JsonWriter(memoryStream, new() { SkipValidation = true });
{
var converter = new NullableIPAddressJsonConverter();
converter.Write(writer, null, JsonSerializerOptions.Default);
}
writer.Flush();
CollectionAssert.AreEqual("null"u8.ToArray(), memoryStream.ToArray());
}
}
1 change: 1 addition & 0 deletions Usbipd.Automation/NullableBusIdJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public override void Write(Utf8JsonWriter writer, BusId? value, JsonSerializerOp
if (value is null)
{
writer.WriteNullValue();
return;
}

writer.WriteStringValue(value.ToString());
Expand Down
1 change: 1 addition & 0 deletions Usbipd.Automation/NullableIPAddressJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public override void Write(Utf8JsonWriter writer, IPAddress? value, JsonSerializ
if (value is null)
{
writer.WriteNullValue();
return;
}

writer.WriteStringValue(value?.ToString());
Expand Down

0 comments on commit 403d8a6

Please sign in to comment.