-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathtest.coffee
More file actions
134 lines (96 loc) · 2.91 KB
/
test.coffee
File metadata and controls
134 lines (96 loc) · 2.91 KB
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
require('coffee-script').register()
fs = require 'fs'
{exec} = require 'child_process'
coffeeCompile = require('coffee-script').compile
require 'colors'
jsdiff = require 'diff'
if process.env.DEBUG
Parser = require '../src/parser'
serialise = require '../src/serialiser'
transform = (code, opts) ->
parseTree = new Parser().parse(code, opts)
console.log(JSON.stringify(parseTree, null, 2))
serialise(parseTree)
else
transform = require '../'
tryTransform = (input, options, desc) ->
try
transformed = transform input, options
catch e
e.message = """
transform error in testcase: #{desc}
#{e.stack}
"""
throw new Error(e.message)
transformed
tryCompile = (input, options, desc) ->
try
compiled = coffeeCompile input, options
catch e
e.message = """
compile error in testcase: #{desc}
#{input}
#{e.stack}
"""
throw new Error(e.message)
compiled
run = ->
runTestcases 'output', "#{__dirname}/output-testcases.txt"
testTypes =
'output':
params: ['desc','input','expected']
runner: (testcase) ->
options = literate: testcase.desc.match /literate/
transformed = tryTransform testcase.input, options, testcase.desc
compiled = tryCompile transformed, options, testcase.desc
# simple assertion of string equality of expected output and actual output
pass = transformed is testcase.expected
diff = if not pass or process.env.VERBOSE
jsdiff.diffChars(testcase.expected, transformed).map((part) ->
color = (if part.added then "green" else (if part.removed then "red" else "grey"))
text = part.value
.replace(/[ ]/g, '█')
.replace(/\n/g, '\n')
text[color]
).join('')
message =
"""
#{testcase.desc}
--- input ---
#{testcase.input}
--- Expected output ---
#{testcase.expected}
--- Actual output ---
#{transformed}
--- Diff ---
#{diff}
"""
console.assert pass, message
if process.env.VERBOSE
console.log message+"""
--- Compiled output ---
#{compiled}
"""
generateTestcasesParser = (params) ->
testcaseMatcher = do ->
paramMatchers = for param in params
"###{param}[ ]*?\\n([\\s\\S]*?)?\\n"
///
#{paramMatchers.join('')}
\#\#end
///gm
(input) ->
while testcase = testcaseMatcher.exec(input)
output = {}
for paramContents, index in testcase[1..]
output[params[index]] = paramContents
output
runTestcases = (type, filepath) ->
parseTestcases = generateTestcasesParser(testTypes[type].params)
testcases = parseTestcases(fs.readFileSync(filepath))
console.time("#{type} tests passed")
for testcase in testcases
# console.log "#{type} #{testcase.desc}"
testTypes[type].runner(testcase)
console.timeEnd("#{type} tests passed")
run() # begin