/*
* Copyright (C) Chaperon. All rights reserved.
* -------------------------------------------------------------------------
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package net.sourceforge.chaperon.test;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sourceforge.chaperon.model.grammar.Associativity;
import net.sourceforge.chaperon.model.grammar.Grammar;
import net.sourceforge.chaperon.model.grammar.Production;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.Terminal;
public class GrammarTestCase extends TestCase
{
private Terminal a;
private Terminal b;
private Nonterminal E;
private Nonterminal F;
private Nonterminal G;
private Production p1;
private Production p2;
private Production p3;
private Grammar grammar;
public GrammarTestCase(String name)
{
super(name);
}
public void setUp()
{
a = new Terminal("a");
b = new Terminal("b");
E = new Nonterminal("E");
F = new Nonterminal("F");
G = new Nonterminal("G");
grammar = new Grammar();
p1 = new Production(F);
p1.getDefinition().addSymbol(E);
p1.getDefinition().addSymbol(a);
p1.getDefinition().addSymbol(E);
grammar.addProduction(p1);
p2 = new Production(F);
p2.getDefinition().addSymbol(E);
p2.getDefinition().addSymbol(b);
p2.getDefinition().addSymbol(E);
grammar.addProduction(p2);
p3 = new Production(G);
p3.getDefinition().addSymbol(E);
p3.getDefinition().addSymbol(F);
grammar.addProduction(p3);
}
public void testContent()
{
assertTrue("Test if grammar contains productions", grammar.contains(p1));
assertTrue("Test if grammar contains productions", grammar.contains(p2));
assertTrue("Test if production are not equal", !p1.equals(p2));
Production p3 = new Production(F);
p3.getDefinition().addSymbol(E);
p3.getDefinition().addSymbol(a);
p3.getDefinition().addSymbol(E);
assertEquals("Test if production are equal", p1, p3);
assertTrue("Test if grammar contains productions", grammar.contains(p3));
assertEquals("Test production indices", 0, grammar.indexOf(p1));
assertEquals("Test production indices", 1, grammar.indexOf(p2));
assertEquals("Test production indices", 0, grammar.indexOf(p3));
}
public void testPriorities()
{
assertEquals("Test terminal priority", 0, grammar.getPriority(a));
assertEquals("Test terminal priority", 0, grammar.getPriority(b));
assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
assertEquals("Test production priority", 0, grammar.getPriority(p2));
grammar.setPriority(a, 4);
grammar.setPriority(b, 3);
assertEquals("Test terminal priority", 4, grammar.getPriority(a));
assertEquals("Test terminal priority", 3, grammar.getPriority(b));
assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
grammar.setPriority(a, 3);
grammar.setPriority(b, 4);
assertEquals("Test terminal priority", 3, grammar.getPriority(a));
assertEquals("Test terminal priority", 4, grammar.getPriority(b));
assertEquals("Test production priority", grammar.getPriority(a), grammar.getPriority(p1));
assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p2));
p1.setPrecedence(b);
assertEquals("Test production priority", grammar.getPriority(b), grammar.getPriority(p1));
}
public void testAssociativities()
{
Associativity left = Associativity.LEFT;
Associativity right = Associativity.RIGHT;
Associativity nonassoc = Associativity.NONASSOC;
assertEquals("Test if associativities are equal", left, new Associativity("left"));
assertEquals("Test if associativities are equal", right, new Associativity("right"));
assertEquals("Test if associativities are equal", nonassoc, new Associativity("nonassoc"));
assertTrue("Test if associativities are not equal", !left.equals(right));
assertTrue("Test if associativities are not equal", !right.equals(nonassoc));
assertTrue("Test if associativities are not equal", !nonassoc.equals(left));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p1));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p2));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
grammar.setAssociativity(b, right);
assertEquals("Test terminal associativities", right, grammar.getAssociativity(b));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p1));
assertEquals("Test production associativities", right, grammar.getAssociativity(p2));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
grammar.setAssociativity(a, left);
assertEquals("Test terminal associativities", left, grammar.getAssociativity(a));
assertEquals("Test production associativities", left, grammar.getAssociativity(p1));
assertEquals("Test production associativities", right, grammar.getAssociativity(p2));
assertEquals("Test production associativities", nonassoc, grammar.getAssociativity(p3));
p1.setPrecedence(b);
assertEquals("Test production priority", right, grammar.getAssociativity(p1));
}
public static Test suite()
{
return new TestSuite(GrammarTestCase.class);
}
}