-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.html
174 lines (160 loc) · 6.74 KB
/
test.html
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
<html lang="en">
<head>
<title>Unit Tests</title>
<!-- Use a monspace font and a dark theme -->
<!-- Colour Palet from http://clrs.cc/ -->
<style>
body {
font-family: Courier, "Courier New", monospace;
font-size: 11;
background-color: #111;
color: #eee;
margin: 0.25in 0.25in 0.25in 0.25in;
}
h2 {
font-size: 24;
}
.red {
color: #FF4136;
}
.green {
color: #2ECC40;
}
.orange {
color: #FF851B;
}
.blue {
color: #0074D9;
}
</style>
</head>
<body>
<h2>Unit Tests</h2>
<div id="output-goes-here"></div>
<script src="compiled/test/goog/base.js" type="text/javascript"></script>
<script src="compiled/test.js" type="text/javascript"></script>
<script>
// --------------------------------------------------------------------
// We're assuming here that "test.js" (in <script> immediately above)
// was created using ":optimizations :none" (see project.clj).
//
// If so, test.js won't contain much. Just a series of calls to
// goog.addDependency()
//
// Each call looks like this:
// goog.addDependency("path/to/a/file.js",
// [namespace-defined-in-the-js],
// [is-dependent-on-these-namespaces])
//
// Collectively, the contents of test.js creates a dependency graph,
// but doesn't actually import any javascript.
//
// To actually trigger the loading of any js, we must use another
// part of the goog API:
// goog.require('the-namespace-I-want').
//
// Each call to goog.require() will:
// - add a <script> for a js page (i.e. load the associated js)
// - plus also one <script> for each dependency.
// - these scripts will be added in the right (dependency) order.
// - but only if that <script> hasn't previously been added.
// So goog looks after all that for us.
//
// To be absolutely clear: nothing happens till we call goog.require()
//
// With a normal application, we'd typically bootstrap via a single:
// goog.require('my.core.namespace')
// which would pull in our top level entry point.
// This would, in turn, trigger a cascade of other dependency
// requires, and bingo, everything we need is pulled in.
//
// But here, in unittest land, we have a problem: there is no single
// "root" namespace to goog.require(). Instead, we have one namespace
// for each unittest cljs file in our test folder. No root. Flat.
// And the list of namespaces changes over time as we add and remove
// unitest cljs files. Uggh. Disaster.
//
// But we do have one ace up our sleeve. All the cljs files containing
// our unittests will have been scooped up by cljsbuild and put into
// the dependency graph in test.js
//
// So we'll need to exploit that (see loop below).
//
// Everything said above only applies when using :optimization :none.
// When using the other :optimization settings, all the code is
// arranged into one great big test.js and there's no need
// for any dependency stuff. Once we import test.js, we get everything.
//
// But building a big test.js is slow. And we want our unittest
// workflow to be fast and iterative.
//
// --------------------------------------------------------------------
//
// So the loop below is a sledge hammer. And a bit of a glorious hack.
// It "goog.requires" every namespace previously registered via calls
// to goog.addDependency()
//
// Every unittest namespace will be mentioned somewhere in test.js,
// so our sledgehammer brings in all the code. Which is exactly
// what we want. (All the unittests will register themselves on import)
//
// Its a hack because we're reaching into 'goog' and exploiting part
// of its implementation. And, of course, depending on implementation
// details is not wise. It might change. But I'll take that risk to
// get the benefits now.
//
//
//
// The loop was originally this simple:
// for(var namespace in goog.dependencies_.nameToPath)
// goog.require(namespace);
//
// But then this happened: http://dev.clojure.org/jira/browse/CLJS-995
//
// Now we require in all namespaces which depend on `cljs.core`
for(var path in goog.dependencies_.requires) {
if (goog.dependencies_.requires[path]["cljs.core"]) { // a cljs file ?
for (var namespace in goog.dependencies_.pathToNames[path]) // find the associated namespaces - there should only ever be one
goog.require(namespace);
}
}
// --------------------------------------------------------------------
// Output
var outputDiv = document.getElementById("output-goes-here")
function testPrintLn(line) {
line = line.replace(/\n/g, "");
if (line == "")
return;
// First, to the console
console.log(line);
// Second, into the HTML
var span = document.createElement("span");
outputDiv.appendChild(span);
// look for colour markers
if (-1 != line.indexOf('ERROR')) {
span.className = "orange"
}
if (-1 != line.indexOf('FAIL')) {
span.className = "red"
}
if (-1 != line.indexOf('Testing complete')) {
span.className = "green"
}
// replace leading blanks with so text lines up
var numLeadingBlanks = line.match(/^\s*/)[0].length;
var leadingNBSP = Array(numLeadingBlanks).join(" ")
span.innerHTML = leadingNBSP + line + "<br>";
}
// --------------------------------------------------------------------
// Run Tests
//
function run_tests() {
cemerick.cljs.test.set_print_fn_BANG_(testPrintLn);
var results = cemerick.cljs.test.run_all_tests();
}
// Don't run any tests till this page is fully loaded.
// Remember there'll be lots of async <script> added by the loop above.
window.addEventListener('load', run_tests);
</script>
</body>
</html>