From c4f3f955179cd217c6e7273561d06468b33e8973 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Thu, 6 Sep 2018 18:08:34 -0400 Subject: update errors, fix filters, add tests --- .../java/org/pablotron/luigi/tests/CacheTest.java | 46 +++++++++++ .../pablotron/luigi/tests/DefaultFiltersTest.java | 53 +++++++++++++ .../org/pablotron/luigi/tests/FiltersTest.java | 88 ++++++++++++++++++++++ .../org/pablotron/luigi/tests/TemplateTest.java | 18 +++++ 4 files changed, 205 insertions(+) create mode 100644 java/src/test/java/org/pablotron/luigi/tests/CacheTest.java create mode 100644 java/src/test/java/org/pablotron/luigi/tests/DefaultFiltersTest.java create mode 100644 java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java (limited to 'java/src/test') 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 TEST_ARGS = new HashMap() {{ + put("bar", "foo"); + }}; + + private static Map TEST_TEMPLATES = new HashMap() {{ + put("foo", "foo%{bar}foo"); + put("foo-custom", "foo%{bar | custom-filter}foo"); + }}; + + private static Map TEST_FILTERS = new HashMap() {{ + put("custom-filter", new Filter.Handler() { + public String filter(String val, String args[], Map 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 args = new HashMap(); + args.put("val", arg); + + return Template.run(String.format("%%{val|%s}", name), args); + } + }; + + private static final List TEST_CASES = new ArrayList() {{ + add(new TestCase("uc", "bar", "BAR")); + add(new TestCase("lc", "BAR", "bar")); + add(new TestCase("h", "asdf<>&\"'\u000f", "asdf<>&"'")); + 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 TEST_ARGS = new HashMap() {{ + put("bar", "foo"); + }}; + + private static final Map TEST_MULTIPLE_ARGS = new HashMap() {{ + put("bar", "foo"); + put("baz", "bar"); + }}; + + private static final Map TEST_FILTERS = new HashMap() {{ + put("barify", new Filter.Handler() { + public String filter(String val, String args[], Map row) { + return String.format("bar-%s-bar", val); + } + }); + + put("wrap", new Filter.Handler() { + public String filter( + String val, + String args[], + Map 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) { @@ -59,6 +66,17 @@ public final class TemplateTest { assertEquals("foofoo", sb.toString()); } + @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}"); -- cgit v1.2.3