Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix windows build failure and add test for anydata array #3

Merged
merged 2 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
# These are Windows script files and should use crlf
*.bat text eol=crlf

# Ensure all Java files use LF.
*.java eol=lf
43 changes: 40 additions & 3 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 Expand Up @@ -1330,16 +1368,15 @@ isolated function testUnalignedJsonContent() returns error? {
{
"a"
:
"h
ello",
"hello",
"b":
1
}`;
record {|
string a;
int b;
|} val = check parseString(jsonStr);
test:assertEquals(val.a, "h\nello");
test:assertEquals(val.a, "hello");
test:assertEquals(val.b, 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
Loading