From 5b5c7a19125474f9d78ab72d76b5700ac6f73683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Tue, 10 Sep 2024 16:47:46 +0200 Subject: [PATCH 1/4] avoid, that a element can be inserted twice --- src/Document/JsonDocument.st | 4 + src/Elements/AbstractJsonElement.st | 4 +- test/Deserializer/TestFindKeyInBuffer.st | 218 +++---- test/Deserializer/TryParse/Test_TryParse.st | 614 +++++++++--------- .../TestNoElementsMustNotBeAddedTwice.st | 23 + test/StringAssert.st | 2 +- 6 files changed, 447 insertions(+), 418 deletions(-) create mode 100644 test/Document/TestNoElementsMustNotBeAddedTwice.st diff --git a/src/Document/JsonDocument.st b/src/Document/JsonDocument.st index 8ec12a0..584399b 100644 --- a/src/Document/JsonDocument.st +++ b/src/Document/JsonDocument.st @@ -126,6 +126,10 @@ NAMESPACE Simatic.Ax.Json _lastElem : IJsonElement; END_VAR _elemMuted ?= elem; + IF (elem = _lastElement) THEN + AddElement := NULL; + RETURN; + END_IF; IF (_firstElement = NULL) THEN _firstElement := _elemMuted; _lastElement := _elemMuted; diff --git a/src/Elements/AbstractJsonElement.st b/src/Elements/AbstractJsonElement.st index aea62ae..fcf03fa 100644 --- a/src/Elements/AbstractJsonElement.st +++ b/src/Elements/AbstractJsonElement.st @@ -60,7 +60,9 @@ NAMESPACE Simatic.Ax.Json VAR_INPUT elem : IJsonElement; END_VAR - _next ?= elem; + IF _next = NULL THEN + _next ?= elem; + END_IF; END_METHOD END_CLASS diff --git a/test/Deserializer/TestFindKeyInBuffer.st b/test/Deserializer/TestFindKeyInBuffer.st index 54f6e86..d96fd8f 100644 --- a/test/Deserializer/TestFindKeyInBuffer.st +++ b/test/Deserializer/TestFindKeyInBuffer.st @@ -1,118 +1,118 @@ -USING Simatic.Ax.Conversion; -USING AxUnit.Assert; -USING System.Strings; -USING Simatic.Ax.Json; +// USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error +// USING AxUnit.Assert; +// USING System.Strings; +// USING Simatic.Ax.Json; -NAMESPACE Deserializer +// NAMESPACE Deserializer - NAMESPACE FindKeys +// NAMESPACE FindKeys - {TestFixture} - CLASS Test_FindKeyInBuffer - VAR PROTECTED - deserializer : Deserializer; - buffer : ARRAY[0..999] OF CHAR; - key : STRING; - value : STRING; - testIndex1 : INT; - testIndex2 : INT; - END_VAR +// {TestFixture} +// CLASS Test_FindKeyInBuffer +// VAR PROTECTED +// deserializer : Deserializer; +// buffer : ARRAY[0..999] OF CHAR; +// key : STRING; +// value : STRING; +// testIndex1 : INT; +// testIndex2 : INT; +// END_VAR - {Test} - METHOD PUBLIC Test_FindKeysPosition_in_JSON_SimpleEntries - VAR - JSON_Entry : STRING := '{"key ": " Elem ent1", "Hello": "something", "Nothing": 456, "Key 2": 1234 }'; - len : DINT; - keyStart : INT; - keyEnd : INT; - keyFound : BOOL; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - key := 'key '; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - key := 'Key 2'; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - key := 'not a key'; - keyFound := deserializer.TryParse(key, value); - Equal(FALSE, keyFound); - END_METHOD +// {Test} +// METHOD PUBLIC Test_FindKeysPosition_in_JSON_SimpleEntries +// VAR +// JSON_Entry : STRING := '{"key ": " Elem ent1", "Hello": "something", "Nothing": 456, "Key 2": 1234 }'; +// len : DINT; +// keyStart : INT; +// keyEnd : INT; +// keyFound : BOOL; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// key := 'key '; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// key := 'Key 2'; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// key := 'not a key'; +// keyFound := deserializer.TryParse(key, value); +// Equal(FALSE, keyFound); +// END_METHOD - {Test} - METHOD PUBLIC Test_FindKeysPosition_in_JSON_Single_Nested_Entry - VAR - JSON_Entry : STRING := '{"nested": {"nested1" : "hello", "nested2" : 1234 }}'; - len : DINT; - keyStart : INT; - keyEnd : INT; - keyFound : BOOL; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - key := 'nested'; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - END_METHOD +// {Test} +// METHOD PUBLIC Test_FindKeysPosition_in_JSON_Single_Nested_Entry +// VAR +// JSON_Entry : STRING := '{"nested": {"nested1" : "hello", "nested2" : 1234 }}'; +// len : DINT; +// keyStart : INT; +// keyEnd : INT; +// keyFound : BOOL; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// key := 'nested'; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// END_METHOD - {Test} - METHOD PUBLIC Test_FindKeysPosition_with_JSON_Nested_Entry - VAR - JSON_Entry : STRING := '{"key ": " Elem ent1", "nested": {"nested1" : "hello", "nested2" : 1234 }, "key2": " Elem ent1"}'; - len : DINT; - keyStart : INT; - keyEnd : INT; - keyFound : BOOL; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - key := 'key '; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - key := 'nested'; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - key := 'key2'; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - END_METHOD +// {Test} +// METHOD PUBLIC Test_FindKeysPosition_with_JSON_Nested_Entry +// VAR +// JSON_Entry : STRING := '{"key ": " Elem ent1", "nested": {"nested1" : "hello", "nested2" : 1234 }, "key2": " Elem ent1"}'; +// len : DINT; +// keyStart : INT; +// keyEnd : INT; +// keyFound : BOOL; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// key := 'key '; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// key := 'nested'; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// key := 'key2'; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// END_METHOD - {Test} - METHOD PUBLIC Test_FindKeysPosition_with_JSON_SimpleArray - VAR - JSON_Entry : STRING := '{ "simpleArray": [123, 456]}'; - len : DINT; - keyStart : INT; - keyEnd : INT; - keyFound : BOOL; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - key := 'simpleArray'; - keyFound := deserializer.TryParse(key, value); - Equal(TRUE, keyFound); - END_METHOD -//Comment in to test the private KeyIsInIndexSpan method -// {Test} -// Method Public Test_KeyIsInIndexSpan_FindsKey -// VAR -// len:DINT; -// keyStart : int := 0; -// keyEnd : int := 2; -// keyFound: BOOL; -// END_VAR -// key := 'key'; -// len:= Strings.ToArray.OfCharCount(str := key, arr := buffer); -// deserializer.SetBuffer(REF(buffer)); -// keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); -// Equal(TRUE, keyFound); -// key := 'not'; -// keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); -// Equal(FALSE, keyFound); -// END_Method - END_CLASS +// {Test} +// METHOD PUBLIC Test_FindKeysPosition_with_JSON_SimpleArray +// VAR +// JSON_Entry : STRING := '{ "simpleArray": [123, 456]}'; +// len : DINT; +// keyStart : INT; +// keyEnd : INT; +// keyFound : BOOL; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// key := 'simpleArray'; +// keyFound := deserializer.TryParse(key, value); +// Equal(TRUE, keyFound); +// END_METHOD +// //Comment in to test the private KeyIsInIndexSpan method +// // {Test} +// // Method Public Test_KeyIsInIndexSpan_FindsKey +// // VAR +// // len:DINT; +// // keyStart : int := 0; +// // keyEnd : int := 2; +// // keyFound: BOOL; +// // END_VAR +// // key := 'key'; +// // len:= Strings.ToArray.OfCharCount(str := key, arr := buffer); +// // deserializer.SetBuffer(REF(buffer)); +// // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); +// // Equal(TRUE, keyFound); +// // key := 'not'; +// // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); +// // Equal(FALSE, keyFound); +// // END_Method +// END_CLASS - END_NAMESPACE +// END_NAMESPACE -END_NAMESPACE +// END_NAMESPACE diff --git a/test/Deserializer/TryParse/Test_TryParse.st b/test/Deserializer/TryParse/Test_TryParse.st index 4c45bed..6017137 100644 --- a/test/Deserializer/TryParse/Test_TryParse.st +++ b/test/Deserializer/TryParse/Test_TryParse.st @@ -1,330 +1,330 @@ -USING Simatic.Ax.Conversion; -USING AxUnit.Assert; -USING System.Strings; -USING Simatic.Ax.Json; +// USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error +// USING AxUnit.Assert; +// USING System.Strings; +// USING Simatic.Ax.Json; -NAMESPACE Deserializer +// NAMESPACE Deserializer - {TestFixture} - CLASS TryParse - VAR PROTECTED - deserializer : Deserializer; - buffer : ARRAY[0..999] OF CHAR; - END_VAR +// {TestFixture} +// CLASS TryParse +// VAR PROTECTED +// deserializer : Deserializer; +// buffer : ARRAY[0..999] OF CHAR; +// END_VAR - {Test} - METHOD PUBLIC Test_TryParse_Value_as_String - VAR - JSON_Entry : STRING := '{ "key1": "nestedkey1"}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : STRING; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal('nestedkey1', value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_Value_as_String +// VAR +// JSON_Entry : STRING := '{ "key1": "nestedkey1"}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : STRING; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal('nestedkey1', value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_Value_as_String2 - VAR - JSON_Entry : STRING := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; - key : STRING := 'Name'; - len : DINT; - keyFound : BOOL; - value : STRING; - END_VAR - JSON_Entry := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; - key := 'Name'; - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal('SIMATIC', value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_Value_as_String2 +// VAR +// JSON_Entry : STRING := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; +// key : STRING := 'Name'; +// len : DINT; +// keyFound : BOOL; +// value : STRING; +// END_VAR +// JSON_Entry := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; +// key := 'Name'; +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal('SIMATIC', value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_String - VAR - JSON_Entry : STRING := '{ "key1": {"nestedkey1": 123}}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : STRING; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal('{"nestedkey1": 123}', value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_String +// VAR +// JSON_Entry : STRING := '{ "key1": {"nestedkey1": 123}}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : STRING; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal('{"nestedkey1": 123}', value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_String_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : STRING; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal('123', value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_String_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : STRING; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal('123', value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_LINT - VAR - JSON_Entry : STRING := '{ "leftBoundary": -9223372036854775808, "rightBoundary": 9223372036854775807, "outofBoundaryLeft": 9223372036854775808, "outofBoundaryRight": -9223372036854775809}'; - key : STRING; - len : DINT; - keyFound : BOOL; - value : LINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - key := 'leftBoundary'; - keyFound := deserializer.TryParse(key, value); - Equal(LINT#-9223372036854775808, value); - key := 'rightBoundary'; - keyFound := deserializer.TryParse(key, value); - Equal(LINT#9223372036854775807, value); - key := 'outofBoundaryLeft'; - keyFound := deserializer.TryParse(key, value); - Equal(FALSE, keyFound); - Equal(0, value); - key := 'outofBoundaryRight'; - keyFound := deserializer.TryParse(key, value); - Equal(FALSE, keyFound); - Equal(0, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_LINT +// VAR +// JSON_Entry : STRING := '{ "leftBoundary": -9223372036854775808, "rightBoundary": 9223372036854775807, "outofBoundaryLeft": 9223372036854775808, "outofBoundaryRight": -9223372036854775809}'; +// key : STRING; +// len : DINT; +// keyFound : BOOL; +// value : LINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// key := 'leftBoundary'; +// keyFound := deserializer.TryParse(key, value); +// Equal(LINT#-9223372036854775808, value); +// key := 'rightBoundary'; +// keyFound := deserializer.TryParse(key, value); +// Equal(LINT#9223372036854775807, value); +// key := 'outofBoundaryLeft'; +// keyFound := deserializer.TryParse(key, value); +// Equal(FALSE, keyFound); +// Equal(0, value); +// key := 'outofBoundaryRight'; +// keyFound := deserializer.TryParse(key, value); +// Equal(FALSE, keyFound); +// Equal(0, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_LINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : LINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_LINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : LINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_SINT - VAR - JSON_Entry : STRING := '{ "key1": -128}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : SINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(-128, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_SINT +// VAR +// JSON_Entry : STRING := '{ "key1": -128}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : SINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(-128, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_SINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : SINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_SINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : SINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_DINT - VAR - JSON_Entry : STRING := '{ "key1": 1234}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : DINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(1234, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_DINT +// VAR +// JSON_Entry : STRING := '{ "key1": 1234}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : DINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(1234, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_DINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : SINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_DINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : SINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_INT - VAR - JSON_Entry : STRING := '{ "key1": 1234}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : INT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(1234, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_INT +// VAR +// JSON_Entry : STRING := '{ "key1": 1234}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : INT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(1234, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_INT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : INT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_INT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : INT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_USINT - VAR - JSON_Entry : STRING := '{ "key1": 123}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : USINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(USINT#123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_USINT +// VAR +// JSON_Entry : STRING := '{ "key1": 123}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : USINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(USINT#123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_USINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : USINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_USINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : USINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_UINT - VAR - JSON_Entry : STRING := '{ "key1": 1234}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : UINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(UINT#1234, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_UINT +// VAR +// JSON_Entry : STRING := '{ "key1": 1234}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : UINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(UINT#1234, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_UINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : UINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(UINT#123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_UINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : UINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(UINT#123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_UDINT - VAR - JSON_Entry : STRING := '{ "key1": 1234}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : UDINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(UDINT#1234, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_UDINT +// VAR +// JSON_Entry : STRING := '{ "key1": 1234}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : UDINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(UDINT#1234, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_UDINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : UDINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(UDINT#123, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_UDINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : UDINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(UDINT#123, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_ULINT - VAR - JSON_Entry : STRING := '{ "key1": 1234}'; - key : STRING := 'key1'; - len : DINT; - keyFound : BOOL; - value : ULINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(key, value); - Equal(ULINT#1234, value); - END_METHOD +// {Test} +// METHOD PUBLIC Test_TryParse_ULINT +// VAR +// JSON_Entry : STRING := '{ "key1": 1234}'; +// key : STRING := 'key1'; +// len : DINT; +// keyFound : BOOL; +// value : ULINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(key, value); +// Equal(ULINT#1234, value); +// END_METHOD - {Test} - METHOD PUBLIC Test_TryParse_ULINT_Nested - VAR - JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; - keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; - len : DINT; - keyFound : BOOL; - value : ULINT; - END_VAR - len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - keyFound := deserializer.TryParse(keyArray, value); - Equal(ULINT#123, value); - END_METHOD - END_CLASS +// {Test} +// METHOD PUBLIC Test_TryParse_ULINT_Nested +// VAR +// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; +// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; +// len : DINT; +// keyFound : BOOL; +// value : ULINT; +// END_VAR +// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); +// deserializer.buffer := REF(buffer); +// keyFound := deserializer.TryParse(keyArray, value); +// Equal(ULINT#123, value); +// END_METHOD +// END_CLASS -END_NAMESPACE +// END_NAMESPACE diff --git a/test/Document/TestNoElementsMustNotBeAddedTwice.st b/test/Document/TestNoElementsMustNotBeAddedTwice.st new file mode 100644 index 0000000..637fb7e --- /dev/null +++ b/test/Document/TestNoElementsMustNotBeAddedTwice.st @@ -0,0 +1,23 @@ +USING AxUnit.Assert; + +NAMESPACE Simatic.Ax.Json + {TestFixture} + CLASS TestNoElementsMustNotBeAddedTwice + VAR PROTECTED + e1 : JsonDInt := (Key := 'Element1', Value := 1); + e2 : JsonDInt := (Key := 'Element2', Value := 1); + doc : JsonDocument; + END_VAR + {Test} + METHOD PUBLIC Add_e1_twice_returns_error + VAR_TEMP + result : IJsonContainerElement; + END_VAR + + result := doc.AddElement(e1); + result := doc.AddElement(e1); + Equal(expected := true, actual := result = NULL); + ; + END_METHOD + END_CLASS +END_NAMESPACE diff --git a/test/StringAssert.st b/test/StringAssert.st index a64f93c..31fb421 100644 --- a/test/StringAssert.st +++ b/test/StringAssert.st @@ -15,7 +15,7 @@ NAMESPACE AxUnit.Assert IF actual = expected THEN axunit_Succeed(); ELSE - axunit_Fail(Concat('Expected ', expected, ', but found ', actual), file, line); + axunit_Fail(Concat('Expected ', ', but found '), file, line); END_IF; END_FUNCTION From e5f072f8173f3bb9ddab0f002347ea8579a627c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Tue, 10 Sep 2024 16:48:01 +0200 Subject: [PATCH 2/4] use latest AxUnit --- apax-lock.json | 130 +++++++++++++++++++++++++------------------------ apax.yml | 4 +- 2 files changed, 69 insertions(+), 65 deletions(-) diff --git a/apax-lock.json b/apax-lock.json index 25c3d2e..c1b7f48 100644 --- a/apax-lock.json +++ b/apax-lock.json @@ -2,13 +2,13 @@ "name": "@simatic-ax/json", "version": "0.0.0-placeholder", "lockFileVersion": "2", - "installStrategy": "strict", + "installStrategy": "overridable", "root": { "name": "@simatic-ax/json", "version": "0.0.0-placeholder", "dependencies": { - "@ax/system-strings": "7.0.17", - "@simatic-ax/conversion": "2405.0.0" + "@simatic-ax/conversion": "2405.0.0", + "@ax/axunitst": "^5.1.23" }, "devDependencies": { "@ax/sdk": "2405.0.0", @@ -50,16 +50,6 @@ "resolved": "https://npm.pkg.github.com/download/@simatic-ax/snippetscollection/1.0.0/60302d7e0da15a914ce0126503398063a42e1917", "dependencies": {} }, - "@ax/system-strings": { - "name": "@ax/system-strings", - "version": "7.0.17", - "integrity": "sha512-xrT2GzVqeTXVF5Nq7wXKwYTb9FqIV3F5DWWGGEUwZvrm5t2unyFqsBDXoFBWDicPuhUFaH0FUwp1NqvMSN95pQ==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/system-strings/-/system-strings-7.0.17.tgz", - "dependencies": { - "@ax/system-math": "7.0.17", - "@ax/system-datetime": "7.0.17" - } - }, "@simatic-ax/conversion": { "name": "@simatic-ax/conversion", "version": "2405.0.0", @@ -70,6 +60,19 @@ "@ax/system-strings": "7.0.17" } }, + "@ax/axunitst": { + "name": "@ax/axunitst", + "version": "5.1.23", + "integrity": "sha512-SAfV92MvnhERKpyZ3rH3gh91ymcrhvU3CRaezbuyVFt2HhZblR/4heF+wQ4mC+dj0Z1U4NHrCiyN1R0N7VuNkg==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst/-/axunitst-5.1.23.tgz", + "dependencies": { + "@ax/axunitst-library": "5.1.23", + "@ax/axunitst-test-director": "5.1.23", + "@ax/axunitst-docs": "5.1.23", + "@ax/build-native": "16.0.3" + }, + "deprecated": "" + }, "@ax/apax-build": { "name": "@ax/apax-build", "version": "1.0.0", @@ -80,18 +83,6 @@ "@ax/st-resources.stc-plugin": "^1.0.3" } }, - "@ax/axunitst": { - "name": "@ax/axunitst", - "version": "5.0.74", - "integrity": "sha512-MPHR89HABeJW9ypbcBKZLWlY23OIq5dw4Z1leGckwuK4GWGU1MUnw+yzzJ6zpw9xXWVHTIwZ45p7Oj/LQsqBhw==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst/-/axunitst-5.0.74.tgz", - "dependencies": { - "@ax/axunitst-library": "5.0.74", - "@ax/axunitst-test-director": "5.0.74", - "@ax/axunitst-docs": "5.0.74", - "@ax/build-native": "16.0.3" - } - }, "@ax/axunitst-ls-contrib": { "name": "@ax/axunitst-ls-contrib", "version": "5.0.74", @@ -263,52 +254,42 @@ "resolved": "https://registry.simatic-ax.siemens.io/@ax/system-math/-/system-math-7.0.17.tgz", "dependencies": {} }, - "@ax/system-datetime": { - "name": "@ax/system-datetime", + "@ax/system-strings": { + "name": "@ax/system-strings", "version": "7.0.17", - "integrity": "sha512-mgB6+Ln5eLXsGJb4nl6/sW7SwAipSrFvttMWGAJMOym3qx7F1MuAdcZ6NaRS0rV0mpyyTEzgG3sk2voMN1OK1Q==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/system-datetime/-/system-datetime-7.0.17.tgz", - "dependencies": {} - }, - "@ax/simatic-package-tool": { - "name": "@ax/simatic-package-tool", - "version": "1.0.3", - "integrity": "sha512-5f5k9y/fNSK657l/zszQxaj16SR9nYk+k4iskuuoPsScRFWr4joVvDfuCgPQ3EBzdyJDzkbXu2rItkoS2Dy84A==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/simatic-package-tool/-/simatic-package-tool-1.0.3.tgz", - "dependencies": {} - }, - "@ax/st-resources.stc-plugin": { - "name": "@ax/st-resources.stc-plugin", - "version": "1.0.5", - "integrity": "sha512-8qEF0A8qtmDMLpikoj52FcQzsF7gkrZWRFUCUtejNFgaRgAYTNI/VWKic3dWROrL3deflSha80lxzPJAeCQD0A==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/st-resources.stc-plugin/-/st-resources.stc-plugin-1.0.5.tgz", - "dependencies": {} + "integrity": "sha512-xrT2GzVqeTXVF5Nq7wXKwYTb9FqIV3F5DWWGGEUwZvrm5t2unyFqsBDXoFBWDicPuhUFaH0FUwp1NqvMSN95pQ==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/system-strings/-/system-strings-7.0.17.tgz", + "dependencies": { + "@ax/system-math": "7.0.17", + "@ax/system-datetime": "7.0.17" + } }, "@ax/axunitst-library": { "name": "@ax/axunitst-library", - "version": "5.0.74", - "integrity": "sha512-JA8f0tido8yggMfKBFBcVQ2XUWfu8Ts0egqeAVkZVD4O2DjCnf1kWMLjCrn/nT9ruLvilaobq9AqV4/ry1dErw==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-library/-/axunitst-library-5.0.74.tgz", + "version": "5.1.23", + "integrity": "sha512-0D0FFFqVLHgHDDSde8xZJx84Zvx4YnoGmxcq+H0x1D9hEwJIsOVQFSVsmR4MfomNNaE5840kQ5rMh9gR0FRR5Q==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-library/-/axunitst-library-5.1.23.tgz", "dependencies": { "@ax/system-strings": "^7.0.17" } }, "@ax/axunitst-test-director": { "name": "@ax/axunitst-test-director", - "version": "5.0.74", - "integrity": "sha512-Y8IfFPrFliUPzDcbvWM6o1JjOWozbcoZNaoefLZ2d+q5d1in6d3MElLoHGR7o0alyFyuqght8leEj82bwSbglw==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director/-/axunitst-test-director-5.0.74.tgz", + "version": "5.1.23", + "integrity": "sha512-ezbqpJdHyU7ig8cfiAtvb90CkAEhQVbmki1XPnCaS4XStixao0sTa06qcfLzzYZC2EVa1CxzRIfgXtyHgu+nMQ==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director/-/axunitst-test-director-5.1.23.tgz", "dependencies": { - "@ax/axunitst-test-director-linux-x64": "5.0.74", - "@ax/axunitst-test-director-win-x64": "5.0.74" + "@ax/axunitst-test-director-linux-x64": "5.1.23", + "@ax/axunitst-test-director-win-x64": "5.1.23" } }, "@ax/axunitst-docs": { "name": "@ax/axunitst-docs", - "version": "5.0.74", - "integrity": "sha512-QitNPL5avajAf8U4w8QlZnFaHev1ohxnKe5t07/H00rDlczn3y4ofEhe3VLdqSNak49i7uwSzUiEf/y7dJLRNw==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-docs/-/axunitst-docs-5.0.74.tgz", - "dependencies": {} + "version": "5.1.23", + "integrity": "sha512-nUjL4G8k0d+JKVSy+/alN947dZOgITDagj4uKSAnFib+BfyqDJsuX4afzv8hizUMpGTlepDODhVYJ75DMvu/Ug==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-docs/-/axunitst-docs-5.1.23.tgz", + "dependencies": {}, + "deprecated": "" }, "@ax/build-native": { "name": "@ax/build-native", @@ -320,6 +301,20 @@ "@ax/build-native-linux": "16.0.3" } }, + "@ax/simatic-package-tool": { + "name": "@ax/simatic-package-tool", + "version": "1.0.3", + "integrity": "sha512-5f5k9y/fNSK657l/zszQxaj16SR9nYk+k4iskuuoPsScRFWr4joVvDfuCgPQ3EBzdyJDzkbXu2rItkoS2Dy84A==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/simatic-package-tool/-/simatic-package-tool-1.0.3.tgz", + "dependencies": {} + }, + "@ax/st-resources.stc-plugin": { + "name": "@ax/st-resources.stc-plugin", + "version": "1.0.5", + "integrity": "sha512-8qEF0A8qtmDMLpikoj52FcQzsF7gkrZWRFUCUtejNFgaRgAYTNI/VWKic3dWROrL3deflSha80lxzPJAeCQD0A==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/st-resources.stc-plugin/-/st-resources.stc-plugin-1.0.5.tgz", + "dependencies": {} + }, "@ax/certificate-management-win-x64": { "name": "@ax/certificate-management-win-x64", "version": "1.1.2", @@ -662,11 +657,19 @@ ], "dependencies": {} }, + "@ax/system-datetime": { + "name": "@ax/system-datetime", + "version": "7.0.17", + "integrity": "sha512-mgB6+Ln5eLXsGJb4nl6/sW7SwAipSrFvttMWGAJMOym3qx7F1MuAdcZ6NaRS0rV0mpyyTEzgG3sk2voMN1OK1Q==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/system-datetime/-/system-datetime-7.0.17.tgz", + "dependencies": {}, + "deprecated": "" + }, "@ax/axunitst-test-director-linux-x64": { "name": "@ax/axunitst-test-director-linux-x64", - "version": "5.0.74", - "integrity": "sha512-iQk6c74Zyu3tcEJRtGZaihIGJ20VHXllZ/9lFkQ9ly6LfaijFBEnRgiY8i+Ho31AltfN8Q5STp3FeP26+6ooMw==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director-linux-x64/-/axunitst-test-director-linux-x64-5.0.74.tgz", + "version": "5.1.23", + "integrity": "sha512-vHEj+8zr5nsfg6tSC5dVLqYVgTtusYW9O0Rkx02ttiPdJa3seRcNgqMDmgdFp1QAb/YcsmhOTohWbvi8cPQcHg==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director-linux-x64/-/axunitst-test-director-linux-x64-5.1.23.tgz", "os": [ "linux" ], @@ -677,9 +680,9 @@ }, "@ax/axunitst-test-director-win-x64": { "name": "@ax/axunitst-test-director-win-x64", - "version": "5.0.74", - "integrity": "sha512-H2+LHDhLRmRDOr3T5XhCcHAnHG5WfQ6SJ2Jdx6cjqDp1OVtmiGnOU10L4S5gcg8UfV1JNeQIJ2VffgwDy0GpXA==", - "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director-win-x64/-/axunitst-test-director-win-x64-5.0.74.tgz", + "version": "5.1.23", + "integrity": "sha512-J/QImr7Fa9Gk+y+df9gp3md0f57HqsEOTdU/19yrOCvQ077PNjs4zl4E2uzWFlVbYo0Cer3JrL/8ARIXuKedpA==", + "resolved": "https://registry.simatic-ax.siemens.io/@ax/axunitst-test-director-win-x64/-/axunitst-test-director-win-x64-5.1.23.tgz", "os": [ "win32" ], @@ -719,7 +722,8 @@ "version": "7.0.52", "integrity": "sha512-iKLQAGv6Au/CsImP/RmCy0nRIUCQMjrAdgo/AmWrH3+MvPLjhowbsNSQsq1YUP34PbYI0ThH6tQjkUta2dFc7A==", "resolved": "https://registry.simatic-ax.siemens.io/@ax/st-docs/-/st-docs-7.0.52.tgz", - "dependencies": {} + "dependencies": {}, + "deprecated": "" } }, "workspaces": {} diff --git a/apax.yml b/apax.yml index d777b19..0727b76 100644 --- a/apax.yml +++ b/apax.yml @@ -20,15 +20,15 @@ targets: - '1500' - 'llvm' # Install Setting -installStrategy: strict +installStrategy: overridable apaxVersion: 3.2.1 # Dependencies devDependencies: "@ax/sdk": 2405.0.0 "@simatic-ax/snippetscollection": 1.0.0 dependencies: - "@ax/system-strings": 7.0.17 "@simatic-ax/conversion": 2405.0.0 + "@ax/axunitst": ^5.1.23 # Files, which will be shipped with the library files: - 'README.md' From 17f475b1cbd13ea903cf178d185c59a3fe99d026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Tue, 10 Sep 2024 16:55:51 +0200 Subject: [PATCH 3/4] enable all tests --- test/Deserializer/TestFindKeyInBuffer.st | 219 +++---- test/Deserializer/TryParse/Test_TryParse.st | 614 ++++++++++---------- 2 files changed, 417 insertions(+), 416 deletions(-) diff --git a/test/Deserializer/TestFindKeyInBuffer.st b/test/Deserializer/TestFindKeyInBuffer.st index d96fd8f..14f59fb 100644 --- a/test/Deserializer/TestFindKeyInBuffer.st +++ b/test/Deserializer/TestFindKeyInBuffer.st @@ -1,118 +1,119 @@ -// USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error -// USING AxUnit.Assert; -// USING System.Strings; -// USING Simatic.Ax.Json; +USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error +USING AxUnit.Assert; +USING System.Strings; +USING Simatic.Ax.Json; -// NAMESPACE Deserializer +NAMESPACE Deserializer -// NAMESPACE FindKeys + NAMESPACE FindKeys -// {TestFixture} -// CLASS Test_FindKeyInBuffer -// VAR PROTECTED -// deserializer : Deserializer; -// buffer : ARRAY[0..999] OF CHAR; -// key : STRING; -// value : STRING; -// testIndex1 : INT; -// testIndex2 : INT; -// END_VAR + {TestFixture} + CLASS Test_FindKeyInBuffer + VAR PROTECTED + deserializer : Deserializer; + buffer : ARRAY[0..999] OF CHAR; + key : STRING; + value : STRING; + testIndex1 : INT; + testIndex2 : INT; + END_VAR -// {Test} -// METHOD PUBLIC Test_FindKeysPosition_in_JSON_SimpleEntries -// VAR -// JSON_Entry : STRING := '{"key ": " Elem ent1", "Hello": "something", "Nothing": 456, "Key 2": 1234 }'; -// len : DINT; -// keyStart : INT; -// keyEnd : INT; -// keyFound : BOOL; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// key := 'key '; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// key := 'Key 2'; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// key := 'not a key'; -// keyFound := deserializer.TryParse(key, value); -// Equal(FALSE, keyFound); -// END_METHOD + {Test} + METHOD PUBLIC Test_FindKeysPosition_in_JSON_SimpleEntries + VAR + JSON_Entry : STRING := '{"key ": " Elem ent1", "Hello": "something", "Nothing": 456, "Key 2": 1234 }'; + len : DINT; + keyStart : INT; + keyEnd : INT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'key '; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + key := 'Key 2'; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + key := 'not a key'; + keyFound := deserializer.TryParse(key, value); + Equal(FALSE, keyFound); + END_METHOD -// {Test} -// METHOD PUBLIC Test_FindKeysPosition_in_JSON_Single_Nested_Entry -// VAR -// JSON_Entry : STRING := '{"nested": {"nested1" : "hello", "nested2" : 1234 }}'; -// len : DINT; -// keyStart : INT; -// keyEnd : INT; -// keyFound : BOOL; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// key := 'nested'; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// END_METHOD + {Test} + METHOD PUBLIC Test_FindKeysPosition_in_JSON_Single_Nested_Entry + VAR + JSON_Entry : STRING := '{"nested": {"nested1" : "hello", "nested2" : 1234 }}'; + len : DINT; + keyStart : INT; + keyEnd : INT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'nested'; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + END_METHOD -// {Test} -// METHOD PUBLIC Test_FindKeysPosition_with_JSON_Nested_Entry -// VAR -// JSON_Entry : STRING := '{"key ": " Elem ent1", "nested": {"nested1" : "hello", "nested2" : 1234 }, "key2": " Elem ent1"}'; -// len : DINT; -// keyStart : INT; -// keyEnd : INT; -// keyFound : BOOL; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// key := 'key '; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// key := 'nested'; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// key := 'key2'; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// END_METHOD + {Test} + METHOD PUBLIC Test_FindKeysPosition_with_JSON_Nested_Entry + VAR + JSON_Entry : STRING := '{"key ": " Elem ent1", "nested": {"nested1" : "hello", "nested2" : 1234 }, "key2": " Elem ent1"}'; + len : DINT; + keyStart : INT; + keyEnd : INT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'key '; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + key := 'nested'; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + key := 'key2'; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + END_METHOD -// {Test} -// METHOD PUBLIC Test_FindKeysPosition_with_JSON_SimpleArray -// VAR -// JSON_Entry : STRING := '{ "simpleArray": [123, 456]}'; -// len : DINT; -// keyStart : INT; -// keyEnd : INT; -// keyFound : BOOL; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// key := 'simpleArray'; -// keyFound := deserializer.TryParse(key, value); -// Equal(TRUE, keyFound); -// END_METHOD -// //Comment in to test the private KeyIsInIndexSpan method -// // {Test} -// // Method Public Test_KeyIsInIndexSpan_FindsKey -// // VAR -// // len:DINT; -// // keyStart : int := 0; -// // keyEnd : int := 2; -// // keyFound: BOOL; -// // END_VAR -// // key := 'key'; -// // len:= Strings.ToArray.OfCharCount(str := key, arr := buffer); -// // deserializer.SetBuffer(REF(buffer)); -// // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); -// // Equal(TRUE, keyFound); -// // key := 'not'; -// // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); -// // Equal(FALSE, keyFound); -// // END_Method -// END_CLASS + {Test} + METHOD PUBLIC Test_FindKeysPosition_with_JSON_SimpleArray + VAR + JSON_Entry : STRING := '{ "simpleArray": [123, 456]}'; + len : DINT; + keyStart : INT; + keyEnd : INT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'simpleArray'; + keyFound := deserializer.TryParse(key, value); + Equal(TRUE, keyFound); + END_METHOD + //Comment in to test the private KeyIsInIndexSpan method -// END_NAMESPACE + // {Test} + // METHOD PUBLIC Test_KeyIsInIndexSpan_FindsKey + // VAR + // len : DINT; + // keyStart : INT := 0; + // keyEnd : INT := 2; + // keyFound : BOOL; + // END_VAR + // key := 'key'; + // len := Strings.ToArray.OfCharCount(str := key, arr := buffer); + // deserializer.buffer := (REF(buffer)); + // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); + // Equal(TRUE, keyFound); + // key := 'not'; + // keyFound := deserializer.KeyIsInIndexSpan(key, keyStart, keyEnd); + // Equal(FALSE, keyFound); + // END_METHOD + END_CLASS -// END_NAMESPACE + END_NAMESPACE + +END_NAMESPACE diff --git a/test/Deserializer/TryParse/Test_TryParse.st b/test/Deserializer/TryParse/Test_TryParse.st index 6017137..330b2d2 100644 --- a/test/Deserializer/TryParse/Test_TryParse.st +++ b/test/Deserializer/TryParse/Test_TryParse.st @@ -1,330 +1,330 @@ -// USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error -// USING AxUnit.Assert; -// USING System.Strings; -// USING Simatic.Ax.Json; +USING Simatic.Ax.Conversion; // TODO Fix AxUnit Error +USING AxUnit.Assert; +USING System.Strings; +USING Simatic.Ax.Json; -// NAMESPACE Deserializer +NAMESPACE Deserializer -// {TestFixture} -// CLASS TryParse -// VAR PROTECTED -// deserializer : Deserializer; -// buffer : ARRAY[0..999] OF CHAR; -// END_VAR + {TestFixture} + CLASS TryParse + VAR PROTECTED + deserializer : Deserializer; + buffer : ARRAY[0..999] OF CHAR; + END_VAR -// {Test} -// METHOD PUBLIC Test_TryParse_Value_as_String -// VAR -// JSON_Entry : STRING := '{ "key1": "nestedkey1"}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : STRING; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal('nestedkey1', value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_Value_as_String + VAR + JSON_Entry : STRING := '{ "key1": "nestedkey1"}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : STRING; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal('nestedkey1', value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_Value_as_String2 -// VAR -// JSON_Entry : STRING := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; -// key : STRING := 'Name'; -// len : DINT; -// keyFound : BOOL; -// value : STRING; -// END_VAR -// JSON_Entry := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; -// key := 'Name'; -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal('SIMATIC', value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_Value_as_String2 + VAR + JSON_Entry : STRING := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; + key : STRING := 'Name'; + len : DINT; + keyFound : BOOL; + value : STRING; + END_VAR + JSON_Entry := '{"Name": "SIMATIC", data: {plc_type: 1518, OPC_SERVER_ENABLED: true}}'; + key := 'Name'; + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal('SIMATIC', value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_String -// VAR -// JSON_Entry : STRING := '{ "key1": {"nestedkey1": 123}}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : STRING; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal('{"nestedkey1": 123}', value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_String + VAR + JSON_Entry : STRING := '{ "key1": {"nestedkey1": 123}}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : STRING; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal('{"nestedkey1": 123}', value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_String_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : STRING; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal('123', value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_String_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : STRING; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal('123', value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_LINT -// VAR -// JSON_Entry : STRING := '{ "leftBoundary": -9223372036854775808, "rightBoundary": 9223372036854775807, "outofBoundaryLeft": 9223372036854775808, "outofBoundaryRight": -9223372036854775809}'; -// key : STRING; -// len : DINT; -// keyFound : BOOL; -// value : LINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// key := 'leftBoundary'; -// keyFound := deserializer.TryParse(key, value); -// Equal(LINT#-9223372036854775808, value); -// key := 'rightBoundary'; -// keyFound := deserializer.TryParse(key, value); -// Equal(LINT#9223372036854775807, value); -// key := 'outofBoundaryLeft'; -// keyFound := deserializer.TryParse(key, value); -// Equal(FALSE, keyFound); -// Equal(0, value); -// key := 'outofBoundaryRight'; -// keyFound := deserializer.TryParse(key, value); -// Equal(FALSE, keyFound); -// Equal(0, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_LINT + VAR + JSON_Entry : STRING := '{ "leftBoundary": -9223372036854775808, "rightBoundary": 9223372036854775807, "outofBoundaryLeft": 9223372036854775808, "outofBoundaryRight": -9223372036854775809}'; + key : STRING; + len : DINT; + keyFound : BOOL; + value : LINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'leftBoundary'; + keyFound := deserializer.TryParse(key, value); + Equal(LINT#-9223372036854775808, value); + key := 'rightBoundary'; + keyFound := deserializer.TryParse(key, value); + Equal(LINT#9223372036854775807, value); + key := 'outofBoundaryLeft'; + keyFound := deserializer.TryParse(key, value); + Equal(FALSE, keyFound); + Equal(0, value); + key := 'outofBoundaryRight'; + keyFound := deserializer.TryParse(key, value); + Equal(FALSE, keyFound); + Equal(0, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_LINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : LINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_LINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : LINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_SINT -// VAR -// JSON_Entry : STRING := '{ "key1": -128}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : SINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(-128, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_SINT + VAR + JSON_Entry : STRING := '{ "key1": -128}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : SINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(-128, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_SINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : SINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_SINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : SINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_DINT -// VAR -// JSON_Entry : STRING := '{ "key1": 1234}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : DINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(1234, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_DINT + VAR + JSON_Entry : STRING := '{ "key1": 1234}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : DINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(1234, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_DINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : SINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_DINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : SINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_INT -// VAR -// JSON_Entry : STRING := '{ "key1": 1234}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : INT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(1234, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_INT + VAR + JSON_Entry : STRING := '{ "key1": 1234}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : INT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(1234, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_INT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : INT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_INT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : INT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_USINT -// VAR -// JSON_Entry : STRING := '{ "key1": 123}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : USINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(USINT#123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_USINT + VAR + JSON_Entry : STRING := '{ "key1": 123}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : USINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(USINT#123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_USINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : USINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_USINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : USINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_UINT -// VAR -// JSON_Entry : STRING := '{ "key1": 1234}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : UINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(UINT#1234, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_UINT + VAR + JSON_Entry : STRING := '{ "key1": 1234}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : UINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(UINT#1234, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_UINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : UINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(UINT#123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_UINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : UINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(UINT#123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_UDINT -// VAR -// JSON_Entry : STRING := '{ "key1": 1234}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : UDINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(UDINT#1234, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_UDINT + VAR + JSON_Entry : STRING := '{ "key1": 1234}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : UDINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(UDINT#1234, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_UDINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : UDINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(UDINT#123, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_UDINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : UDINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(UDINT#123, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_ULINT -// VAR -// JSON_Entry : STRING := '{ "key1": 1234}'; -// key : STRING := 'key1'; -// len : DINT; -// keyFound : BOOL; -// value : ULINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(key, value); -// Equal(ULINT#1234, value); -// END_METHOD + {Test} + METHOD PUBLIC Test_TryParse_ULINT + VAR + JSON_Entry : STRING := '{ "key1": 1234}'; + key : STRING := 'key1'; + len : DINT; + keyFound : BOOL; + value : ULINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(key, value); + Equal(ULINT#1234, value); + END_METHOD -// {Test} -// METHOD PUBLIC Test_TryParse_ULINT_Nested -// VAR -// JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; -// keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; -// len : DINT; -// keyFound : BOOL; -// value : ULINT; -// END_VAR -// len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); -// deserializer.buffer := REF(buffer); -// keyFound := deserializer.TryParse(keyArray, value); -// Equal(ULINT#123, value); -// END_METHOD -// END_CLASS + {Test} + METHOD PUBLIC Test_TryParse_ULINT_Nested + VAR + JSON_Entry : STRING := '{ "aKey": "hallo", "anotherKey": 1234, "key1": {"nestedkey1": 123, "nestedkey2": "hallo", "nestedkey3": {"nestedInNestedkey1": 1234 }}}'; + keyArray : ARRAY[0..1] OF STRING := ['key1', 'nestedkey1']; + len : DINT; + keyFound : BOOL; + value : ULINT; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + keyFound := deserializer.TryParse(keyArray, value); + Equal(ULINT#123, value); + END_METHOD + END_CLASS -// END_NAMESPACE +END_NAMESPACE From 6d78fc40b6102ee761d9e382997b92d8b38bf6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Tue, 10 Sep 2024 18:07:08 +0200 Subject: [PATCH 4/4] enable all tests --- test/Document/TestJsonDocument.st | 493 ++++++++++++----------- test/Document/TestResetNestedDocument.st | 204 +++++----- 2 files changed, 354 insertions(+), 343 deletions(-) diff --git a/test/Document/TestJsonDocument.st b/test/Document/TestJsonDocument.st index 217b228..db45698 100644 --- a/test/Document/TestJsonDocument.st +++ b/test/Document/TestJsonDocument.st @@ -1,246 +1,247 @@ -//TODO tests running endless -// USING AxUnit.Assert; -// USING System.Strings; -// USING Simatic.Ax.Conversion; -// USING Simatic.Ax.Json; - -// NAMESPACE Document -// {TestFixture} -// CLASS TestJsonDocument - -// VAR PROTECTED -// e : JsonDInt := (Value := 999, Key := 'test'); -// e2 : JsonDInt := (Value := -999, Key := 'second_key'); -// e3 : JsonDInt := (Value := 1, Key := '3rd'); -// doc : JsonDocument; -// o2 : JsonObject := (Key := 'Name'); - -// oa : JsonObject := (Key := 'O1'); -// ob : JsonObject := (Key := 'O3'); -// oc : JsonObject := (Key := 'O5'); -// ea : JsonDInt := (Key := 'ea', Value := 1); -// eb : JsonDInt := (Key := 'eb', Value := 2); -// ec : JsonDInt := (Key := 'ec', Value := 3); -// ed : JsonDInt := (Key := 'ed', Value := 4); - -// ee : JsonString := (Key := 'ee', Value := 'ee'); -// ef : JsonString := (Key := 'ef', Value := 'ef'); -// eg : JsonString := (Key := 'eg', Value := 'eg'); - -// iElem : IJsonElement; -// rElem : REF_TO JsonDInt; -// expArray : ARRAY[0..399] OF CHAR; -// expArrayCnt : DINT; -// expString : STRING; -// actString : STRING; -// result : BOOL; -// buffer : ARRAY[0..200] OF CHAR; - - -// END_VAR - -// {Test} -// METHOD PUBLIC Test_Empty_object_ToString_Expect_empty_object - -// // expected string -// expString := '{}'; - -// actString := doc.ToString(); -// Equal(expected := expString, actual := actString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); - -// END_METHOD - -// {Test} -// METHOD PUBLIC Test_object_with_one_element_And_Call_Serialize_twice_Expectstill_correct_string -// // expected string -// expString := '{"test": 999}'; - -// doc.AddElement(e); -// actString := doc.ToString(); -// Equal(expected := expString, actual := actString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - - -// {Test} -// METHOD PUBLIC Test_object_with_one_element_ToString_Expect_correct_string -// // expected string -// expString := '{"test": 999}'; - -// doc.AddElement(e); -// actString := doc.ToString(); -// Equal(expected := expString, actual := actString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - -// {Test} -// METHOD PUBLIC Test_object_in_object_expect_nested_object -// // expected string -// expString := '{{}}'; - -// doc.AddElement(o2); -// Equal(expected := doc.ToString(), actual := expString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - - -// {Test} -// METHOD PUBLIC Test_object_in_object_with_property_expect_nested_object -// // expected string -// expString := '{"Name": {"test": 999}}'; - -// doc.AddElement(o2); -// o2.AddElement(e); - -// actString := doc.ToString(); -// Equal(expected := actString, actual := expString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - - - -// {Test} -// METHOD PUBLIC Test_object_with_two_property_expect_object -// // expected string -// expString := '{"test": 999, "second_key": -999}'; - - -// doc.AddElement(e); -// doc.AddElement(e2); -// Equal(expected := doc.ToString(), actual := expString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - -// {Test} -// METHOD PUBLIC Test_object_with_three_property_expect_object -// // expected string -// expString := '{"test": 999, "second_key": -999, "3rd": 1}'; - -// doc.AddElement(e); -// doc.AddElement(e2); -// doc.AddElement(e3); -// Equal(expected := doc.ToString(), actual := expString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); -// END_METHOD - -// {Test} -// METHOD PUBLIC CreateTwoObjects_and_expect_correct_values -// // expected string -// expString := '{"test": 999, "second_key": -999}'; - -// doc.AddElement(e); -// doc.AddElement(e2); -// Equal(expected := doc.ToString(), actual := expString); - - -// // expected string -// expString := '"NestedObject": {"3rd": 1}'; - -// o2.Key := 'NestedObject'; -// o2.AddElement(e3); -// actString := o2.ToString(); -// Equal(expected := expString, actual := actString); - - -// // 0 1 2 3 4 5 6 7 -// // 1234567890123456789012345678901234567890123456789012345678901234567890 -// expString := '{"test": 999, "second_key": -999, "NestedObject": {"3rd": 1}}'; - -// doc.AddElement(o2); -// actString := o2.ToString(); - -// actString := doc.ToString(); -// Equal(expected := expString , actual := actString); - -// // // Test Serialize -// // expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// // doc.Serialize(); -// // result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// // Equal(expected := TRUE, actual := result); -// END_METHOD - -// {Test} -// METHOD PUBLIC Test_two_objects_with_two_nested_elements_in_each_object -// // expected string -// expString := '{"O1": {"ea": 1, "eb": 2}, "O3": {"ec": 3, "ed": 4}, "O5": {"ee": "ee", "ef": "ef", "eg": "eg"}}'; - -// doc.AddElement(oa).AddElement(ob).AddElement(oc); -// // doc.AddElement(ob); -// // doc.AddElement(oc); - -// oa.AddElement(ea).AddElement(eb); - -// ob.AddElement(ec).AddElement(ed); - -// oc.AddElement(ee).AddElement(ef).AddElement(eg); - - -// // doc.AddElement(oa); -// Equal(expected := doc.ToString(), actual := expString); - -// // Test Serialize -// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); -// doc.buffer := REF(buffer); -// doc.Serialize(); -// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); -// Equal(expected := TRUE, actual := result); - -// END_METHOD - -// {Test} -// METHOD PUBLIC When_adding_elemens_by_building_pattern_the_correct_Json_will_be_Returned - -// expString := '{"ea": 1, "eb": 2, "ec": 3}'; -// doc.AddElement(ea).AddElement(eb).AddElement(ec); - -// Equal(expected := doc.ToString(), actual := expString); - -// ; // AxUnit.Assert.Equal(y, x) -// END_METHOD - -// END_CLASS -// END_NAMESPACE \ No newline at end of file +// TODO tests running endless +USING AxUnit.Assert; +USING System.Strings; +USING Simatic.Ax.Conversion; +USING Simatic.Ax.Json; + +NAMESPACE Document + + {TestFixture} + CLASS TestJsonDocument + VAR PROTECTED + e : JsonDInt := (Value := 999, Key := 'test'); + e2 : JsonDInt := (Value := -999, Key := 'second_key'); + e3 : JsonDInt := (Value := 1, Key := '3rd'); + doc : JsonDocument; + o2 : JsonObject := (Key := 'Name'); + oa : JsonObject := (Key := 'O1'); + ob : JsonObject := (Key := 'O3'); + oc : JsonObject := (Key := 'O5'); + ea : JsonDInt := (Key := 'ea', Value := 1); + eb : JsonDInt := (Key := 'eb', Value := 2); + ec : JsonDInt := (Key := 'ec', Value := 3); + ed : JsonDInt := (Key := 'ed', Value := 4); + ee : JsonString := (Key := 'ee', Value := 'ee'); + ef : JsonString := (Key := 'ef', Value := 'ef'); + eg : JsonString := (Key := 'eg', Value := 'eg'); + iElem : IJsonElement; + rElem : REF_TO JsonDInt; + expArray : ARRAY[0..399] OF CHAR; + expArrayCnt : DINT; + expString : STRING; + actString : STRING; + result : BOOL; + buffer : ARRAY[0..200] OF CHAR; + // Stateless + eStateLess : JsonDInt := (Value := 999, Key := 'test'); + e2StateLess : JsonDInt := (Value := -999, Key := 'second_key'); + e3StateLess : JsonDInt := (Value := 1, Key := '3rd'); + docStateLess : JsonDocument; + o2StateLess : JsonObject := (Key := 'Name'); + oaStateLess : JsonObject := (Key := 'O1'); + obStateLess : JsonObject := (Key := 'O3'); + ocStateLess : JsonObject := (Key := 'O5'); + eaStateLess : JsonDInt := (Key := 'ea', Value := 1); + ebStateLess : JsonDInt := (Key := 'eb', Value := 2); + ecStateLess : JsonDInt := (Key := 'ec', Value := 3); + edStateLess : JsonDInt := (Key := 'ed', Value := 4); + eeStateLess : JsonString := (Key := 'ee', Value := 'ee'); + efStateLess : JsonString := (Key := 'ef', Value := 'ef'); + egStateLess : JsonString := (Key := 'eg', Value := 'eg'); + iElemStateLess : IJsonElement; + rElemStateLess : REF_TO JsonDInt; + expArrayStateLess : ARRAY[0..399] OF CHAR; + bufferStateLess : ARRAY[0..200] OF CHAR; + END_VAR + + {TestSetup} + METHOD PUBLIC TestSetup + // Stateless + e := eStateLess; + e2 := e2StateLess; + e3 := e3StateLess; + doc := docStateLess; + o2 := o2StateLess; + oa := oaStateLess; + ob := obStateLess; + oc := ocStateLess; + ea := eaStateLess; + eb := ebStateLess; + ec := ecStateLess; + ed := edStateLess; + ee := eeStateLess; + ef := efStateLess; + eg := egStateLess; + iElem := iElemStateLess; + rElem := rElemStateLess; + expArray := expArrayStateLess; + expArrayCnt := 0; + expString := ''; + actString := ''; + result := FALSE; + buffer := bufferStateLess; + END_METHOD + + {Test} + METHOD PUBLIC Test_Empty_object_ToString_Expect_empty_object + // expected string + expString := '{}'; + actString := doc.ToString(); + Equal(expected := expString, actual := actString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_with_one_element_And_Call_Serialize_twice_Expectstill_correct_string + // expected string + expString := '{"test": 999}'; + doc.AddElement(e); + actString := doc.ToString(); + Equal(expected := expString, actual := actString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_with_one_element_ToString_Expect_correct_string + // expected string + expString := '{"test": 999}'; + doc.AddElement(e); + actString := doc.ToString(); + Equal(expected := expString, actual := actString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_in_object_expect_nested_object + // expected string + expString := '{{}}'; + doc.AddElement(o2); + Equal(expected := doc.ToString(), actual := expString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_in_object_with_property_expect_nested_object + // expected string + expString := '{"Name": {"test": 999}}'; + doc.AddElement(o2); + o2.AddElement(e); + actString := doc.ToString(); + Equal(expected := actString, actual := expString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_with_two_property_expect_object + // expected string + expString := '{"test": 999, "second_key": -999}'; + doc.AddElement(e); + doc.AddElement(e2); + Equal(expected := doc.ToString(), actual := expString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_object_with_three_property_expect_object + // expected string + expString := '{"test": 999, "second_key": -999, "3rd": 1}'; + doc.AddElement(e); + doc.AddElement(e2); + doc.AddElement(e3); + Equal(expected := doc.ToString(), actual := expString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC CreateTwoObjects_and_expect_correct_values + // expected string + expString := '{"test": 999, "second_key": -999}'; + doc.AddElement(e); + doc.AddElement(e2); + Equal(expected := doc.ToString(), actual := expString); + // expected string + expString := '"NestedObject": {"3rd": 1}'; + o2.Key := 'NestedObject'; + o2.AddElement(e3); + actString := o2.ToString(); + Equal(expected := expString, actual := actString); + // 0 1 2 3 4 5 6 7 + // 1234567890123456789012345678901234567890123456789012345678901234567890 + expString := '{"test": 999, "second_key": -999, "NestedObject": {"3rd": 1}}'; + doc.AddElement(o2); + actString := o2.ToString(); + actString := doc.ToString(); + Equal(expected := expString, actual := actString); +// // Test Serialize +// expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); +// doc.Serialize(); +// result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); +// Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC Test_two_objects_with_two_nested_elements_in_each_object + // expected string + expString := '{"O1": {"ea": 1, "eb": 2}, "O3": {"ec": 3, "ed": 4}, "O5": {"ee": "ee", "ef": "ef", "eg": "eg"}}'; + doc.AddElement(oa).AddElement(ob).AddElement(oc); + // doc.AddElement(ob); + // doc.AddElement(oc); + oa.AddElement(ea).AddElement(eb); + ob.AddElement(ec).AddElement(ed); + oc.AddElement(ee).AddElement(ef).AddElement(eg); + // doc.AddElement(oa); + Equal(expected := doc.ToString(), actual := expString); + // Test Serialize + expArrayCnt := Strings.ToArray.OfCharCount(str := expString, arr := expArray); + doc.buffer := REF(buffer); + doc.Serialize(); + result := CompareArray(first := buffer, scnd := expArray, count := TO_INT(expArrayCnt)); + Equal(expected := TRUE, actual := result); + END_METHOD + + {Test} + METHOD PUBLIC When_adding_elemens_by_building_pattern_the_correct_Json_will_be_Returned + expString := '{"ea": 1, "eb": 2, "ec": 3}'; + doc.AddElement(ea).AddElement(eb).AddElement(ec); + Equal(expected := doc.ToString(), actual := expString); + ; // AxUnit.Assert.Equal(y, x) + END_METHOD + END_CLASS + +END_NAMESPACE diff --git a/test/Document/TestResetNestedDocument.st b/test/Document/TestResetNestedDocument.st index 12b0a1d..aff3318 100644 --- a/test/Document/TestResetNestedDocument.st +++ b/test/Document/TestResetNestedDocument.st @@ -1,108 +1,118 @@ // ToDo -> tests with exit code 255 -// USING AxUnit.Assert; -// USING System.Strings; -// USING Simatic.Ax.Conversion; -// USING Simatic.Ax.Json; +USING AxUnit.Assert; +USING System.Strings; +USING Simatic.Ax.Conversion; +USING Simatic.Ax.Json; -// NAMESPACE Document -// {TestFixture} -// CLASS TestResetNestedDocument -// VAR PUBLIC -// doc : JsonDocument; -// cmpBuff : ARRAY[0..20] OF CHAR; +NAMESPACE Document -// str, s : STRING; + {TestFixture} + CLASS TestResetNestedDocument + VAR PUBLIC + doc : JsonDocument; + cmpBuff : ARRAY[0..20] OF CHAR; + str : STRING; + s : STRING; + myBoolean : JsonBoolean := (Value := TRUE, Key := 'myBoolean'); + myInt : JsonInt := (Value := 1234, Key := 'myInt'); + myDint : JsonDInt := (Value := DINT#12345678, Key := 'myDint'); + myString : JsonString := (Value := 'this is my string', Key := 'myString'); + myObject : JsonObject := (Key := 'myObject'); + buffer : ARRAY[0..200] OF CHAR; + // Stateless + docStateLess : JsonDocument; + cmpBuffStateLess : ARRAY[0..20] OF CHAR; + strStateLess : STRING; + sStateLess : STRING; + myBooleanStateLess : JsonBoolean := (Value := TRUE, Key := 'myBoolean'); + myIntStateLess : JsonInt := (Value := 1234, Key := 'myInt'); + myDintStateLess : JsonDInt := (Value := DINT#12345678, Key := 'myDint'); + myStringStateLess : JsonString := (Value := 'this is my string', Key := 'myString'); + myObjectStateLess : JsonObject := (Key := 'myObject'); + bufferStateLess : ARRAY[0..200] OF CHAR; + END_VAR -// myBoolean : JsonBoolean := (Value := TRUE, Key := 'myBoolean'); -// myInt : JsonInt := (Value := 1234, Key := 'myInt'); -// myDint : JsonDInt := (Value := DINT#12345678, Key := 'myDint'); -// myString : JsonString := (Value := 'this is my string', Key := 'myString'); -// myObject : JsonObject := (Key := 'myObject'); -// buffer : ARRAY[0..200] OF CHAR; -// END_VAR + {TestSetup} + METHOD PUBLIC TestSetup + doc := docStateLess; + cmpBuff := cmpBuffStateLess; + str := strStateLess; + s := sStateLess; + myBoolean := myBooleanStateLess; + myInt := myIntStateLess; + myDint := myDintStateLess; + myString := myStringStateLess; + myObject := myObjectStateLess; + buffer := bufferStateLess; + END_METHOD -// {Test} -// METHOD PUBLIC Serialize_BOOLEAN -// doc.buffer := REF(buffer); + {Test} + METHOD PUBLIC Serialize_BOOLEAN + doc.buffer := REF(buffer); + doc.AddElement(myBoolean); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 18); + Equal(expected := '{"myBoolean": true}', actual := str); + END_METHOD -// doc.AddElement(myBoolean); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 18); -// Equal(expected := '{"myBoolean": true}', actual := str); -// END_METHOD + {Test} + METHOD PUBLIC Serialize_INT + doc.buffer := REF(buffer); + doc.AddElement(myInt); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 14); + Equal(expected := '{"myInt": 1234}', actual := str); + END_METHOD -// {Test} -// METHOD PUBLIC Serialize_INT -// doc.buffer := REF(buffer); -// doc.AddElement(myInt); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 14); -// Equal(expected := '{"myInt": 1234}', actual := str); -// END_METHOD + {Test} + METHOD PUBLIC Serialize_DINT + doc.buffer := REF(buffer); + doc.AddElement(myDint); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 19); + Equal(expected := '{"myDint": 12345678}', actual := str); + END_METHOD -// {Test} -// METHOD PUBLIC Serialize_DINT -// doc.buffer := REF(buffer); -// doc.AddElement(myDint); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 19); -// Equal(expected := '{"myDint": 12345678}', actual := str); -// END_METHOD - -// {Test} -// METHOD PUBLIC Serialize_STRING -// doc.buffer := REF(buffer); -// doc.AddElement(myString); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 32); -// Equal(expected := '{"myString": "this is my string"}', actual := str); -// END_METHOD + {Test} + METHOD PUBLIC Serialize_STRING + doc.buffer := REF(buffer); + doc.AddElement(myString); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 32); + Equal(expected := '{"myString": "this is my string"}', actual := str); + END_METHOD -// {Test} -// METHOD PUBLIC Serialize_INT_DINT -// doc.buffer := REF(buffer); -// doc.AddElement(myInt); -// doc.AddElement(myDint); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 34); -// Equal(expected := '{"myInt": 1234, "myDint": 12345678}', actual := str); -// END_METHOD + {Test} + METHOD PUBLIC Serialize_INT_DINT + doc.buffer := REF(buffer); + doc.AddElement(myInt); + doc.AddElement(myDint); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 34); + Equal(expected := '{"myInt": 1234, "myDint": 12345678}', actual := str); + END_METHOD -// {Test} -// METHOD PUBLIC Reset_nested_document_and_get_correct_result_after_adding_elements_again -// myObject.AddElement(myInt); -// myObject.AddElement(myDint); - -// doc.AddElement(myBoolean); -// doc.AddElement(myObject); - -// doc.buffer := REF(buffer); -// doc.Serialize(); - -// str := Arrays.ToString(buffer, 0, 67); -// Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str); + {Test} + METHOD PUBLIC Reset_nested_document_and_get_correct_result_after_adding_elements_again + myObject.AddElement(myInt); + myObject.AddElement(myDint); + doc.AddElement(myBoolean); + doc.AddElement(myObject); + doc.buffer := REF(buffer); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 67); + Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str); + // Reset document + doc.Reset(); + // Create new document + myObject.AddElement(myInt); + myObject.AddElement(myDint); + doc.AddElement(myBoolean); + doc.AddElement(myObject); + doc.Serialize(); + str := Arrays.ToString(buffer, 0, 67); + Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str); + END_METHOD + END_CLASS - -// // Reset document -// doc.Reset(); - -// // Create new document -// myObject.AddElement(myInt); -// myObject.AddElement(myDint); - -// doc.AddElement(myBoolean); -// doc.AddElement(myObject); - -// doc.Serialize(); -// str := Arrays.ToString(buffer, 0, 67); -// Equal(expected := '{"myBoolean": true, "myObject": {"myInt": 1234, "myDint": 12345678}}', actual := str); - -// END_METHOD - -// END_CLASS -// END_NAMESPACE \ No newline at end of file +END_NAMESPACE