Skip to content

Commit

Permalink
Sync with main
Browse files Browse the repository at this point in the history
  • Loading branch information
prakanth97 committed Jul 25, 2024
2 parents 1990aeb + 53c2019 commit 2f4694a
Show file tree
Hide file tree
Showing 11 changed files with 649 additions and 363 deletions.
8 changes: 4 additions & 4 deletions ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
org = "ballerina"
name = "data.xmldata"
version = "0.1.4"
version = "1.0.0"
authors = ["Ballerina"]
keywords = ["xml"]
repository = "https://github.com/ballerina-platform/module-ballerina-data-xmldata"
license = ["Apache-2.0"]
distribution = "2201.9.0"
distribution = "2201.10.0"
export = ["data.xmldata"]

[[platform.java17.dependency]]
groupId = "io.ballerina.lib"
artifactId = "data-native"
version = "0.1.4"
path = "../native/build/libs/data.xmldata-native-0.1.4-SNAPSHOT.jar"
version = "1.0.0"
path = "../native/build/libs/data.xmldata-native-1.0.0-SNAPSHOT.jar"

[[platform.java17.dependency]]
groupId = "io.ballerina.stdlib"
Expand Down
2 changes: 1 addition & 1 deletion ballerina/CompilerPlugin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ id = "constraint-compiler-plugin"
class = "io.ballerina.lib.data.xmldata.compiler.XmldataCompilerPlugin"

[[dependency]]
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-0.1.4-SNAPSHOT.jar"
path = "../compiler-plugin/build/libs/data.xmldata-compiler-plugin-1.0.0-SNAPSHOT.jar"
4 changes: 2 additions & 2 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.9.0"
distribution-version = "2201.10.0-20240724-114000-40e856f7"

[[package]]
org = "ballerina"
Expand All @@ -22,7 +22,7 @@ modules = [
[[package]]
org = "ballerina"
name = "data.xmldata"
version = "0.1.4"
version = "1.0.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "io"},
Expand Down
284 changes: 210 additions & 74 deletions ballerina/tests/fromXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1620,52 +1620,6 @@ function testCommentMiddleInContent2() returns error? {
test:assertEquals(rec2.A, "John Doe");
}

@test:Config
function testRegexAsFieldTypeWithParseString() returns error? {
string xmlStr = string `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|} rec1 = check parseString(xmlStr);
test:assertEquals(rec1.length(), 3);
test:assertEquals(rec1.A, [1, 2]);
test:assertEquals(rec1.B, "Code");
test:assertEquals(rec1.C.name, "Kanth");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType() returns error? {
xml xmlVal = xml `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|} rec1 = check parseAsType(xmlVal);
test:assertEquals(rec1.length(), 3);
test:assertEquals(rec1.A, [1, 2]);
test:assertEquals(rec1.B, "Code");
test:assertEquals(rec1.C.name, "Kanth");
}

