package urban.shapes;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import urban.model.Rule;
import urban.parser.UrbanParser;
import urban.transformers.RuleToGeneratorTransformer;
public class RuleMergerTest {
private void doTest(String expected, String rule1, String rule2) throws RecognitionException {
Rule r1 = rule(rule1);
Rule r2 = rule(rule2);
if (expected == null){
nullTest(r1, r2);
nullTest(r2, r1);
} else {
assertEquals(expected + " @ 0, 0\n", RuleMerger.merge(r1, r2).toString());
assertEquals(expected + " @ 0, 0\n", RuleMerger.merge(r2, r1).toString());
}
}
public void nullTest(Rule r1, Rule r2) {
Rule merge = RuleMerger.merge(r1, r2);
assertNull(""+merge, RuleMerger.merge(r1, r2));
}
@Test
public void testMerge_Null_NoBond() throws RecognitionException {
String expected = "A(a~0, b) <-> A(a~1, b)";
doTest(expected, expected, null);
}
@Test
public void testMerge_Null_Bond() throws RecognitionException {
String expected = "A(a~0, b!1),A(b!1) <-> A(a~1, b!1),A(b!1)";
doTest(expected, expected, null);
}
@Test
public void testMerge_Null_Pinch() throws RecognitionException {
String expected = "A(a~0, b!1, c!2),A(b!1, c!2) <-> A(a~1, b!1, c!2),A(b!1, c!2)";
doTest(expected, expected, null);
}
@Test
public void testMerge_Null_AnyBond() throws RecognitionException {
String expected = "A(a~0, b!_) <-> A(a~1, b!_)";
doTest(expected, expected, null);
}
@Test
public void testMerge_Bonding() throws RecognitionException {
String expected = "A(a),B(b) <-> A(a!1),B(b!1)";
doTest(expected, expected, null);
}
@Test
public void testMerge_UnBonding() throws RecognitionException {
String rule = "A(a!1),B(b!1) <-> A(a),B(b)";
String expected = "A(a),B(b) <-> A(a!1),B(b!1)";
doTest(expected, rule, null);
}
@Test
public void testMerge_Null_Cyclic() throws RecognitionException {
String expected = "A(a~0, c!1, d!2),A(b!3, c!1),A(b!3, d!2) <-> A(a~1, c!1, d!2),A(b!3, c!1),A(b!3, d!2)";
doTest(expected, expected, null);
}
@Test
public void testMerge_Bond_Pinch() throws RecognitionException {
String shape1 = "A(a~0,b!1),A(b!1) <-> A(a~1,b!1),A(b!1)";
String shape2 = "A(a~0,b!1,c!2),A(b!1,c!2) <-> A(a~1,b!1,c!2),A(b!1,c!2)";
String expected = "A(a~0, b!1, c!2),A(b!1, c!2) <-> A(a~1, b!1, c!2),A(b!1, c!2)";
doTest(expected, shape1, shape2);
}
@Test
public void testMerge_Pinch_Pinch() throws RecognitionException {
String shape1 = "A(a~0,b!1,d!2),A(b!1,d!2) <-> A(a~1,b!1,d!2),A(b!1,d!2)";
String shape2 = "A(a~0,b!1,c!2),A(b!1,c!2) <-> A(a~1,b!1,c!2),A(b!1,c!2)";
String expected = "A(a~0, b!1, c!2, d!3),A(b!1, c!2, d!3) <-> A(a~1, b!1, c!2, d!3),A(b!1, c!2, d!3)";
doTest(expected, shape1, shape2);
}
@Test
public void testMerge_Shape_Pinch_NoMerge() throws RecognitionException {
String shape1 = "A(a~0,b!1,c!2),A(b!1),A(c!2) <-> A(a~1,b!1,c!2),A(b!1),A(c!2)";
String shape2 = "A(a~0,b!1,c!2),A(b!1,c!2) <-> A(a~1,b!1,c!2),A(b!1,c!2)";
doTest(null, shape1, shape2);
}
@Test
public void testMerge_Shape_Pinch_NoMerge2() throws RecognitionException {
String shape1 = "A(b,c!1),B(a,c!2),C(a!1),C(b!2) <-> A(b!3,c!1),B(a!3,c!2),C(a!1),C(b!2)";
String shape2 = "A(b,c!1),B(a,c!2),C(a!1,b!2) <-> A(b!3,c!1),B(a!3,c!2),C(a!1,b!2)";
doTest(null, shape1, shape2);
}
@Test
public void testMerge_Bond_Bond() throws RecognitionException {
String shape1 = "A(a~0, b!1),A(b!1) <-> A(a~1, b!1),A(b!1)";
String shape2 = "A(a~0, c!1),A(c!1) <-> A(a~1, c!1),A(c!1)";
String expected = "A(a~0, b!1, c!2),A(b!1),A(c!2) <-> A(a~1, b!1, c!2),A(b!1),A(c!2)";
doTest(expected, shape1, shape2);
}
@Test
public void testMerge_Bond_Cyclic() throws RecognitionException {
String shape1 = "A(a~0, c!1),A(c!1) <-> A(a~1, c!1),A(c!1)";
String shape2 = "A(a~0, c!1, d!2),A(b!3, c!1),A(b!3, d!2) <-> A(a~1, c!1, d!2),A(b!3, c!1),A(b!3, d!2)";
String expected = shape2;
doTest(expected, shape1, shape2);
}
@Test
public void testMerge_Bond_Cyclic2() throws RecognitionException {
String shape1 = "A(b),B(a, c!3),C(b!3) <-> A(b!1),B(a!1, c!3),C(b!3)";
String shape2 = "A(b, d!1),B(a, c!2),D(a!1, c!3),C(b!2, d!3) <-> A(b!1, d!2),B(a!1, c!3),D(a!2, c!4),C(b!3, d!4)";
String expected = shape2;
doTest(expected, shape1, shape2);
}
@Test
public void testMerge_Bond_Bond_NoMerge() throws RecognitionException {
String shape1 = "A(a~0, b!1),A(b!1) <-> A(a~1, b!1),A(b!1)";
String shape2 = "A(a~0, b!1),A(c!1) <-> A(a~1, b!1),A(c!1)";
doTest(null, shape1, shape2);
}
@Test
public void testMerge_Bond_NoBond_NoMerge() throws RecognitionException {
String shape1 = "A(a~0, b!1),A(b!1) <-> A(a~1, b!1),A(b!1)";
String shape2 = "A(a~0, b) <-> A(a~1, b)";
doTest(null, shape1, shape2);
}
@Test
public void testMerge_AnyBond_NoBond_NoMerge() throws RecognitionException {
String shape1 = "A(a~0, b!_) <-> A(a~1, b!_)";
String shape2 = "A(a~0, b) <-> A(a~1, b)";
doTest(null, shape1, shape2);
}
@Test
public void testMerge_Bonding2() throws RecognitionException {
String a = "A(a,c),B(b) <-> A(a!1,c),B(b!1)";
String b = "A(a),B(b,d!1),C(a!1) <-> A(a!1),B(b!1,d!2),C(a!2)";
String expected = "A(a, c),B(b, d!1),C(a!1) <-> A(a!1, c),B(b!1, d!2),C(a!2)";
doTest(expected, a,b);
}
@Test
public void testMerge_Bonding3() throws RecognitionException {
String a = "B(c),C(a!1, b),A(c!1) <-> B(c!2),C(a!1, b!2),A(c!1) @ 0, 0";
String b = "B(a!1, c),C(a!2, b),A(b!1, c!2) <-> B(a!1, c!2),C(a!3, b!2),A(b!1, c!3) @ 0, 0";
String expected = "B(a!1, c),C(a!2, b),A(b!1, c!2) <-> B(a!1, c!2),C(a!3, b!2),A(b!1, c!3)";
doTest(expected, a,b);
}
private Rule rule(String string) throws RecognitionException {
if (string == null)
return null;
return UrbanParser.parse(string+ " @ 0,0\n").rule();
}
}