From 1c31efa57732d700d7ed2734fb97a055300476a9 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 30 Jul 2017 14:12:17 -0700 Subject: [PATCH] Write more tests for the Lexer. --- .../org/nwapw/abacus/tests/LexerTests.java | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/test/java/org/nwapw/abacus/tests/LexerTests.java b/src/test/java/org/nwapw/abacus/tests/LexerTests.java index c6c0a75..268a06c 100644 --- a/src/test/java/org/nwapw/abacus/tests/LexerTests.java +++ b/src/test/java/org/nwapw/abacus/tests/LexerTests.java @@ -15,6 +15,7 @@ public class LexerTests { lexer.register("abc", 0); lexer.register("def", 1); List> matchedIntegers = lexer.lexAll("abcdefabc", 0, Integer::compare); + Assert.assertNotNull(matchedIntegers); Assert.assertEquals(matchedIntegers.get(0).getType(), Integer.valueOf(0)); Assert.assertEquals(matchedIntegers.get(1).getType(), Integer.valueOf(1)); Assert.assertEquals(matchedIntegers.get(2).getType(), Integer.valueOf(0)); @@ -40,5 +41,93 @@ public class LexerTests { lexer.register("a?", 0); Assert.assertNull(lexer.lexAll("", 0, Integer::compare)); } + + @Test + public void testOneOrMore(){ + Lexer lexer = new Lexer<>(); + lexer.register("a+", 0); + List> tokens = lexer.lexAll("aaaa", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 1); + } + + @Test + public void testZeroOrMore(){ + Lexer lexer = new Lexer<>(); + lexer.register("a*", 0); + List> tokens = lexer.lexAll("aaaa", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 1); + } + + @Test + public void testZeroOrOne(){ + Lexer lexer = new Lexer<>(); + lexer.register("a?", 0); + List> tokens = lexer.lexAll("aaaa", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 4); + } + + @Test + public void testGreedyMatching(){ + Lexer lexer = new Lexer<>(); + lexer.register("a*a", 0); + List> tokens = lexer.lexAll("aaaa", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 1); + } + + @Test + public void testAnyCharacter(){ + String testString = "abcdef"; + Lexer lexer = new Lexer<>(); + lexer.register(".", 0); + List> tokens = lexer.lexAll(testString, 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), testString.length()); + for(int i = 0; i < tokens.size(); i++){ + Assert.assertEquals(testString.substring(i, i + 1), tokens.get(i).getContent()); + } + } + + @Test + public void testBasicGroup(){ + Lexer lexer = new Lexer<>(); + lexer.register("(abc)", 0); + List> tokens = lexer.lexAll("abc", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 1); + Assert.assertEquals(tokens.get(0).getContent(), "abc"); + } + + @Test + public void testBasicRangeSuccess(){ + String testString = "abcdef"; + Lexer lexer = new Lexer<>(); + lexer.register("[a-f]", 0); + List> tokens = lexer.lexAll(testString, 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(testString.length(), tokens.size()); + for(int i = 0; i < tokens.size(); i++){ + Assert.assertEquals(testString.substring(i, i + 1), tokens.get(i).getContent()); + } + } + + @Test + public void testBasicRangeFailure(){ + Lexer lexer = new Lexer<>(); + lexer.register("[a-f]", 0); + Assert.assertNull(lexer.lexAll("g", 0, Integer::compare)); + } + @Test + public void testGroupAndOperator(){ + Lexer lexer = new Lexer<>(); + lexer.register("(abc)+", 0); + List> tokens = lexer.lexAll("abcabc", 0, Integer::compare); + Assert.assertNotNull(tokens); + Assert.assertEquals(tokens.size(), 1); + } + }