Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions NWN.Anvil.Tests/src/main/API/ColorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Text.Json;
using Anvil.API;
using Newtonsoft.Json;
using NUnit.Framework;

namespace Anvil.Tests.API
Expand Down Expand Up @@ -46,7 +46,7 @@ public void CreateColorTokenContainsNoNullTerminators(Color color)
[TestCase("""{"r":255,"g":100,"b":10,"a":30}""", 255, 100, 10, 30)]
public void DeserializeColorCreatesCorrectColor(string json, byte expectedRed, byte expectedGreen, byte expectedBlue, byte expectedAlpha)
{
Color color = JsonConvert.DeserializeObject<Color>(json);
Color color = JsonSerializer.Deserialize<Color>(json);
Assert.That(color, Is.EqualTo(new Color(expectedRed, expectedGreen, expectedBlue, expectedAlpha)));
}

Expand Down Expand Up @@ -96,8 +96,8 @@ public void ParseRGBAReturnsCorrectColor(uint packedColor, byte expectedRed, byt
[TestCaseSource(nameof(ColorTestCases))]
public void SerializeColorRetainsColorValues(Color color)
{
string serializedColor = JsonConvert.SerializeObject(color);
Color deserializedColor = JsonConvert.DeserializeObject<Color>(serializedColor);
string serializedColor = JsonSerializer.Serialize(color);
Color deserializedColor = JsonSerializer.Deserialize<Color>(serializedColor);

Assert.That(deserializedColor, Is.EqualTo(color));
}
Expand Down
2 changes: 1 addition & 1 deletion NWN.Anvil.Tests/src/main/API/EngineStructures/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class JsonTests
public void CreateAndDisposeJsonFreesNativeStructure()
{
int[] test = [1, 2];
Json json = JsonUtility.ToJsonStructure(test);
Json json = Json.Serialize(test);

Assert.That(json.IsValid, Is.True, "Json struct was not valid after creation.");
json.Dispose();
Expand Down
25 changes: 13 additions & 12 deletions NWN.Anvil.Tests/src/main/API/Nui/Bindings/NuiBindTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Anvil.API;
using NUnit.Framework;

Expand All @@ -10,44 +11,44 @@ public sealed class NuiBindTests
public void SerializeNuiBindStringReturnsValidJson()
{
NuiBind<string> bind = new NuiBind<string>("test");
Assert.That(JsonUtility.ToJson(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonUtility.ToJson<NuiProperty<string>>(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize((NuiProperty<string>)bind), Is.EqualTo("""{"bind":"test"}"""));
}

[Test(Description = "Serializing a NuiBind<string> creates a valid JSON structure.")]
public void SerializeNuiBindStrRefReturnsValidJson()
{
NuiBindStrRef bind = new NuiBindStrRef("test");
Assert.That(JsonUtility.ToJson(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonUtility.ToJson<NuiProperty<string>>(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize((NuiProperty<string>)bind), Is.EqualTo("""{"bind":"test"}"""));
}

[Test(Description = "Serializing a NuiBind<NuiRect> creates a valid JSON structure.")]
public void SerializeNuiBindNuiRectReturnsValidJson()
{
NuiBind<NuiRect> bind = new NuiBind<NuiRect>("test");
Assert.That(JsonUtility.ToJson(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonUtility.ToJson<NuiProperty<NuiRect>>(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize(bind), Is.EqualTo("""{"bind":"test"}"""));
Assert.That(JsonSerializer.Serialize((NuiProperty<NuiRect>)bind), Is.EqualTo("""{"bind":"test"}"""));
}

[Test(Description = "Deerializing a NuiBind<string> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiBind<string> creates a valid value/object.")]
public void DeserializeNuiBindStringReturnsValidJson()
{
NuiBind<string>? bind = JsonUtility.FromJson<NuiBind<string>>("""{"bind":"test"}""");
NuiBind<string>? bind = JsonSerializer.Deserialize<NuiBind<string>>("""{"bind":"test"}""");
Assert.That(bind?.Key, Is.EqualTo("test"));
}

[Test(Description = "Deerializing a NuiBind<string> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiBind<string> creates a valid value/object.")]
public void DeserializeNuiBindStrRefReturnsValidJson()
{
NuiBind<NuiBindStrRef>? bind = JsonUtility.FromJson<NuiBind<NuiBindStrRef>>("""{"bind":"test"}""");
NuiBind<NuiBindStrRef>? bind = JsonSerializer.Deserialize<NuiBind<NuiBindStrRef>>("""{"bind":"test"}""");
Assert.That(bind?.Key, Is.EqualTo("test"));
}

[Test(Description = "Deerializing a NuiBind<NuiRect> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiBind<NuiRect> creates a valid value/object.")]
public void DeserializeNuiBindNuiRectReturnsValidJson()
{
NuiBind<NuiRect>? bind = JsonUtility.FromJson<NuiBind<NuiRect>>("""{"bind":"test"}""");
NuiBind<NuiRect>? bind = JsonSerializer.Deserialize<NuiBind<NuiRect>>("""{"bind":"test"}""");
Assert.That(bind?.Key, Is.EqualTo("test"));
}
}
Expand Down
111 changes: 38 additions & 73 deletions NWN.Anvil.Tests/src/main/API/Nui/Bindings/NuiValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.Json;
using Anvil.API;
using NUnit.Framework;

Expand All @@ -18,8 +19,8 @@ public sealed class NuiValueTests
public void SerializeNuiValueStringReturnsValidJson(string rawValue, string expected)
{
NuiValue<string> value = new NuiValue<string>(rawValue);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<string>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<string>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValueStrRef creates a valid JSON structure.")]
Expand All @@ -29,8 +30,8 @@ public void SerializeNuiValueStringReturnsValidJson(string rawValue, string expe
public void SerializeNuiValueStrRefReturnsValidJson(uint? rawValue, string expected)
{
NuiValueStrRef value = new NuiValueStrRef(rawValue != null ? new StrRef(rawValue.Value) : null);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<string>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<string>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<int> creates a valid JSON structure.")]
Expand All @@ -43,8 +44,8 @@ public void SerializeNuiValueStrRefReturnsValidJson(uint? rawValue, string expec
public void SerializeNuiValueIntReturnsValidJson(int rawValue, string expected)
{
NuiValue<int> value = new NuiValue<int>(rawValue);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<int>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<int>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<int?> creates a valid JSON structure.")]
Expand All @@ -58,72 +59,54 @@ public void SerializeNuiValueIntReturnsValidJson(int rawValue, string expected)
public void SerializeNuiValueNullableIntReturnsValidJson(int? rawValue, string expected)
{
NuiValue<int?> value = new NuiValue<int?>(rawValue);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<int?>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<int?>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<float> creates a valid JSON structure.")]
[TestCase(0f, @"0.0")]
[TestCase(0f, @"0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[TestCase(2f, @"2")]
[TestCase(2.5f, @"2.5")]
[TestCase(2.5122f, @"2.5122")]
[TestCase(float.NaN, """
"NaN"
""")]
[TestCase(float.NegativeInfinity, """
"-Infinity"
""")]
[TestCase(float.PositiveInfinity, """
"Infinity"
""")]
public void SerializeNuiValueFloatReturnsValidJson(float rawValue, string expected)
{
NuiValue<float> value = new NuiValue<float>(rawValue);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<float>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<float>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<float?> creates a valid JSON structure.")]
[TestCase(0f, @"0.0")]
[TestCase(0f, @"0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[TestCase(2f, @"2")]
[TestCase(2.5f, @"2.5")]
[TestCase(2.5122f, @"2.5122")]
[TestCase(null, @"null")]
[TestCase(float.NaN, """
"NaN"
""")]
[TestCase(float.NegativeInfinity, """
"-Infinity"
""")]
[TestCase(float.PositiveInfinity, """
"Infinity"
""")]
public void SerializeNuiValueFloatNullableReturnsValidJson(float? rawValue, string expected)
{
NuiValue<float?> value = new NuiValue<float?>(rawValue);
Assert.That(JsonUtility.ToJson(value), Is.EqualTo(expected));
Assert.That(JsonUtility.ToJson<NuiProperty<float?>>(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize(value), Is.EqualTo(expected));
Assert.That(JsonSerializer.Serialize((NuiProperty<float?>)value), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<NuiRect> creates a valid JSON structure.")]
public void SerializeNuiValueNuiRectReturnsValidJson()
{
NuiValue<NuiRect> value = new NuiValue<NuiRect>(new NuiRect(100f, 50.251f, 30.11f, 20f));
Assert.That(JsonUtility.ToJson<NuiProperty<NuiRect>>(value), Is.EqualTo("""{"h":20.0,"w":30.11,"x":100.0,"y":50.251}"""));
Assert.That(JsonSerializer.Serialize((NuiProperty<NuiRect>)value), Is.EqualTo("""{"h":20,"w":30.11,"x":100,"y":50.251}"""));
}

[Test(Description = "Serializing a NuiValue<List<int>> creates a valid JSON structure.")]
public void SerializeNuiValueIntListReturnsValidJson()
{
NuiValue<List<int>> value = new NuiValue<List<int>>([1, 2, 3]);
Assert.That(JsonUtility.ToJson<NuiProperty<List<int>>>(value), Is.EqualTo(@"[1,2,3]"));
Assert.That(JsonSerializer.Serialize((NuiProperty<List<int>>)value), Is.EqualTo(@"[1,2,3]"));
}

[Test(Description = "Deerializing a NuiValue<string> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiValue<string> creates a valid value/object.")]
[TestCase("test", """
"test"
""")]
Expand All @@ -133,21 +116,21 @@ public void SerializeNuiValueIntListReturnsValidJson()
""")]
public void DeserializeNuiValueStringReturnsValidJson(string expected, string serialized)
{
NuiValue<string>? value = JsonUtility.FromJson<NuiValue<string>>(serialized);
NuiValue<string>? value = JsonSerializer.Deserialize<NuiValue<string>>(serialized);
Assert.That(value?.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValueStrRef creates a valid value/object.")]
[Test(Description = "Deserializing a NuiValueStrRef creates a valid value/object.")]
[TestCase(0u, """{"strref":0}""")]
[TestCase(null, @"null")]
[TestCase(1000u, """{"strref":1000}""")]
public void DeserializeNuiValueStrRefReturnsValidJson(uint? expected, string serialized)
{
NuiValueStrRef? value = JsonUtility.FromJson<NuiValueStrRef>(serialized);
NuiValueStrRef? value = JsonSerializer.Deserialize<NuiValueStrRef>(serialized);
Assert.That(value?.Value?.Id, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<int> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiValue<int> creates a valid value/object.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
Expand All @@ -156,11 +139,11 @@ public void DeserializeNuiValueStrRefReturnsValidJson(uint? expected, string ser
[TestCase(int.MinValue, @"-2147483648")]
public void DeserializeNuiValueIntReturnsValidJson(int expected, string serialized)
{
NuiValue<int>? value = JsonUtility.FromJson<NuiValue<int>>(serialized);
NuiValue<int>? value = JsonSerializer.Deserialize<NuiValue<int>>(serialized);
Assert.That(value?.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<int?> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiValue<int?> creates a valid value/object.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
Expand All @@ -170,59 +153,41 @@ public void DeserializeNuiValueIntReturnsValidJson(int expected, string serializ
[TestCase(int.MinValue, @"-2147483648")]
public void DeserializeNuiValueNullableIntReturnsValidJson(int? expected, string serialized)
{
NuiValue<int?>? value = JsonUtility.FromJson<NuiValue<int?>>(serialized);
NuiValue<int?>? value = JsonSerializer.Deserialize<NuiValue<int?>>(serialized);
Assert.That(value?.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<float> creates a valid value/object.")]
[TestCase(0f, @"0.0")]
[Test(Description = "Deserializing a NuiValue<float> creates a valid value/object.")]
[TestCase(0f, @"0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[TestCase(2f, @"2")]
[TestCase(2.5f, @"2.5")]
[TestCase(2.5122f, @"2.5122")]
[TestCase(float.NaN, """
"NaN"
""")]
[TestCase(float.NegativeInfinity, """
"-Infinity"
""")]
[TestCase(float.PositiveInfinity, """
"Infinity"
""")]
public void DeserializeNuiValueFloatReturnsValidJson(float expected, string serialized)
{
NuiValue<float>? value = JsonUtility.FromJson<NuiValue<float>>(serialized);
NuiValue<float>? value = JsonSerializer.Deserialize<NuiValue<float>>(serialized);
Assert.That(value?.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<float?> creates a valid value/object.")]
[TestCase(0f, @"0.0")]
[Test(Description = "Deserializing a NuiValue<float?> creates a valid value/object.")]
[TestCase(0f, @"0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[TestCase(2f, @"2")]
[TestCase(2.5f, @"2.5")]
[TestCase(2.5122f, @"2.5122")]
[TestCase(null, @"null")]
[TestCase(float.NaN, """
"NaN"
""")]
[TestCase(float.NegativeInfinity, """
"-Infinity"
""")]
[TestCase(float.PositiveInfinity, """
"Infinity"
""")]
public void DeserializeNuiValueFloatNullableReturnsValidJson(float? expected, string serialized)
{
NuiValue<float?>? value = JsonUtility.FromJson<NuiValue<float?>>(serialized);
NuiValue<float?>? value = JsonSerializer.Deserialize<NuiValue<float?>>(serialized);
Assert.That(value?.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<NuiRect> creates a valid value/object.")]
[Test(Description = "Deserializing a NuiValue<NuiRect> creates a valid value/object.")]
public void DeserializeNuiValueNuiRectReturnsValidJson()
{
NuiValue<NuiRect>? value = JsonUtility.FromJson<NuiValue<NuiRect>>("""{"h":20.0,"w":30.11,"x":100.0,"y":50.251}""");
NuiValue<NuiRect>? value = JsonSerializer.Deserialize<NuiValue<NuiRect>>("""{"h":20,"w":30.11,"x":100,"y":50.251}""");
NuiRect expected = new NuiRect(100.0f, 50.251f, 30.11f, 20.0f);

Assert.That(value?.Value, Is.EqualTo(expected));
Expand All @@ -231,7 +196,7 @@ public void DeserializeNuiValueNuiRectReturnsValidJson()
[Test(Description = "Deserializing a NuiValue<List<int>> creates a valid value/object.")]
public void DeserializeNuiValueIntListReturnsValidJson()
{
NuiValue<List<int>>? value = JsonUtility.FromJson<NuiValue<List<int>>>(@"[1,2,3]");
NuiValue<List<int>>? value = JsonSerializer.Deserialize<NuiValue<List<int>>>(@"[1,2,3]");
List<int> expected = [1, 2, 3];

Assert.That(value?.Value, Is.EqualTo(expected));
Expand Down
5 changes: 3 additions & 2 deletions NWN.Anvil.Tests/src/main/API/Nui/Layout/NuiColumnTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Anvil.API;
using NUnit.Framework;

Expand Down Expand Up @@ -28,8 +29,8 @@ public void SerializeNuiColumnReturnsValidJson()
],
};

Assert.That(JsonUtility.ToJson(layout), Is.EqualTo("""{"type":"col","children":[{"text_halign":1,"value":"test","type":"label","text_valign":1},{"type":"row","children":[]}],"aspect":1.5,"enabled":{"bind":"enabled_bind"},"foreground_color":{"bind":"color_bind"},"height":10.0,"id":"test_column","margin":2.0,"padding":3.0,"tooltip":"test_tooltip","visible":false,"width":100.0}"""));
Assert.That(JsonUtility.ToJson<NuiLayout>(layout), Is.EqualTo("""{"type":"col","children":[{"text_halign":1,"value":"test","type":"label","text_valign":1},{"type":"row","children":[]}],"aspect":1.5,"enabled":{"bind":"enabled_bind"},"foreground_color":{"bind":"color_bind"},"height":10.0,"id":"test_column","margin":2.0,"padding":3.0,"tooltip":"test_tooltip","visible":false,"width":100.0}"""));
Assert.That(JsonSerializer.Serialize(layout), Is.EqualTo("""{"type":"col","children":[{"text_halign":1,"value":"test","type":"label","text_valign":1},{"type":"row","children":[]}],"aspect":1.5,"enabled":{"bind":"enabled_bind"},"foreground_color":{"bind":"color_bind"},"height":10,"id":"test_column","margin":2,"padding":3,"tooltip":"test_tooltip","visible":false,"width":100}"""));
Assert.That(JsonSerializer.Serialize((NuiLayout)layout), Is.EqualTo("""{"type":"col","children":[{"text_halign":1,"value":"test","type":"label","text_valign":1},{"type":"row","children":[]}],"aspect":1.5,"enabled":{"bind":"enabled_bind"},"foreground_color":{"bind":"color_bind"},"height":10,"id":"test_column","margin":2,"padding":3,"tooltip":"test_tooltip","visible":false,"width":100}"""));
}
}
}
Loading
Loading