Skip to content

Commit 5588658

Browse files
authored
Merge pull request #4378 from GeoffreyBooth/fix-browser-tests
Fix browser tests
2 parents ac26360 + d99ae0e commit 5588658

File tree

7 files changed

+256
-221
lines changed

7 files changed

+256
-221
lines changed

Cakefile

Lines changed: 95 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -45,60 +45,6 @@ run = (args, cb) ->
4545
log = (message, color, explanation) ->
4646
console.log color + message + reset + ' ' + (explanation or '')
4747

48-
codeFor = ->
49-
counter = 0
50-
hljs = require 'highlight.js'
51-
hljs.configure classPrefix: ''
52-
(file, executable = false, showLoad = true) ->
53-
counter++
54-
return unless fs.existsSync "docs/v#{majorVersion}/examples/#{file}.js"
55-
cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8'
56-
js = fs.readFileSync "docs/v#{majorVersion}/examples/#{file}.js", 'utf-8'
57-
js = js.replace /^\/\/ generated.*?\n/i, ''
58-
59-
cshtml = "<pre><code>#{hljs.highlight('coffeescript', cs).value}</code></pre>"
60-
# Temporary fix until highlight.js adds support for newer CoffeeScript keywords
61-
# Added in https://github.com/isagalaev/highlight.js/pull/1357, awaiting release
62-
if file in ['generator_iteration', 'generators', 'modules']
63-
cshtml = cshtml.replace /(yield|import|export|from|as|default) /g, '<span class="keyword">$1</span> '
64-
jshtml = "<pre><code>#{hljs.highlight('javascript', js).value}</code></pre>"
65-
append = if executable is yes then '' else "alert(#{executable});"
66-
if executable and executable isnt yes
67-
cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}"
68-
run = if executable is true then 'run' else "run: #{executable}"
69-
name = "example#{counter}"
70-
script = "<script>window.#{name} = #{JSON.stringify cs}</script>"
71-
load = if showLoad then "<div class='minibutton load' onclick='javascript: loadConsole(#{name});'>load</div>" else ''
72-
button = if executable then "<div class='minibutton ok' onclick='javascript: #{js};#{append}'>#{run}</div>" else ''
73-
"<div class='code'>#{cshtml}#{jshtml}#{script}#{load}#{button}<br class='clear' /></div>"
74-
75-
monthNames = [
76-
'January'
77-
'February'
78-
'March'
79-
'April'
80-
'May'
81-
'June'
82-
'July'
83-
'August'
84-
'September'
85-
'October'
86-
'November'
87-
'December'
88-
]
89-
90-
formatDate = (date) ->
91-
date.replace /^(\d\d\d\d)-(\d\d)-(\d\d)$/, (match, $1, $2, $3) ->
92-
"#{monthNames[$2 - 1]} #{+$3}, #{$1}"
93-
94-
releaseHeader = (date, version, prevVersion) -> """
95-
<div class="anchor" id="#{version}"></div>
96-
<b class="header">
97-
#{prevVersion and "<a href=\"https://github.com/jashkenas/coffeescript/compare/#{prevVersion}...#{version}\">#{version}</a>" or version}
98-
<span class="timestamp"> &mdash; <time datetime="#{date}">#{formatDate date}</time></span>
99-
</b>
100-
"""
101-
10248
option '-p', '--prefix [DIR]', 'set the installation prefix for `cake install`'
10349

10450
task 'install', 'install CoffeeScript into /usr/local (or --prefix)', (options) ->
@@ -137,7 +83,7 @@ task 'build:full', 'rebuild the source twice, and run the tests', ->
13783

13884

13985
task 'build:parser', 'rebuild the Jison parser (run build first)', ->
140-
helpers.extend global, require('util')
86+
helpers.extend global, require 'util'
14187
require 'jison'
14288
parser = require('./lib/coffee-script/grammar').parser
14389
fs.writeFileSync 'lib/coffee-script/parser.js', parser.generate()
@@ -182,13 +128,93 @@ task 'build:browser', 'rebuild the merged script for inclusion in the browser',
182128

183129

