-
Notifications
You must be signed in to change notification settings - Fork 4
/
scaffold.js
135 lines (123 loc) · 5.18 KB
/
scaffold.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
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
var slice = [].slice
var BAILOUT = {}
var util = require('util')
var deepEqual = require('deep-equal')
function pad (number, width) {
number = String(number)
return (new Array(width + 1).join(' ') + number).substr(-Math.max(width, number.length))
}
module.exports = function (count, test) {
return function (globals, stream, exit, _callback) {
var expected = Math.abs(count), passed = 0, actual = 0, delayed
function comment (lines) {
lines = lines.split(/\n/).map(function (line) { return '# ' + line })
lines.push('')
return lines.join('\n')
}
function assert () {
var vargs = slice.call(arguments), ok, message
if (vargs.length == 3) {
ok = deepEqual(vargs[0], vargs[1], { strict: true })
message = ' ' + vargs[2]
} else {
ok = !! vargs[0]
message = vargs.length == 2 ? (' ' + vargs[1]) : ''
}
if (ok) {
passed++
stream.write('ok ' + (++actual) + message + '\n')
} else {
stream.write('not ok ' + (++actual) + message + '\n')
var detail = vargs.length == 3
? { EXPECTED: vargs[1], GOT: vargs[0] }
: { FALSE: vargs[0] }
stream.write(comment(util.inspect(detail, { depth: null })))
}
}
assert.die = function () {
throw { isBailout: BAILOUT, vargs: slice.call(arguments) }
}
assert.inspect = function (value, depth) {
stream.write(comment(util.inspect(value, { depth: depth || null })))
}
assert.say = function () {
stream.write(comment(util.format.apply(util.format, slice.call(arguments))))
}
assert.inc = function (count) {
expected += count
}
assert.leak = function () {
globals.push.apply(globals, arguments)
}
function callback (error) {
if (error) {
if (error.isBailout === BAILOUT) {
var message = typeof error.vargs[0] == 'string'
? (' ' + error.vargs.shift())
: ''
stream.write('Bail out!' + message + '\n')
if (error.vargs.length != 0) {
assert.inspect(error.vargs.shift())
}
} else {
stream.write('Bail out!\n')
if (error.stack) {
throw error
} else {
assert.inspect(error)
}
}
exit(1)
} else {
if (delayed) {
stream.write('1..' + expected + '\n')
}
var leaked = Object.keys(global).filter(function (global) {
return !~globals.indexOf(global)
})
if (leaked.length) {
stream.write('Bail out! Variables leaked into global namespace.\n')
assert.inspect(leaked)
exit(1)
} else {
var failed = actual - passed
var width = Math.max.apply(Math, [
expected, actual, passed
].map(function (number) { return String(number).length }))
stream.write('# expected ' + pad(expected, width) + '\n')
stream.write('# passed ' + pad(passed, width) + '\n')
if (failed != 0) {
stream.write('# failed ' + pad(failed, width) + '\n')
}
if (actual < expected) {
stream.write('# missing ' + pad(expected - actual, width) + '\n')
} else if (actual > expected) {
stream.write('# unexpected ' + pad(actual - expected, width) + '\n')
}
exit(failed == 0 && passed == expected ? 0 : 1)
}
}
_callback()
}
if (!(delayed = count < 1)) {
stream.write('1..' + expected + '\n')
}
// Found where line numbers are added, syntax context is added. Will
// have to read carefully to see if we can recreate, but then it would
// be Node.js/V8 specific, specific to Node.js. Although I have no
// intention of running in other JavaScript environments, it is probably
// best to treat the Proof scaffold as a minimal process wrapper, let
// Node.js do its thing.
//
// https://github.com/nodejs/node/blob/4db97b832b2522551d1bfacc1b95e3cbbf2df097/src/node.cc#L1440
// Hoplessness. Add answer.
// http://stackoverflow.com/questions/13746831/how-can-i-get-the-line-number-of-a-syntaxerror-thrown-by-requireid-in-node-js
// So, I'm removing a try/catch block here.
try {
test.call(null, assert, callback)
if (test.length == 1) callback()
} catch (error) {
callback(error)
}
}
}