Skip to content

Commit

Permalink
Add tests with anydata array as expected type
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Mar 21, 2024
1 parent d689f4f commit a537e99
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
38 changes: 38 additions & 0 deletions ballerina/tests/from_json_string_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,44 @@ isolated function testAnydataAsExpTypeForParseString() returns Error? {
test:assertEquals(val5, [[1], 2]);
}

@test:Config
isolated function testAnydataArrayAsExpTypeForParseString() returns Error? {
string jsonStr1 = string `[["1"], 2.0]`;
anydata[] val1 = check parseString(jsonStr1);
test:assertEquals(val1, [[1], 2.0]);

string jsonStr2 = string `[["1", 2], 2.0]`;
anydata[] val2 = check parseString(jsonStr2);
test:assertEquals(val2, [[1, 2], 2.0]);

string jsonStr3 = string `[["1", 2], [2, "3"]]`;
anydata[] val3 = check parseString(jsonStr3);
test:assertEquals(val3, [[1, 2], [2, 3]]);

string jsonStr4 = string `{"val" : [[1, 2], "2.0", 3.0, [5, 6]]}`;
record {|
anydata[] val;
|} val4 = check parseString(jsonStr4);
test:assertEquals(val4, {val: [[1, 2], 2.0, 3.0, [5, 6]]});

string jsonStr41 = string `{"val1" : [[1, 2], "2.0", 3.0, [5, 6]], "val2" : [[1, 2], "2.0", 3.0, [5, 6]]}`;
record {|
anydata[] val1;
anydata[] val2;
|} val41 = check parseString(jsonStr41);
test:assertEquals(val41, {val1: [[1, 2], 2.0, 3.0, [5, 6]], val2: [[1, 2], 2.0, 3.0, [5, 6]]});

string jsonStr5 = string `{"val" : [["1", 2], [2, "3"]]}`;
record {|
anydata[] val;
|} val5 = check parseString(jsonStr5);
test:assertEquals(val5, {val: [[1, 2], [2, 3]]});

string jsonStr6 = string `[{"val" : [["1", 2], [2, "3"]]}]`;
[record {|anydata[][] val;|}] val6 = check parseString(jsonStr6);
test:assertEquals(val6, [{val: [[1, 2], [2, 3]]}]);
}

@test:Config
isolated function testJsonAsExpTypeForParseString() returns Error? {
string jsonStr1 = string `1`;
Expand Down
38 changes: 38 additions & 0 deletions ballerina/tests/from_json_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,44 @@ isolated function testSingletonAsExpectedTypeForParseAsType() returns Error? {
test:assertEquals(val5.value, 1);
}

@test:Config
isolated function testAnydataArrayAsExpTypeForParseAsType() returns Error? {
json jsonVal1 = [[1], 2.0];
anydata[] val1 = check parseAsType(jsonVal1);
test:assertEquals(val1, [[1], 2.0]);

json jsonVal2 = [["1", 2], 2.0];
anydata[] val2 = check parseAsType(jsonVal2);
test:assertEquals(val2, [["1", 2], 2.0]);

json jsonVal3 = [["1", 2], [2, "3"]];
anydata[] val3 = check parseAsType(jsonVal3);
test:assertEquals(val3, [["1", 2], [2, "3"]]);

json jsonVal4 = {val : [[1, 2], "2.0", 3.0, [5, 6]]};
record {|
anydata[] val;
|} val4 = check parseAsType(jsonVal4);
test:assertEquals(val4, {val: [[1, 2], "2.0", 3.0, [5, 6]]});

json jsonVal41 = {val1 : [[1, 2], 2.0, 3.0, [5, 6]], val2 : [[1, 2], "2.0", 3.0, [5, 6]]};
record {|
anydata[] val1;
anydata[] val2;
|} val41 = check parseAsType(jsonVal41);
test:assertEquals(val41, {val1: [[1, 2], 2.0, 3.0, [5, 6]], val2: [[1, 2], "2.0", 3.0, [5, 6]]});

json jsonVal5 = {val : [["1", 2], [2, "3"]]};
record {|
anydata[] val;
|} val5 = check parseAsType(jsonVal5);
test:assertEquals(val5, {val: [["1", 2], [2, "3"]]});

json jsonVal6 = [{val : [[1, 2], [2, "James"]]}];
[record {|anydata[][] val;|}] val6 = check parseAsType(jsonVal6);
test:assertEquals(val6, [{val: [[1, 2], [2, "James"]]}]);
}

@test:Config
isolated function testParseAsTypeNegative1() returns Error? {
json jsonContent = {
Expand Down

0 comments on commit a537e99

Please sign in to comment.