From e993567c8a4d95f156cbd660792702b1f1009081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Seidel?= Date: Tue, 13 Feb 2024 09:42:09 +0100 Subject: [PATCH] add tests for deserializer --- .../GetValues/Test_GetValues_Arrays.st | 120 +++++----- .../Test_GetValues_BasicDataTypes.st | 78 +++---- .../Test_GetValues_NestedElements.st | 220 ++++++++---------- 3 files changed, 191 insertions(+), 227 deletions(-) diff --git a/test/Deserializer/GetValues/Test_GetValues_Arrays.st b/test/Deserializer/GetValues/Test_GetValues_Arrays.st index 0dc1536..efe8940 100644 --- a/test/Deserializer/GetValues/Test_GetValues_Arrays.st +++ b/test/Deserializer/GetValues/Test_GetValues_Arrays.st @@ -3,72 +3,62 @@ USING AxUnit.Assert; USING System.Strings; USING Simatic.Ax.Json; - NAMESPACE Deserializer -NAMESPACE GetValues - -{TestFixture} -CLASS Test_Deserializer_GetValues_Arrays - VAR protected - deserializer : Deserializer; - - buffer : ARRAY[0..999] OF CHAR; - key: STRING; - value: STRING; - END_VAR - - {Test} - Method Public Test_FindValue_in_JSON_SimpleArray - VAR - JSON_Entry: STRING := '{ "arraykey": [123, 456]}'; - - len:DINT; - keyFound: BOOL; - END_VAR - - len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - - key := 'arraykey'; - keyFound := deserializer.TryParse( key, value); - Equal('[123, 456]', value); - END_Method - - {Test} - Method Public Test_FindValue_in_JSON_ComplexArray - VAR - JSON_Entry: STRING := '{ "key1": 1234,"arraykey": [123, 456], "key2": 456}'; - - len:DINT; - keyFound: BOOL; - END_VAR - - len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - - key := 'arraykey'; - keyFound := deserializer.TryParse( key, value); - Equal('[123, 456]', value); - END_Method - - {Test} - Method Public Test_FindValue_in_JSON_NestedArray - VAR - JSON_Entry: STRING := '{ "key1": 1234,"arraykey": [{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}], "key2": 456}'; - - len:DINT; - keyFound: BOOL; - END_VAR - - len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - - key := 'arraykey'; - keyFound := deserializer.TryParse( key, value); - Equal('[{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}]', value); - END_Method -END_CLASS + NAMESPACE GetValues + + {TestFixture} + CLASS Test_Deserializer_GetValues_Arrays + VAR PROTECTED + deserializer : Deserializer; + buffer : ARRAY[0..999] OF CHAR; + key : STRING; + value : STRING; + END_VAR + + {Test} + METHOD PUBLIC Test_FindValue_in_JSON_SimpleArray + VAR + JSON_Entry : STRING := '{ "arraykey": [123, 456]}'; + len : DINT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'arraykey'; + keyFound := deserializer.TryParse(key, value); + Equal('[123, 456]', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_in_JSON_ComplexArray + VAR + JSON_Entry : STRING := '{ "key1": 1234,"arraykey": [123, 456], "key2": 456}'; + len : DINT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'arraykey'; + keyFound := deserializer.TryParse(key, value); + Equal('[123, 456]', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_in_JSON_NestedArray + VAR + JSON_Entry : STRING := '{ "key1": 1234,"arraykey": [{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}], "key2": 456}'; + len : DINT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'arraykey'; + keyFound := deserializer.TryParse(key, value); + Equal('[{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}]', value); + END_METHOD + END_CLASS + + END_NAMESPACE END_NAMESPACE -END_NAMESPACE \ No newline at end of file diff --git a/test/Deserializer/GetValues/Test_GetValues_BasicDataTypes.st b/test/Deserializer/GetValues/Test_GetValues_BasicDataTypes.st index 01f5114..f2b9c9e 100644 --- a/test/Deserializer/GetValues/Test_GetValues_BasicDataTypes.st +++ b/test/Deserializer/GetValues/Test_GetValues_BasicDataTypes.st @@ -1,50 +1,44 @@ USING Simatic.Ax.Conversion; USING AxUnit.Assert; USING System.Strings; -using Simatic.Ax.Json; - +USING Simatic.Ax.Json; NAMESPACE Deserializer -NAMESPACE GetValues - -{TestFixture} -CLASS Test_Deserializer_GetValues_BasicDataTypes - VAR protected - deserializer : Deserializer; - - buffer : ARRAY[0..999] OF CHAR; - key: STRING; - value: STRING; - END_VAR - - {Test} - Method Public Test_FindValue_in_JSON_SimpleEntries - VAR - JSON_Entry: STRING := '{"key": "Element1", "key 2": "something", "key 3": 456, "Key 4": 1234 }'; - - len:DINT; - 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(8, LengthOf(value)); - Equal('Element1', value); - - key := 'Key 4'; - keyFound := deserializer.TryParse( key, value); - Equal(4, LengthOf(value)); - Equal('1234', value); - - key := 'not a key'; - keyFound := deserializer.TryParse( key, value); - Equal('', value); - END_Method -END_CLASS + NAMESPACE GetValues + + {TestFixture} + CLASS Test_Deserializer_GetValues_BasicDataTypes + VAR PROTECTED + deserializer : Deserializer; + buffer : ARRAY[0..999] OF CHAR; + key : STRING; + value : STRING; + END_VAR + + {Test} + METHOD PUBLIC Test_FindValue_in_JSON_SimpleEntries + VAR + JSON_Entry : STRING := '{"key": "Element1", "key 2": "something", "key 3": 456, "Key 4": 1234 }'; + len : DINT; + 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(8, LengthOf(value)); + Equal('Element1', value); + key := 'Key 4'; + keyFound := deserializer.TryParse(key, value); + Equal(4, LengthOf(value)); + Equal('1234', value); + key := 'not a key'; + keyFound := deserializer.TryParse(key, value); + Equal('', value); + END_METHOD + END_CLASS + + END_NAMESPACE END_NAMESPACE -END_NAMESPACE \ No newline at end of file diff --git a/test/Deserializer/GetValues/Test_GetValues_NestedElements.st b/test/Deserializer/GetValues/Test_GetValues_NestedElements.st index 585b514..f061208 100644 --- a/test/Deserializer/GetValues/Test_GetValues_NestedElements.st +++ b/test/Deserializer/GetValues/Test_GetValues_NestedElements.st @@ -1,128 +1,108 @@ USING Simatic.Ax.Conversion; USING AxUnit.Assert; USING System.Strings; -using Simatic.Ax.Json; - +USING Simatic.Ax.Json; NAMESPACE Deserializer -NAMESPACE GetValues - -{TestFixture} -CLASS Test_Deserializer_GetValues_NestedElements - VAR protected - deserializer : Deserializer; - - buffer : ARRAY[0..999] OF CHAR; - key: STRING; - value: STRING; - END_VAR - - {Test} - Method Public Test_FindValue_in_JSON_NestedArray - VAR - JSON_Entry: STRING := '{ "key1": 1234,"arraykey": [{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}], "key2": 456}'; - - len:DINT; - keyFound: BOOL; - END_VAR - - len:= Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); - deserializer.buffer := REF(buffer); - - key := 'arraykey'; - keyFound := deserializer.TryParse( key, value); - Equal('[{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}]', value); - END_Method - - {Test} - Method Public Test_FindValue_NestedElement_in_JSON_SimpleEntries - VAR - JSON_Entry: STRING := '{"key": {"nested1" : "hello", "nested2" : 1234 } }'; - len:DINT; - keyFound: BOOL; - keyArray: ARRAY [0..1] OF STRING:= ['key', 'nested1']; - 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); - Equal('{"nested1" : "hello", "nested2" : 1234 }', value); - - keyFound := deserializer.TryParse( keyArray, value); - Equal('hello', value); - - END_Method - - {Test} - Method Public Test_FindValue_NestedElement_in_JSON_MoreEntries - VAR - JSON_Entry: STRING := '{"anotherkey": 123, "key": {"nested1" : "hello", "nested2" : 1234 }, "differentKey": 12 }'; - len:DINT; - keyFound: BOOL; - keyArray: ARRAY [0..1] OF STRING:= ['key', 'nested1']; - 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); - Equal('{"nested1" : "hello", "nested2" : 1234 }', value); - - keyFound := deserializer.TryParse( keyArray, value); - Equal('hello', value); - - END_Method - - {Test} - Method Public Test_FindValue_NestedElement_in_JSON_NestedInNested - VAR - JSON_Entry: STRING := '{"anotherkey": 123, "key": {"nested1" : "hello", "nestedkey": {"nInNested": "goodbye", "k": 1234}, "nested2" : 1234 }, "differentKey": 12 }'; - len:DINT; - keyFound: BOOL; - keyArray: ARRAY [0..2] OF STRING:= ['key', 'nestedkey', 'nInNested']; - 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); - Equal('{"nested1" : "hello", "nestedkey": {"nInNested": "goodbye", "k": 1234}, "nested2" : 1234 }', value); - - keyFound := deserializer.TryParse( keyArray, value); - Equal('goodbye', value); - - END_Method - - {Test} - Method Public Test_FindValue_NestedElement_in_JSON_ArrayInNested - VAR - JSON_Entry: STRING := '{"anotherkey": 123, "key": {"nested1" : ["hello", "123"], "nested2" : 1234 }, "differentKey": 12 }'; - len:DINT; - keyFound: BOOL; - keyArray: ARRAY [0..1] OF STRING:= ['key', 'nested1']; - 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); - Equal('{"nested1" : ["hello", "123"], "nested2" : 1234 }', value); - - keyFound := deserializer.TryParse( keyArray, value); - Equal('["hello", "123"]', value); - - END_Method -END_CLASS + NAMESPACE GetValues + + {TestFixture} + CLASS Test_Deserializer_GetValues_NestedElements + VAR PROTECTED + deserializer : Deserializer; + buffer : ARRAY[0..999] OF CHAR; + key : STRING; + value : STRING; + END_VAR + + {Test} + METHOD PUBLIC Test_FindValue_in_JSON_NestedArray + VAR + JSON_Entry : STRING := '{ "key1": 1234,"arraykey": [{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}], "key2": 456}'; + len : DINT; + keyFound : BOOL; + END_VAR + len := Strings.ToArray.OfCharCount(str := JSON_Entry, arr := buffer); + deserializer.buffer := REF(buffer); + key := 'arraykey'; + keyFound := deserializer.TryParse(key, value); + Equal('[{"nestedkey1": 1234, "nestedkey2": 456}, {"nestedkey3": 1234, "nestedkey4": 456}]', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_NestedElement_in_JSON_SimpleEntries + VAR + JSON_Entry : STRING := '{"key": {"nested1" : "hello", "nested2" : 1234 } }'; + len : DINT; + keyFound : BOOL; + keyArray : ARRAY[0..1] OF STRING := ['key', 'nested1']; + 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); + Equal('{"nested1" : "hello", "nested2" : 1234 }', value); + keyFound := deserializer.TryParse(keyArray, value); + Equal('hello', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_NestedElement_in_JSON_MoreEntries + VAR + JSON_Entry : STRING := '{"anotherkey": 123, "key": {"nested1" : "hello", "nested2" : 1234 }, "differentKey": 12 }'; + len : DINT; + keyFound : BOOL; + keyArray : ARRAY[0..1] OF STRING := ['key', 'nested1']; + 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); + Equal('{"nested1" : "hello", "nested2" : 1234 }', value); + keyFound := deserializer.TryParse(keyArray, value); + Equal('hello', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_NestedElement_in_JSON_NestedInNested + VAR + JSON_Entry : STRING := '{"anotherkey": 123, "key": {"nested1" : "hello", "nestedkey": {"nInNested": "goodbye", "k": 1234}, "nested2" : 1234 }, "differentKey": 12 }'; + len : DINT; + keyFound : BOOL; + keyArray : ARRAY[0..2] OF STRING := ['key', 'nestedkey', 'nInNested']; + 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); + Equal('{"nested1" : "hello", "nestedkey": {"nInNested": "goodbye", "k": 1234}, "nested2" : 1234 }', value); + keyFound := deserializer.TryParse(keyArray, value); + Equal('goodbye', value); + END_METHOD + + {Test} + METHOD PUBLIC Test_FindValue_NestedElement_in_JSON_ArrayInNested + VAR + JSON_Entry : STRING := '{"anotherkey": 123, "key": {"nested1" : ["hello", "123"], "nested2" : 1234 }, "differentKey": 12 }'; + len : DINT; + keyFound : BOOL; + keyArray : ARRAY[0..1] OF STRING := ['key', 'nested1']; + 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); + Equal('{"nested1" : ["hello", "123"], "nested2" : 1234 }', value); + keyFound := deserializer.TryParse(keyArray, value); + Equal('["hello", "123"]', value); + END_METHOD + END_CLASS + + END_NAMESPACE END_NAMESPACE -END_NAMESPACE \ No newline at end of file