Testing parsing mutiple nodes

This commit is contained in:
Fedor Isakov 2014-07-09 13:50:12 +02:00
parent 49c96f1526
commit 1c7fd82419
4 changed files with 91 additions and 24 deletions

View File

@ -0,0 +1,33 @@
/*
* Copyright 2014 JetBrains s.r.o.
*
* Licensed 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 jetbrains.mps.unification;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
/**
* @author fyodor on 09.07.2014.
*/
public class AssertAll {
public static void assertEqualsAll(Collection<Node> parsed, Node ... nodes) {
assertEquals(parsed, Arrays.asList(nodes));
}
}

View File

@ -16,9 +16,7 @@
package jetbrains.mps.unification;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -29,16 +27,24 @@ import static jetbrains.mps.unification.MockNode.*;
*/
public class MockTreeParser {
public static Node parse(String str) {
public static Collection<Node> parseAll(String str) {
return new RecursiveDescent().parse(str);
}
public static Node parse(String str) {
List<Node> nodes = new RecursiveDescent().parse(str);
if (nodes.size() != 1) {
throw new IllegalArgumentException("expected single term or var");
}
return nodes.get(0);
}
public static Term parseTerm(String str) {
return (Term) new RecursiveDescent().parse(str);
return (Term) parse(str);
}
public static Var parseVar(String str) {
return (Var) new RecursiveDescent().parse(str);
return (Var) parse(str);
}
private static class RecursiveDescent {
@ -47,12 +53,12 @@ public class MockTreeParser {
private LinkedList<String> termsStack = new LinkedList<String>();
private LinkedList<List<Node>> childrenStack = new LinkedList<List<Node>>();
private Node parse(String toParse) {
private List<Node> parse(String toParse) {
parseNextToken(Token.START, null);
loop(toParse);
parseNextToken(Token.END, null);
checkFinalState();
return childrenStack.pop().get(0);
return Collections.unmodifiableList(childrenStack.pop());
}
private void loop(String toParse) {

View File

@ -22,18 +22,19 @@ import static org.junit.Assert.*;
import static jetbrains.mps.unification.MockNode.*;
import static jetbrains.mps.unification.MockTreeParser.*;
import static jetbrains.mps.unification.AssertAll.*;
/**
* Created by fyodor on 10.06.2014.
*/
public class ParserTests {
@Test
public void testNormal() {
public void testSingle() {
assertEquals(parse("a"), term("a"));
assertEquals(parseTerm("a").symbol(), term("a").symbol());
assertEquals(parseVar("X").name(), var("X").name());
assertEquals(parse("a b"), term("a"));
assertEquals(parse("X b"), var("X"));
assertEquals(parse("X"), var("X"));
assertEquals(parse("a{b}"), term("a", term("b")));
assertEquals(parseTerm("a{b}").symbol(), term("a", term("b")).symbol());
assertEquals(parse("a{b c}"), term("a", term("b"), term("c")));
@ -42,12 +43,17 @@ public class ParserTests {
assertEquals(parse("a{X Y}"), term("a", var("X"), var("Y")));
}
@Test
public void testMuti() {
assertEqualsAll(parseAll("a b"), term("a"), term("b"));
assertEqualsAll(parseAll("X Y"), var("X"), var("Y"));
assertEqualsAll(parseAll("a{b} a{b}"), term("a", term("b")), term("a", term("b")));
}
@Test
public void testWhitespace() {
assertEquals(parse(" a"), term("a"));
assertEquals(parse(" a b"), term("a"));
assertEquals(parse(" a Y"), term("a"));
assertEquals(parse(" Y b"), var("Y"));
assertEquals(parse(" Y "), var("Y"));
assertEquals(parse("a{ b }"), term("a", term("b")));
assertEquals(parse("a{ b c }"), term("a", term("b"), term("c")));
assertEquals(parse("a{b X} "), term("a", term("b"), var("X")));
@ -72,16 +78,6 @@ public class ParserTests {
var("Z"), term("e"), var("W"), term("f"), term("g")),
var("Y")))));
}
@Test(expected = MockTreeParser.ParseException.class)
public void testEmptyFail() {
parse("");
}
@Test(expected = MockTreeParser.ParseException.class)
public void testEmptyChildrenFail() {
parse("a{}");
}
@Test(expected = MockTreeParser.ParseException.class)
public void testUclosedFail() {
@ -103,6 +99,26 @@ public class ParserTests {
parse("a{X b}}");
}
@Test(expected = IllegalArgumentException.class)
public void testFailMulti1() {
parse(" a Y");
}
@Test(expected = IllegalArgumentException.class)
public void testFailMulti2() {
parse("a b");
}
@Test(expected = MockTreeParser.ParseException.class)
public void testEmptyFail() {
parse("");
}
@Test(expected = MockTreeParser.ParseException.class)
public void testEmptyChildrenFail() {
parse("a{}");
}
@Test(expected = MockTreeParser.ParseException.class)
public void testWrongStartFail() {
parse("{a}");

View File

@ -146,6 +146,18 @@ public class SolverTests {
);
}
@Test
public void test8() throws Exception {
assertUnifiesWithBindings(
parseTerm("a{b{c d} e{f g} }"),
parseTerm("a{b{X Y} e{Z} }"),
bind(var("X"), parseTerm("c")),
bind(var("Y"), parseTerm("d")),
bind(var("Z"), parseTerm("f"))
);
}
@Test
public void testFail1() throws Exception {
assertUnifificationFails(