This repository was archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
309 lines (291 loc) · 8.34 KB
/
Test.java
File metadata and controls
309 lines (291 loc) · 8.34 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// Copyright 2016, Yahoo Inc.
// Copyrights licensed under the New BSD License.
// See the accompanying LICENSE file for terms.
//
// [](https://travis-ci.org/jminusminus/simplebdd)
// ## Stability: 1 - Stable
// Super simple behavior-driven development style test writer for [Jmm](https://github.com/jminusminus/jmm).
package github.com.jminusminus.simplebdd;
// The default test runner for Jmm.
//
// ## Usage
//
// Create a test file.
//
// ```java
// // My_test.java
// package path.to.package;
//
// import github.com.jminusminus.simplebdd.Test;
//
// public class My_test extends Test {
// public static void main(String[] args) {
// My_test test = new My_test();
// test.run();
// }
//
// public void testThatThisIsTrue() {
// this.should("return that boolean matches boolean");
// this.assertEqual(true, true);
// }
// }
// ```
//
// Run the jmm test command.
//
// jmm test ./My_test.java
//
// Read the results.
//
// github.com.jminusminus.jmmexample.Helloworld_test
//
// ✓ should return the congratulations text
//
// ✓ 1 tests complete
//
// ## Extend Test
//
// To create a test extend the Test class and create an instance of it in the main method.
//
// ```java
// public class My_test extends Test {
// public static void main(String[] args) {
// My_test test = new My_test();
// test.run();
// }
// }
// ```
//
// ## test_xxx()
//
// Any method that starts with "test" will be executed by SimpleBDD.
//
// ```java
// public void test_name() {
// // Test code
// }
// ```
//
// ## before()
//
// Method called before any test is executed.
//
// ```java
// public void before() {
// // Prep-code
// }
// ```
//
// ## beforeEach()
//
// Method called before each test is executed.
//
// ```java
// public void beforeEach() {
// // Prep-code
// }
// ```
//
// ## after()
//
// Method called after all tests have executed.
//
// ```java
// public void after() {
// // Cleanup code
// }
// ```
//
// ## afterEach()
//
// Method called after each test has executed.
//
// ```java
// public void afterEach() {
// // Cleanup code
// }
// ```
public class Test {
protected java.lang.reflect.Method before;
protected java.lang.reflect.Method beforeEach;
protected java.lang.reflect.Method after;
protected java.lang.reflect.Method afterEach;
protected java.lang.reflect.Method[] tests = new java.lang.reflect.Method[0];
protected Result[] results = new Result[0];
// Runs the test by calling each method that begins with "test" outputting the results to `System.out`. The command will exit with the number of failed tests.
//
// ```java
// public static void main(String[] args) {
// My_test test = new My_test();
// test.run();
// }
// ```
public void run() {
java.lang.reflect.Method[] methods = this.getClass().getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
java.lang.reflect.Method method = methods[i];
if (method.getName().startsWith("test")) {
this.addTest(method);
} else if (method.getName().equals("before")) {
this.before = method;
} else if (method.getName().equals("beforeEach")) {
this.beforeEach = method;
} else if (method.getName().equals("after")) {
this.after = method;
} else if (method.getName().equals("afterEach")) {
this.afterEach = method;
}
}
this.executeTests();
this.report();
}
// Adds a description of what the preceding code "should" do.
//
// ```java
// public void test_boolean_is_boolean() {
// this.should("return that boolean matches boolean");
// }
// ```
public void should(String should) {
this.resultStart();
this.results[this.results.length-1].should = should;
}
// Used to test if two types are the same. Supported types are Short, Int, Long, Float, Double, Boolean, Char and String.
//
// ```java
// public void testName() {
// this.assertEqual(true, true);
// }
// ```
public boolean assertEqual(Object a, Object b) {
if (a.equals(b)) {
return true;
}
this.resultFailure(a, b);
return false;
}
// Used to test if two types are NOT the same. Supported types are Short, Int, Long, Float, Double, Boolean, Char and String.
//
// ```java
// public void testName() {
// this.assertNotEqual(false, true);
// }
// ```
public boolean assertNotEqual(Object a, Object b) {
if (!a.equals(b)) {
return true;
}
this.resultFailure(a, b);
return false;
}
protected void report() {
new Report(this.getClass().getName(), this.results);
int failures = 0;
for (int i = 0; i < this.results.length; i++) {
if (this.results[i].failed) {
failures++;
}
}
System.exit(failures);
}
protected void addTest(java.lang.reflect.Method v) {
int len = this.tests.length;
java.lang.reflect.Method[] a = new java.lang.reflect.Method[len + 1];
a[len] = v;
for (int i = 0; i < len; i++ ) {
a[i] = this.tests[i];
}
this.tests = a;
}
protected void resultStart() {
int len = this.results.length;
Result[] a = new Result[len + 1];
a[len] = new Result();
for (int i = 0; i < len; i++ ) {
a[i] = this.results[i];
}
this.results = a;
}
protected void resultFailure() {
this.results[this.results.length-1].failed = true;
}
protected void resultFailure(Object a, Object b) {
this.results[this.results.length-1].expected = a;
this.results[this.results.length-1].got = b;
this.results[this.results.length-1].failed = true;
}
protected void executeTests() {
this.executeBefore();
for (int i = 0; i < this.tests.length; i++) {
this.executeTest(this.tests[i]);
}
this.executeAfter();
}
protected void executeTest(java.lang.reflect.Method method) {
this.executeBeforeEach();
try {
method.invoke(this);
} catch (IllegalAccessException e) {
this.resultFailure();
System.out.println(e.getCause());
} catch (java.lang.reflect.InvocationTargetException e) {
this.resultFailure();
System.out.println(e.getCause());
e.printStackTrace(System.out);
}
this.executeAfterEach();
}
protected void executeBefore() {
if (this.before == null) {
return;
}
try {
this.before.invoke(this);
} catch (IllegalAccessException e) {
System.out.println(e.getCause());
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println(e.getCause());
e.printStackTrace(System.out);
}
}
protected void executeAfter() {
if (this.after == null) {
return;
}
try {
this.after.invoke(this);
} catch (IllegalAccessException e) {
System.out.println(e.getCause());
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println(e.getCause());
e.printStackTrace(System.out);
}
}
protected void executeBeforeEach() {
if (this.beforeEach == null) {
return;
}
try {
this.beforeEach.invoke(this);
} catch (IllegalAccessException e) {
System.out.println(e.getCause());
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println(e.getCause());
e.printStackTrace(System.out);
}
}
protected void executeAfterEach() {
if (this.afterEach == null) {
return;
}
try {
this.afterEach.invoke(this);
} catch (IllegalAccessException e) {
System.out.println(e.getCause());
} catch (java.lang.reflect.InvocationTargetException e) {
System.out.println(e.getCause());
e.printStackTrace(System.out);
}
}
}