From 373464d30d87bd8702fe27b920ed1406a0833ef3 Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Fri, 27 Mar 2020 16:39:52 -0400 Subject: Refactor test structure Tests are now in a 'test' sub-package, so it is clear that they are indeed test code, not just disjoint parts of the main code --- .../java/bjc/utils/cli/objects/CommandTest.java | 52 ------- .../java/bjc/utils/data/BooleanToggleTest.java | 36 ----- .../java/bjc/utils/data/CircularIteratorTest.java | 49 ------- .../java/bjc/utils/data/QueuedIteratorTest.java | 71 ---------- .../bjc/utils/funcutils/IteratorUtilsTest.java | 23 ---- .../java/bjc/utils/funcutils/StringUtilsTest.java | 39 ------ .../java/bjc/utils/ioutils/LevelSplitterTest.java | 46 ------- .../java/bjc/utils/ioutils/ReportWriterTest.java | 28 ---- .../java/bjc/utils/parserutils/TokenUtilsTest.java | 153 --------------------- .../bjc/utils/test/cli/objects/CommandTest.java | 54 ++++++++ .../bjc/utils/test/data/BooleanToggleTest.java | 38 +++++ .../bjc/utils/test/data/CircularIteratorTest.java | 51 +++++++ .../bjc/utils/test/data/QueuedIteratorTest.java | 73 ++++++++++ .../utils/test/funcutils/IteratorUtilsTest.java | 23 ++++ .../bjc/utils/test/funcutils/StringUtilsTest.java | 39 ++++++ .../bjc/utils/test/ioutils/LevelSplitterTest.java | 48 +++++++ .../bjc/utils/test/ioutils/ReportWriterTest.java | 30 ++++ .../utils/test/ioutils/SimplePropertiesTest.java | 60 ++++++++ .../bjc/utils/test/parserutils/TokenUtilsTest.java | 153 +++++++++++++++++++++ 19 files changed, 569 insertions(+), 497 deletions(-) delete mode 100644 base/src/test/java/bjc/utils/cli/objects/CommandTest.java delete mode 100644 base/src/test/java/bjc/utils/data/BooleanToggleTest.java delete mode 100644 base/src/test/java/bjc/utils/data/CircularIteratorTest.java delete mode 100644 base/src/test/java/bjc/utils/data/QueuedIteratorTest.java delete mode 100644 base/src/test/java/bjc/utils/funcutils/IteratorUtilsTest.java delete mode 100644 base/src/test/java/bjc/utils/funcutils/StringUtilsTest.java delete mode 100644 base/src/test/java/bjc/utils/ioutils/LevelSplitterTest.java delete mode 100644 base/src/test/java/bjc/utils/ioutils/ReportWriterTest.java delete mode 100644 base/src/test/java/bjc/utils/parserutils/TokenUtilsTest.java create mode 100644 base/src/test/java/bjc/utils/test/cli/objects/CommandTest.java create mode 100644 base/src/test/java/bjc/utils/test/data/BooleanToggleTest.java create mode 100644 base/src/test/java/bjc/utils/test/data/CircularIteratorTest.java create mode 100644 base/src/test/java/bjc/utils/test/data/QueuedIteratorTest.java create mode 100644 base/src/test/java/bjc/utils/test/funcutils/IteratorUtilsTest.java create mode 100644 base/src/test/java/bjc/utils/test/funcutils/StringUtilsTest.java create mode 100644 base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java create mode 100644 base/src/test/java/bjc/utils/test/ioutils/ReportWriterTest.java create mode 100644 base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java create mode 100644 base/src/test/java/bjc/utils/test/parserutils/TokenUtilsTest.java (limited to 'base/src/test/java/bjc') diff --git a/base/src/test/java/bjc/utils/cli/objects/CommandTest.java b/base/src/test/java/bjc/utils/cli/objects/CommandTest.java deleted file mode 100644 index 2bebaa9..0000000 --- a/base/src/test/java/bjc/utils/cli/objects/CommandTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package bjc.utils.cli.objects; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -/** - * Test that CLI command objects work correctly. - * - * @author bjculkin - * - */ -public class CommandTest { - - /** - * Test basic things work right. - */ - @Test - public void testBasic() { - Command com = Command.fromString("a b c", 1, "console"); - - assertEquals("a b c", com.full); - assertEquals("a", com.name); - assertEquals("b c", com.remn); - - assertEquals("b", com.trimTo(' ')); - assertEquals("c", com.remn); - - com = Command.fromString("a b c", 1, "console"); - - assertEquals("a b c", com.full); - assertEquals("a", com.name); - assertEquals("b c", com.remn); - - assertEquals("b", com.trimTo(" ")); - assertEquals("c", com.remn); - } - - /** - * Test regex trimming works right. - */ - @Test - public void testRX() { - Command com = Command.fromString("a try1ZZZtry2ZZtry3", 1, "console"); - - assertEquals("try1", com.trimToRX("Z+")); - assertEquals("try2ZZtry3", com.remn); - - assertEquals("try2", com.trimToRX("Z+")); - assertEquals("try3", com.remn); - } -} diff --git a/base/src/test/java/bjc/utils/data/BooleanToggleTest.java b/base/src/test/java/bjc/utils/data/BooleanToggleTest.java deleted file mode 100644 index 66f44c0..0000000 --- a/base/src/test/java/bjc/utils/data/BooleanToggleTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package bjc.utils.data; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; - -/** - * Test for boolean toggles. - * @author bjculkin - * - */ -public class BooleanToggleTest { - - /** - * Test that boolean toggles work right. - */ - @Test - public void test() { - BooleanToggle tog = new BooleanToggle(); - - // Check initial value is false. - assertEquals(false, tog.peek()); - // Check that 'get' returns the old value - assertEquals(false, tog.get()); - // Check that 'get' swaps the value - assertEquals(true, tog.peek()); - // Check that we can round-trip back. - assertEquals(true, tog.get()); - assertEquals(false, tog.peek()); - - tog.set(true); - - // Check set works - assertEquals(true, tog.peek()); - } -} diff --git a/base/src/test/java/bjc/utils/data/CircularIteratorTest.java b/base/src/test/java/bjc/utils/data/CircularIteratorTest.java deleted file mode 100644 index 5e20a17..0000000 --- a/base/src/test/java/bjc/utils/data/CircularIteratorTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package bjc.utils.data; - -import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; - -import java.util.Arrays; -import java.util.List; - -import org.junit.Test; - -/** - * Test for circular iterators., - * - * @author bjculkin - * - */ -public class CircularIteratorTest { - - /** - * Test regular repetition of the entire iterator. - */ - @Test - public void testRegular() { - List lst = Arrays.asList("a", "b", "c"); - - CircularIterator itr = new CircularIterator<>(lst); - - // Check we get initial values correctly, and have more remaining - assertIteratorEquals(true, itr, "a", "b", "c"); - - // Check we repeat correctly, and can still repeat - assertIteratorEquals(true, itr, "a", "b", "c"); - } - - /** - * Test that the last element repeats correctly. - */ - @Test - public void testRepLast() { - List lst = Arrays.asList("a", "b", "c"); - - CircularIterator itr = new CircularIterator<>(lst, false); - - // Check we get initial values correctly, and have more remaining - assertIteratorEquals(true, itr, "a", "b", "c"); - - // Check we repeat correctly, and can still repeat - assertIteratorEquals(true, itr, "c", "c", "c"); - } -} diff --git a/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java b/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java deleted file mode 100644 index bbbca56..0000000 --- a/base/src/test/java/bjc/utils/data/QueuedIteratorTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package bjc.utils.data; - -import static bjc.utils.data.QueuedIterator.queued; -import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; -import static java.util.Arrays.asList; - -import org.junit.Test; - -/** - * Test of QueuedIterator. - * - * @author bjculkin - * - */ -public class QueuedIteratorTest { - - /** - * Test of functionality. - */ - @Test - public void test() { - assertIteratorEquals(false, queued()); - - assertIteratorEquals(false, queued(1, 2, 3), 1, 2, 3); - assertIteratorEquals(false, queued(asList(1, 2, 3), asList(3, 2, 1)), 1, 2, 3, 3, 2, 1); - - } - - /** - * Test of before() method. - */ - @Test - public void testBefore() { - QueuedIterator itr = queued(1, 2, 3); - - assertIteratorEquals(true, itr, 1, 2); - - itr.before(1, 2, 3); - - assertIteratorEquals(false, itr, 1, 2, 3, 3); - } - - /** - * Test of after() method. - */ - @Test - public void testAfter() { - QueuedIterator itr = queued(1, 2, 3); - - assertIteratorEquals(true, itr, 1, 2); - - itr.after(1, 2, 3); - - assertIteratorEquals(false, itr, 3, 1, 2, 3); - } - - /** - * Test of last() method. - */ - @Test - public void testLast() { - QueuedIterator itr = queued(1, 2, 3); - - assertIteratorEquals(true, itr, 1, 2); - - itr.after(4); - itr.last(1, 2, 3); - - assertIteratorEquals(false, itr, 3, 4, 1, 2, 3); - } -} diff --git a/base/src/test/java/bjc/utils/funcutils/IteratorUtilsTest.java b/base/src/test/java/bjc/utils/funcutils/IteratorUtilsTest.java deleted file mode 100644 index bf88038..0000000 --- a/base/src/test/java/bjc/utils/funcutils/IteratorUtilsTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package bjc.utils.funcutils; - -import org.junit.Test; - -import static bjc.utils.funcutils.IteratorUtils.*; -import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; - -import static java.util.Arrays.asList; - -/** - * Test IteratorUtils functionality. - * - * @author Ben Culkin - */ -public class IteratorUtilsTest { - /** - * Test the chain() method works correctly. - */ - @Test - public void testChain() { - assertIteratorEquals(chain(I(asList("a b", "b c")), (arg) -> I(asList(arg.split(" ")))), "a", "b", "b", "c"); - } -} diff --git a/base/src/test/java/bjc/utils/funcutils/StringUtilsTest.java b/base/src/test/java/bjc/utils/funcutils/StringUtilsTest.java deleted file mode 100644 index 434912c..0000000 --- a/base/src/test/java/bjc/utils/funcutils/StringUtilsTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package bjc.utils.funcutils; - -import java.io.StringReader; - -import java.util.Scanner; - -import org.junit.Test; - -import static bjc.utils.funcutils.StringUtils.readLines; -import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; - -/** - * Tests of stuff in StringUtils. - * - * @author Ben Culkin - */ -public class StringUtilsTest { - @SuppressWarnings("javadoc") - @Test - public void testReadLines() { - assertReadLines("", ""); - - assertReadLines("hallo there", "hallo there"); - - assertReadLines("hallo there\na second line", "hallo there", "a second line"); - - assertReadLines("hallo there \\\na continued line", "hallo there a continued line"); - - assertReadLines("hallo there\\\\\na second line", "hallo there\\", "a second line"); - - assertReadLines("a\n\nb", "a", "", "b"); - } - - private static void assertReadLines(String inp, String... outp) { - StringReader sr = new StringReader(inp); - Scanner scn = new Scanner(sr); - assertIteratorEquals(readLines(scn), outp); - } -} diff --git a/base/src/test/java/bjc/utils/ioutils/LevelSplitterTest.java b/base/src/test/java/bjc/utils/ioutils/LevelSplitterTest.java deleted file mode 100644 index bdc5d8d..0000000 --- a/base/src/test/java/bjc/utils/ioutils/LevelSplitterTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package bjc.utils.ioutils; - -import static bjc.utils.funcutils.TestUtils.assertListEquals; -import static bjc.utils.ioutils.LevelSplitterTest.RXPair.pair; - -import org.junit.Test; - -/** - * Test of LevelSplitter. - * - * @author bjculkin - * - */ -public class LevelSplitterTest { - static final class RXPair { - public String inp; - public String[] outp; - - public RXPair(String inp, String... outp) { - this.inp = inp; - this.outp = outp; - } - - public static RXPair pair(String inp, String... outp) { - return new RXPair(inp, outp); - } - } - - /** - * Test regex splitter. - */ - @Test - public void testRXSplit() { - //LevelSplitter splitter = LevelSplitter.def; - - // Check generic splitting works - assertRXSplit("\\s+", pair("", ""), pair("a", "a"), pair("a b", "a", "b"), pair("a b", "a", "b"), - pair("a\t \tb", "a", "b")); - } - - private static void assertRXSplit(String pat, RXPair... pairs) { - for (RXPair pair : pairs) { - assertListEquals(LevelSplitter.def.levelSplitRX(pair.inp, pat), pair.outp); - } - } -} diff --git a/base/src/test/java/bjc/utils/ioutils/ReportWriterTest.java b/base/src/test/java/bjc/utils/ioutils/ReportWriterTest.java deleted file mode 100644 index a9f7072..0000000 --- a/base/src/test/java/bjc/utils/ioutils/ReportWriterTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package bjc.utils.ioutils; - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; -import java.io.StringWriter; - -import org.junit.Test; - -/** - * Tests for ReportWriter. - * - * @author EVE - * - */ -@SuppressWarnings("javadoc") -public class ReportWriterTest { - @Test - public void testWriteString() { - try (ReportWriter rw = new ReportWriter(new StringWriter())) { - rw.write("foo"); - - assertEquals("foo", rw.toString()); - } catch (IOException ioex) { - - } - } -} diff --git a/base/src/test/java/bjc/utils/parserutils/TokenUtilsTest.java b/base/src/test/java/bjc/utils/parserutils/TokenUtilsTest.java deleted file mode 100644 index 08606b3..0000000 --- a/base/src/test/java/bjc/utils/parserutils/TokenUtilsTest.java +++ /dev/null @@ -1,153 +0,0 @@ -package bjc.utils.parserutils; - -import static bjc.utils.parserutils.TokenUtils.descapeString; -import static bjc.utils.parserutils.TokenUtils.removeDQuotedStrings; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.hasItems; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import java.util.List; - -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -/* - * Tests for TokenUtils - */ -@SuppressWarnings("javadoc") -public class TokenUtilsTest { - @Rule - public ExpectedException exp = ExpectedException.none(); - - /* - * Test removeDQuoted - */ - - /* - * Check handling of mismatched strings with no matching strings. - */ - @Test - public void testRemoveDQuoted_MismatchedStringNoMatch() throws IllegalArgumentException { - exp.expect(IllegalArgumentException.class); - exp.expectMessage(containsString("Opening quote was at position 0")); - - removeDQuotedStrings("\"hello"); - } - - /* - * Check handling of mismatched strings with a matching string. - */ - @Test - public void testRemoveDQuoted_MismatchedStringMatch() throws IllegalArgumentException { - exp.expect(IllegalArgumentException.class); - exp.expectMessage(containsString("Opening quote was at position 7")); - - removeDQuotedStrings("\"hello\"\""); - } - - /* - * Check handling of strings with a single embedded string. - */ - @Test - public void testRemoveDQuoted_SingleString() { - final List onSingleMatchString = removeDQuotedStrings("hello\"there\""); - - assertThat(onSingleMatchString, hasItems("hello", "\"there\"")); - } - - /* - * Check handling of strings with multiple quoted strings in a row. - */ - @Test - public void testRemoveDQuoted_MultipleSerialString() { - final List onMultipleSerialMatchString = removeDQuotedStrings("\"hello\"\"there\""); - - assertThat(onMultipleSerialMatchString, hasItems("\"hello\"", "\"there\"")); - } - - /* - * Check handling of strings with multiple interleaved strings. - */ - @Test - public void testRemoveDQuoted_MultipleInterleavedString() { - final List onMultipleInterleaveMatchString = removeDQuotedStrings("one\"two\"three\"four\""); - - assertThat(onMultipleInterleaveMatchString, hasItems("one", "\"two\"", "three", "\"four\"")); - } - - /* - * Check handling of strings without embedded strings. - */ - @Test - public void testRemoveDQuote_NoString() { - final List onNonmatchingString = removeDQuotedStrings("hello"); - - assertThat(onNonmatchingString, hasItems("hello")); - } - - /* - * Check handling of empty strings. - */ - @Test - public void testRemoveDQuote_EmptyString() { - final List onEmptyString = removeDQuotedStrings(""); - - assertThat(onEmptyString, hasItems("")); - } - - /* - * Test descapeString - */ - /* - * Check handling of empty strings. - */ - @Test - public void testDescapeString_EmptyString() { - final String onEmptyString = descapeString(""); - - assertThat(onEmptyString, is("")); - } - - /* - * Check handling of strings without escapes - */ - @Test - public void testDescapeString_NonescapeString() { - final String onNonescapeString = descapeString("hello there"); - - assertThat(onNonescapeString, is("hello there")); - } - - /* - * Check handling of strings with single escapes. - */ - @Test - public void testDescapeString_SingleEscapeString() { - final String onSingleEscapeString = descapeString("hello\\tthere"); - - assertThat(onSingleEscapeString, is("hello\tthere")); - } - - /* - * Check handling of strings with multiple escapes. - */ - @Test - public void testDescapeString_MultipleEscapeString() { - final String onMultipleEscapeString = descapeString("hello\\tthere\\tworld"); - - assertThat(onMultipleEscapeString, is("hello\tthere\tworld")); - } - - /* - * Check handling of strings with invalid single escapes. - */ - @Test - public void testDescapeString_InvalidSingleEscapeString() throws IllegalArgumentException { - exp.expect(IllegalArgumentException.class); - exp.expectMessage(containsString("at position 0")); - - descapeString("\\x"); - } -} \ No newline at end of file diff --git a/base/src/test/java/bjc/utils/test/cli/objects/CommandTest.java b/base/src/test/java/bjc/utils/test/cli/objects/CommandTest.java new file mode 100644 index 0000000..1e253be --- /dev/null +++ b/base/src/test/java/bjc/utils/test/cli/objects/CommandTest.java @@ -0,0 +1,54 @@ +package bjc.utils.test.cli.objects; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import bjc.utils.cli.objects.Command; + +/** + * Test that CLI command objects work correctly. + * + * @author bjculkin + * + */ +public class CommandTest { + + /** + * Test basic things work right. + */ + @Test + public void testBasic() { + Command com = Command.fromString("a b c", 1, "console"); + + assertEquals("a b c", com.full); + assertEquals("a", com.name); + assertEquals("b c", com.remn); + + assertEquals("b", com.trimTo(' ')); + assertEquals("c", com.remn); + + com = Command.fromString("a b c", 1, "console"); + + assertEquals("a b c", com.full); + assertEquals("a", com.name); + assertEquals("b c", com.remn); + + assertEquals("b", com.trimTo(" ")); + assertEquals("c", com.remn); + } + + /** + * Test regex trimming works right. + */ + @Test + public void testRX() { + Command com = Command.fromString("a try1ZZZtry2ZZtry3", 1, "console"); + + assertEquals("try1", com.trimToRX("Z+")); + assertEquals("try2ZZtry3", com.remn); + + assertEquals("try2", com.trimToRX("Z+")); + assertEquals("try3", com.remn); + } +} diff --git a/base/src/test/java/bjc/utils/test/data/BooleanToggleTest.java b/base/src/test/java/bjc/utils/test/data/BooleanToggleTest.java new file mode 100644 index 0000000..1373389 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/data/BooleanToggleTest.java @@ -0,0 +1,38 @@ +package bjc.utils.test.data; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import bjc.utils.data.BooleanToggle; + +/** + * Test for boolean toggles. + * @author bjculkin + * + */ +public class BooleanToggleTest { + + /** + * Test that boolean toggles work right. + */ + @Test + public void test() { + BooleanToggle tog = new BooleanToggle(); + + // Check initial value is false. + assertEquals(false, tog.peek()); + // Check that 'get' returns the old value + assertEquals(false, tog.get()); + // Check that 'get' swaps the value + assertEquals(true, tog.peek()); + // Check that we can round-trip back. + assertEquals(true, tog.get()); + assertEquals(false, tog.peek()); + + tog.set(true); + + // Check set works + assertEquals(true, tog.peek()); + } +} diff --git a/base/src/test/java/bjc/utils/test/data/CircularIteratorTest.java b/base/src/test/java/bjc/utils/test/data/CircularIteratorTest.java new file mode 100644 index 0000000..7aae211 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/data/CircularIteratorTest.java @@ -0,0 +1,51 @@ +package bjc.utils.test.data; + +import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; + +import bjc.utils.data.CircularIterator; + +/** + * Test for circular iterators., + * + * @author bjculkin + * + */ +public class CircularIteratorTest { + + /** + * Test regular repetition of the entire iterator. + */ + @Test + public void testRegular() { + List lst = Arrays.asList("a", "b", "c"); + + CircularIterator itr = new CircularIterator<>(lst); + + // Check we get initial values correctly, and have more remaining + assertIteratorEquals(true, itr, "a", "b", "c"); + + // Check we repeat correctly, and can still repeat + assertIteratorEquals(true, itr, "a", "b", "c"); + } + + /** + * Test that the last element repeats correctly. + */ + @Test + public void testRepLast() { + List lst = Arrays.asList("a", "b", "c"); + + CircularIterator itr = new CircularIterator<>(lst, false); + + // Check we get initial values correctly, and have more remaining + assertIteratorEquals(true, itr, "a", "b", "c"); + + // Check we repeat correctly, and can still repeat + assertIteratorEquals(true, itr, "c", "c", "c"); + } +} diff --git a/base/src/test/java/bjc/utils/test/data/QueuedIteratorTest.java b/base/src/test/java/bjc/utils/test/data/QueuedIteratorTest.java new file mode 100644 index 0000000..b34723e --- /dev/null +++ b/base/src/test/java/bjc/utils/test/data/QueuedIteratorTest.java @@ -0,0 +1,73 @@ +package bjc.utils.test.data; + +import static bjc.utils.data.QueuedIterator.queued; +import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; +import static java.util.Arrays.asList; + +import org.junit.Test; + +import bjc.utils.data.QueuedIterator; + +/** + * Test of QueuedIterator. + * + * @author bjculkin + * + */ +public class QueuedIteratorTest { + + /** + * Test of functionality. + */ + @Test + public void test() { + assertIteratorEquals(false, queued()); + + assertIteratorEquals(false, queued(1, 2, 3), 1, 2, 3); + assertIteratorEquals(false, queued(asList(1, 2, 3), asList(3, 2, 1)), 1, 2, 3, 3, 2, 1); + + } + + /** + * Test of before() method. + */ + @Test + public void testBefore() { + QueuedIterator itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.before(1, 2, 3); + + assertIteratorEquals(false, itr, 1, 2, 3, 3); + } + + /** + * Test of after() method. + */ + @Test + public void testAfter() { + QueuedIterator itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.after(1, 2, 3); + + assertIteratorEquals(false, itr, 3, 1, 2, 3); + } + + /** + * Test of last() method. + */ + @Test + public void testLast() { + QueuedIterator itr = queued(1, 2, 3); + + assertIteratorEquals(true, itr, 1, 2); + + itr.after(4); + itr.last(1, 2, 3); + + assertIteratorEquals(false, itr, 3, 4, 1, 2, 3); + } +} diff --git a/base/src/test/java/bjc/utils/test/funcutils/IteratorUtilsTest.java b/base/src/test/java/bjc/utils/test/funcutils/IteratorUtilsTest.java new file mode 100644 index 0000000..965582b --- /dev/null +++ b/base/src/test/java/bjc/utils/test/funcutils/IteratorUtilsTest.java @@ -0,0 +1,23 @@ +package bjc.utils.test.funcutils; + +import org.junit.Test; + +import static bjc.utils.funcutils.IteratorUtils.*; +import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; + +import static java.util.Arrays.asList; + +/** + * Test IteratorUtils functionality. + * + * @author Ben Culkin + */ +public class IteratorUtilsTest { + /** + * Test the chain() method works correctly. + */ + @Test + public void testChain() { + assertIteratorEquals(chain(I(asList("a b", "b c")), (arg) -> I(asList(arg.split(" ")))), "a", "b", "b", "c"); + } +} diff --git a/base/src/test/java/bjc/utils/test/funcutils/StringUtilsTest.java b/base/src/test/java/bjc/utils/test/funcutils/StringUtilsTest.java new file mode 100644 index 0000000..b53d3e9 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/funcutils/StringUtilsTest.java @@ -0,0 +1,39 @@ +package bjc.utils.test.funcutils; + +import java.io.StringReader; + +import java.util.Scanner; + +import org.junit.Test; + +import static bjc.utils.funcutils.StringUtils.readLines; +import static bjc.utils.funcutils.TestUtils.assertIteratorEquals; + +/** + * Tests of stuff in StringUtils. + * + * @author Ben Culkin + */ +public class StringUtilsTest { + @SuppressWarnings("javadoc") + @Test + public void testReadLines() { + assertReadLines("", ""); + + assertReadLines("hallo there", "hallo there"); + + assertReadLines("hallo there\na second line", "hallo there", "a second line"); + + assertReadLines("hallo there \\\na continued line", "hallo there a continued line"); + + assertReadLines("hallo there\\\\\na second line", "hallo there\\", "a second line"); + + assertReadLines("a\n\nb", "a", "", "b"); + } + + private static void assertReadLines(String inp, String... outp) { + StringReader sr = new StringReader(inp); + Scanner scn = new Scanner(sr); + assertIteratorEquals(readLines(scn), outp); + } +} diff --git a/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java b/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java new file mode 100644 index 0000000..aadea72 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/ioutils/LevelSplitterTest.java @@ -0,0 +1,48 @@ +package bjc.utils.test.ioutils; + +import static bjc.utils.funcutils.TestUtils.assertListEquals; +import static bjc.utils.test.ioutils.LevelSplitterTest.RXPair.pair; + +import org.junit.Test; + +import bjc.utils.ioutils.LevelSplitter; + +/** + * Test of LevelSplitter. + * + * @author bjculkin + * + */ +public class LevelSplitterTest { + static final class RXPair { + public String inp; + public String[] outp; + + public RXPair(String inp, String... outp) { + this.inp = inp; + this.outp = outp; + } + + public static RXPair pair(String inp, String... outp) { + return new RXPair(inp, outp); + } + } + + /** + * Test regex splitter. + */ + @Test + public void testRXSplit() { + //LevelSplitter splitter = LevelSplitter.def; + + // Check generic splitting works + assertRXSplit("\\s+", pair("", ""), pair("a", "a"), pair("a b", "a", "b"), pair("a b", "a", "b"), + pair("a\t \tb", "a", "b")); + } + + private static void assertRXSplit(String pat, RXPair... pairs) { + for (RXPair pair : pairs) { + assertListEquals(LevelSplitter.def.levelSplitRX(pair.inp, pat), pair.outp); + } + } +} diff --git a/base/src/test/java/bjc/utils/test/ioutils/ReportWriterTest.java b/base/src/test/java/bjc/utils/test/ioutils/ReportWriterTest.java new file mode 100644 index 0000000..4918b95 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/ioutils/ReportWriterTest.java @@ -0,0 +1,30 @@ +package bjc.utils.test.ioutils; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; + +import org.junit.Test; + +import bjc.utils.ioutils.ReportWriter; + +/** + * Tests for ReportWriter. + * + * @author EVE + * + */ +@SuppressWarnings("javadoc") +public class ReportWriterTest { + @Test + public void testWriteString() { + try (ReportWriter rw = new ReportWriter(new StringWriter())) { + rw.write("foo"); + + assertEquals("foo", rw.toString()); + } catch (IOException ioex) { + + } + } +} diff --git a/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java b/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java new file mode 100644 index 0000000..81f7ac8 --- /dev/null +++ b/base/src/test/java/bjc/utils/test/ioutils/SimplePropertiesTest.java @@ -0,0 +1,60 @@ +package bjc.utils.test.ioutils; + +import static org.junit.Assert.*; + +import java.io.StringReader; + +import org.junit.Test; + +import bjc.utils.ioutils.SimpleProperties; +import bjc.utils.ioutils.SimpleProperties.DuplicateKeys; +import bjc.utils.ioutils.SimpleProperties.InvalidLineFormat; + +/** + * Tests for SimpleProperties. + * + * @author Ben Culkin + * + */ +public class SimplePropertiesTest { + + @Test + public void testSimpleProperties() { + SimpleProperties props = new SimpleProperties(); + + assertEquals(0, props.size()); + assertTrue(props.isEmpty()); + } + + @Test + public void testLoadFrom() { + SimpleProperties props = new SimpleProperties(); + + StringReader rdr = new StringReader("a a\nb b\nc c1\nc c2\n#c c3"); + + props.loadFrom(rdr, true); + + assertEquals(3, props.size()); + assertEquals("a", props.get("a")); + assertEquals("b", props.get("b")); + assertEquals("c2", props.get("c")); + } + + @Test(expected = DuplicateKeys.class) + public void testDuplicateKeys() { + SimpleProperties props = new SimpleProperties(); + + StringReader rdr = new StringReader("a a\nb b\nb b"); + + props.loadFrom(rdr, false); + } + + @Test(expected = InvalidLineFormat.class) + public void testInvalidFormat() { + SimpleProperties props = new SimpleProperties(); + + StringReader rdr = new StringReader("a"); + + props.loadFrom(rdr, false); + } +} \ No newline at end of file diff --git a/base/src/test/java/bjc/utils/test/parserutils/TokenUtilsTest.java b/base/src/test/java/bjc/utils/test/parserutils/TokenUtilsTest.java new file mode 100644 index 0000000..99593ed --- /dev/null +++ b/base/src/test/java/bjc/utils/test/parserutils/TokenUtilsTest.java @@ -0,0 +1,153 @@ +package bjc.utils.test.parserutils; + +import static bjc.utils.parserutils.TokenUtils.descapeString; +import static bjc.utils.parserutils.TokenUtils.removeDQuotedStrings; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/* + * Tests for TokenUtils + */ +@SuppressWarnings("javadoc") +public class TokenUtilsTest { + @Rule + public ExpectedException exp = ExpectedException.none(); + + /* + * Test removeDQuoted + */ + + /* + * Check handling of mismatched strings with no matching strings. + */ + @Test + public void testRemoveDQuoted_MismatchedStringNoMatch() throws IllegalArgumentException { + exp.expect(IllegalArgumentException.class); + exp.expectMessage(containsString("Opening quote was at position 0")); + + removeDQuotedStrings("\"hello"); + } + + /* + * Check handling of mismatched strings with a matching string. + */ + @Test + public void testRemoveDQuoted_MismatchedStringMatch() throws IllegalArgumentException { + exp.expect(IllegalArgumentException.class); + exp.expectMessage(containsString("Opening quote was at position 7")); + + removeDQuotedStrings("\"hello\"\""); + } + + /* + * Check handling of strings with a single embedded string. + */ + @Test + public void testRemoveDQuoted_SingleString() { + final List onSingleMatchString = removeDQuotedStrings("hello\"there\""); + + assertThat(onSingleMatchString, hasItems("hello", "\"there\"")); + } + + /* + * Check handling of strings with multiple quoted strings in a row. + */ + @Test + public void testRemoveDQuoted_MultipleSerialString() { + final List onMultipleSerialMatchString = removeDQuotedStrings("\"hello\"\"there\""); + + assertThat(onMultipleSerialMatchString, hasItems("\"hello\"", "\"there\"")); + } + + /* + * Check handling of strings with multiple interleaved strings. + */ + @Test + public void testRemoveDQuoted_MultipleInterleavedString() { + final List onMultipleInterleaveMatchString = removeDQuotedStrings("one\"two\"three\"four\""); + + assertThat(onMultipleInterleaveMatchString, hasItems("one", "\"two\"", "three", "\"four\"")); + } + + /* + * Check handling of strings without embedded strings. + */ + @Test + public void testRemoveDQuote_NoString() { + final List onNonmatchingString = removeDQuotedStrings("hello"); + + assertThat(onNonmatchingString, hasItems("hello")); + } + + /* + * Check handling of empty strings. + */ + @Test + public void testRemoveDQuote_EmptyString() { + final List onEmptyString = removeDQuotedStrings(""); + + assertThat(onEmptyString, hasItems("")); + } + + /* + * Test descapeString + */ + /* + * Check handling of empty strings. + */ + @Test + public void testDescapeString_EmptyString() { + final String onEmptyString = descapeString(""); + + assertThat(onEmptyString, is("")); + } + + /* + * Check handling of strings without escapes + */ + @Test + public void testDescapeString_NonescapeString() { + final String onNonescapeString = descapeString("hello there"); + + assertThat(onNonescapeString, is("hello there")); + } + + /* + * Check handling of strings with single escapes. + */ + @Test + public void testDescapeString_SingleEscapeString() { + final String onSingleEscapeString = descapeString("hello\\tthere"); + + assertThat(onSingleEscapeString, is("hello\tthere")); + } + + /* + * Check handling of strings with multiple escapes. + */ + @Test + public void testDescapeString_MultipleEscapeString() { + final String onMultipleEscapeString = descapeString("hello\\tthere\\tworld"); + + assertThat(onMultipleEscapeString, is("hello\tthere\tworld")); + } + + /* + * Check handling of strings with invalid single escapes. + */ + @Test + public void testDescapeString_InvalidSingleEscapeString() throws IllegalArgumentException { + exp.expect(IllegalArgumentException.class); + exp.expectMessage(containsString("at position 0")); + + descapeString("\\x"); + } +} \ No newline at end of file -- cgit v1.2.3