aboutsummaryrefslogtreecommitdiff
path: root/java/src/test
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2018-09-06 18:08:34 -0400
committerPaul Duncan <pabs@pablotron.org>2018-09-06 18:08:34 -0400
commitc4f3f955179cd217c6e7273561d06468b33e8973 (patch)
tree7a81b08c2847bdbd43f9fe4d0dbf4a1790ab3b15 /java/src/test
parent2be7535e09e359d70b924013c9f4d25149fdf0ae (diff)
downloadluigi-template-c4f3f955179cd217c6e7273561d06468b33e8973.tar.bz2
luigi-template-c4f3f955179cd217c6e7273561d06468b33e8973.zip
update errors, fix filters, add tests
Diffstat (limited to 'java/src/test')
-rw-r--r--java/src/test/java/org/pablotron/luigi/tests/CacheTest.java46
-rw-r--r--java/src/test/java/org/pablotron/luigi/tests/DefaultFiltersTest.java53
-rw-r--r--java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java88
-rw-r--r--java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java18
4 files changed, 205 insertions, 0 deletions
diff --git a/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java b/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java
new file mode 100644
index 0000000..c91b727
--- /dev/null
+++ b/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java
@@ -0,0 +1,46 @@
+import java.util.Map;
+import java.util.HashMap;
+
+import org.pablotron.luigi.Template;
+import org.pablotron.luigi.Filter;
+import org.pablotron.luigi.Cache;
+import org.pablotron.luigi.LuigiError;
+import org.pablotron.luigi.UnknownKeyError;
+import org.pablotron.luigi.UnknownFilterError;
+import org.pablotron.luigi.UnknownTemplateError;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.Test;
+
+public final class CacheTest {
+ private static Map<String, String> TEST_ARGS = new HashMap<String, String>() {{
+ put("bar", "foo");
+ }};
+
+ private static Map<String, String> TEST_TEMPLATES = new HashMap<String, String>() {{
+ put("foo", "foo%{bar}foo");
+ put("foo-custom", "foo%{bar | custom-filter}foo");
+ }};
+
+ private static Map<String, Filter.Handler> TEST_FILTERS = new HashMap<String, Filter.Handler>() {{
+ put("custom-filter", new Filter.Handler() {
+ public String filter(String val, String args[], Map<String, String> row) {
+ return String.format("-custom-%s-filter-", val);
+ }
+ });
+ }};
+
+ @Test
+ public void testCache() throws LuigiError {
+ final Cache cache = new Cache(TEST_TEMPLATES);
+
+ assertEquals("foofoofoo", cache.run("foo", TEST_ARGS));
+ }
+
+ @Test
+ public void testCacheWithCustomFilters() throws LuigiError {
+ final Cache cache = new Cache(TEST_TEMPLATES, TEST_FILTERS);
+ assertEquals("foo-custom-foo-filter-foo", cache.run("foo-custom", TEST_ARGS));
+ }
+};
diff --git a/java/src/test/java/org/pablotron/luigi/tests/DefaultFiltersTest.java b/java/src/test/java/org/pablotron/luigi/tests/DefaultFiltersTest.java
new file mode 100644
index 0000000..c839ded
--- /dev/null
+++ b/java/src/test/java/org/pablotron/luigi/tests/DefaultFiltersTest.java
@@ -0,0 +1,53 @@
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.pablotron.luigi.LuigiError;
+import org.pablotron.luigi.Template;
+import org.pablotron.luigi.Filter;
+import org.pablotron.luigi.FilterError;
+import org.pablotron.luigi.ResultHandler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import org.junit.jupiter.api.Test;
+
+public final class DefaultFiltersTest {
+ private static final class TestCase {
+ private final String name;
+ private final String arg;
+ public final String expect;
+
+ public TestCase(final String name, final String arg, final String expect) {
+ this.name = name;
+ this.arg = arg;
+ this.expect = expect;
+ }
+
+ public String run() throws LuigiError {
+ final Map<String, String> args = new HashMap<String, String>();
+ args.put("val", arg);
+
+ return Template.run(String.format("%%{val|%s}", name), args);
+ }
+ };
+
+ private static final List<TestCase> TEST_CASES = new ArrayList<TestCase>() {{
+ add(new TestCase("uc", "bar", "BAR"));
+ add(new TestCase("lc", "BAR", "bar"));
+ add(new TestCase("h", "asdf<>&\"'\u000f", "asdf&lt;&gt;&amp;&quot;&apos;&#15;"));
+ add(new TestCase("u", "asdf<>&\"' \u000f", "asdf%3C%3E%26%22%27+%0F"));
+ add(new TestCase("trim", " \r\n\tfoo", "foo"));
+ add(new TestCase("trim", " \r\n\tfoo \r\n\t", "foo"));
+ add(new TestCase("trim", "foo \r\n\t", "foo"));
+ }};
+
+ @Test
+ public void testDefaultFilters() throws LuigiError {
+ for (final TestCase t: TEST_CASES) {
+ assertEquals(t.expect, t.run());
+ }
+ }
+};
diff --git a/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java b/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java
new file mode 100644
index 0000000..9168529
--- /dev/null
+++ b/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java
@@ -0,0 +1,88 @@
+import java.util.Map;
+import java.util.HashMap;
+
+import org.pablotron.luigi.LuigiError;
+import org.pablotron.luigi.Template;
+import org.pablotron.luigi.Filter;
+import org.pablotron.luigi.FilterError;
+import org.pablotron.luigi.ResultHandler;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import org.junit.jupiter.api.Test;
+
+
+public final class FiltersTest {
+ private static final Map<String, String> TEST_ARGS = new HashMap<String, String>() {{
+ put("bar", "foo");
+ }};
+
+ private static final Map<String, String> TEST_MULTIPLE_ARGS = new HashMap<String, String>() {{
+ put("bar", "foo");
+ put("baz", "bar");
+ }};
+
+ private static final Map<String, Filter.Handler> TEST_FILTERS = new HashMap<String, Filter.Handler>() {{
+ put("barify", new Filter.Handler() {
+ public String filter(String val, String args[], Map<String, String> row) {
+ return String.format("bar-%s-bar", val);
+ }
+ });
+
+ put("wrap", new Filter.Handler() {
+ public String filter(
+ String val,
+ String args[],
+ Map<String, String> row
+ ) throws FilterError {
+ switch (args.length) {
+ case 2:
+ return String.format("(%s, %s, %s)", args[0], val, args[1]);
+ case 1:
+ return String.format("(%s in %s)", val, args[0], val);
+ case 0:
+ return val;
+ default:
+ throw new FilterError("invalid filter argument count");
+ }
+ }
+ });
+ }};
+
+ @Test
+ public void testFilter() throws LuigiError {
+ final String r = Template.run("foo%{bar | lc}", TEST_ARGS);
+
+ assertEquals("foofoo", r);
+ }
+
+ @Test
+ public void testFilterChain() throws LuigiError {
+ final String r = Template.run("foo%{bar | lc | uc}", TEST_ARGS);
+
+ assertEquals("fooFOO", r);
+ }
+
+ @Test
+ public void testCustomFilter() throws LuigiError {
+ final String r = Template.run("foo%{bar | barify}", TEST_ARGS, TEST_FILTERS);
+
+ assertEquals("foobar-foo-bar", r);
+ }
+
+ @Test
+ public void testCustomFilterWithArgs() throws LuigiError {
+ // test two arguments
+ final String plain = Template.run("%{bar | wrap}", TEST_ARGS, TEST_FILTERS);
+ assertEquals("foo", plain);
+
+ // test one argument
+ final String sandwich = Template.run("%{bar | wrap bread}", TEST_ARGS, TEST_FILTERS);
+ assertEquals("(foo in bread)", sandwich);
+
+ // test two arguments
+ final String pizza = Template.run("%{bar | wrap crust cheese}", TEST_ARGS, TEST_FILTERS);
+ assertEquals("(crust, foo, cheese)", pizza);
+ }
+};
diff --git a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java
index 94e259e..eadaab1 100644
--- a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java
+++ b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java
@@ -36,6 +36,13 @@ public final class TemplateTest {
assertEquals("foofoo", r);
}
+ @Test
+ public void testStaticRun() throws LuigiError {
+ final String r = Template.run("foo%{bar}", TEST_ARGS);
+
+ assertEquals("foofoo", r);
+ }
+
private static final class TestResultHandler implements ResultHandler {
private final StringBuilder sb;
public TestResultHandler(final StringBuilder sb) {
@@ -60,6 +67,17 @@ public final class TemplateTest {
}
@Test
+ public void testStaticResultHandler() throws LuigiError {
+ final StringBuilder sb = new StringBuilder();
+ final TestResultHandler rh = new TestResultHandler(sb);
+
+ Template.run("foo%{bar}", TEST_ARGS, rh);
+ final String r = sb.toString();
+
+ assertEquals("foofoo", sb.toString());
+ }
+
+ @Test
public void testMultipleKeys() throws LuigiError {
final Template t = new Template("foo%{bar}%{baz}");
final String r = t.run(TEST_MULTIPLE_ARGS);