@test:Config
function testAnydataAsRestFieldWithParseString() returns error? {
string xmlStr = string `<Data>
Expand Down Expand Up @@ -2812,6 +2766,128 @@ function testProjectionWithXmlAttributeForParseAsType() returns error? {
test:assertEquals(rec.A, "2");
}

type DataN3 record {|
int[4] A;
|};

@test:Config {
groups: ["fromXmlString"]
}
function testXmlStringToRecordWithArrayAsFieldType() returns error? {
string xmlStr1 = "<Data><A>1</A><A>2</A><A>3</A></Data>";
DataN3 rec1 = check parseString(xmlStr1);
test:assertEquals(rec1.A, [1, 2, 3, 0]);
}

@test:Config {
groups: ["fromXml"]
}
function testXmlToRecordWithArrayAsFieldType() returns error? {
xml xmlVal1 = xml `<Data><A>1</A><A>2</A><A>3</A></Data>`;
DataN3 rec1 = check parseAsType(xmlVal1);
test:assertEquals(rec1.A, [1, 2, 3, 0]);
}

type Student record {|
string name;
int age;
string code = "Admitted";
|};

@test:Config
function testXmlToRecordWithDefaultValuesForParseString1() returns error? {
string xmlStr = string `
<Student>
<name>Walter</name>
<age>27</age>
</Student>
`;

Student student = check parseString(xmlStr);
test:assertEquals(student.name, "Walter");
test:assertEquals(student.age, 27);
test:assertEquals(student.code, "Admitted");
}

@test:Config
function testXmlToRecordWithDefaultValuesForPasrseAsType1() returns error? {
xml xmlVal = xml `
<Student>
<name>Walter</name>
<age>27</age>
</Student>
`;

Student student = check parseAsType(xmlVal);
test:assertEquals(student.name, "Walter");
test:assertEquals(student.age, 27);
test:assertEquals(student.code, "Admitted");
}

type University record {|
Student[] student;
string name;
string category = "State";
|};

@test:Config
function testXmlToRecordWithDefaultValuesForParseString2() returns error? {
string xmlStr = string `
<University>
<student>
<name>Walter</name>
<age>27</age>
</student>
<student>
<name>Jessy</name>
<age>18</age>
</student>
<name>Standford</name>
</University>
`;

University university = check parseString(xmlStr);
test:assertEquals(university.student[0].name, "Walter");
test:assertEquals(university.student[0].age, 27);
test:assertEquals(university.student[0].code, "Admitted");

test:assertEquals(university.student[1].name, "Jessy");
test:assertEquals(university.student[1].age, 18);
test:assertEquals(university.student[1].code, "Admitted");

test:assertEquals(university.name, "Standford");
test:assertEquals(university.category, "State");
}

@test:Config
function testXmlToRecordWithDefaultValuesForParseAsType2() returns error? {
xml xmlVal = xml `
<University>
<student>
<name>Walter</name>
<age>27</age>
</student>
<student>
<name>Jessy</name>
<age>18</age>
</student>
<name>Standford</name>
</University>
`;

University university = check parseAsType(xmlVal);
test:assertEquals(university.student[0].name, "Walter");
test:assertEquals(university.student[0].age, 27);
test:assertEquals(university.student[0].code, "Admitted");

test:assertEquals(university.student[1].name, "Jessy");
test:assertEquals(university.student[1].age, 18);
test:assertEquals(university.student[1].code, "Admitted");

test:assertEquals(university.name, "Standford");
test:assertEquals(university.category, "State");
}

type RecType1 record {
string name;
@Name {
Expand All @@ -2838,15 +2914,15 @@ isolated function testElementAndAttributeInSameScopeHaveSameName() returns error
<Data name="Kevin">
<name>Kanth</name>
</Data>
`;
`;
RecType1 rec11 = check parseString(xmlStr);
test:assertEquals(rec11.name, "Kanth");
test:assertEquals(rec11.duplicateName, "Kevin");

RecType2 rec12 = check parseString(xmlStr);
test:assertEquals(rec12.name.\#content, "Kanth");
test:assertEquals(rec12.duplicateName, "Kevin");
test:assertEquals(rec12.duplicateName, "Kevin");

xml xmlVal = xml `
<Data name="Kevin">
<name>Kanth</name>
Expand All @@ -2858,7 +2934,7 @@ isolated function testElementAndAttributeInSameScopeHaveSameName() returns error

RecType2 rec22 = check parseAsType(xmlVal);
test:assertEquals(rec22.name.\#content, "Kanth");
test:assertEquals(rec22.duplicateName, "Kevin");
test:assertEquals(rec22.duplicateName, "Kevin");
}

type RecNs3 record {|
Expand Down Expand Up @@ -2888,7 +2964,7 @@ isolated function testElementWithDifferentNamespace() returns error? {
RecNs3 rec = check parseString(xmlStr);
test:assertEquals(rec.name, "Kevin");
test:assertEquals(rec.duplicateName, "Kanth");

xml xmlVal = xml `
<Data xmlns:ns1="example1.com" xmlns:ns2="example2.com">
<ns1:name>Kevin</ns1:name>
Expand Down Expand Up @@ -2959,28 +3035,6 @@ function testXmlToRecordNegative4() {
test:assertEquals((<error>rec1).message(), "expected 'int' value for the field 'A' found 'array' value");
}

type DataN3 record {|
int[4] A;
|};

@test:Config {
groups: ["fromXmlString"]
}
function testXmlStringToRecordNegative5() {
string xmlStr1 = "<Data><A>1</A><A>2</A><A>3</A></Data>";
DataN3|error rec1 = parseString(xmlStr1);
test:assertEquals((<error>rec1).message(), "array size is not compatible with the expected size");
}

@test:Config {
groups: ["fromXml"]
}
function testXmlToRecordNegative5() {
xml xmlVal1 = xml `<Data><A>1</A><A>2</A><A>3</A></Data>`;
DataN3|error rec1 = parseAsType(xmlVal1);
test:assertEquals((<error>rec1).message(), "array size is not compatible with the expected size");
}

type DataN4 record {|
string...;
|};
Expand Down Expand Up @@ -3329,6 +3383,88 @@ function testInvalidNamespaceInOpenRecordForParseAsType2() {
test:assertEquals((<error>err).message(), "undefined field 'name' in record 'data.xmldata:AuthorOpen'");
}

@test:Config
function testRegexAsFieldTypeWithParseStringNegative1() {
string xmlStr = string `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|}|Error err = parseString(xmlStr);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseStringNegative2() {
string xmlStr = string `<Data>
<A>1</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp A;
string B;
record {|
string name;
|} C;
|}|Error err = parseString(xmlStr);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType() {
xml xmlVal = xml `<Data>
<A>1</A>
<A>2</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp[] A;
string B;
record {|
string name;
|} C;
|}|error err = parseAsType(xmlVal);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

@test:Config
function testRegexAsFieldTypeWithParseAsType2() {
xml xmlVal = xml `<Data>
<A>1</A>
<B>Code</B>
<C>
<name>Kanth</name>
</C>
</Data>`;
record {|
string:RegExp A;
string B;
record {|
string name;
|} C;
|}|error err = parseAsType(xmlVal);
test:assertTrue(err is error);
test:assertEquals((<error>err).message(), "unsupported input type");
}

type RecTypeDup1 record {
string name;
@Name {
Expand Down Expand Up @@ -3377,7 +3513,7 @@ isolated function testDuplicateField() {
RecTypeDup2|Error err3 = parseString(xmlStr2);
test:assertTrue(err3 is Error);
test:assertEquals((<Error> err3).message(), "duplicate field 'name'");

xml xmlVal2 = xml `
<Data name="Kevin">
<name>Kanth</name>
Expand Down
Loading

0 comments on commit 2f4694a

Please sign in to comment.