184130
task 'doc:site', 'watch and continually rebuild the documentation for the website', ->
131+
# Helpers
132+
codeFor = ->
133+
counter = 0
134+
hljs = require 'highlight.js'
135+
hljs.configure classPrefix: ''
136+
(file, executable = false, showLoad = true) ->
137+
counter++
138+
return unless fs.existsSync "docs/v#{majorVersion}/examples/#{file}.js"
139+
cs = fs.readFileSync "documentation/examples/#{file}.coffee", 'utf-8'
140+
js = fs.readFileSync "docs/v#{majorVersion}/examples/#{file}.js", 'utf-8'
141+
js = js.replace /^\/\/ generated.*?\n/i, ''
142+
143+
cshtml = "<pre><code>#{hljs.highlight('coffeescript', cs).value}</code></pre>"
144+
# Temporary fix until highlight.js adds support for newer CoffeeScript keywords
145+
# Added in https://github.com/isagalaev/highlight.js/pull/1357, awaiting release
146+
if file in ['generator_iteration', 'generators', 'modules']
147+
cshtml = cshtml.replace /(yield|import|export|from|as|default) /g, '<span class="keyword">$1</span> '
148+
jshtml = "<pre><code>#{hljs.highlight('javascript', js).value}</code></pre>"
149+
append = if executable is yes then '' else "alert(#{executable});"
150+
if executable and executable isnt yes
151+
cs.replace /(\S)\s*\Z/m, "$1\n\nalert #{executable}"
152+
run = if executable is true then 'run' else "run: #{executable}"
153+
name = "example#{counter}"
154+
script = "<script>window.#{name} = #{JSON.stringify cs}</script>"
155+
load = if showLoad then "<div class='minibutton load' onclick='javascript: loadConsole(#{name});'>load</div>" else ''
156+
button = if executable then "<div class='minibutton ok' onclick='javascript: #{js};#{append}'>#{run}</div>" else ''
157+
"<div class='code'>#{cshtml}#{jshtml}#{script}#{load}#{button}<br class='clear' /></div>"
158+
159+
monthNames = [
160+
'January'
161+
'February'
162+
'March'
163+
'April'
164+
'May'
165+
'June'
166+
'July'
167+
'August'
168+
'September'
169+
'October'
170+
'November'
171+
'December'
172+
]
173+
174+
formatDate = (date) ->
175+
date.replace /^(\d\d\d\d)-(\d\d)-(\d\d)$/, (match, $1, $2, $3) ->
176+
"#{monthNames[$2 - 1]} #{+$3}, #{$1}"
177+
178+
releaseHeader = (date, version, prevVersion) -> """
179+
<div class="anchor" id="#{version}"></div>
180+
<b class="header">
181+
#{prevVersion and "<a href=\"https://github.com/jashkenas/coffeescript/compare/#{prevVersion}...#{version}\">#{version}</a>" or version}
182+
<span class="timestamp"> &mdash; <time datetime="#{date}">#{formatDate date}</time></span>
183+
</b>
184+
"""
185+
186+
testHelpers = fs.readFileSync('test/support/helpers.coffee', 'utf-8').replace /exports\./g, '@'
187+
188+
testsInScriptBlocks = ->
189+
output = ''
190+
excludedTestFiles = ['error_messages.coffee']
191+
for filename in fs.readdirSync 'test'
192+
continue if filename in excludedTestFiles
193+
194+
if filename.indexOf('.coffee') isnt -1
195+
type = 'coffeescript'
196+
else if filename.indexOf('.litcoffee') isnt -1
197+
type = 'literate-coffeescript'
198+
else
199+
continue
200+
201+
# Set the type to text/x-coffeescript or text/x-literate-coffeescript
202+
# to prevent the browser compiler from automatically running the script
203+
output += """
204+
<script type="text/x-#{type}" class="test" id="#{filename.split('.')[0]}">
205+
#{fs.readFileSync "test/#{filename}", 'utf-8'}
206+
</script>\n
207+
"""
208+
output
209+
210+
# Task
185211
examplesSourceFolder = 'documentation/examples'
186212
examplesOutputFolder = "docs/v#{majorVersion}/examples"
187213
fs.mkdirSync examplesOutputFolder unless fs.existsSync examplesOutputFolder
188214
do renderExamples = ->
189215
execSync "bin/coffee -bc -o #{examplesOutputFolder} #{examplesSourceFolder}/*.coffee"
190216

191-
indexFile = 'documentation/index.html.js'
217+
indexFile = 'documentation/index.html'
192218
do renderIndex = ->
193219
render = _.template fs.readFileSync(indexFile, 'utf-8')
194220
output = render
@@ -198,10 +224,22 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
198224
fs.writeFileSync "docs/v#{majorVersion}/index.html", output
199225
log 'compiled', green, "#{indexFile} → docs/v#{majorVersion}/index.html"
200226

227+
testFile = 'documentation/test.html'
228+
do renderTest = ->
229+
render = _.template fs.readFileSync(testFile, 'utf-8')
230+
output = render
231+
testHelpers: testHelpers
232+
tests: testsInScriptBlocks()
233+
majorVersion: majorVersion
234+
fs.writeFileSync "docs/v#{majorVersion}/test.html", output
235+
log 'compiled', green, "#{testFile} → docs/v#{majorVersion}/test.html"
236+
201237
fs.watch examplesSourceFolder, interval: 200, ->
202238
renderExamples()
203239
renderIndex()
204240
fs.watch indexFile, interval: 200, renderIndex
241+
fs.watch testFile, interval: 200, renderTest
242+
fs.watch 'test', interval: 200, renderTest
205243
log 'watching...' , green
206244

