import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertArrayEquals; import org.junit.Test; import java.util.*; public class GrammarTest { @Test public void testMixedNonAndTerminals() { List grammar = new ArrayList(); grammar.add(": |"); grammar.add(":Test"); grammar.add(":"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 1); String[] expected = new String[1]; Arrays.fill(expected, "Test"); assertArrayEquals(expected, result); } @Test(expected = IllegalArgumentException.class) public void testGenerateNonTermDoesntExist() { List grammar = new ArrayList(); grammar.add(":Test|:Other"); GrammarSolver solver = new GrammarSolver(grammar); solver.generate("", 1); } @Test(expected = IllegalArgumentException.class) public void testGenerateTimesInvalid() { List grammar = new ArrayList(); grammar.add(":Test|:Other"); GrammarSolver solver = new GrammarSolver(grammar); solver.generate("", 0); solver.generate("", -1); } @Test public void testGenerate() { String[] expected = new String[2]; Arrays.fill(expected, "Test"); List grammar = new ArrayList(); grammar.add(":Test"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 2); assertArrayEquals(expected, result); } @Test public void testMultipleRulePaths() { String[] expected = new String[3]; Arrays.fill(expected, "Test Nest"); List grammar = new ArrayList(); grammar.add(": "); grammar.add(":Test"); grammar.add(":Nest"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 3); assertArrayEquals(expected, result); } @Test public void testMultipleSymbolsAsRules() { String[] expected = new String[3]; Arrays.fill(expected, "Nest Nest"); List grammar = new ArrayList(); grammar.add(": "); grammar.add(":"); grammar.add(":Nest"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 3); assertArrayEquals(expected, result); } @Test public void testGenerateNestedTwice() { String[] expected = new String[3]; Arrays.fill(expected, "Nest"); List grammar = new ArrayList(); grammar.add(":"); grammar.add(":"); grammar.add(":Nest"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 3); assertArrayEquals(expected, result); } @Test public void testGenerateNested() { String[] expected = new String[2]; Arrays.fill(expected, "Nest"); List grammar = new ArrayList(); grammar.add(":"); grammar.add(":Nest"); GrammarSolver solver = new GrammarSolver(grammar); String[] result = solver.generate("", 2); assertArrayEquals(expected, result); } @Test public void testGrammarListImmutable() { List grammar = new ArrayList(); grammar.add(":Test"); GrammarSolver solver = new GrammarSolver(grammar); String expected = grammar.get(0); String actual = solver.getGrammarList().get(0); assertEquals("got back different than instantiation.", expected, actual); assertArrayEquals("different grammars.", grammar.toArray(), solver.getGrammarList().toArray()); } @Test public void testGrammarListImmutableWithPipe() { List grammar = new ArrayList(); grammar.add(":Test|Other"); GrammarSolver solver = new GrammarSolver(grammar); assertArrayEquals("different grammars.", grammar.toArray(), solver.getGrammarList().toArray()); } @Test public void testNonTerminalExtract() { String entry = ":Test"; String expected = ""; String actual = GrammarSolver.nonTerminal(entry); assertEquals("failure - extraction failed", expected, actual); } @Test public void testRuleExtractContent() { String entry = ":Test"; List expected = new ArrayList(); expected.add("Test"); List actual = GrammarSolver.terminals(entry); assertArrayEquals("failure - rule extraction failed", expected.toArray(), actual.toArray()); } @Test public void testRuleCount() { String entry = ":Test"; List expected = new ArrayList(); expected.add("Test"); List actual = GrammarSolver.terminals(entry); assertEquals("rule count different", expected.size(), actual.size()); } @Test(expected = IllegalArgumentException.class) public void testNoColon() { List grammar = new ArrayList(); grammar.add("test"); GrammarSolver solver = new GrammarSolver(grammar); } @Test(expected = IllegalArgumentException.class) public void testDuplicateGrammar() { List grammar = new ArrayList(); grammar.add(":Test|Other"); grammar.add(":Test|Other"); GrammarSolver solver = new GrammarSolver(grammar); } @Test(expected = IllegalArgumentException.class) public void testGrammarContainsSingleColon() { List grammar = new ArrayList(); grammar.add(":Test|:Other"); GrammarSolver solver = new GrammarSolver(grammar); } @Test public void testGrammarContains() { List grammar = new ArrayList(); grammar.add(":Test"); GrammarSolver solver = new GrammarSolver(grammar); assertEquals(true, solver.grammarContains("")); assertEquals(false, solver.grammarContains("a")); assertEquals(false, solver.grammarContains("")); } @Test public void testGrammarContainsCaseInsensitive() { List grammar = new ArrayList(); grammar.add(":Test"); GrammarSolver solver = new GrammarSolver(grammar); assertEquals(true, solver.grammarContains("")); assertEquals(false, solver.grammarContains("")); } @Test public void testGetSymbols() { List grammar = new ArrayList(); grammar.add(":Test"); grammar.add(":Test|Other"); GrammarSolver solver = new GrammarSolver(grammar); assertEquals("[, ]", solver.getSymbols()); } @Test public void testGetSymbolsSorted() { List grammar = new ArrayList(); grammar.add(":Test|Other"); grammar.add(":Test"); grammar.add(":Test|Other"); GrammarSolver solver = new GrammarSolver(grammar); assertEquals("[, , ]", solver.getSymbols()); } }