-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move json serialize/deserialize to JsonUtility. Add Nui tests (#471)
* 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.
- v8193.36.2-dev
- v8193.36.1
- v8193.36.1-dev
- v8193.36.0-dev
- v8193.35.3
- v8193.35.2
- v8193.35.2-dev
- v8193.35.1
- v8193.35.1-dev
- v8193.35.0
- v8193.35.0-dev
- v8193.34.28
- v8193.34.28-dev
- v8193.34.27
- v8193.34.27-dev
- v8193.34.26
- v8193.34.26-dev
- v8193.34.25
- v8193.34.25-dev
- v8193.34.24
- v8193.34.24-dev
- v8193.34.23
- v8193.34.23-dev
- v8193.34.22
- v8193.34.22-dev
- v8193.34.21
- v8193.34.21-dev
- v8193.34.20
- v8193.34.20-dev
- v8193.34.19
- v8193.34.19-dev
- v8193.34.18
- v8193.34.18-dev
- v8193.34.17
- v8193.34.17-dev
- v8193.34.16
- v8193.34.16-dev
- v8193.34.15
- v8193.34.15-dev
- v8193.34.14
- v8193.34.14-dev
- v8193.34.13
- v8193.34.13-dev
- v8193.34.12
- v8193.34.12-dev
- v8193.34.11
- v8193.34.11-dev
- v8193.34.10
- v8193.34.10-dev
- v8193.34.9
- v8193.34.9-dev
- v8193.34.8
- v8193.34.8-dev
- v8193.34.7
- v8193.34.7-dev
- v8193.34.6
- v8193.34.6-dev
- v8193.34.5
- v8193.34.5-dev
- v8193.34.4
- v8193.34.4-dev
- v8193.34.3
1 parent
0a00f14
commit f82de2b
Showing
21 changed files
with
762 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
182
NWN.Anvil.Tests/src/main/API/Nui/Bindings/NuiValueTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}")); | ||
} | ||
} | ||
} |
Oops, something went wrong.