-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
doctest.coffee
471 lines (407 loc) · 12.6 KB
/
doctest.coffee
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
###
>>>
>>> >>> >>>
>>>>>>>> >>>>>>> >>>>>>> >>>>> >>>>>>> >>>>>> >>>>>
>>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>>
>>> >>> >>> >>> >>> >>> >>>>>>>>> >>>>>>>> >>>
>>> >>> >>> >>> >>> >>> >>> >>> >>> >>>
>>>>>>>> >>>>>>> >>>>>>> >>>> >>>>>>> >>>>>> >>>>
.....................x.......xx.x.................................
###
doctest = (path, options = {}, callback = noop) ->
validateOption = (name, validValues) ->
if name of options and not R.contains options[name], validValues
throw new Error "Invalid #{name} `#{options[name]}'"
validateOption 'module', ['amd', 'commonjs']
validateOption 'type', ['coffee', 'js']
type = options.type ? do ->
match = R.match /[.](coffee|js)$/, path
if match is null
throw new Error 'Cannot infer type from extension'
match[1]
fetch path, options, (text) ->
source = toModule rewrite(text, type), options.module
if options.print
console.log R.replace(/\n$/, '', source) unless options.silent
callback source
source
else
results = if options.module is 'commonjs'
commonjsEval source, path
else
functionEval source
log results unless options.silent
callback results
results
doctest.version = '0.8.0'
if typeof window isnt 'undefined'
{CoffeeScript, esprima, R} = window
window.doctest = doctest
else
fs = require 'fs'
pathlib = require 'path'
CoffeeScript = require 'coffee-script'
esprima = require 'esprima-fb'
R = require 'ramda'
module.exports = doctest
# indentN :: Number -> String -> String
indentN = R.pipe(
R.times R.always ' '
R.join ''
R.replace /^(?!$)/gm
)
# indent2 :: String -> String
indent2 = indentN 2
# noop :: * -> ()
noop = ->
# reduce :: a -> (a,b -> a) -> [b] -> a
reduce = R.flip R.reduce
# toPairs :: [a] -> [(Number,a)]
toPairs = R.converge(
R.zip
R.pipe R.length, R.range 0
R.identity
)
fetch = (path, options, callback) ->
silent = options.silent or options.print
wrapper = (text) ->
name = R.last R.split '/', path
console.log "running doctests in #{name}..." unless silent
callback text
console.log "retrieving #{path}..." unless silent
if typeof window isnt 'undefined'
jQuery.ajax path, dataType: 'text', success: wrapper
else
wrapper fs.readFileSync path, 'utf8'
rewrite = (input, type) ->
rewrite[type] input.replace(/\r\n?/g, '\n').replace(/^#!.*/, '')
# toModule :: String,String? -> String
toModule = (source, moduleType) -> switch moduleType
when 'amd'
"""
#{source}
function define() {
for (var idx = 0; idx < arguments.length; idx += 1) {
if (typeof arguments[idx] == 'function') {
arguments[idx]();
break;
}
}
}
"""
when 'commonjs'
iifeWrap = (s) -> "void function() {\n#{indent2 s}}.call(this);"
iifeWrap """
var __doctest = {
queue: [],
input: function(fn) {
__doctest.queue.push([fn]);
},
output: function(num, fn) {
__doctest.queue.push([fn, num]);
}
};
#{iifeWrap source}
(module.exports || exports).__doctest = __doctest;
"""
else
source
# transformComments :: [Object] -> [Object]
#
# Returns a list of {input,output} pairs representing the doctests
# present in the given list of esprima comment objects.
#
# > transformComments [
# . type: 'Line'
# . value: ' > 6 * 7'
# . loc: start: {line: 1, column: 0}, end: {line: 1, column: 10}
# . ,
# . type: 'Line'
# . value: ' 42'
# . loc: start: {line: 2, column: 0}, end: {line: 2, column: 5}
# . ]
# [
# . commentIndex: 1
# . input:
# . value: '6 * 7'
# . loc: start: {line: 1, column: 0}, end: {line: 1, column: 10}
# . output:
# . value: '42'
# . loc: start: {line: 2, column: 0}, end: {line: 2, column: 5}
# . ]
transformComments = R.pipe(
toPairs
reduce ['default', []], ([state, accum], [commentIndex, comment]) -> R.pipe(
R.prop 'value'
R.split '\n'
toPairs
reduce [state, accum], ([state, accum], [idx, line]) ->
switch comment.type
when 'Block'
normalizedLine = R.replace /^\s*[*]?\s*/, '', line
start = end = line: comment.loc.start.line + idx
when 'Line'
normalizedLine = R.replace /^\s*/, '', line
{start, end} = comment.loc
[prefix, value] = R.tail R.match /^(>|[.]*)[ ]?(.*)$/, normalizedLine
if prefix is '>'
['input', R.appendTo accum, {
commentIndex
input: {loc: {start, end}, value}
}]
else if state is 'default'
['default', accum]
else if state is 'input'
if prefix
['input', R.appendTo R.init(accum), {
commentIndex
input:
loc: {start: R.last(accum).input.loc.start, end}
value: "#{R.last(accum).input.value}\n#{value}"
}]
else
['output', R.appendTo R.init(accum), {
commentIndex
input: R.last(accum).input
output: {loc: {start, end}, value}
}]
else if state is 'output'
if prefix
['output', R.appendTo R.init(accum), {
commentIndex
input: R.last(accum).input
output:
loc: {start: R.last(accum).output.loc.start, end}
value: "#{R.last(accum).output.value}\n#{value}"
}]
else
['default', accum]
) comment
R.last
)
# substring :: String,{line,column},{line,column} -> String
#
# Returns the substring between the start and end positions.
# Positions are specified in terms of line and column rather than index.
# {line: 1, column: 0} represents the first character of the first line.
#
# > substring "hello\nworld", {line: 1, column: 3}, {line: 2, column: 2}
# "lo\nwo"
# > substring "hello\nworld", {line: 1, column: 0}, {line: 1, column: 0}
# ""
substring = (input, start, end) ->
return '' if start.line is end.line and start.column is end.column
R.pipe(
R.split /^/m
toPairs
reduce ['', no], (accum, [idx, line]) ->
isStartLine = idx + 1 is start.line
isEndLine = idx + 1 is end.line
R.pipe(
R.split ''
toPairs
reduce ['', R.last accum], ([chrs, inComment], [column, chr]) ->
if (isStartLine and column is start.column) or
inComment and not (isEndLine and column is end.column)
[R.concat(chrs, chr), yes]
else
[chrs, no]
R.converge(
R.prepend
R.pipe R.head, R.concat R.head accum
R.tail
)
) line
R.head
) input
wrap = R.curry (type, test) -> R.pipe(
R.filter R.partialRight R.has, test
R.map (dir) -> wrap[type][dir] test
R.join '\n'
) ['input', 'output']
wrap.js = wrap 'js'
wrap.js.input = (test) -> """
__doctest.input(function() {
return #{test.input.value};
});
"""
wrap.js.output = (test) -> """
__doctest.output(#{test.output.loc.start.line}, function() {
return #{test.output.value};
});
"""
wrap.coffee = wrap 'coffee'
wrap.coffee.input = (test) -> """
__doctest.input ->
#{indent2 test.input.value}
"""
wrap.coffee.output = (test) -> """
__doctest.output #{test.output.loc.start.line}, ->
#{indent2 test.output.value}
"""
rewrite.js = (input) ->
# 1. Locate block comments and line comments within the input text.
#
# 2. Create a list of comment chunks from the list of line comments
# located in step 1 by grouping related comments.
#
# 3. Create a list of code chunks from the remaining input text.
# Note that if there are N comment chunks there are N + 1 code
# chunks. A trailing empty comment enables the final code chunk
# to be captured:
bookend = value: '', loc: start: line: Infinity, column: Infinity
# 4. Map each comment chunk in the list produced by step 2 to a
# string of JavaScript code derived from the chunk's doctests.
#
# 5. Zip the lists produced by steps 3 and 4; flatten; and join.
#
# 6. Find block comments in the source code produced by step 5.
# (The locations of block comments located in step 1 are not
# applicable to the rewritten source.)
#
# 7. Repeat steps 3 through 5 for the list of block comments
# produced by step 6 (substituting "step 6" for "step 2").
getComments = R.pipe(
R.partialRight esprima.parse, comment: yes, loc: yes
R.prop 'comments'
)
[blockTests, lineTests] = R.pipe(
getComments
R.partition R.propEq 'type', 'Block'
R.map transformComments
) input
source = R.pipe(
R.append input: bookend
reduce [[], {line: 1, column: 0}], ([chunks, start], test) -> [
R.appendTo chunks, substring input, start, test.input.loc.start
(test.output ? test.input).loc.end
]
R.head
R.partialRight R.zip, R.append '', R.map wrap.js, lineTests
R.flatten
R.join ''
) lineTests
R.pipe(
getComments
R.filter R.propEq 'type', 'Block'
R.append bookend
toPairs
reduce [[], {line: 1, column: 0}], ([chunks, start], [idx, comment]) ->
R.pipe(
R.filter R.propEq 'commentIndex', idx
R.map wrap.js
R.join '\n'
R.appendTo R.append substring(source, start, comment.loc.start), chunks
R.of
R.append comment.loc.end
) blockTests
R.head
R.join ''
) source
rewrite.coffee = (input) ->
[literalChunks, commentChunks] = R.pipe(
R.match /.*\n/g
toPairs
R.reduce ([literalChunks, commentChunks, inCommentChunk], [idx, line]) ->
isComment = /^[ \t]*#(?!##)/.test line
current = if isComment then commentChunks else literalChunks
if isComment is inCommentChunk
current[current.length - 1].value += line
else
current[current.length] = value: line, loc: start: line: idx + 1
[literalChunks, commentChunks, isComment]
, [[value: '', loc: start: line: 1], [], no]
) input
matchLine = R.match /^([ \t]*)#[ \t]*(>|[.]*)(.*\n)/
testChunks = R.map R.pipe(
(commentChunk) -> R.pipe(
R.prop 'value'
R.match /.*\n/g
toPairs
reduce ['default', []], ([state, tests], [idx, line]) ->
[indent, prefix, value] = R.tail matchLine line
if prefix is '>'
tests[tests.length] = {indent, input: {value}}
['input', tests]
else if prefix
tests[tests.length - 1][state].value += value
[state, tests]
else if state is 'input'
loc = start: line: commentChunk.loc.start.line + idx
tests[tests.length - 1].output = {loc, value}
['output', tests]
else
['default', tests]
) commentChunk
R.last
R.map R.converge(
R.apply
R.pipe R.prop('indent'), R.length, indentN
R.pipe wrap.coffee, R.of
)
R.join '\n'
), commentChunks
R.pipe(
R.zip
R.flatten
R.join '\n'
CoffeeScript.compile
) R.pluck('value', literalChunks), R.append('', testChunks)
functionEval = (source) ->
# Functions created via the Function function are always run in the
# global context, which ensures that doctests can't access variables
# in _this_ context.
#
# The `evaluate` function takes one argument, named `__doctest`.
evaluate = Function '__doctest', source
queue = []
evaluate
input: (fn) -> queue.push [fn]
output: (num, fn) -> queue.push [fn, num]
run queue
commonjsEval = (source, path) ->
abspath = pathlib.resolve(path).replace(/[.][^.]+$/, "-#{Date.now()}.js")
fs.writeFileSync abspath, source
try
{queue} = require(abspath).__doctest
finally
fs.unlinkSync abspath
run queue
run = (queue) ->
results = []; input = noop
for arr in queue
switch arr.length
when 1
input()
input = arr[0]
when 2
actual = try input() catch error then error.constructor
expected = arr[0]()
results.push [
R.eqDeep actual, expected
repr expected
repr actual
arr[1]
]
input = noop
results
log = (results) ->
console.log R.join '', ((if pass then '.' else 'x') for [pass] in results)
for [pass, expected, actual, num] in results when not pass
console.log "FAIL: expected #{expected} on line #{num} (got #{actual})"
return
# > repr 'foo \\ bar \\ baz'
# '"foo \\\\ bar \\\\ baz"'
# > repr 'foo "bar" baz'
# '"foo \\"bar\\" baz"'
# > repr TypeError
# 'TypeError'
# > repr 42
# 42
repr = (val) -> switch Object::toString.call val
when '[object String]'
'"' + val.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'
when '[object Function]'
val.name
else
val