Skip to content

Commit

Permalink
- F XmlXomApprovals fixes #466
Browse files Browse the repository at this point in the history
Co-authored-by: Jay Bazuzi <[email protected]>
Co-authored-by: Llewellyn Falco <[email protected]>
  • Loading branch information
3 people committed Oct 7, 2024
1 parent aa8d2c3 commit 69d5e6c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void getLineNumberOfThisMethod() throws Exception
var expected = """
(line 5,col 3)-(line 7,col 3)
""";
Method method = XmlApprovals.class.getMethod("verify", String.class);
Method method = XmlXomApprovals.class.getMethod("verify", String.class);
Range r = ParserUtilities.getLineNumbersForMethod(method);
Approvals.verify(r, new Options().inline(expected));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package org.approvaltests;

import nu.xom.*;
import org.approvaltests.core.Options;
import org.junit.jupiter.api.Test;

import java.io.*;

class XmlFormattingTest
{
// @Disabled("SPIKE for #466 - continue next time")
@Test
void xmlWithEmojiesAndAmpersands()
{
Expand All @@ -19,33 +15,6 @@ void xmlWithEmojiesAndAmpersands()
</a>
""";
String minimizedXml = expected.replaceAll("\n", " ").replace(" ", "");
verifyXml(minimizedXml, expected);
}
private static void verifyXml(String minimizedXml, String expected)
{
Options options = new Options().inline(expected);
final String formattedXml = prettyPrintXml(minimizedXml, 2);
Approvals.verify(formattedXml, options.forFile().withExtension(".xml"));
}
private static String prettyPrintXml(String minimizedXml, int indent)
{
try
{
Builder builder = new Builder();
Document doc = builder.build(new StringReader(minimizedXml));
// create a string output stream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Serializer serializer = new Serializer(byteArrayOutputStream, "UTF-8");
serializer.setIndent(indent); // Increase indentation to make XML more readable
serializer.setLineSeparator("\n"); // Ensure new lines are added
serializer.setMaxLength(0); // Prevent line wrapping
serializer.write(doc);
return byteArrayOutputStream.toString("UTF-8");
}
catch (Exception e)
{
e.printStackTrace();
return minimizedXml + "\n\nXML Parsing Error:\n" + e.getMessage();
}
XmlXomApprovals.verifyXml(minimizedXml, new Options().inline(expected));
}
}
7 changes: 7 additions & 0 deletions approvaltests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.3.9</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down

This file was deleted.

41 changes: 41 additions & 0 deletions approvaltests/src/main/java/org/approvaltests/XmlXomApprovals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.approvaltests;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Serializer;
import org.approvaltests.core.Options;

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

public class XmlXomApprovals
{
public static void verifyXml(String xml)
{
verifyXml(xml, new Options());
}
public static void verifyXml(String xml, Options options)
{
Approvals.verifyXml(xml, x -> prettyPrintXml(x, 2), options);
}
public static String prettyPrintXml(String minimizedXml, int indent)
{
try
{
Builder builder = new Builder();
Document doc = builder.build(new StringReader(minimizedXml));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
Serializer serializer = new Serializer(byteArrayOutputStream, "UTF-8");
serializer.setIndent(indent);
serializer.setLineSeparator("\n");
serializer.setMaxLength(0);
serializer.write(doc);
return byteArrayOutputStream.toString("UTF-8");
}
catch (Exception e)
{
e.printStackTrace();
return minimizedXml + "\n\nXML Parsing Error:\n" + e.getMessage();
}
}
}

0 comments on commit 69d5e6c

Please sign in to comment.