From 9eea270aaec82bc38d297f7aa2282877d48cd3c1 Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 17 Oct 2022 08:45:08 -0700 Subject: Split tests into packages --- README.md | 8 +-- src/test/model/CssParserTest.java | 68 ------------------ src/test/model/HtmlParserTest.java | 118 -------------------------------- src/test/model/css/CssParserTest.java | 68 ++++++++++++++++++ src/test/model/html/HtmlParserTest.java | 118 ++++++++++++++++++++++++++++++++ 5 files changed, 190 insertions(+), 190 deletions(-) delete mode 100644 src/test/model/CssParserTest.java delete mode 100644 src/test/model/HtmlParserTest.java create mode 100644 src/test/model/css/CssParserTest.java create mode 100644 src/test/model/html/HtmlParserTest.java diff --git a/README.md b/README.md index 033a880..c5b7808 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ I've heard that Java Swing has a native HTML rendering component. I hope to enti ## user stories -- As a user, I want to open and render an HTML file. -- As a user, I want to be able to add a new tab to a list of open tabs. -- As a user, I want to be able to click links on a rendered webpage and view the linked pages. -- As a user, I want to be able to apply basic styling to an open page. \ No newline at end of file +- As a user, I want to be able to construct a structural representation of an HTML file. +- As a user, I want to be able to construct a structural representation of a CSS file. +- As a user, I want to be able to add multiple Nodes to an HTML file. +- As a user, I want to be able to open and render an arbitrary HTML file. diff --git a/src/test/model/CssParserTest.java b/src/test/model/CssParserTest.java deleted file mode 100644 index 35f5310..0000000 --- a/src/test/model/CssParserTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package model; - -import model.css.CssParser; -import org.javatuples.Pair; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class CssParserTest { - - @Test - void testIdiomaticCss() { - var idiomaticCss = "body { background-color: '#\\'f0f0f2'; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe\\\" UI\", 'Open\\' Sans', \"Helvetica Neue\", Helvetica, Arial, sans-serif;}div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);}a:link, a:visited { color: #38488f; text-decoration: none;}@media (max - width : 700px) { @media () {div { margin: 0 auto; width: auto }}}"; - - ArrayList>>> expected = new ArrayList<>(); - ArrayList> body = new ArrayList<>(); - ArrayList> divOne = new ArrayList<>(); - ArrayList> selectors = new ArrayList<>(); - ArrayList> divTwo = new ArrayList<>(); - expected.add(new Pair<>("body", body)); - expected.add(new Pair<>("div", divOne)); - expected.add(new Pair<>("a:link,a:visited", selectors)); - expected.add(new Pair<>("div", divTwo)); - body.add(new Pair<>("background-color", "'#'f0f0f2'")); - body.add(new Pair<>("margin", "0")); - body.add(new Pair<>("padding", "0")); - body.add(new Pair<>("font-family", "-apple-system,system-ui,BlinkMacSystemFont,\"Segoe\" UI\",'Open' Sans',\"Helvetica Neue\",Helvetica,Arial,sans-serif")); - divOne.add(new Pair<>("width", "600px")); - divOne.add(new Pair<>("margin", "5emauto")); - divOne.add(new Pair<>("padding", "2em")); - divOne.add(new Pair<>("background-color", "#fdfdff")); - divOne.add(new Pair<>("border-radius", "0.5em")); - divOne.add(new Pair<>("box-shadow", "2px3px7px2pxrgba(0,0,0,0.02)")); - selectors.add(new Pair<>("color", "#38488f")); - selectors.add(new Pair<>("text-decoration", "none")); - divTwo.add(new Pair<>("margin", "0auto")); - divTwo.add(new Pair<>("width", "auto")); - - CssParser parser = new CssParser(); - assertEqualsCss(parser.parseCSS(idiomaticCss), expected); - // System.out.println(parser.parseCSS(idiomaticCss)); - } - - @Test - void testConversions() { - assertEquals(CssParser.parseUnits("0gosf"), 0); - assertEquals(CssParser.parseUnits("0px"), 0); - assertEquals(CssParser.parseUnits("0.0pc"), 0); - assertEquals(CssParser.parseUnits("0.00pt"), 0); - assertEquals(CssParser.parseUnits("0cm"), 0); - assertEquals(CssParser.parseUnits("0mm"), 0); - assertEquals(CssParser.parseUnits("0Q"), 0); - assertEquals(CssParser.parseUnits("0in"), 0); - } - - public static void assertEqualsCss(ArrayList>>> css, - ArrayList>>> expected) { - for (int i = 0; i < css.size(); i++) { - assertEquals(css.get(i).getValue0(), expected.get(i).getValue0()); - for (int j = 0; j < css.get(i).getValue1().size(); j++) { - assertEquals(css.get(i).getValue1().get(j).getValue0(), expected.get(i).getValue1().get(j).getValue0()); - assertEquals(css.get(i).getValue1().get(j).getValue1(), expected.get(i).getValue1().get(j).getValue1()); - } - } - } -} diff --git a/src/test/model/HtmlParserTest.java b/src/test/model/HtmlParserTest.java deleted file mode 100644 index 36c5935..0000000 --- a/src/test/model/HtmlParserTest.java +++ /dev/null @@ -1,118 +0,0 @@ -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() { - 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(); - assertEqualsHtml(parser.parseHtml(idiomaticHtml), expected); - // displayHtmlTree(parser.parseHtml(idiomaticHtml)); - } - - @Test - void testBrokenHtml() { - 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(); - assertEqualsHtml(parser.parseHtml(brokenHtml), expected); - // displayHtmlTree(parser.parseHtml(brokenHtml)); - } - - @Test - void testTrailingTextHtml() { - 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(); - 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()); - } - } - } - - /** - * Simple helper function for debugging. - * EFFECTS: prints a representation of the tree to the console for debugging purposes - */ - private void displayHtmlTree(ArrayList tree) { - for (Node node : tree) { - if (node instanceof ElementNode) { - System.out.print(((ElementNode) node).getTag() + ": "); - for (Node n : ((ElementNode) node).getChildren()) { - System.out.print(n.getData() + " "); - } - System.out.println(); - displayHtmlTree(((ElementNode) node).getChildren()); - } else { - System.out.println("Text: " + node.getData()); - } - } - } -} diff --git a/src/test/model/css/CssParserTest.java b/src/test/model/css/CssParserTest.java new file mode 100644 index 0000000..a2335f2 --- /dev/null +++ b/src/test/model/css/CssParserTest.java @@ -0,0 +1,68 @@ +package model.css; + +import model.css.CssParser; +import org.javatuples.Pair; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CssParserTest { + + @Test + void testIdiomaticCss() { + var idiomaticCss = "body { background-color: '#\\'f0f0f2'; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, \"Segoe\\\" UI\", 'Open\\' Sans', \"Helvetica Neue\", Helvetica, Arial, sans-serif;}div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);}a:link, a:visited { color: #38488f; text-decoration: none;}@media (max - width : 700px) { @media () {div { margin: 0 auto; width: auto }}}"; + + ArrayList>>> expected = new ArrayList<>(); + ArrayList> body = new ArrayList<>(); + ArrayList> divOne = new ArrayList<>(); + ArrayList> selectors = new ArrayList<>(); + ArrayList> divTwo = new ArrayList<>(); + expected.add(new Pair<>("body", body)); + expected.add(new Pair<>("div", divOne)); + expected.add(new Pair<>("a:link,a:visited", selectors)); + expected.add(new Pair<>("div", divTwo)); + body.add(new Pair<>("background-color", "'#'f0f0f2'")); + body.add(new Pair<>("margin", "0")); + body.add(new Pair<>("padding", "0")); + body.add(new Pair<>("font-family", "-apple-system,system-ui,BlinkMacSystemFont,\"Segoe\" UI\",'Open' Sans',\"Helvetica Neue\",Helvetica,Arial,sans-serif")); + divOne.add(new Pair<>("width", "600px")); + divOne.add(new Pair<>("margin", "5emauto")); + divOne.add(new Pair<>("padding", "2em")); + divOne.add(new Pair<>("background-color", "#fdfdff")); + divOne.add(new Pair<>("border-radius", "0.5em")); + divOne.add(new Pair<>("box-shadow", "2px3px7px2pxrgba(0,0,0,0.02)")); + selectors.add(new Pair<>("color", "#38488f")); + selectors.add(new Pair<>("text-decoration", "none")); + divTwo.add(new Pair<>("margin", "0auto")); + divTwo.add(new Pair<>("width", "auto")); + + CssParser parser = new CssParser(); + assertEqualsCss(parser.parseCSS(idiomaticCss), expected); + // System.out.println(parser.parseCSS(idiomaticCss)); + } + + @Test + void testConversions() { + assertEquals(CssParser.parseUnits("0gosf"), 0); + assertEquals(CssParser.parseUnits("0px"), 0); + assertEquals(CssParser.parseUnits("0.0pc"), 0); + assertEquals(CssParser.parseUnits("0.00pt"), 0); + assertEquals(CssParser.parseUnits("0cm"), 0); + assertEquals(CssParser.parseUnits("0mm"), 0); + assertEquals(CssParser.parseUnits("0Q"), 0); + assertEquals(CssParser.parseUnits("0in"), 0); + } + + public static void assertEqualsCss(ArrayList>>> css, + ArrayList>>> expected) { + for (int i = 0; i < css.size(); i++) { + assertEquals(css.get(i).getValue0(), expected.get(i).getValue0()); + for (int j = 0; j < css.get(i).getValue1().size(); j++) { + assertEquals(css.get(i).getValue1().get(j).getValue0(), expected.get(i).getValue1().get(j).getValue0()); + assertEquals(css.get(i).getValue1().get(j).getValue1(), expected.get(i).getValue1().get(j).getValue1()); + } + } + } +} diff --git a/src/test/model/html/HtmlParserTest.java b/src/test/model/html/HtmlParserTest.java new file mode 100644 index 0000000..a7093e1 --- /dev/null +++ b/src/test/model/html/HtmlParserTest.java @@ -0,0 +1,118 @@ +package model.html; + +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() { + 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(); + assertEqualsHtml(parser.parseHtml(idiomaticHtml), expected); + // displayHtmlTree(parser.parseHtml(idiomaticHtml)); + } + + @Test + void testBrokenHtml() { + 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(); + assertEqualsHtml(parser.parseHtml(brokenHtml), expected); + // displayHtmlTree(parser.parseHtml(brokenHtml)); + } + + @Test + void testTrailingTextHtml() { + 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(); + 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()); + } + } + } + + /** + * Simple helper function for debugging. + * EFFECTS: prints a representation of the tree to the console for debugging purposes + */ + private void displayHtmlTree(ArrayList tree) { + for (Node node : tree) { + if (node instanceof ElementNode) { + System.out.print(((ElementNode) node).getTag() + ": "); + for (Node n : ((ElementNode) node).getChildren()) { + System.out.print(n.getData() + " "); + } + System.out.println(); + displayHtmlTree(((ElementNode) node).getChildren()); + } else { + System.out.println("Text: " + node.getData()); + } + } + } +} -- cgit v1.2.3-70-g09d2