Skip to content

Commit

Permalink
add tests for deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuergen committed Feb 13, 2024
1 parent 7c4ad0f commit e993567
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 227 deletions.
120 changes: 55 additions & 65 deletions test/Deserializer/GetValues/Test_GetValues_Arrays.st
Original file line number Diff line number Diff line change
Expand Up @@ -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
78 changes: 36 additions & 42 deletions test/Deserializer/GetValues/Test_GetValues_BasicDataTypes.st
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit e993567

Please sign in to comment.