Skip to content

Commit

Permalink
add try parse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuergen committed Feb 13, 2024
1 parent e993567 commit 3bc9753
Show file tree
Hide file tree
Showing 2 changed files with 432 additions and 497 deletions.
241 changes: 110 additions & 131 deletions test/Deserializer/TestFindKeyInBuffer.st
Original file line number Diff line number Diff line change
@@ -1,139 +1,118 @@
USING Simatic.Ax.Conversion;
USING AxUnit.Assert;
USING System.Strings;
using Simatic.Ax.Json;

USING Simatic.Ax.Json;

NAMESPACE Deserializer
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

{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_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
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

{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_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

END_NAMESPACE

END_NAMESPACE
END_NAMESPACE
Loading

0 comments on commit 3bc9753

Please sign in to comment.