/*
* 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.build.EmptyList;
import net.sourceforge.chaperon.model.symbol.Nonterminal;
import net.sourceforge.chaperon.model.symbol.Terminal;
public class SymbolTestCase extends TestCase
{
public SymbolTestCase()
{
super("SymbolTestCase");
}
public static void assertNotEquals(String message, Object a, Object b)
{
if ((a==null) || (a==null))
return;
if (a.equals(b))
{
String formatted = "";
if (message!=null)
formatted = message+" ";
fail(formatted+"<"+a+"> equals <"+b+">");
}
}
public void runTest() throws Throwable
{
// Test for terminals
Terminal a = new Terminal("a");
Terminal b = new Terminal("b");
assertEquals("Test if symbol names are equal", "a", a.getName());
assertEquals("Test if symbol names are equal", "b", b.getName());
assertEquals("Test if symbols are equal", a, a);
assertNotEquals("Test if symbols are not equal", a, b);
assertEquals("Test if hashcodes are equals", a.hashCode(), a.hashCode());
Terminal a2 = new Terminal("a");
assertEquals("Test if symbols are equal", a, a2);
assertNotEquals("Test if symbols are not equal", a2, b);
assertEquals("Test if hashcodes are equals", a.hashCode(), a2.hashCode());
assertTrue("Test if hashcodes are no equal", a2.hashCode()!=b.hashCode());
// Test for nonterminals
Nonterminal A = new Nonterminal("A");
Nonterminal B = new Nonterminal("B");
assertEquals("Test if symbol names are equal", "A", A.getName());
assertEquals("Test if symbol names are equal", "B", B.getName());
assertEquals("Test if symbols are equal", A, A);
assertNotEquals("Test if symbols are not equal", A, B);
assertEquals("Test if hashcodes are equals", A.hashCode(), A.hashCode());
Nonterminal A2 = new Nonterminal("A");
assertEquals("Test if symbols are equal", A, A2);
assertNotEquals("Test if symbols are not equal", A2, B);
assertEquals("Test if hashcodes are equal", A.hashCode(), A2.hashCode());
assertTrue("Test if hashcodes are no equal", A2.hashCode()!=B.hashCode());
// Test for emptylist symbols
EmptyList emptylist = new EmptyList();
EmptyList emptylist2 = new EmptyList();
assertEquals("Test if symbols are equal", emptylist, emptylist);
assertEquals("Test if symbols are equal", emptylist, emptylist2);
// Composite tests
Terminal a3 = new Terminal("A");
Nonterminal A3 = new Nonterminal("a");
assertNotEquals("Test if symbols are not equal", a3, A);
assertNotEquals("Test if symbols are not equal", a, A3);
assertNotEquals("Test if symbols are not equal", a, emptylist);
assertNotEquals("Test if symbols are not equal", A, emptylist);
assertTrue("Test if hashcodes are no equal", a.hashCode()!=emptylist.hashCode());
assertTrue("Test if hashcodes are no equal", A.hashCode()!=emptylist.hashCode());
try
{
Terminal a4 = new Terminal(null);
fail("Test if exception occurs");
}
catch (Exception e) {}
try
{
Nonterminal A4 = new Nonterminal(null);
fail("Test if exception occurs");
}
catch (Exception e) {}
try
{
Terminal a4 = new Terminal("");
fail("Test if exception occurs");
}
catch (Exception e) {}
try
{
Nonterminal A4 = new Nonterminal("");
fail("Test if exception occurs");
}
catch (Exception e) {}
}
public static Test suite()
{
TestSuite suite = new TestSuite("Symbol tests");
suite.addTest(new SymbolTestCase());
return suite;
}
}