feat(serialize-xml): port formatting tests

This commit is contained in:
Johannes Frohnmeyer 2024-04-20 15:55:02 +02:00
parent 5e89193244
commit 2a9a6300ef
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 58 additions and 0 deletions

View File

@ -365,4 +365,62 @@ public final class NativeXmlWriterTest {
writer.close();
writer.close();
}
@Test
public void testSetGetFormattingStyle() throws IOException {
String lineSeparator = "\r\n";
StringWriter stringWriter = new StringWriter();
NativeXmlWriter jsonWriter = new NativeXmlWriter(stringWriter);
// Default should be FormattingStyle.COMPACT
jsonWriter.setIndent(" \t ").setNewline(lineSeparator);
jsonWriter.beginTag("tag");
jsonWriter.text("true");
jsonWriter.text("text");
jsonWriter.text("5.0");
jsonWriter.text(null);
jsonWriter.endTag();
String expected = """
<tag>\r
\t true\r
\t <!---->\r
\t text\r
\t <!---->\r
\t 5.0\r
\t <!---->\r
\t null\r
</tag>""";
assertThat(stringWriter.toString()).isEqualTo(expected);
assertThat(jsonWriter.getNewline()).isEqualTo(lineSeparator);
}
@Test
public void testIndentOverwritesFormattingStyle() throws IOException {
StringWriter stringWriter = new StringWriter();
NativeXmlWriter jsonWriter = new NativeXmlWriter(stringWriter);
// Should overwrite formatting style
jsonWriter.setIndent(" ");
jsonWriter.beginTag("tag");
jsonWriter.attributeName("a");
jsonWriter.attributeValue("b");
jsonWriter.beginTag("tag");
jsonWriter.text("1");
jsonWriter.text("2");
jsonWriter.endTag();
jsonWriter.endTag();
String expected = """
<tag a="b">
<tag>
1
<!---->
2
</tag>
</tag>""";
assertThat(stringWriter.toString()).isEqualTo(expected);
}
}