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

Support default values for record and fix usage of value creator apis #20

Merged
merged 14 commits into from
Jul 25, 2024
Merged
272 changes: 204 additions & 68 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");
}

// Negative cases
type DataN1 record {|
int A;
Expand Down Expand Up @@ -2871,28 +2947,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 @@ -3240,3 +3294,85 @@ function testInvalidNamespaceInOpenRecordForParseAsType2() {
test:assertTrue(err is error);
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");
}
2 changes: 1 addition & 1 deletion ballerina/tests/from_xml_with_options.bal
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function testDisableProjectionInArrayForParseStringNegative() {
test:assertEquals((<error>rec).message(), "array size is not compatible with the expected size");
}

@test:Config
@test:Config
function testDisableProjectionInArrayForParseAsTypeNegative() {
xml xmlVal1 = xml `<DataProj>
<A><B>1</B></A>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ private Constants() {}
public static final BString ALLOW_DATA_PROJECTION = StringUtils.fromString("allowDataProjection");
public static final String NON_NUMERIC_STRING_REGEX = "[^a-zA-Z\\d\s]";
public static final String NS_ANNOT_NOT_DEFINED = "$$ns_annot_not_defined$$";
public static final String REGEXP_MODULE_NAME = "lang.regexp";
public static final String REGEXP_TYPE_NAME = "RegExp";
}
Loading
Loading