-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestRunner.cs
123 lines (99 loc) · 3.48 KB
/
TestRunner.cs
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
using System;
using System.IO;
using Noesis.Javascript;
namespace HeadlessTestingDemo
{
public class TestRunner
{
private readonly JavascriptContext context = new JavascriptContext();
private readonly string projectPath;
public TestRunner()
{
projectPath = Directory.GetCurrentDirectory().Replace(@"bin\Debug", "");
LoadFile(@"Jasmine\env.therubyracer.js");
LoadFile(@"Jasmine\window.js");
LoadFile(@"Jasmine\jasmine.js");
context.SetParameter("console", new SystemConsole());
LoadFile(@"Scripts\jquery-1.4.2.js");
LoadFile(@"Scripts\Roman.js");
LoadFile(@"Specs\SpecHelper.js");
LoadFile(@"Specs\RomanSpec.js");
}
public void LoadFile(string path)
{
var fullPath = Path.GetFullPath(projectPath + path);
context.Run(File.ReadAllText(fullPath) + ";null;");
}
public void Run(ConsoleReporter reporter)
{
context.SetParameter("dotNetReporter", reporter);
context.Run(@"
jasmine.JsApiReporter.prototype.reportSpecResults = function(spec) {
this.totalSpecs += 1;
if (spec.results().failedCount == 0) {
dotNetReporter.Passed(spec.getFullName());
} else {
this.totalFailures += 1;
var errors = [];
var results = spec.results().getItems();
for (var i = 0; i < results.length; i++) {
if (results[i].trace.stack) {
errors.push(results[i].trace.stack);
}
}
dotNetReporter.Failed(spec.getFullName(), errors);
}
};
var jasmineEnv = jasmine.getEnv();
var reporter = new jasmine.JsApiReporter();
reporter.totalSpecs = 0;
reporter.totalFailures = 0;
jasmineEnv.addReporter(reporter);
jasmineEnv.execute();
dotNetReporter.Finished();
");
}
public class SystemConsole
{
public void log(string message)
{
Console.WriteLine(message);
}
public void print(string message)
{
Console.Write(message);
}
}
public class ConsoleReporter
{
public int TotalSpecs;
public int TotalFailures;
public void Passed(string name)
{
TotalSpecs++;
Console.Write(".");
}
public void Failed(string name, object [] errors)
{
TotalSpecs++;
TotalFailures++;
Console.WriteLine("X\n{0}", name);
foreach (var error in errors)
{
Console.WriteLine(error);
}
}
public void Finished()
{
Console.WriteLine("\n\n{0} specs run, {1} failures\n\n", TotalSpecs, TotalFailures);
}
}
public static int Main()
{
var runner = new TestRunner();
var reporter = new ConsoleReporter();
runner.Run(reporter);
return reporter.TotalFailures == 0 ? 0 : 1;
}
}
}