ContextFreeGrammar extends Grammar { public Rule findRule(Symbol s){ assert (s != null) : "Symbol in findRule is:" + s; Iterator<Rule> iter = rules.iterator(); Rule r; while(iter.hasNext()) { r = iter.next(); if(r.getLHS().equals(s)) { return r; } } return null; } ContextFreeGrammar();   ContextFreeGrammar(ContextFreeGrammar g); abstract boolean genotype2Phenotype(); abstract boolean phenotype2Genotype(); abstract int getUsedCodons(); abstract int getUsedWraps(); @SuppressWarnings({"UnusedReturnValue", "IOResourceOpenedButNotSafelyClosed"}) boolean readBNFFile(String file_name); Rule findRule(Symbol s); Rule findRule(String s); ArrayList getTerminalRules(); ArrayList getNonTerminalRules();  }
@Test public void testFindRule_Symbol() { System.out.println("* ContextFreeGrammarTest: findRule"); Symbol s = null; Rule result = new Rule(); try { result = g.findRule(s); } catch (AssertionError e) { assertTrue(true); } catch (NullPointerException e) { assertTrue(true); } assertEquals(new Rule(), result); s = new Symbol("<string>", Enums.SymbolType.NTSymbol); Rule r = ContextFreeGrammarMock.getTestRule(); assertTrue(r.toString().equals(g.findRule(s).toString())); assertEquals(true, r!=g.findRule(s)); assertEquals(g.getRules().get(0), g.findRule(s)); s = new Symbol("_", Enums.SymbolType.TSymbol); assertEquals(null, g.findRule(s)); s = new Symbol("<test>", Enums.SymbolType.NTSymbol); assertEquals(null, g.findRule(s)); }
@Test public void testFindRule_String() { System.out.println("* ContextFreeGrammarTest: findRule"); String s = ""; Rule result = new Rule(); try { result = g.findRule(s); } catch (AssertionError e) { assertTrue(true); } catch (NullPointerException e) { assertTrue(true); } assertEquals(null, result); s = null; result = ContextFreeGrammarMock.getTestRule(); try { result = g.findRule(s); } catch (NullPointerException e) { assertTrue(true); } s = "<string>"; Rule r = ContextFreeGrammarMock.getTestRule(); assertTrue(r.toString().equals(g.findRule(s).toString())); assertEquals(true, r!=g.findRule(s)); assertEquals(g.getRules().get(0), g.findRule(s)); s = "_"; assertEquals(null, g.findRule(s)); s = "<test>"; assertEquals(null, g.findRule(s)); }