-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestParserErrorHandling.java
More file actions
87 lines (68 loc) · 2.51 KB
/
Copy pathTestParserErrorHandling.java
File metadata and controls
87 lines (68 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package cop5555sp15;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import cop5555sp15.Parser.SyntaxException;
import cop5555sp15.TokenStream.Kind;
import cop5555sp15.ast.ASTNode;
import static cop5555sp15.TokenStream.Kind.*;
public class TestParserErrorHandling {
@Rule
public ExpectedException exception = ExpectedException.none();
private void parseIncorrectInput(String input,
Kind... expectedIncorrectTokenKind) {
TokenStream stream = new TokenStream(input);
Scanner scanner = new Scanner(stream);
scanner.scan();
Parser parser = new Parser(stream);
// System.out.println(stream);
ASTNode ast =parser.parse();
assertNull(ast);
List<SyntaxException> exceptions = parser.getExceptionList();
for(SyntaxException e: exceptions){
System.out.println(e.getMessage());
}
assertEquals(expectedIncorrectTokenKind.length, exceptions.size());
for (int i = 0; i < exceptions.size(); ++i){
assertEquals(expectedIncorrectTokenKind[i], exceptions.get(i).t.kind); // class is the incorrect token
}
}
@Test
public void import3() throws SyntaxException {
System.out.println("***********\nimport3");
String input = "import class A { } "; // this input is wrong.
System.out.println(input);
Kind ExpectedIncorrectTokenKind = KW_CLASS;
parseIncorrectInput(input, ExpectedIncorrectTokenKind);
}
@Test
public void def_simple_type2() throws SyntaxException {
System.out.println("***********\ndef_simple_type2");
String input = "class A {def B:int; def C:boolean; def S: string} ";
System.out.println(input);
parseIncorrectInput(input, RCURLY);
}
@Test
public void multiple_errors1() throws SyntaxException {
System.out.println("***********\nmultiple_errors1");
String input = "class A {def B:int; def C:boolean; def S: strings; def F: sing} ";
System.out.println(input);
parseIncorrectInput(input, IDENT, IDENT);
}
@Test
public void multiple_errors2() throws SyntaxException {
System.out.println("***********\nmultiple_errors2 ");
String input = "class A {def C={->x=&true; z = false; w =& true;};} ";
System.out.println(input);
parseIncorrectInput(input, AND, AND );
}
@Test
public void factor7() throws SyntaxException {
System.out.println("***********\nfactor7");
String input = "class A {def C={->x= &y; z = !y;};} ";
System.out.println(input);
parseIncorrectInput(input,AND);
}
}