From 97c994d8669cf7f9123ccac6b275c8b8252d5e0d Mon Sep 17 00:00:00 2001 From: Dimitry Ivanov Date: Fri, 24 Aug 2018 23:25:48 +0300 Subject: [PATCH] Remove attrs object from format --- .../renderer/visitor/TestDataReader.java | 113 +++++++++++------- markwon/src/test/resources/tests/first.yaml | 3 +- markwon/src/test/resources/tests/html.yaml | 12 +- .../src/test/resources/tests/single-a.yaml | 3 +- .../src/test/resources/tests/single-img.yaml | 7 +- .../src/test/resources/tests/single-ol.yaml | 3 +- .../resources/tests/single-task-list.yaml | 5 +- .../src/test/resources/tests/single-ul.yaml | 3 +- 8 files changed, 84 insertions(+), 65 deletions(-) diff --git a/markwon/src/test/java/ru/noties/markwon/renderer/visitor/TestDataReader.java b/markwon/src/test/java/ru/noties/markwon/renderer/visitor/TestDataReader.java index 427be11e..c994e592 100644 --- a/markwon/src/test/java/ru/noties/markwon/renderer/visitor/TestDataReader.java +++ b/markwon/src/test/java/ru/noties/markwon/renderer/visitor/TestDataReader.java @@ -18,16 +18,38 @@ import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import ix.Ix; import ix.IxFunction; import ix.IxPredicate; +import static ru.noties.markwon.renderer.visitor.TestSpan.BLOCK_QUOTE; +import static ru.noties.markwon.renderer.visitor.TestSpan.BULLET_LIST; +import static ru.noties.markwon.renderer.visitor.TestSpan.CODE; +import static ru.noties.markwon.renderer.visitor.TestSpan.CODE_BLOCK; +import static ru.noties.markwon.renderer.visitor.TestSpan.EMPHASIS; +import static ru.noties.markwon.renderer.visitor.TestSpan.HEADING; +import static ru.noties.markwon.renderer.visitor.TestSpan.IMAGE; +import static ru.noties.markwon.renderer.visitor.TestSpan.LINK; +import static ru.noties.markwon.renderer.visitor.TestSpan.ORDERED_LIST; +import static ru.noties.markwon.renderer.visitor.TestSpan.PARAGRAPH; +import static ru.noties.markwon.renderer.visitor.TestSpan.STRIKE_THROUGH; +import static ru.noties.markwon.renderer.visitor.TestSpan.STRONG_EMPHASIS; +import static ru.noties.markwon.renderer.visitor.TestSpan.SUB_SCRIPT; +import static ru.noties.markwon.renderer.visitor.TestSpan.SUPER_SCRIPT; +import static ru.noties.markwon.renderer.visitor.TestSpan.TABLE_ROW; +import static ru.noties.markwon.renderer.visitor.TestSpan.TASK_LIST; +import static ru.noties.markwon.renderer.visitor.TestSpan.THEMATIC_BREAK; +import static ru.noties.markwon.renderer.visitor.TestSpan.UNDERLINE; + abstract class TestDataReader { private static final String FOLDER = "tests/"; @@ -79,9 +101,40 @@ abstract class TestDataReader { static class Reader { - private static final String ATTRS = "attrs"; private static final String TEXT = "text"; + private static final Set TAGS; + + static { + TAGS = new HashSet<>(Arrays.asList( + STRONG_EMPHASIS, + EMPHASIS, + BLOCK_QUOTE, + CODE, + CODE_BLOCK, + ORDERED_LIST, + BULLET_LIST, + THEMATIC_BREAK, + HEADING, + STRIKE_THROUGH, + TASK_LIST, + TABLE_ROW, + PARAGRAPH, + IMAGE, + LINK, + SUPER_SCRIPT, + SUB_SCRIPT, + UNDERLINE, + HEADING + "1", + HEADING + "2", + HEADING + "3", + HEADING + "4", + HEADING + "5", + HEADING + "6", + TEXT + )); + } + private final String file; Reader(@NonNull String file) { @@ -160,8 +213,7 @@ abstract class TestDataReader { // it can additionally contain "attrs" key which is the attributes // b: // - text: "bold" - // attrs: - // href: "my-href" + // href: "my-href" final int size = array.size(); @@ -172,16 +224,25 @@ abstract class TestDataReader { final JsonObject object = array.get(i).getAsJsonObject(); String name = null; - Map attributes = null; + Map attributes = new HashMap<>(0); for (String key : object.keySet()) { - if (ATTRS.equals(key)) { - attributes = attributes(object.get(key)); - } else if (name == null) { - name = key; + if (TAGS.contains(key)) { + if (name == null) { + name = key; + } else { + throw new RuntimeException("Unexpected key in object: " + object); + } } else { - // we allow only 2 keys: span and/or attributes and no more - throw new RuntimeException("Unexpected key in object: " + object); + // fill attribute map with it + final String value; + final JsonElement valueElement = object.get(key); + if (valueElement.isJsonNull()) { + value = null; + } else { + value = valueElement.getAsString(); + } + attributes.put(key, value); } } @@ -189,10 +250,6 @@ abstract class TestDataReader { throw new RuntimeException("Object is missing tag name: " + object); } - if (attributes == null) { - attributes = Collections.emptyMap(); - } - final JsonElement element = object.get(name); if (TEXT.equals(name)) { @@ -263,33 +320,5 @@ abstract class TestDataReader { return new TestConfig(map); } - - @NonNull - private static Map attributes(@NonNull JsonElement element) { - - final JsonObject object = element.isJsonObject() - ? element.getAsJsonObject() - : null; - - final Map attributes; - - if (object != null) { - attributes = new HashMap<>(object.size()); - for (String key : object.keySet()) { - final String value; - final JsonElement valueElement = object.get(key); - if (valueElement.isJsonNull()) { - value = null; - } else { - value = valueElement.getAsString(); - } - attributes.put(key, value); - } - } else { - attributes = Collections.emptyMap(); - } - - return attributes; - } } } diff --git a/markwon/src/test/resources/tests/first.yaml b/markwon/src/test/resources/tests/first.yaml index 9559a9cd..a278acea 100644 --- a/markwon/src/test/resources/tests/first.yaml +++ b/markwon/src/test/resources/tests/first.yaml @@ -13,8 +13,7 @@ config: output: - text: "Here is some " - a: "link" - attrs: - href: "https://my.href" + href: "https://my.href" - text: " " - b: - text: "bold " diff --git a/markwon/src/test/resources/tests/html.yaml b/markwon/src/test/resources/tests/html.yaml index d9f966f1..a1eb063e 100644 --- a/markwon/src/test/resources/tests/html.yaml +++ b/markwon/src/test/resources/tests/html.yaml @@ -66,8 +66,7 @@ output: - h2: "link" - text: "\n" - a: "a" - attrs: - href: "a://href" + href: "a://href" - text: "\n" - h2: "unordered-list" - text: "\n" @@ -78,18 +77,15 @@ output: - h2: "ordered-list" - text: "\n" - ol: "ol1" - attrs: - start: 1 + start: 1 - text: "\n" - ol: "ol2" - attrs: - start: 2 + start: 2 - text: "\n" - h2: "image" - text: "\n" - img: "img" - attrs: - src: "img://src" + src: "img://src" - text: "\n" - h2: "blockquote" - text: "\n" diff --git a/markwon/src/test/resources/tests/single-a.yaml b/markwon/src/test/resources/tests/single-a.yaml index c5824108..82b1b134 100644 --- a/markwon/src/test/resources/tests/single-a.yaml +++ b/markwon/src/test/resources/tests/single-a.yaml @@ -2,5 +2,4 @@ input: "[link](#href)" output: - a: "link" - attrs: - href: "#href" \ No newline at end of file + href: "#href" \ No newline at end of file diff --git a/markwon/src/test/resources/tests/single-img.yaml b/markwon/src/test/resources/tests/single-img.yaml index fd5a1343..9c55a2f6 100644 --- a/markwon/src/test/resources/tests/single-img.yaml +++ b/markwon/src/test/resources/tests/single-img.yaml @@ -2,7 +2,6 @@ input: "![image](#href)" output: - img: "image" - attrs: - src: "#href" - imageSize: null - replacementTextIsLink: false \ No newline at end of file + src: "#href" + imageSize: null + replacementTextIsLink: false \ No newline at end of file diff --git a/markwon/src/test/resources/tests/single-ol.yaml b/markwon/src/test/resources/tests/single-ol.yaml index 9c7ac05b..a2046cb1 100644 --- a/markwon/src/test/resources/tests/single-ol.yaml +++ b/markwon/src/test/resources/tests/single-ol.yaml @@ -2,5 +2,4 @@ input: "1. ol" output: - ol: "ol" - attrs: - start: 1 \ No newline at end of file + start: 1 \ No newline at end of file diff --git a/markwon/src/test/resources/tests/single-task-list.yaml b/markwon/src/test/resources/tests/single-task-list.yaml index f9667289..cbb15186 100644 --- a/markwon/src/test/resources/tests/single-task-list.yaml +++ b/markwon/src/test/resources/tests/single-task-list.yaml @@ -2,6 +2,5 @@ input: "- [ ] task-list" output: - task-list: "task-list" - attrs: - blockIdent: 1 - done: false \ No newline at end of file + blockIdent: 1 + done: false \ No newline at end of file diff --git a/markwon/src/test/resources/tests/single-ul.yaml b/markwon/src/test/resources/tests/single-ul.yaml index 2fab7dc8..d5fc6647 100644 --- a/markwon/src/test/resources/tests/single-ul.yaml +++ b/markwon/src/test/resources/tests/single-ul.yaml @@ -2,5 +2,4 @@ input: "* ul" output: - ul: "ul" - attrs: - level: 0 \ No newline at end of file + level: 0 \ No newline at end of file