Skip to content

Commit

Permalink
Add source generated json
Browse files Browse the repository at this point in the history
  • Loading branch information
ThadHouse committed Jan 21, 2024
1 parent cde322c commit 4b15e88
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/wpimath.test/Geometry/Rotation2dTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using UnitsNet.NumberExtensions.NumberToAngle;
using WPIMath.Geometry;
using Xunit;
Expand Down Expand Up @@ -129,4 +130,46 @@ public void TestInterpolate()
interpolated = MathExtras.Lerp(rot1, rot2, 0.5);
Assert.Equal(-175.0, interpolated.Angle.Degrees, Epsilon);
}

[Fact]
public void TestDeserialization()
{

Rotation2d r = JsonSerializer.Deserialize<Rotation2d>("{\"radians\":5}");
Assert.Equal(5, r.Angle.Radians, Epsilon);
}

[Fact]
public void TestSerialization()
{
Rotation2d rot = 5.Radians();

var serializerOptions = new JsonSerializerOptions
{
WriteIndented = false,
};
string serialized = JsonSerializer.Serialize(rot, serializerOptions);
Assert.Equal("{\"radians\":5}", serialized);
}

[Fact]
public void TestDeserializationSourceGenerated()
{
Rotation2d r = JsonSerializer.Deserialize("{\"radians\":5}", Rotation2dJsonContext.Default.Rotation2d);

Check failure on line 158 in test/wpimath.test/Geometry/Rotation2dTest.cs

View workflow job for this annotation

GitHub Actions / build-windows (8.x, win-x64)

The name 'Rotation2dJsonContext' does not exist in the current context
Assert.Equal(5, r.Angle.Radians, Epsilon);
}

[Fact]
public void TestSerializationSourceGenerated()
{
Rotation2d rot = 5.Radians();

var serializerOptions = new JsonSerializerOptions
{
WriteIndented = false,
};
var context = new Rotation2dJsonContext(serializerOptions).Rotation2d;

Check failure on line 171 in test/wpimath.test/Geometry/Rotation2dTest.cs

View workflow job for this annotation

GitHub Actions / build-windows (8.x, win-x64)

The type or namespace name 'Rotation2dJsonContext' could not be found (are you missing a using directive or an assembly reference?)
string serialized = JsonSerializer.Serialize(rot, context);
Assert.Equal("{\"radians\":5}", serialized);
}
}

0 comments on commit 4b15e88

Please sign in to comment.