207245

@@ -258,23 +296,7 @@ runTests = (CoffeeScript) ->
258296
description: description if description?
259297
source: fn.toString() if fn.toString?
260298

261-
# See http://wiki.ecmascript.org/doku.php?id=harmony:egal
262-
egal = (a, b) ->
263-
if a is b
264-
a isnt 0 or 1/a is 1/b
265-
else
266-
a isnt a and b isnt b
267-
268-
# A recursive functional equivalence helper; uses egal for testing equivalence.
269-
arrayEgal = (a, b) ->
270-
if egal a, b then yes
271-
else if a instanceof Array and b instanceof Array
272-
return no unless a.length is b.length
273-
return no for el, idx in a when not arrayEgal el, b[idx]
274-
yes
275-
276-
global.eq = (a, b, msg) -> ok egal(a, b), msg ? "Expected #{a} to equal #{b}"
277-
global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg ? "Expected #{a} to deep equal #{b}"
299+
helpers.extend global, require './test/support/helpers'
278300

279301
# When all the tests have run, collect and print errors.
280302
# If a stacktrace is available, output the compiled function source.

docs/current

Lines changed: 0 additions & 1 deletion
This file was deleted.
File renamed without changes.

documentation/test.html

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
5+
<title>CoffeeScript Test Suite</title>
6+
<script src="browser-compiler/coffee-script.js"></script>
7+
<script src="https://cdn.jsdelivr.net/underscorejs/1.8.3/underscore-min.js"></script>
8+
<style>
9+
body, pre {
10+
font-family: Consolas, Menlo, Monaco, monospace;
11+
}
12+
body {
13+
margin: 1em;
14+
}
15+
h1 {
16+
font-size: 1.3em;
17+
}
18+
div {
19+
margin: 0.6em;
20+
}
21+
.good {
22+
color: #22b24c
23+
}
24+
.bad {
25+
color: #eb6864
26+
}
27+
.subtle {
28+
font-size: 0.7em;
29+
color: #999999
30+
}
31+
</style>
32+
</head>
33+
<body>
34+
35+
<h1>CoffeeScript Test Suite</h1>
36+
37+
<pre id="stdout"></pre>
38+
39+
<script type="text/coffeescript">
40+
@testingBrowser = yes
41+
@global = window
42+
stdout = document.getElementById 'stdout'
43+
start = new Date
44+
success = total = done = failed = 0
45+
46+
say = (msg, className) ->
47+
div = document.createElement 'div'
48+
div.className = className if className?
49+
div.appendChild document.createTextNode msg
50+
stdout.appendChild div
51+
msg
52+
53+
@test = (description, fn) ->
54+
++total
55+
try
56+
fn.call(fn)
57+
++success
58+
catch exception
59+
say "#{description}:", 'bad'
60+
say fn.toString(), 'subtle' if fn.toString?
61+
say exception, 'bad'
62+
console.error exception
63+
64+
@ok = (good, msg = 'Error') ->
65+
throw Error msg unless good
66+
67+
# Polyfill Node assert's fail
68+
@fail = ->
69+
ok no
70+
71+
# Polyfill Node assert's deepEqual with Underscore's isEqual
72+
@deepEqual = (a, b) ->
73+
ok _.isEqual(a, b), "Expected #{JSON.stringify a} to deep equal #{JSON.stringify b}"
74+
75+
<%= testHelpers %>
76+
77+
@doesNotThrow = (fn) ->
78+
fn()
79+
ok yes
80+
81+
@throws = (fun, err, msg) ->
82+
try
83+
fun()
84+
catch e
85+
if err
86+
if typeof err is 'function' and e instanceof err # Handle comparing exceptions
87+
ok yes
88+
else
89+
eq e, err
90+
else
91+
ok yes
92+
return
93+
ok no
94+
95+
96+
# Run the tests
97+
for test in document.getElementsByClassName 'test'
98+
say '\u2714 ' + test.id
99+
options = {}
100+
options.literate = yes if test.type is 'text/x-literate-coffeescript'
101+
CoffeeScript.run test.innerHTML, options
102+
103+
# Finish up
104+
yay = success is total and not failed
105+
sec = (new Date - start) / 1000
106+
msg = "passed #{success} tests in #{ sec.toFixed 2 } seconds"
107+
msg = "failed #{ total - success } tests and #{msg}" unless yay
108+
say msg, (if yay then 'good' else 'bad')
109+
</script>
110+
111+
<%= tests %>
112+
113+
</body>
114+
</html>

0 commit comments

Comments
 (0)