- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
145 lines (132 loc) · 4.36 KB
/
Copy pathMain.java
File metadata and controls
145 lines (132 loc) · 4.36 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
importjava.io.FileNotFoundException;
importjava.io.File;
importjava.util.Scanner;
importorg.junit.runner.JUnitCore;
importorg.junit.runner.Request;
importorg.junit.runner.Result;
importorg.junit.runner.notification.Failure;
/**
* Main file for running automated tests on repl.it.
*
* If no command line argument is given, assumes there is a file name
* Tests.txt in the current directory. If a command line argument is
* given, then that file is used as the tests file. If last argument is
* --verbose, then prints the stack trace for failures in addition to
* a message.
*
* Test file should have one test per line. If a # is present in the line,
* that is assumed to separate the class name from the test method name.
* If no # is present, then the line is assumed to represent the class name
* and all tests in that class will be run.
*
* @author Anna Rafferty
*/
publicclassMain {
privatestaticbooleanverbose = false;
/**
* Uses Junit to run the given test and print out either that the test
* passed or information about the failure(s). If testMethodName is null,
* this will run all tests in the class named testClassName.
*/
publicstaticbooleanrunTest(StringtestClassName, StringtestMethodName) {
try {
Requestrequest;
ClasstestClass = Class.forName(testClassName);
if(testMethodName == null) {
request = Request.aClass(testClass);
} else {
request = Request.method(testClass, testMethodName);
}
Resultresult = newJUnitCore().run(request);
java.util.List<Failure> failures = result.getFailures();
System.out.print(testClassName + getTestMethodDisplayText(testMethodName) + " ");
if(failures.isEmpty()) {
System.out.println("passed.");
} else {
System.out.println("had failures:");
for(Failurefailure : failures) {
System.err.println(failure);
if(verbose) {
System.err.println(failure.getTrace());
}
}
}
returnresult.wasSuccessful();
} catch(Exceptione) {
System.err.println("Failed to load a test");
e.printStackTrace();
}
returnfalse;
}
/**
* Returns an empty string if testMethodName is null, and otherwise
* prepends # to the string.
*/
privatestaticStringgetTestMethodDisplayText(StringtestMethodName) {
if(testMethodName == null) {
return"";
} else {
return"#" + testMethodName;
}
}
/**
* Returns testLine with no leading/trailing spaces and with any
* test following // (comment) removed.
*/
privatestaticStringremoveCommentFromTest(StringtestLine) {
intcommentStart = testLine.indexOf("//");
if (commentStart >= 0) {
testLine = testLine.substring(0,commentStart);
}
returntestLine.trim();
}
/**
* Runs all tests specified in testNamesFile. See
* top of class for proper format for this file.
*/
publicstaticvoidrunTests(StringtestNamesFile) {
Scannerscanner = null;
try {
booleanallPassed = true;
scanner = newScanner(newFile(testNamesFile));
while(scanner.hasNextLine()) {
Stringline = scanner.nextLine();
line = removeCommentFromTest(line);
if(line.length() > 0) {
String[] testClassAndMethod = line.split("#");
StringclassName = testClassAndMethod[0];
StringmethodName = null;
if (testClassAndMethod.length > 1) {
methodName = testClassAndMethod[1];
}
booleanpassed = runTest(className, methodName);
allPassed = allPassed && passed;
}
}
System.out.println("All tests passed: " + allPassed);
} catch (FileNotFoundExceptione) {
System.err.println("Test file not found.");
e.printStackTrace();
} finally {
if(scanner != null) {
scanner.close();
}
}
}
/**
* Runs test in file given by first commandline argument,
* or if no argument is given, runs tests in Tests.txt.
* If last argument is --verbose, then prints the stack trace
* for failures.
*/
publicstaticvoidmain(String[] args) {
StringtestsFile = "Tests.txt";
if(args.length > 0 && args[args.length - 1].equals("--verbose")) {
Main.verbose = true;
}
if(args.length > 0 && !args[0].startsWith("--")) {
testsFile = args[0];
}
runTests(testsFile);
}
}