aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2022-12-29 02:01:08 +0000
committerJJ2022-12-29 02:01:08 +0000
commitddf614b732649875f20b54af3ef622c5c39127ff (patch)
treeb2d093026868b6f4e8347f1afd097eb4f1a6efb3
parent4d55ecb842fab83e25adfd2cac76bc6b1ba8d0da (diff)
Clean up tests some and remove console interface
-rw-r--r--src/main/ui/BrowserApp.java86
-rw-r--r--src/test/model/css/CssParserTest.java16
-rw-r--r--src/test/model/html/HtmlParserTest.java40
3 files changed, 25 insertions, 117 deletions
diff --git a/src/main/ui/BrowserApp.java b/src/main/ui/BrowserApp.java
deleted file mode 100644
index 6f871c5..0000000
--- a/src/main/ui/BrowserApp.java
+++ /dev/null
@@ -1,86 +0,0 @@
-package ui;
-
-import model.html.*;
-
-import java.nio.file.*;
-import java.util.*;
-
-/**
- * The console interface to Apus.
- */
-public class BrowserApp {
- private Scanner input;
- private static final String border = "===============================================";
- private String pathString;
- private ArrayList<Node> parsed;
- private ArrayDeque<String> tabs;
-
- // Renders an arbitrary HTML page and arbitrary HTML input.
- public BrowserApp() {
- println("apus: currently a barebones html/css renderer");
- this.input = new Scanner(System.in);
- this.tabs = new ArrayDeque<>();
-
- while (true) {
- try {
- String file = Files.readString(Path.of(pathString));
- HtmlParser parser = new HtmlParser();
- parsed = parser.parseHtml(file);
- println(border);
- renderHtml(parsed);
- println(border);
- println("Page rendered. Input additional commands if desired.");
- println("Impemented commands: newuri, newtab, nexttab, quit");
- handleInput(this.input.next());
- println(border);
- } catch (Exception e) {
- println("Reading from the file failed with " + e.toString());
- println("Please try again.");
- }
- }
- }
-
- // Barebones HTML rendering. Iterates through a list of Nodes and their children and prints any text.
- private void renderHtml(ArrayList<Node> html) {
- for (Node node: html) {
- switch (node) {
- case ElementNode e -> {
- renderHtml(e.children);
- }
- default -> {
- println(node.data());
- }
- }
- }
- }
-
- // Handles user input after rendering an initial site
- private void handleInput(String input) {
- switch (input) {
- case "newuri" -> {
- println("please provide a path to a file (examples located in data/*):");
- pathString = this.input.next();
- }
- case "newtab" -> {
- this.tabs.add(pathString);
- println("please provide a path to a file (examples located in data/*):");
- pathString = this.input.next();
- }
- case "nexttab" -> {
- this.tabs.add(pathString);
- pathString = this.tabs.removeFirst();
- }
- case "quit" -> System.exit(0);
- default -> println("Sorry, I didn't quite get that. Please try again.");
- }
- }
-
- private void print(String toPrint) {
- System.out.print(toPrint);
- }
-
- private void println(String toPrint) {
- System.out.println(toPrint);
- }
-
-}
diff --git a/src/test/model/css/CssParserTest.java b/src/test/model/css/CssParserTest.java
index 42cdd9f..e6ea519 100644
--- a/src/test/model/css/CssParserTest.java
+++ b/src/test/model/css/CssParserTest.java
@@ -1,6 +1,5 @@
package model.css;
-import model.css.CssParser;
import org.javatuples.Pair;
import org.junit.jupiter.api.Test;
@@ -10,15 +9,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class CssParserTest {
+ String 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 }}}";
+
@Test
void testIdiomaticCss() {
- String 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<Pair<String, ArrayList<Pair<String, String>>>> expected = new ArrayList<>();
- ArrayList<Pair<String, String>> body = new ArrayList<>();
- ArrayList<Pair<String, String>> divOne = new ArrayList<>();
- ArrayList<Pair<String, String>> selectors = new ArrayList<>();
- ArrayList<Pair<String, String>> divTwo = new ArrayList<>();
+ var expected = new ArrayList<Pair<String, ArrayList<Pair<String, String>>>>();
+ var body = new ArrayList<Pair<String, String>>();
+ var divOne = new ArrayList<Pair<String, String>>();
+ var selectors = new ArrayList<Pair<String, String>>();
+ var divTwo = new ArrayList<Pair<String, String>>();
expected.add(new Pair<>("body", body));
expected.add(new Pair<>("div", divOne));
expected.add(new Pair<>("a:link,a:visited", selectors));
@@ -40,7 +39,6 @@ public class CssParserTest {
CssParser parser = new CssParser();
assertEqualsCss(parser.parseCSS(idiomaticCss), expected);
- // System.out.println(parser.parseCSS(idiomaticCss));
}
@Test
diff --git a/src/test/model/html/HtmlParserTest.java b/src/test/model/html/HtmlParserTest.java
index 4a35320..743f9f7 100644
--- a/src/test/model/html/HtmlParserTest.java
+++ b/src/test/model/html/HtmlParserTest.java
@@ -16,57 +16,54 @@ public class HtmlParserTest {
@Test
void testIdiomaticHtml() {
- ArrayList<Node> expected = new ArrayList<>();
- ArrayList<Node> expectedChildren = new ArrayList<>();
- ArrayList<Node> expectedGrandChildren = new ArrayList<>();
- ArrayList<Node> expectedGreatGrandChildren = new ArrayList<>();
+ var expected = new ArrayList<Node>();
+ var expectedChildren = new ArrayList<Node>();
+ var expectedGrandChildren = new ArrayList<Node>();
+ var expectedGreatGrandChildren = new ArrayList<Node>();
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();
+ var parser = new HtmlParser();
assertEqualsHtml(parser.parseHtml(idiomaticHtml), expected);
- // displayHtmlTree(parser.parseHtml(idiomaticHtml));
}
@Test
void testBrokenHtml() {
- ArrayList<Node> expected = new ArrayList<>();
- ArrayList<Node> expectedChildren = new ArrayList<>();
- ArrayList<Node> expectedGrandChildren = new ArrayList<>();
+ var expected = new ArrayList<Node>();
+ var expectedChildren = new ArrayList<Node>();
+ var expectedGrandChildren = new ArrayList<Node>();
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();
+ var parser = new HtmlParser();
assertEqualsHtml(parser.parseHtml(brokenHtml), expected);
- // displayHtmlTree(parser.parseHtml(brokenHtml));
}
@Test
void testTrailingTextHtml() {
- ArrayList<Node> expected = new ArrayList<>();
- ArrayList<Node> expectedChildren = new ArrayList<>();
- ArrayList<Node> expectedGrandChildren = new ArrayList<>();
+ var expected = new ArrayList<Node>();
+ var expectedChildren = new ArrayList<Node>();
+ var expectedGrandChildren = new ArrayList<Node>();
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();
+ var parser = new HtmlParser();
assertEqualsHtml(parser.parseHtml(trailingTextHtml), expected);
- // displayHtmlTree(parser.parseHtml(trailingTextHtml));
}
@Test
void testAttributesHtml() {
- ArrayList<Node> expected = new ArrayList<>();
- ArrayList<Node> expectedChildren = new ArrayList<>();
- ArrayList<Pair<String, String>> expectedAttributes = new ArrayList<>();
+ var expected = new ArrayList<Node>();
+ var expectedChildren = new ArrayList<Node>();
+ var expectedAttributes = new ArrayList<Pair<String, String>>();
expected.add(new ElementNode("html", new ArrayList<>(), expectedChildren));
expectedChildren.add(new ElementNode("attr", expectedAttributes));
expectedAttributes.add(new Pair<>("hello", "world"));
@@ -74,8 +71,8 @@ public class HtmlParserTest {
expectedAttributes.add(new Pair<>("strange", "cha\"rm"));
expectedAttributes.add(new Pair<>("up", "do'wn"));
- HtmlParser parser = new HtmlParser();
- ArrayList<Node> parsed = parser.parseHtml(attributesHtml);
+ var parser = new HtmlParser();
+ var parsed = parser.parseHtml(attributesHtml);
displayHtmlTree(parsed);
assertEqualsHtml(parsed, expected);
}
@@ -86,7 +83,6 @@ public class HtmlParserTest {
private static void assertEqualsHtml(ArrayList<Node> html, ArrayList<Node> expected) {
for (int i = 0; i < html.size(); i++) {
assertEquals(html.get(i).data(), expected.get(i).data());
- // System.out.println(html.get(i).getData() + " " + expected.get(i).getData());
switch (html.get(i)) {
case ElementNode e ->
assertEqualsHtml(e.children, ((ElementNode) expected.get(i)).children);