From 21b8e5f6cdcd9fab275efce5e88f02addfd19e7e Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 17 Oct 2022 08:29:53 -0700 Subject: Implement fairly comprehensive ParserTests 100% class and method coverage, 95% line coverage --- src/test/model/HtmlParserTest.java | 82 +++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 11 deletions(-) (limited to 'src/test/model/HtmlParserTest.java') diff --git a/src/test/model/HtmlParserTest.java b/src/test/model/HtmlParserTest.java index 58b4555..36c5935 100644 --- a/src/test/model/HtmlParserTest.java +++ b/src/test/model/HtmlParserTest.java @@ -2,39 +2,99 @@ package model; import model.html.ElementNode; import model.html.HtmlParser; +import model.html.TextNode; import model.util.Node; +import org.javatuples.Pair; import org.junit.jupiter.api.Test; import java.util.*; +import static org.junit.jupiter.api.Assertions.*; + public class HtmlParserTest { String idiomaticHtml = "

Hello, world!

"; - String brokenHtml = ""; @Test void testIdiomaticHtml() { - String[] idiomaticHtmlArray = {"","","","","","

","Hello,world!","

","",""}; + ArrayList expected = new ArrayList<>(); + ArrayList expectedChildren = new ArrayList<>(); + ArrayList expectedGrandChildren = new ArrayList<>(); + ArrayList expectedGreatGrandChildren = new ArrayList<>(); + expected.add(new ElementNode("html", new ArrayList<>(), expectedChildren)); + expectedChildren.add(new ElementNode("head")); + expectedChildren.add(new ElementNode("body", new ArrayList<>(), expectedGrandChildren)); + expectedGrandChildren.add(new ElementNode("p", new ArrayList<>(), expectedGreatGrandChildren)); + expectedGreatGrandChildren.add(new TextNode("Hello, world!")); + HtmlParser parser = new HtmlParser(); - displayHtmlTree(parser.parseHtml(idiomaticHtml)); -// assertEquals(HtmlParser.parseHtmlLL(idiomaticHtml), Arrays.asList(idiomaticHtmlArray)); + assertEqualsHtml(parser.parseHtml(idiomaticHtml), expected); + // displayHtmlTree(parser.parseHtml(idiomaticHtml)); } @Test void testBrokenHtml() { - String[] brokenHtmlArray = {"","","","",""}; + ArrayList expected = new ArrayList<>(); + ArrayList expectedChildren = new ArrayList<>(); + ArrayList expectedGrandChildren = new ArrayList<>(); + expected.add(new ElementNode("html", new ArrayList<>(), expectedChildren)); + expectedChildren.add(new ElementNode("foo", new ArrayList<>(), expectedGrandChildren)); + expectedGrandChildren.add(new ElementNode("bar", new ArrayList<>())); + expectedGrandChildren.add(new TextNode("<>")); + HtmlParser parser = new HtmlParser(); - displayHtmlTree(parser.parseHtml(brokenHtml)); -// assertEquals(HtmlParser.parseHtmlLL(brokenHtml), Arrays.asList(brokenHtmlArray)); + assertEqualsHtml(parser.parseHtml(brokenHtml), expected); + // displayHtmlTree(parser.parseHtml(brokenHtml)); } @Test void testTrailingTextHtml() { - String[] trailingTextHtmlArray = {"","","","","ba"}; + ArrayList expected = new ArrayList<>(); + ArrayList expectedChildren = new ArrayList<>(); + ArrayList expectedGrandChildren = new ArrayList<>(); + expected.add(new TextNode("bot")); + expected.add(new ElementNode("html", new ArrayList<>(), expectedChildren)); + expected.add(new TextNode("ba")); + expectedChildren.add(new ElementNode("foo", new ArrayList<>(), expectedGrandChildren)); + expectedGrandChildren.add(new ElementNode("bar", new ArrayList<>())); + HtmlParser parser = new HtmlParser(); - displayHtmlTree(parser.parseHtml(trailingTextHtml)); -// assertEquals(HtmlParser.parseHtmlLL(trailingTextHtml), Arrays.asList(trailingTextHtmlArray)); + assertEqualsHtml(parser.parseHtml(trailingTextHtml), expected); + // displayHtmlTree(parser.parseHtml(trailingTextHtml)); + } + + @Test + void testAttributesHtml() { + ArrayList expected = new ArrayList<>(); + ArrayList expectedChildren = new ArrayList<>(); + ArrayList> expectedAttributes = new ArrayList<>(); + expected.add(new ElementNode("html", new ArrayList<>(), expectedChildren)); + expectedChildren.add(new ElementNode("attr", expectedAttributes)); + expectedAttributes.add(new Pair<>("hello", "world")); + expectedAttributes.add(new Pair<>("foo", "bar")); + expectedAttributes.add(new Pair<>("strange", "cha\"rm")); + expectedAttributes.add(new Pair<>("up", "do'wn")); + + HtmlParser parser = new HtmlParser(); + var parsed = parser.parseHtml(attributesHtml); + displayHtmlTree(parsed); + assertEqualsHtml(parsed, expected); + } + + /** + * Complicated helper function for tests. + */ + private static void assertEqualsHtml(ArrayList html, ArrayList expected) { + for (int i = 0; i < html.size(); i++) { + assertEquals(html.get(i).getData(), expected.get(i).getData()); + // System.out.println(html.get(i).getData() + " " + expected.get(i).getData()); + if (html.get(i) instanceof ElementNode) { + assertEqualsHtml(((ElementNode) html.get(i)).getChildren(), ((ElementNode) expected.get(i)).getChildren()); + } + } } /** -- cgit v1.2.3-70-g09d2