You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by el...@apache.org on 2006/12/04 19:41:31 UTC
svn commit: r482284 [3/3] - in /labs/dungeon: ./ src/ src/main/
src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/dungeon/ src/main/java/org/apache/dungeon/meta/
src/main/java/org/apache/dungeon/meta/parser/ src/main/...
Added: labs/dungeon/src/test/java/org/apache/dungeon/meta/parser/MetaParserTest.java
URL: http://svn.apache.org/viewvc/labs/dungeon/src/test/java/org/apache/dungeon/meta/parser/MetaParserTest.java?view=auto&rev=482284
==============================================================================
--- labs/dungeon/src/test/java/org/apache/dungeon/meta/parser/MetaParserTest.java (added)
+++ labs/dungeon/src/test/java/org/apache/dungeon/meta/parser/MetaParserTest.java Mon Dec 4 10:41:29 2006
@@ -0,0 +1,274 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+package org.apache.dungeon.meta.parser;
+
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit test for the MetaParser
+ *
+ * @author <a href="mailto:dev@labs.apache.org">Dungeon Project</a>
+ */
+public class MetaParserTest extends TestCase
+{
+ public void testNullGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+
+ MetaGrammar mg = mp.parseGrammar( (char[])null );
+
+ assertNull( mg );
+ }
+
+ public void testEmptyGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+
+ MetaGrammar mg = mp.parseGrammar( "".toCharArray() );
+
+ assertNull( mg );
+ }
+
+ public void testEpsilonGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar=" <test> ::= # ;".toCharArray();
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 1, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof EpsilonTransition );
+ }
+
+ public void testSimpleVTGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar=" <test> ::= 'abcdef' ;".toCharArray();
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 1, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VT );
+ assertEquals( "abcdef", symbol.getName() );
+ }
+
+ public void testSimpleVNGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar=" <test> ::= <test> ;".toCharArray();
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 1, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VN );
+ assertEquals( "test", symbol.getName() );
+ }
+
+ public void testDoubleVTGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar =
+ (" <test> ::= 'abcdef' ; \n\n" +
+ " <test2> ::= 'ghijkl';").toCharArray();
+
+ String expected1 =
+ "Production \n"+
+ "<test> ::= 'abcdef'";
+
+ String expected2 =
+ "Production \n"+
+ "<test2> ::= 'ghijkl'";
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 2, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VT );
+
+ assertEquals( expected1, production.toString() );
+
+ production = productions.get( 1 );
+ rhs = ((MetaProduction)production).getRhs();
+ assertNotNull( rhs );
+
+ symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VT );
+
+ assertEquals( expected2, production.toString() );
+ }
+
+ public void testDoubleVNGrammar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar =
+ (" <test> ::= <test2> ; \n\n" +
+ " <test2> ::= <test>;").toCharArray();
+
+ String expected1 =
+ "Production \n"+
+ "<test> ::= <test2>";
+
+ String expected2 =
+ "Production \n"+
+ "<test2> ::= <test>";
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 2, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VN );
+
+ assertEquals( expected1, production.toString() );
+
+ production = productions.get( 1 );
+ rhs = ((MetaProduction)production).getRhs();
+ assertNotNull( rhs );
+
+ symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 1, symbols.size() );
+
+ symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VN );
+
+ assertEquals( expected2, production.toString() );
+ }
+
+
+ public void testVTBar() throws ParserException
+ {
+ MetaParser mp = new MetaParser();
+ char[] grammar =
+ " <test> ::= 'abcdef' | 'ghijkl' ;".toCharArray();
+
+ String expected =
+ "Production \n"+
+ "<test> ::= 'abcdef' | 'ghijkl'";
+
+ MetaGrammar mg = mp.parseGrammar( grammar );
+
+ assertNotNull( mg );
+ assertNotNull( mg.getProductions() );
+ assertEquals( 1, mg.getProductions().size() );
+
+ List<Production> productions = mg.getProductions();
+
+ Production production = productions.get( 0 );
+ MetaRhs rhs = ((MetaProduction)production).getRhs();
+
+ assertNotNull( rhs );
+
+ List<Symbol> symbols = rhs.getSymbols();
+ assertNotNull( symbols );
+ assertEquals( 2, symbols.size() );
+
+ Symbol symbol = symbols.get( 0 );
+ assertTrue( symbol instanceof VT );
+
+ symbol = symbols.get( 1 );
+ assertTrue( symbol instanceof VT );
+
+ assertEquals( expected, production.toString() );
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org