-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDocoptTest.java
More file actions
258 lines (198 loc) · 5.69 KB
/
DocoptTest.java
File metadata and controls
258 lines (198 loc) · 5.69 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package org.docopt;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import static org.docopt.Python.list;
import static org.docopt.Python.partition;
public final class DocoptTest extends TestCase {
private static final String FILE_PROPERTY = DocoptTest.class.getName()
+ ".file";
private static final String VERBOSE_PROPERTY = DocoptTest.class.getName()
+ ".verbose";
private static final boolean VERBOSE = Boolean.getBoolean(VERBOSE_PROPERTY);
private static final String MESSAGE_FORMAT = "\n\"\"\"%s\"\"\"\n$ %s\n\b";
private static final String USER_ERROR = "\"user-error\"";
private static final TypeReference<Map<String, Object>> TYPE_REFERENCE = new TypeReference<Map<String, Object>>() {
};
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
public static Test suite() {
String file = System.getProperty(FILE_PROPERTY);
try {
final URL url;
if (file == null) {
url = DocoptTest.class.getResource("/testcases.docopt");
file = url.toString();
}
else {
url = url(file);
}
return parse(url);
}
catch (final IOException e) {
final String message;
if (e instanceof FileNotFoundException) {
message = "No such file";
}
else {
message = e.getMessage();
}
throw new DocoptTestError(message);
}
}
private static DocoptTestError readError(final String file,
final String message) {
return new DocoptTestError(String.format("Failed to parse %s: %s",
file, message));
}
private static URL url(final String file) throws IOException {
URI uri;
try {
uri = new URI(file);
}
catch (final URISyntaxException e) {
throw readError(file, e.getMessage());
}
// If the URI is not absolute, assume that it is a file path.
if (!uri.isAbsolute()) {
uri = new File(file).getAbsoluteFile().toURI();
}
// If the URI is a file, make sure that it is readable.
if ("file".equals(uri.getScheme())) {
final File f = new File(uri);
// If the file does not exist, an exception will be thrown
// when we attempt to read it. These errors just give a little
// more detail.
if (f.exists()) {
if (f.isDirectory()) {
throw readError(file, "Is a directory");
}
if (!f.canRead()) {
throw readError(file, "Permission denied");
}
}
}
return uri.toURL();
}
private static TestSuite parse(final URL url) throws IOException {
if (VERBOSE) {
System.out.println("Generating test cases from " + url);
}
final String name = pureBaseName(url);
String raw = Docopt.read(url.openStream());
raw = Pattern.compile("#.*$", Pattern.MULTILINE).matcher(raw)
.replaceAll("");
if (raw.startsWith("\"\"\"")) {
raw = raw.substring(3);
}
int index = 0;
final TestSuite suite = new TestSuite("docopt");
for (final String fixture : raw.split("r\"\"\"")) {
if (fixture.isEmpty()) {
continue;
}
final String doc;
final String body;
// >>> doc, u, body = fixture.partition('"""')
{
final String[] u = partition(fixture, "\"\"\"");
doc = u[0];
body = u[2];
}
boolean first = true;
for (final String _case : body.split("\\$")) {
if (first) {
first = false;
continue;
}
final String argv;
final String expect;
// >>> argv, u, expect = case.strip().partition('\n')
{
final String[] u = partition(_case.trim(), "\n");
argv = u[0];
expect = u[2];
}
suite.addTest(new DocoptTest(String.format("%s_%d", name,
++index), doc, argv(argv), expect(expect)));
}
}
return suite;
}
private static String pureBaseName(final URL url) {
final String name = url.getPath();
if (name.isEmpty()) {
return name;
}
return name.replaceFirst("^.+/", "").replaceFirst("\\.[^.]+$", "");
}
private static List<String> argv(final String argv) {
final List<String> u = list(argv.trim().split("\\s+"));
u.remove(0);
return u;
}
private static String argv(final List<String> argv) {
final StringBuilder sb = new StringBuilder();
for (final String arg : argv) {
sb.append("\"");
sb.append(arg.replaceAll("\"", "\\\""));
sb.append("\" ");
}
if (!argv.isEmpty()) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
private static Object expect(final String expect) {
if (USER_ERROR.equals(expect)) {
return USER_ERROR;
}
try {
return OBJECT_MAPPER.readValue(expect, TYPE_REFERENCE);
}
catch (final IOException e) {
throw new IllegalStateException(
"could not parse JSON object from:\n" + expect, e);
}
}
private final String doc;
private final List<String> argv;
private final Object expected;
private DocoptTest(final String name, final String doc,
final List<String> argv, final Object expected) {
super(name);
this.doc = doc;
this.argv = argv;
this.expected = expected; // TODO: Make a defensive copy?
}
@Override
protected void runTest() throws Throwable {
Object actual = null;
try {
actual = new Docopt(doc).withStdOut(null).withStdErr(null)
.withExit(false).parse(argv);
}
catch (final DocoptExitException e) {
actual = USER_ERROR;
}
final String message = (!VERBOSE) ? null : String.format(
MESSAGE_FORMAT, doc, argv(argv));
try {
assertEquals(message, expected, actual);
}
catch (final junit.framework.AssertionFailedError e) {
e.setStackTrace(new StackTraceElement[0]);
throw e;
}
}
}