-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRunner.js
69 lines (59 loc) · 1.83 KB
/
TestRunner.js
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
import System.Text.RegularExpressions;
class TestRunner extends MonoBehaviour {
var total;
var errors;
var failures;
var assertions;
function Start() {
if (Application.isEditor) {
yield RunTests();
}
}
function RunTests() : IEnumerator {
Debug.Log('** Running tests...');
total = assertions = failures = errors = 0;
for (assembly in System.AppDomain.CurrentDomain.GetAssemblies()) {
for (type in assembly.GetTypes()) {
if (type.IsSubclassOf(typeof(UnitTest))) {
test = gameObject.AddComponent(type);
for (method in type.GetMethods()) {
if (method.Name.Contains('Test')) {
Run(test, method);
yield;
}
}
assertions += test.assertions;
Destroy(test);
}
}
}
Debug.Log(String.Format('** {0} examples, {1} failures, {2} errors. {3} assertions.', total, failures, errors, assertions));
}
function Run(test, method) {
total += 1;
try {
method.Invoke(test, null);
} catch(e : System.Reflection.TargetInvocationException) {
ee = e.InnerException;
if (ee.GetType() == TestFailure) {
failures += 1;
} else {
errors += 1;
}
LogFailure(ee, test, method);
}
}
function LogFailure(error, test, method) {
lineNumber = null;
failurePrefix = test.GetType() + "." + method.Name;
//Class.Method ... (at Assets/Scripts/Test/Class.js:555)
for (line in error.StackTrace.Split("\n"[0])) {
if (line.Contains(failurePrefix)) {
lineNumber = Regex(":([0-9]+)").Match(line).Groups[1].Value;
}
}
errorMessage = String.Format("at {0}, line {1}", failurePrefix, lineNumber);
newError = System.Activator.CreateInstance(error.GetType(), [errorMessage, error]);
Debug.LogError(newError);
}
}