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 namespaces related issues for toXml API in data.xmldata module #66

Open
wants to merge 6 commits into
base: after-u11-fixes
Choose a base branch
from
Open
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
269 changes: 269 additions & 0 deletions ballerina/tests/test_ref_types_with_annotations.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
import ballerina/test;

@Name {
value: "root"
}
@Namespace {
uri: "http://ballerina.io",
prefix: "a1"
}
type TypeRefType1 record {
@Name {
value: "nestedA"
}
NestedTypeRefType1 a;

@Name {
value: "nestedB"
}
NestedTypeRefType2 b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedA"
}
type NestedTypeRefType1 record {
@Name {
value: "A"
}
int a;

@Name {
value: "B"
}
int b;
};

type NestedTypeRefType2 record {
@Name {
value: "A"
}
int a;

@Name {
value: "B"
}
int b;
};

type TypeRefA1 TypeRefType1;

@Namespace {
uri: "http://ballerina.io",
prefix: "A1"
}
type TypeRefB1 TypeRefA1;

@Namespace {
uri: "http://ballerina.io",
prefix: "A1"
}
@Name {
value: "r"
}
type TypeRefC1 TypeRefA1;

@test:Config
function testTypeRefAnnotations() returns error? {
TypeRefType1 t = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB><A>3</A><B>4</B></nestedB></a1:root>`);

TypeRefA1 ta = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xmlResult = check toXml(ta);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB><A>3</A><B>4</B></nestedB></a1:root>`);

TypeRefB1 tb = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xmlResult = check toXml(tb);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<A1:root xmlns:A1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB><A>3</A><B>4</B></nestedB></A1:root>`);

TypeRefC1 tc = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xmlResult = check toXml(tc);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<A1:r xmlns:A1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB><A>3</A><B>4</B></nestedB></A1:r>`);
}

@Namespace {
uri: "http://ballerina.io",
prefix: "a1"
}
@Name {
value: "root"
}
type TypeRefType2 record {
@Name {
value: "nestedA"
}
NestedTypeRefType3 a;

@Name {
value: "nestedB"
}
NestedTypeRefType4 b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedA"
}
type NestedTypeRefType3 record {
@Name {
value: "A"
}
int a;

@Name {
value: "B"
}
int b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedB"
}
type NestedTypeRefType4 record {
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int a;

@Name {
value: "B"
}
int b;
};

@test:Config
function testTypeRefAnnotations2() returns error? {
TypeRefType2 t = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><A>1</A><B>2</B></nestedA:nestedA><nestedB:nestedB xmlns:nestedB="http://nested-ballerina.io"><a:a xmlns:a="http://nested-ballerina.io/a">3</a:a><B>4</B></nestedB:nestedB></a1:root>`);
}

@Name {
value: "root"
}
type TypeRefType3 record {
@Namespace {
uri: "http://www.example.com/",
prefix: "ex"
}
@Name {
value: "A"
}
int a;
};

@test:Config
function testTypeRefAnnotations3() returns error? {
TypeRefType3 t = {a: 1};
xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<root><ex:A xmlns:ex="http://www.example.com/">1</ex:A></root>`);
}

@Namespace {
uri: "http://ballerina.io",
prefix: "a1"
}
@Name {
value: "root"
}
type TypeRefType4 record {
@Name {
value: "nestedA"
}
NestedTypeRefType5 a;

@Name {
value: "nestedB"
}
NestedTypeRefType6 b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedA"
}
type NestedTypeRefType5 record {
@Name {
value: "A"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int a;

@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
@Name {
value: "B"
}
int b;
};

@Namespace {
uri: "http://nested-ballerina.io",
prefix: "nestedB"
}
type NestedTypeRefType6 record {
@Name {
value: "A"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int a;

@Name {
value: "B"
}
@Namespace {
uri: "http://nested-ballerina.io/a",
prefix: "a"
}
int b;
};

@test:Config
function testTypeRefAnnotations4() returns error? {
TypeRefType4 t = {
a: {a: 1, b: 2},
b: {a: 3, b: 4}
};

xml xmlResult = check toXml(t);
test:assertTrue(xmlResult is xml:Element);
test:assertEquals(xmlResult.toString(), string `<a1:root xmlns:a1="http://ballerina.io"><nestedA:nestedA xmlns:nestedA="http://nested-ballerina.io"><a:A xmlns:a="http://nested-ballerina.io/a">1</a:A><a:B xmlns:a="http://nested-ballerina.io/a">2</a:B></nestedA:nestedA><nestedB:nestedB xmlns:nestedB="http://nested-ballerina.io"><a:A xmlns:a="http://nested-ballerina.io/a">3</a:A><a:B xmlns:a="http://nested-ballerina.io/a">4</a:B></nestedB:nestedB></a1:root>`);
}
63 changes: 63 additions & 0 deletions ballerina/tests/toXml_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -1378,3 +1378,66 @@ function testMapXmltoXmlNegative() {
xml|Error result = toXml(data);
test:assertEquals((<Error>result).message(), "unsupported input type");
}

type ContentRec record {
string \#content;
};

@Name {
value: "root1"
}
type ContentRec2 record {
string \#content;
};

type ContentRec3 record {
@Name {
value: "value"
}
string \#content;
};

@Namespace {
uri: "www.example.com",
prefix: "ns"
}
type ContentRec4 record {
string \#content;
};

@test:Config {
groups: ["toXml"]
}
function testToXmlWithContentField() returns error? {
ContentRec document = {
\#content: "this is a string"
};
xml xmlDocument = check toXml(document);
test:assertEquals(xmlDocument.toString(), "<ContentRec>this is a string</ContentRec>");

ContentRec2 document2 = {
\#content: "this is a string"
};
xmlDocument = check toXml(document2);
test:assertEquals(xmlDocument.toString(), "<root1>this is a string</root1>");

ContentRec3 document3 = {
\#content: "this is a string"
};
xmlDocument = check toXml(document3);
test:assertEquals(xmlDocument.toString(), "<ContentRec3><value>this is a string</value></ContentRec3>");

ContentRec4 document4 = {
\#content: "this is a string"
};
xmlDocument = check toXml(document4);
test:assertEquals(xmlDocument.toString(), "<ns:ContentRec4 xmlns:ns=\"www.example.com\">this is a string</ns:ContentRec4>");

record {
int \#content;
} document5 = {
\#content: 2
};
xmlDocument = check toXml(document5);
test:assertEquals(xmlDocument.toString(), "<root>2</root>");
}
Loading
Loading