Skip to content

Commit

Permalink
Move json serialize/deserialize to JsonUtility. Add Nui tests (#471)
Browse files Browse the repository at this point in the history
* Implement JsonUtility. Use JsonUtility for json conversion in API.

* Add JsonUtility tests.

* Add NuiBinding tests. Fix NuiValue deserialization.

* Cleanup usings.

* Add NuiLayout tests.

* Add closure tests.

* Move tests to subdirectories/sub classes.

* Ignore unused json fields for Nui Colors.

* Add NwTask.Delay tests.

* Add scheduler service tests.

* Add NuiDrawList tests.

* Update namespace ignores.
jhett12321 authored Feb 26, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 0a00f14 commit f82de2b
Showing 21 changed files with 762 additions and 38 deletions.
8 changes: 7 additions & 1 deletion NWN.Anvil.Tests/NWN.Anvil.Tests.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -2,8 +2,14 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Casync/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cenginestructure/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui_005Cbindings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui_005Clayout/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cnui_005Cwidgets/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cobject/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Ctlk/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Ctwodimarray/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cutils/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Capi_005Cvariable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Cservices_005Cresources/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Cservices_005Cresources/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=src_005Cmain_005Cservices_005Cscheduler/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
18 changes: 18 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Async/NwTaskTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Anvil.API;
using Anvil.Services;
@@ -13,6 +14,7 @@ public sealed class NwTaskTests
private static VirtualMachine VirtualMachine { get; set; }

[Test(Description = "Starts an async task, then attempts to switch back to the main thread & script context.")]
[Timeout(10000)]
public async Task ReturnToMainThreadAfterSwitch()
{
await Task.Run(async () =>
@@ -24,5 +26,21 @@ await Task.Run(async () =>
await NwTask.SwitchToMainThread();
Assert.That(VirtualMachine.IsInScriptContext, Is.True, "Did not return to the main thread after SwitchToMainThread.");
}

[Test(Description = "Await a fixed delay then continue execution.")]
[Timeout(10000)]
[TestCase(500)]
[TestCase(1000)]
[TestCase(5000)]
public async Task AwaitDelayContinuesExecutionAtExpectedTime(int delayMs)
{
TimeSpan delay = TimeSpan.FromMilliseconds(delayMs);
Stopwatch stopwatch = Stopwatch.StartNew();

await NwTask.Delay(delay);

Assert.That(stopwatch.Elapsed.TotalMilliseconds, Is.EqualTo(delay.TotalMilliseconds).Within(2).Percent, "Delay was not within the margin of error.");
Assert.That(VirtualMachine.IsInScriptContext, Is.True, "Did not return to the main thread after NwTask.Delay.");
}
}
}
37 changes: 37 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Nui/Bindings/NuiBindTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Anvil.API;
using NUnit.Framework;

namespace Anvil.Tests.API
{
[TestFixture(Category = "API.Nui")]
public sealed class NuiBindTests
{
[Test(Description = "Serializing a NuiBind<string> creates a valid JSON structure.")]
public void SerializeNuiBindStringReturnsValidJsonStructure()
{
NuiBind<string> test = new NuiBind<string>("test");
Assert.That(JsonUtility.ToJson(test), Is.EqualTo(@"{""bind"":""test""}"));
}

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

[Test(Description = "Deerializing a NuiBind<string> creates a valid JSON structure.")]
public void DeserializeNuiBindStringReturnsValidJsonStructure()
{
NuiBind<string> test = JsonUtility.FromJson<NuiBind<string>>(@"{""bind"":""test""}");
Assert.That(test.Key, Is.EqualTo("test"));
}

[Test(Description = "Deerializing a NuiBind<NuiRect> creates a valid JSON structure.")]
public void DeserializeNuiBindNuiRectReturnsValidJsonStructure()
{
NuiBind<NuiRect> test = JsonUtility.FromJson<NuiBind<NuiRect>>(@"{""bind"":""test""}");
Assert.That(test.Key, Is.EqualTo("test"));
}
}
}
182 changes: 182 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Nui/Bindings/NuiValueTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
using System.Collections.Generic;
using Anvil.API;
using NUnit.Framework;

namespace Anvil.Tests.API
{
[TestFixture(Category = "API.Nui")]
public class NuiValueTests
{
[Test(Description = "Serializing a NuiValue<string> creates a valid JSON structure.")]
[TestCase("test", @"""test""")]
[TestCase(null, @"null")]
[TestCase("", @"""""")]
public void SerializeNuiValueStringReturnsValidJsonStructure(string value, string expected)
{
NuiValue<string> test = new NuiValue<string>(value);
Assert.That(JsonUtility.ToJson(test), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<int> creates a valid JSON structure.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
[TestCase(-10, @"-10")]
[TestCase(int.MaxValue, @"2147483647")]
[TestCase(int.MinValue, @"-2147483648")]
public void SerializeNuiValueIntReturnsValidJsonStructure(int value, string expected)
{
NuiValue<int> test = new NuiValue<int>(value);
Assert.That(JsonUtility.ToJson(test), Is.EqualTo(expected));
}

[Test(Description = "Serializing a NuiValue<int?> creates a valid JSON structure.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
[TestCase(-10, @"-10")]
[TestCase(null, @"null")]
[TestCase(int.MaxValue, @"2147483647")]
[TestCase(int.MinValue, @"-2147483648")]
public void SerializeNuiValueNullableIntReturnsValidJsonStructure(int? value, string expected)
{
NuiValue<int?> test = new NuiValue<int?>(value);
Assert.That(JsonUtility.ToJson(test), Is.EqualTo(expected));
}

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

[Test(Description = "Serializing a NuiValue<float?> creates a valid JSON structure.")]
[TestCase(0f, @"0.0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[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 SerializeNuiValueFloatNullableReturnsValidJsonStructure(float? value, string expected)
{
NuiValue<float?> test = new NuiValue<float?>(value);
Assert.That(JsonUtility.ToJson(test), Is.EqualTo(expected));
}

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

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

[Test(Description = "Deerializing a NuiValue<string> creates a valid JSON structure.")]
[TestCase("test", @"""test""")]
[TestCase(null, @"null")]
[TestCase("", @"""""")]
public void DeserializeNuiValueStringReturnsValidJsonStructure(string expected, string serialized)
{
NuiValue<string> test = JsonUtility.FromJson<NuiValue<string>>(serialized);
Assert.That(test.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<int> creates a valid JSON structure.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
[TestCase(-10, @"-10")]
[TestCase(int.MaxValue, @"2147483647")]
[TestCase(int.MinValue, @"-2147483648")]
public void DeserializeNuiValueIntReturnsValidJsonStructure(int expected, string serialized)
{
NuiValue<int> test = JsonUtility.FromJson<NuiValue<int>>(serialized);
Assert.That(test.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<int?> creates a valid JSON structure.")]
[TestCase(0, @"0")]
[TestCase(-0, @"0")]
[TestCase(10, @"10")]
[TestCase(-10, @"-10")]
[TestCase(null, @"null")]
[TestCase(int.MaxValue, @"2147483647")]
[TestCase(int.MinValue, @"-2147483648")]
public void DeserializeNuiValueNullableIntReturnsValidJsonStructure(int? expected, string serialized)
{
NuiValue<int?> test = JsonUtility.FromJson<NuiValue<int?>>(serialized);
Assert.That(test.Value, Is.EqualTo(expected));
}

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

[Test(Description = "Deerializing a NuiValue<float?> creates a valid JSON structure.")]
[TestCase(0f, @"0.0")]
[TestCase(0.1f, @"0.1")]
[TestCase(0.125f, @"0.125")]
[TestCase(2f, @"2.0")]
[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 DeserializeNuiValueFloatNullableReturnsValidJsonStructure(float? expected, string serialized)
{
NuiValue<float?> test = JsonUtility.FromJson<NuiValue<float?>>(serialized);
Assert.That(test.Value, Is.EqualTo(expected));
}

[Test(Description = "Deerializing a NuiValue<NuiRect> creates a valid JSON structure.")]
public void DeserializeNuiValueNuiRectReturnsValidJsonStructure()
{
NuiValue<NuiRect> test = JsonUtility.FromJson<NuiValue<NuiRect>>(@"{""h"":20.0,""w"":30.11,""x"":100.0,""y"":50.251}");
NuiRect expected = new NuiRect(100.0f, 50.251f, 30.11f, 20.0f);

Assert.That(test.Value, Is.EqualTo(expected));
}

[Test(Description = "Deserializing a NuiValue<List<int>> creates a valid JSON structure.")]
public void DeserializeNuiValueIntListReturnsValidJsonStructure()
{
NuiValue<List<int>> test = JsonUtility.FromJson<NuiValue<List<int>>>(@"[1,2,3]");
List<int> expected = new List<int> { 1, 2, 3 };

Assert.That(test.Value, Is.EqualTo(expected));
}
}
}
35 changes: 35 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Nui/Layout/NuiColumnTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using Anvil.API;
using NUnit.Framework;

namespace Anvil.Tests.API
{
[TestFixture(Category = "API.Nui")]
public sealed class NuiColumnTests
{
[Test(Description = "Serializing a NuiColumn creates a valid JSON structure.")]
public void SerializeNuiColumnReturnsValidJsonStructure()
{
NuiColumn nuiColumn = new NuiColumn
{
Id = "test_column",
Aspect = 1.5f,
Enabled = new NuiBind<bool>("enabled_bind"),
Height = 10f,
Margin = 2f,
Padding = 3f,
ForegroundColor = new NuiBind<Color>("color_bind"),
Tooltip = "test_tooltip",
Width = 100f,
Visible = false,
Children = new List<NuiElement>
{
new NuiLabel("test"),
new NuiRow(),
},
};

Assert.That(JsonUtility.ToJson(nuiColumn), 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}"));
}
}
}
39 changes: 39 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Nui/Layout/NuiGroupTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;
using Anvil.API;
using NUnit.Framework;

namespace Anvil.Tests.API
{
[TestFixture(Category = "API.Nui")]
public sealed class NuiGroupTests
{
[Test(Description = "Serializing a NuiGroup creates a valid JSON structure.")]
public void SerializeNuiGroupReturnsValidJsonStructure()
{
NuiGroup nuiGroup = new NuiGroup
{
Id = "test_group",
Aspect = 1.5f,
Border = true,
Enabled = new NuiBind<bool>("enabled_bind"),
Height = 10f,
Margin = 2f,
Padding = 3f,
ForegroundColor = new NuiBind<Color>("color_bind"),
Scrollbars = NuiScrollbars.Both,
Tooltip = "test_tooltip",
Width = 100f,
Visible = false,
Layout = new NuiColumn
{
Children = new List<NuiElement>
{
new NuiLabel("Test"),
},
},
};

Assert.That(JsonUtility.ToJson(nuiGroup), Is.EqualTo(@"{""border"":true,""scrollbars"":3,""type"":""group"",""children"":[{""type"":""col"",""children"":[{""text_halign"":1,""value"":""Test"",""type"":""label"",""text_valign"":1}]}],""aspect"":1.5,""enabled"":{""bind"":""enabled_bind""},""foreground_color"":{""bind"":""color_bind""},""height"":10.0,""id"":""test_group"",""margin"":2.0,""padding"":3.0,""tooltip"":""test_tooltip"",""visible"":false,""width"":100.0}"));
}
}
}
35 changes: 35 additions & 0 deletions NWN.Anvil.Tests/src/main/API/Nui/Layout/NuiRowTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using Anvil.API;
using NUnit.Framework;

namespace Anvil.Tests.API
{
[TestFixture(Category = "API.Nui")]
public sealed class NuiRowTests
{
[Test(Description = "Serializing a NuiRow creates a valid JSON structure.")]
public void SerializeNuiRowReturnsValidJsonStructure()
{
NuiRow nuiRow = new NuiRow
{
Id = "test_row",
Aspect = 1.5f,
Enabled = new NuiBind<bool>("enabled_bind"),
Height = 10f,
Margin = 2f,
Padding = 3f,
ForegroundColor = new NuiBind<Color>("color_bind"),
Tooltip = "test_tooltip",
Width = 100f,
Visible = false,
Children = new List<NuiElement>
{
new NuiLabel("test"),
new NuiRow(),
},
};

Assert.That(JsonUtility.ToJson(nuiRow), Is.EqualTo(@"{""type"":""row"",""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_row"",""margin"":2.0,""padding"":3.0,""tooltip"":""test_tooltip"",""visible"":false,""width"":100.0}"));
}
}
}
Loading

0 comments on commit f82de2b

Please sign in to comment.