/
CMinusTest.java
182 lines (167 loc) · 5.35 KB
/
CMinusTest.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
*
* Copyright 2010-2011 (C) The original author or authors
*
* 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 com.toolazydogs.aunit;
import static com.toolazydogs.aunit.Assert.assertToken;
import static com.toolazydogs.aunit.Assert.assertTree;
import static com.toolazydogs.aunit.CoreOptions.lexer;
import static com.toolazydogs.aunit.CoreOptions.options;
import static com.toolazydogs.aunit.CoreOptions.parser;
import static com.toolazydogs.aunit.CoreOptions.walker;
import static com.toolazydogs.aunit.Work.args;
import static com.toolazydogs.aunit.Work.parse;
import static com.toolazydogs.aunit.Work.resultOf;
import static com.toolazydogs.aunit.Work.rule;
import static com.toolazydogs.aunit.Work.scan;
import static com.toolazydogs.aunit.Work.walk;
import static com.toolazydogs.aunit.Work.withRule;
import static org.junit.Assert.fail;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.toolazydogs.aunit.tests.CMinusLexer;
import com.toolazydogs.aunit.tests.CMinusParser;
import com.toolazydogs.aunit.tests.CMinusWalker;
/**
*
*/
@RunWith(AntlrTestRunner.class)
public class CMinusTest
{
/**
* Configuration for tests whose name starts with "test".
* <p/>
* This is a simple example of how to assign a different configuration to
* tests whose name starts with "test".
*
* @return a configuration for tests whose name starts with "test"
*/
@Configuration
@AppliesTo("test.*")
public static Option[] configureTest()
{
return options(
lexer(CMinusLexer.class),
parser(CMinusParser.class),
walker(CMinusWalker.class)
);
}
/**
* Configuration for tests that will not fail on lexer/parse errors.
* <p/>
* This is a simple example of how to assign a different configuration to
* a set of tests.
*
* @return a configuration for tests that will not fail on lexer/parse errors
*/
@Configuration
@AppliesTo("oddballTest")
public static Option[] configureSpecial()
{
return options(
lexer(CMinusLexer.class).failOnError(false),
parser(CMinusParser.class).failOnError(false),
walker(CMinusWalker.class).failOnError(false)
);
}
@Test
public void test() throws Exception
{
try
{
parse("+ 1 2", rule("expression", args(15)));
fail("Should have failed parsing");
}
catch (ParserException e)
{
}
}
@Test
public void testOne() throws Exception
{
/**
* Here's an example of specifying token type to be returned by the lexer
*/
assertToken(CMinusLexer.ID, "abc", scan("abc"));
}
@Test
public void testTwo() throws Exception
{
/**
* Here's an example of specifying the text of the root token
*/
assertTree("=", "(= a (EXPR (+ 1 2))) ", parse(" a = 1 + 2; ", rule("statement")));
/**
* Here's an example of specifying an imaginary token that should be at the root of the tree
*/
assertTree(CMinusParser.BLOCK, "(BLOCK (= a (EXPR (+ 1 2))) ) ", parse("{ a = 1 + 2; }", rule("block")));
/**
* Here's an example of invoking a rule that takes parameters
*/
assertTree(CMinusParser.EXPR, "(EXPR (+ 1 2))", parse("1 + 2", rule("expression", args(15))));
}
@Test
public void oddballTest() throws Exception
{
try
{
parse("+ 1 2", rule("expression", args(15)));
}
catch (ParserException e)
{
fail("This configuration should not have thrown an exception");
}
}
@Test
public void testTreeTest() throws Exception
{
try
{
walk(withRule("testExpr"), resultOf(parse("1 + 2", rule("expression", args(15)))));
}
catch (ParserException e)
{
fail("This configuration should not have thrown an exception");
}
}
/**
* This test does not have a name of "oddballTest" or that begins with
* "test" and so using the static assert methods will throw
* {@link IllegalStateException} to indicate that the test was not
* configured.
*
* @throws Exception if there is an error scanning or parsing
*/
@Test
public void unconfiguredTest() throws Exception
{
try
{
assertToken(CMinusLexer.ID, "abc", scan("abc"));
fail("Unconfigured test should have failed");
}
catch (IllegalStateException e)
{
}
try
{
assertTree(CMinusParser.EXPR, "(EXPR (+ 1 2))", parse("1 + 2", rule("expression", args(15))));
fail("Unconfigured test should have failed");
}
catch (IllegalStateException e)
{
}
}
}