Browse Source

Add tests for new `JSONPointer` methods

core_refactor
Anton Tarasenko 2 years ago
parent
commit
9bb471bdf4
  1. 514
      sources/Text/Tests/TEST_JSON.uc

514
sources/Text/Tests/TEST_JSON.uc

@ -1,6 +1,6 @@
/**
* Set of tests for functionality of JSON printing/parsing.
* Copyright 2021-2022 Anton Tarasenko
* Copyright 2021-2023 Anton Tarasenko
*------------------------------------------------------------------------------
* This file is part of Acedia.
*
@ -22,11 +22,18 @@ class TEST_JSON extends TestCase
var string simpleJSONObject, complexJSONObject;
protected static function JSONPointer MakePtr(string str)
{
return __().json.Pointer(__().text.FromString(str));
}
protected static function TESTS()
{
Test_Pointer();
Test_Print();
Test_Parse();
Test_Copy();
Test_Incrementing();
}
protected static function Test_Pointer()
@ -37,6 +44,10 @@ protected static function Test_Pointer()
SubTest_PointerPushPop();
SubTest_PointerNumeric();
SubTest_PopWithoutRemoving();
SubTest_Append();
SubText_Copy();
SubTest_StartsWith();
SubTest_IsComponentArrayApplicable();
}
protected static function SubTest_PointerCreate()
@ -187,6 +198,114 @@ protected static function SubTest_PopWithoutRemoving()
TEST_ExpectTrue(pointer.Pop(true).ToString() == "simple");
}
protected static function SubTest_Append()
{
local JSONPointer pointer, append;
Issue("Appending another JSON pointer is not working correctly.");
pointer = __().json.Pointer(P("/object/hey/1/there/"));
append = __().json.Pointer(P("/A/B/7/C"));
pointer.Append(append);
TEST_ExpectTrue(
pointer.ToText().ToString()
== "/object/hey/1/there//A/B/7/C");
pointer = __().json.Pointer(P(""));
append = __().json.Pointer(P("/A/B/7/C"));
pointer.Append(append);
TEST_ExpectTrue(pointer.ToText().ToString() == "/A/B/7/C");
pointer = __().json.Pointer(P("/object/hey/1/there/"));
append = __().json.Pointer(P(""));
pointer.Append(append);
TEST_ExpectTrue(pointer.ToText().ToString() == "/object/hey/1/there/");
pointer = __().json.Pointer(P("/object/hey/1/there/"));
pointer.Append(none);
TEST_ExpectTrue(pointer.ToText().ToString() == "/object/hey/1/there/");
}
protected static function SubText_Copy()
{
Issue("JSON pointer's `Copy()` method does not correctly copy the whole"
@ "pointer.");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy().ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("/").Copy().ToText().ToString() == "/");
TEST_ExpectTrue(MakePtr("").Copy().ToText().ToString() == "");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(0, 4).ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(-2).ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(0, 7).ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(-3, 7).ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(-5, 20).ToText().ToString()
== "/A/B/3/D");
TEST_ExpectTrue(MakePtr("").Copy(-1).ToText().ToString() == "");
Issue("JSON pointer's `Copy()` method does not correctly copy empty range"
@ "of the pointer.");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(-5, 4).ToText().ToString() == "");
TEST_ExpectTrue(MakePtr("/A/B/3/D").Copy(4, 11).ToText().ToString() == "");
Issue("JSON pointer's `Copy()` method does not correctly copy partial"
@ "intersection range of the pointer.");
TEST_ExpectTrue(MakePtr("/A//3/D").Copy(-5, 8).ToText().ToString()
== "/A//3"); // left
TEST_ExpectTrue(MakePtr("/A//3/D").Copy(1, 11).ToText().ToString()
== "//3/D"); // right
}
protected static function SubTest_StartsWith()
{
local JSONPointer pointer;
Issue("Any pointers start with `none` JSON pointer.");
TEST_ExpectTrue(__().json.Pointer(P("/A/B/C")).StartsWith(none));
TEST_ExpectTrue(__().json.Pointer(P("/")).StartsWith(none));
TEST_ExpectTrue(__().json.Pointer(P("")).StartsWith(none));
Issue("`StartsWith()` correctly detects JSON pointers that are actually"
@ "their prefixes.");
TEST_ExpectTrue(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P("/A/7/C"))));
// Same, but constructed manually to handle components added as numeric
pointer = __().json.Pointer().Push(P("A")).PushNumeric(7).Push(P("C"));
TEST_ExpectTrue(pointer.StartsWith(__().json.Pointer(P("/A/7/C"))));
TEST_ExpectTrue(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P("/A/7"))));
TEST_ExpectTrue(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P(""))));
TEST_ExpectTrue(__().json.Pointer(P(""))
.StartsWith(__().json.Pointer(P(""))));
Issue("`StartsWith()` correctly detects JSON pointers that aren't actually"
@ "their prefixes.");
TEST_ExpectFalse(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P("/A/3/C"))));
// Constructed manually to handle components added as numeric
pointer = __().json.Pointer().Push(P("A")).PushNumeric(8).Push(P("C"));
TEST_ExpectFalse(pointer.StartsWith(__().json.Pointer(P("/A/3/C"))));
TEST_ExpectFalse(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P("/A/7/"))));
TEST_ExpectFalse(__().json.Pointer(P("/A/7/C"))
.StartsWith(__().json.Pointer(P("/"))));
}
protected static function SubTest_IsComponentArrayApplicable()
{
Issue("`IsComponentArrayApplicable()` method wrongly detects numeric"
@ "components.");
TEST_ExpectFalse(
__().json.Pointer(P("/A/B/C")).IsComponentArrayApplicable(0));
TEST_ExpectTrue(
__().json.Pointer(P("/A/2/C")).IsComponentArrayApplicable(1));
TEST_ExpectTrue(
__().json.Pointer(P("/A/B/-")).IsComponentArrayApplicable(2));
TEST_ExpectFalse(
__().json.Pointer(P("/A/7/C")).IsComponentArrayApplicable(-2));
TEST_ExpectFalse(
__().json.Pointer(P("/A/7/C")).IsComponentArrayApplicable(10));
}
protected static function Test_Print()
{
Context("Testing printing simple JSON values.");
@ -651,6 +770,399 @@ protected static function SubTest_ParseComplex()
TEST_ExpectTrue(FloatBox(inner.GetItem(P("maybe"))).Get() == 0.003);
}
protected static function Test_Copy()
{
Context("Testing method for copying JSON values.");
SubTest_CopySimple();
SubTest_CopyComplex();
}
protected static function SubTest_CopySimple()
{
Issue("JSON's `Copy()` method incorrectly copies copy boxed built-in"
@ "types.");
TEST_ExpectNone(__().json.Copy(none));
TEST_ExpectTrue(
BoolBox(__().json.Copy(__().box.bool(true))).Get()
== true);
TEST_ExpectTrue(
BoolRef(__().json.Copy(__().ref.bool(false))).Get()
== false);
TEST_ExpectTrue(IntBox(__().json.Copy(__().box.int(-7))).Get() == -7);
TEST_ExpectTrue(
IntRef(__().json.Copy(__().ref.int(234234))).Get()
== 234234);
TEST_ExpectTrue(
FloatBox(__().json.Copy(__().box.float(3.76))).Get()
== 3.76);
TEST_ExpectTrue(
FloatRef(__().json.Copy(__().ref.float(-213.1))).Get()
== -213.1);
TEST_ExpectTrue(Text(__().json.Copy(P("Hey!"))).ToString() == "Hey!");
TEST_ExpectTrue(
MutableText(__().json.Copy(__().text.FromStringM("Hey!")))
.ToString()
== "Hey!");
}
protected static function HashTable ConstructComplexJSONObject()
{
local HashTable result, innerObject, deepObject, oneMoreObject;
local ArrayList innerArray;
deepObject = __().collections.EmptyHashTable();
deepObject.SetItem(P("something \"here\""), P("yes"));
deepObject.SetFloat(P("maybe"), 0.003);
innerArray = __().collections.EmptyArrayList();
innerArray.AddString("Engine.Actor");
innerArray.AddBool(false);
innerArray.AddItem(none);
innerArray.AddItem(deepObject);
innerArray.AddFloat(56.6);
oneMoreObject = __().collections.EmptyHashTable();
oneMoreObject.SetInt(P("nope"), 324532);
oneMoreObject.SetBool(P("whatever"), false);
oneMoreObject.SetString(P("o rly?"), "ya rly");
innerObject = __().collections.EmptyHashTable();
innerObject.SetBool(P("my_bool"), true);
innerObject.SetItem(P("array"), innerArray);
innerObject.SetItem(P("one more"), oneMoreObject);
innerObject.SetInt(P("my_int"), -9823452);
result = __().collections.EmptyHashTable();
result.SetItem(P("innerObject"), innerObject);
result.SetFloat(P("some_var"), -7.32);
result.SetString(P("another_var"), "aye!");
return result;
}
protected static function SubTest_CopyComplex()
{
local HashTable complexCopy;
complexCopy = HashTable(__().json.Copy(ConstructComplexJSONObject()));
TEST_ExpectTrue(complexCopy.GetBoolBy(P("/innerObject/my_bool")) == true);
TEST_ExpectTrue(
complexCopy.GetStringBy(P("/innerObject/array/0")) == "Engine.Actor");
TEST_ExpectTrue(
complexCopy.GetBoolBy(P("/innerObject/array/1")) == false);
TEST_ExpectTrue(
complexCopy.GetItemBy(P("/innerObject/array/2")) == none);
TEST_ExpectTrue(
complexCopy.GetStringBy(P("/innerObject/array/3/something \"here\""))
== "yes");
TEST_ExpectTrue(
complexCopy.GetFloatBy(P("/innerObject/array/3/maybe")) == 0.003);
TEST_ExpectTrue(
complexCopy.GetFloatBy(P("/innerObject/array/4")) == 56.6);
TEST_ExpectTrue(
complexCopy.GetIntBy(P("/innerObject/one more/nope")) == 324532);
TEST_ExpectTrue(
complexCopy.GetBoolBy(P("/innerObject/one more/whatever")) == false);
TEST_ExpectTrue(
complexCopy.GetStringBy(P("/innerObject/one more/o rly?")) == "ya rly");
TEST_ExpectTrue(
complexCopy.GetIntBy(P("/innerObject/my_int")) == -9823452);
TEST_ExpectTrue(
complexCopy.GetFloatBy(P("/some_var")) == -7.32);
TEST_ExpectTrue(
complexCopy.GetStringBy(P("/another_var")) == "aye!");
}
protected static function Test_Incrementing()
{
Context("Testing incrementing JSON values with `_.json.Increment()`.");
SubTest_Incrementing_Null();
SubTest_Incrementing_Bool();
SubTest_Incrementing_Number();
SubTest_Incrementing_String();
SubTest_Incrementing_Array();
SubTest_Incrementing_Object();
SubTest_Incrementing_Incompatible();
}
protected static function SubTest_Incrementing_Null()
{
local FloatRef ref;
Issue("Null values aren't incremented correctly.");
TEST_ExpectNone(__().json.Increment(none, none));
TEST_ExpectTrue(BoolBox(__().json.Increment(
none, __().box.bool(true))).Get() == true);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.float(11.5), none)).Get() == 11.5);
Issue("Incrementing null values simply copies reference.");
ref = __().ref.float(1032423.91);
TEST_ExpectFalse(__().json.Increment(ref, none) == none);
TEST_ExpectFalse(__().json.Increment(none, ref) == none);
}
protected static function SubTest_Incrementing_Bool()
{
Issue("Boolean values aren't incremented correctly.");
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(false), __().box.bool(false))).Get() == false);
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(true), __().box.bool(false))).Get() == true);
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(false), __().box.bool(true))).Get() == true);
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(true), __().box.bool(true))).Get() == true);
Issue("Incrementing boolean values produces incorrect type.");
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(false), __().box.bool(true))).Get() == true);
TEST_ExpectTrue(BoolBox(__().json.Increment(
__().box.bool(false), __().ref.bool(true))).Get() == true);
TEST_ExpectTrue(BoolRef(__().json.Increment(
__().ref.bool(false), __().box.bool(true))).Get() == true);
TEST_ExpectTrue(BoolRef(__().json.Increment(
__().ref.bool(false), __().ref.bool(true))).Get() == true);
}
protected static function SubTest_Incrementing_Number()
{
SubSubTest_Incrementing_Number_Pure();
SubSubTest_Incrementing_Number_Mixed();
}
protected static function SubSubTest_Incrementing_Number_Pure()
{
Issue("Numeric values aren't incremented correctly (for boxed `int`s).");
TEST_ExpectTrue(IntBox(__().json.Increment(
__().box.int(3), __().box.int(-3))).Get() == 0);
TEST_ExpectTrue(IntBox(__().json.Increment(
__().box.int(-4), __().ref.int(11))).Get() == 7);
TEST_ExpectTrue(IntRef(__().json.Increment(
__().ref.int(124), __().box.int(624))).Get() == 748);
TEST_ExpectTrue(IntRef(__().json.Increment(
__().ref.int(345), __().ref.int(-23423))).Get() == -23078);
Issue("Numeric values aren't incremented correctly (for boxed `float`s).");
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.float(11.2), __().box.float(-0.2))).Get() == 11);
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.float(1012.78), __().ref.float(0.12))).Get() == 1012.9);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.float(12), __().box.float(13))).Get() == 25);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.float(-0.32), __().ref.float(0.32))).Get() == 0);
}
protected static function SubSubTest_Incrementing_Number_Mixed()
{
Issue("Numeric values aren't incremented correctly (for mixed `int`s and"
@ "`float`s).");
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.float(11.2), __().box.int(0))).Get() == 11.2);
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.float(1012.78), __().ref.int(2))).Get() == 1014.78);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.float(12), __().box.int(13))).Get() == 25);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.float(-0.32), __().ref.int(14))).Get() == 13.68);
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.int(11), __().box.float(-0.2))).Get() == 10.8);
TEST_ExpectTrue(FloatBox(__().json.Increment(
__().box.int(1012), __().ref.float(7.12))).Get() == 1019.12);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.int(12), __().box.float(13.1))).Get() == 25.1);
TEST_ExpectTrue(FloatRef(__().json.Increment(
__().ref.int(-10), __().ref.float(0.32))).Get() == -9.68);
}
protected static function SubTest_Incrementing_String()
{
Issue("String values aren't incremented correctly.");
TEST_ExpectTrue(Text(__().json.Increment(
__().text.FromString("Whatever"), __().text.FromString("revetahW")))
.ToString() == "WhateverrevetahW");
TEST_ExpectTrue(MutableText(__().json.Increment(
__().text.FromStringM("Whatever"), __().text.FromString("revetahW")))
.ToString() == "WhateverrevetahW");
TEST_ExpectTrue(Text(__().json.Increment(
__().text.FromString("Whatever"), __().text.FromStringM("revetahW")))
.ToString() == "WhateverrevetahW");
TEST_ExpectTrue(MutableText(__().json.Increment(
__().text.FromStringM("Whatever"), __().text.FromStringM("revetahW")))
.ToString() == "WhateverrevetahW");
}
protected static function SubTest_Incrementing_Array()
{
local ArrayList array1, array2, result;
Issue("Array values aren't incremented correctly.");
array1 = __().collections.EmptyArrayList();
array2 = __().collections.EmptyArrayList();
array1.AddItem(__().box.int(5));
array2.AddItem(__().box.int(3));
array2.AddItem(__().box.int(-7));
result = ArrayList(__().json.Increment(array1, array2));
TEST_ExpectTrue(__().json.Print(result).ToString() == "[5,3,-7]");
TEST_ExpectTrue(result.GetItem(0) != array1.GetItem(0));
TEST_ExpectTrue(result.GetItem(1) != array2.GetItem(0));
TEST_ExpectTrue(result.GetItem(2) != array2.GetItem(1));
Issue("Incrementing array values incorrectly handles reference counts.");
// +1 after copy, +2 after getters (before and here)
TEST_ExpectTrue(result.GetItem(0)._getRefCount() == 3);
TEST_ExpectTrue(result.GetItem(1)._getRefCount() == 3);
TEST_ExpectTrue(result.GetItem(2)._getRefCount() == 3);
TEST_ExpectTrue(array1._getRefCount() == 1);
TEST_ExpectTrue(array2._getRefCount() == 1);
TEST_ExpectTrue(result._getRefCount() == 1);
}
protected static function SubTest_Incrementing_Object()
{
local HashTable table1, table2, result;
Issue("Object values aren't incremented correctly.");
table1 = __().collections.EmptyHashTable();
table2 = __().collections.EmptyHashTable();
table1.Setitem(P("A"), __().box.int(5));
table2.Setitem(P("B"), __().box.int(3));
table2.Setitem(P("C"), __().box.int(-7));
result = HashTable(__().json.Increment(table1, table2));
TEST_ExpectTrue(result.GetLength() == 3);
TEST_ExpectTrue(result.GetInt(P("A")) == 5);
TEST_ExpectTrue(result.GetInt(P("B")) == 3);
TEST_ExpectTrue(result.GetInt(P("C")) == -7);
TEST_ExpectTrue(result.GetItem(P("A")) != table1.GetItem(P("A")));
TEST_ExpectTrue(result.GetItem(P("B")) != table2.GetItem(P("B")));
TEST_ExpectTrue(result.GetItem(P("C")) != table2.GetItem(P("C")));
// +1 after copy, +2 after getters (before and here)
Issue("Incrementing object values incorrectly handles reference counts.");
TEST_ExpectTrue(result.GetItem(P("A"))._getRefCount() == 3);
TEST_ExpectTrue(result.GetItem(P("B"))._getRefCount() == 3);
TEST_ExpectTrue(result.GetItem(P("C"))._getRefCount() == 3);
TEST_ExpectTrue(table1._getRefCount() == 1);
TEST_ExpectTrue(table2._getRefCount() == 1);
TEST_ExpectTrue(result._getRefCount() == 1);
}
protected static function SubTest_Incrementing_Incompatible()
{
Issue("Incrementing with incompatible values doesn't produce `none`.");
SubSubTest_Incrementing_Incompatible_bool();
SubSubTest_Incrementing_Incompatible_int();
SubSubTest_Incrementing_Incompatible_float();
SubSubTest_Incrementing_Incompatible_text();
SubSubTest_Incrementing_Incompatible_arraylist();
SubSubTest_Incrementing_Incompatible_hashtable();
}
protected static function SubSubTest_Incrementing_Incompatible_bool()
{
TEST_ExpectNone(__().json.Increment(
__().box.bool(true),
__().ref.int(32)));
TEST_ExpectNone(__().json.Increment(
__().box.bool(true),
__().ref.float(32.5)));
TEST_ExpectNone(__().json.Increment(
__().box.bool(true),
__().text.FromString("Hello there!")));
TEST_ExpectNone(__().json.Increment(
__().box.bool(true),
__().collections.EmptyArrayList()));
TEST_ExpectNone(__().json.Increment(
__().box.bool(true),
__().collections.EmptyHashTable()));
}
protected static function SubSubTest_Incrementing_Incompatible_int()
{
TEST_ExpectNone(__().json.Increment(
__().box.int(3),
__().ref.bool(false)));
TEST_ExpectNone(__().json.Increment(
__().box.int(234),
__().text.FromString("Hello there!")));
TEST_ExpectNone(__().json.Increment(
__().box.int(2),
__().collections.EmptyArrayList()));
TEST_ExpectNone(__().json.Increment(
__().box.int(782),
__().collections.EmptyHashTable()));
}
protected static function SubSubTest_Incrementing_Incompatible_float()
{
TEST_ExpectNone(__().json.Increment(
__().box.float(3),
__().ref.bool(false)));
TEST_ExpectNone(__().json.Increment(
__().box.float(234),
__().text.FromString("Hello there!")));
TEST_ExpectNone(__().json.Increment(
__().box.float(2),
__().collections.EmptyArrayList()));
TEST_ExpectNone(__().json.Increment(
__().box.float(782),
__().collections.EmptyHashTable()));
}
protected static function SubSubTest_Incrementing_Incompatible_text()
{
TEST_ExpectNone(__().json.Increment(
__().text.FromString("yo"),
__().ref.bool(true)));
TEST_ExpectNone(__().json.Increment(
__().text.FromString("yo"),
__().ref.int(32)));
TEST_ExpectNone(__().json.Increment(
__().text.FromString("yo"),
__().ref.float(32.5)));
TEST_ExpectNone(__().json.Increment(
__().text.FromString("yo"),
__().collections.EmptyArrayList()));
TEST_ExpectNone(__().json.Increment(
__().text.FromString("yo"),
__().collections.EmptyHashTable()));
}
protected static function SubSubTest_Incrementing_Incompatible_arraylist()
{
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyArrayList(),
__().ref.bool(true)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyArrayList(),
__().ref.int(32)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyArrayList(),
__().ref.float(32.5)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyArrayList(),
__().text.FromString("Not a collection!")));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyArrayList(),
__().collections.EmptyHashTable()));
}
protected static function SubSubTest_Incrementing_Incompatible_hashtable()
{
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyHashTable(),
__().ref.bool(true)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyHashTable(),
__().ref.int(32)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyHashTable(),
__().ref.float(32.5)));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyHashTable(),
__().text.FromString("Not a collection!")));
TEST_ExpectNone(__().json.Increment(
__().collections.EmptyHashTable(),
__().collections.EmptyArrayList()));
}
defaultproperties
{
caseName = "JSON"

Loading…
Cancel
Save