Skip to content

Commit

Permalink
Add negative tests for union types
Browse files Browse the repository at this point in the history
  • Loading branch information
SasinduDilshara committed Aug 28, 2024
1 parent 00ca648 commit d9f5eb3
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 21 deletions.
19 changes: 9 additions & 10 deletions ballerina/tests/parse_string_union_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ function testParseStringUnionTypes2() {
test:assertTrue(a22 is Error);
test:assertEquals((<error> a22).message(), "source value cannot convert into 'ballerina/data.xmldata:1:S22'");

// // bug #4
// https://github.com/ballerina-platform/ballerina-library/issues/6925
// S23|error a23 = parseString(s2);
// test:assertEquals(a23, {"#content":"Sample Text","a1":2024, "a2": <decimal>3.14});

// // bug #5
// https://github.com/ballerina-platform/ballerina-library/issues/6925
// S24|error a24 = parseString(s2);
// test:assertEquals(a24, {"a1":2024, "a2": <float>3.14});

Expand Down Expand Up @@ -105,9 +105,8 @@ type S35 record {|

@test:Config
function testParseStringUnionTypes3() {
// custom bug
// S31|error a31 = parseString(s3);
// test:assertEquals(a31, {"B":"100"});
S31|error a31 = parseString(s3);
test:assertEquals(a31, {"B":"100"});

S31P2|error a31p2 = parseString(s3);
test:assertEquals(a31p2, {"b":"100"});
Expand All @@ -121,7 +120,7 @@ function testParseStringUnionTypes3() {
S34|error a34 = parseString(s3);
test:assertEquals(a34, {"B":[{"#content":100}]});

// custom bug
// https://github.com/ballerina-platform/ballerina-library/issues/6929
// S35|error a35 = parseString(s3);
// test:assertEquals(a35, {"B":{"#content":"100"}});
}
Expand All @@ -145,7 +144,7 @@ type S43 record{|int[]|record{|int \#content; float|decimal|int...;|} B;|}|S43P2

@test:Config
function testParseStringUnionTypes4() {
// // bug #2
// https://github.com/ballerina-platform/ballerina-library/issues?q=is%3Aopen+is%3Aissue+author%3ASasinduDilshara+label%3Amodule%2Fdata.xmldata
// S41|error a41 = parseString(s4);
// test:assertEquals(a41, {"B":{"#content":"Nested Content","b1":99,"b2":"45.67"}});

Expand Down Expand Up @@ -190,7 +189,7 @@ function testParseStringUnionTypes5() {
// S54|error a54 = parseString(s5);
// test:assertEquals(a54, {});

// // bug #6
// https://github.com/ballerina-platform/ballerina-library/issues?q=is%3Aopen+is%3Aissue+author%3ASasinduDilshara+label%3Amodule%2Fdata.xmldata
// S55|error a55 = parseString(s5);
// test:assertEquals(a55, {"B":[123,456]});

Expand Down Expand Up @@ -249,13 +248,13 @@ function testParseStringUnionTypes7() {

S73|error a73 = parseString(s7);
test:assertTrue(a73 is Error);
test:assertEquals((<error>a73).message(), "unsupported input type");
test:assertEquals((<error>a73).message(), "field 'B' cannot convert into the type '(data.xmldata:record {| string @content; |}|string[])'");

// //bug https://github.com/ballerina-platform/ballerina-library/issues/6907
// S74|error a74 = parseString(s7);
// test:assertEquals(a74, {"B":{"#content":"Water"},"C":{"#content":"Air"});

// // bug #6
// https://github.com/ballerina-platform/ballerina-library/issues?q=is%3Aopen+is%3Aissue+author%3ASasinduDilshara+label%3Amodule%2Fdata.xmldata
// S75|error a75 = parseString(s7);
// test:assertEquals(a75, {"B":[{"#content":"Brick"},{"#content":"Water"}],"C":[{"#content":"Air"}]});

Expand Down
14 changes: 7 additions & 7 deletions ballerina/tests/parse_type_union_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ function testTraverseUnionTypes2() {
test:assertTrue(a22 is Error);
test:assertEquals((<error> a22).message(), "source value cannot convert into 'ballerina/data.xmldata:1:A22'");

// // bug #4
// https://github.com/ballerina-platform/ballerina-library/issues/6925
// A23|error a23 = parseAsType(x2);
// test:assertEquals(a23, {"#content":"Sample Text","a1":2024, "a2": <decimal>3.14});

// // bug #5
// https://github.com/ballerina-platform/ballerina-library/issues/6925
// A24|error a24 = parseAsType(x2);
// test:assertEquals(a24, {"a1":2024, "a2": <float>3.14});

Expand Down Expand Up @@ -143,7 +143,7 @@ type A43 record{|int[]|record{|int \#content; float|decimal|int...;|} B;|}|A43P2

@test:Config
function testTraverseUnionTypes4() {
// // bug #2
// https://github.com/ballerina-platform/ballerina-library/issues/6925
// A41|error a41 = parseAsType(x4);
// test:assertEquals(a41, {"B":{"#content":"Nested Content","b1":99,"b2":"45.67"}});

Expand Down Expand Up @@ -188,7 +188,7 @@ function testTraverseUnionTypes5() {
// A54|error a54 = parseAsType(x5);
// test:assertEquals(a54, {});

// // bug #6
// https://github.com/ballerina-platform/ballerina-library/issues?q=is%3Aopen+is%3Aissue+author%3ASasinduDilshara+label%3Amodule%2Fdata.xmldata
// A55|error a55 = parseAsType(x5);
// test:assertEquals(a55, {"B":[123,456]});

Expand Down Expand Up @@ -247,13 +247,13 @@ function testTraverseUnionTypes7() {

A73|error a73 = parseAsType(x7);
test:assertTrue(a73 is Error);
test:assertEquals((<error>a73).message(), "unsupported input type");
test:assertEquals((<error>a73).message(), "field 'B' cannot convert into the type '(data.xmldata:record {| string @content; |}|string[])'");

// //bug https://github.com/ballerina-platform/ballerina-library/issues/6907
// A74|error a74 = parseAsType(x7);
// test:assertEquals(a74, {"B":{"#content":"Water"},"C":{"#content":"Air"});

// // bug #6
// https://github.com/ballerina-platform/ballerina-library/issues?q=is%3Aopen+is%3Aissue+author%3ASasinduDilshara+label%3Amodule%2Fdata.xmldata
// A75|error a75 = parseAsType(x7);
// test:assertEquals(a75, {"B":[{"#content":"Brick"},{"#content":"Water"}],"C":[{"#content":"Air"}]});

Expand All @@ -265,7 +265,7 @@ function testTraverseUnionTypes7() {
// A77|error a77 = parseAsType(x7);
// test:assertEquals(a77, {"B":[{"#content":"Brick"},{"#content":"Water"}]});

// // //bug https://github.com/ballerina-platform/ballerina-library/issues/6907
// //bug https://github.com/ballerina-platform/ballerina-library/issues/6907
// A78|error a78 = parseAsType(x7);
// test:assertEquals(a78, {"B":{"#content":"Water"},"C":{"#content":"Air"}});
}
Expand Down
149 changes: 149 additions & 0 deletions ballerina/tests/union_error_tests.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import ballerina/test;

xml xe1 = xml `<A>text</A>`;
xml xe2 = xml `<A><B>text</B></A>`;
xml xe3 = xml `<A><B>text</B><B>text</B></A>`;
xml xe4 = xml `<A>
<B><C>text</C><C>text</C><D>text</D></B>
<B><C>text</C><C>text</C><D>text</D></B>
</A>`;
xml xe5 = xml `<ns1:A xmlns:ns1="http://example.com/ns1" xmlns:ns2="http://example.com/ns2" ns1:a1="outer">
<ns1:B ns1:b1="middle">
<ns1:C ns1:c1="inner">Deep Value1</ns1:C>
<ns1:C ns1:c1="inner">Deep Value2</ns1:C>
</ns1:B>
<ns2:B ns1:b1="middle">
<ns1:C ns1:c1="inner">Deep Value3</ns1:C>
<ns1:C ns1:c1="inner">Deep Value2</ns1:C>
</ns2:B>
</ns1:A>`;

type E1 record {
boolean|()|int|decimal|record {} \#content;
};

type E2 record {
record {boolean|()|int|decimal|record {} \#content;} B;
};

type E22 record {
record {boolean|()|int|decimal|record {} \#content;}|record {record {int \#content;} \#content;} B;
};

type E3 record {
(record {int \#content;}|record {boolean \#content;})[] B;
};

type E32 record {
record {int \#content;}[]|record {boolean \#content;}[] B;
};

type E4P1 record {boolean \#content;};
type E4P12 record {int \#content;}|E4P1;

type E4 record {
record{E4P12[] C;}[] B;
};

type E4P2 record {boolean \#content;}[];
type E4P22 record {int \#content;}[]|E4P2;

type E42 record {
record{E4P22 C;}[] B;
};

@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
type E5 record {
string a1;
(F111|F112)[] B;
};

@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
type F111 record {
@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
string b1;
(G111|G112)[] C;
};

@Namespace {
prefix: "ns2",
uri: "http://example.com/ns2"
}
type F112 record {
@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
string b1;
(G111|G112)[] C;
};

@Namespace {
prefix: "ns2",
uri: "http://example.com/ns1"
}
type G111 record {
@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
string c1;
string \#content;
};

@Namespace {
prefix: "ns2",
uri: "http://example.com/ns1"
}
type G112 record {
@Namespace {
prefix: "ns1",
uri: "http://example.com/ns1"
}
string c1;
string \#content;
};

@test:Config
function testErrorUnion() {
E1|error e1 = parseAsType(xe1);
test:assertTrue(e1 is error);
test:assertEquals((<error>e1).message(), "field '#content' cannot convert into the type '(boolean|int|decimal|data.xmldata:record {| anydata...; |})?'");

E2|error e2 = parseAsType(xe2);
test:assertTrue(e2 is error);
test:assertEquals((<error>e2).message(), "field '#content' cannot convert into the type '(boolean|int|decimal|data.xmldata:record {| anydata...; |})?'");

E22|error e22 = parseAsType(xe2);
test:assertTrue(e22 is error);
test:assertEquals((<error>e22).message(), "field 'B' cannot convert into the type '(data.xmldata:record {| (boolean|int|decimal|record {| anydata...; |})? #content; anydata...; |}|data.xmldata:record {| record {| int #content; anydata...; |} #content; anydata...; |})'");

E3|error e3 = parseAsType(xe3);
test:assertTrue(e3 is error);
test:assertEquals((<error>e3).message(), "field 'B' cannot convert into the type '(data.xmldata:record {| int #content; anydata...; |}|data.xmldata:record {| boolean #content; anydata...; |})[]'");

E32|error e32 = parseAsType(xe3);
test:assertTrue(e32 is error);
test:assertEquals((<error>e32).message(), "field 'B' cannot convert into the type '(data.xmldata:record {| int #content; anydata...; |}[]|data.xmldata:record {| boolean #content; anydata...; |}[])'");

E4|error e4 = parseAsType(xe4);
test:assertTrue(e4 is error);
test:assertEquals((<error>e4).message(), "field 'C' cannot convert into the type 'data.xmldata:E4P12[]'");

E42|error e42 = parseAsType(xe4);
test:assertTrue(e42 is error);
test:assertEquals((<error>e42).message(), "field 'C' cannot convert into the type 'ballerina/data.xmldata:1:E4P22'");

E5|error e5 = parseAsType(xe5);
test:assertTrue(e5 is error);
test:assertEquals((<error>e5).message(), "field 'B' cannot convert into the type '(data.xmldata:F111|data.xmldata:F112)[]'");
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
}
}
if (convertedValue == null) {
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldType);
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldName, fieldType);
}
} else {
convertedValue = DataUtils.convertStringToExpType(StringUtils.fromString(text), fieldType);
Expand All @@ -222,7 +222,7 @@ private void convertText(String text, XmlAnalyzerData analyzerData) {
((BArray) value).add(currentIndex, convertedValue);
} else {
if (fieldType.getTag() == TypeTags.ARRAY_TAG) {
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldType);
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldName, fieldType);
}
mapValue.put(fieldName, convertedValue);
}
Expand Down Expand Up @@ -322,7 +322,7 @@ private void convertFieldTypeToUnion(BXmlItem xmlItem, Field currentField, Strin
// ignore
}
}
throw DiagnosticLog.error(DiagnosticErrorCode.UNSUPPORTED_TYPE);
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, fieldName, currentFieldType);
}

private void convertToArrayType(BXmlItem xmlItem, Field field, BMap<BString, Object> mapValue,
Expand Down Expand Up @@ -541,7 +541,7 @@ private void checkRestTypeAndConvertForUnionTypes(BXmlItem xmlItem, String elemN
// ignore
}
}
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, elementType);
throw DiagnosticLog.error(DiagnosticErrorCode.FIELD_CANNOT_CAST_INTO_TYPE, elemName, elementType);
}

private void handleArrayValueForRestType(BXmlItem xmlItem, String elemName, Type restType,
Expand Down

0 comments on commit d9f5eb3

Please sign in to comment.