Skip to content

Commit

Permalink
Refactor test.html to be part of the docs output, with the tests embe…
Browse files Browse the repository at this point in the history
…dded inside it; update test.html styles; move UTF-8 comment test out of test.html and into test/comments.coffee where it belongs
  • Loading branch information
GeoffreyBooth committed Nov 30, 2016
1 parent 6d29086 commit 06b3180
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 122 deletions.
29 changes: 29 additions & 0 deletions Cakefile
Expand Up @@ -183,6 +183,25 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
</b>
"""

testsInScriptBlocks = ->
output = ''
for filename in fs.readdirSync 'test'
if filename.indexOf('.coffee') isnt -1
type = 'coffeescript'
else if filename.indexOf('.litcoffee') isnt -1
type = 'literate-coffeescript'
else
continue

# Set the type to text/x-coffeescript or text/x-literate-coffeescript
# to prevent the browser compiler from automatically running the script
output += """
<script type="text/x-#{type}" class="test" id="#{filename.split('.')[0]}">
#{fs.readFileSync "test/#{filename}", 'utf-8'}
</script>\n
"""
output

# Task
examplesSourceFolder = 'documentation/examples'
examplesOutputFolder = "docs/v#{majorVersion}/examples"
Expand All @@ -200,10 +219,20 @@ task 'doc:site', 'watch and continually rebuild the documentation for the websit
fs.writeFileSync "docs/v#{majorVersion}/index.html", output
log 'compiled', green, "#{indexFile} → docs/v#{majorVersion}/index.html"

testFile = 'documentation/test.html'
do renderTest = ->
render = _.template fs.readFileSync(testFile, 'utf-8')
output = render
tests: testsInScriptBlocks()
majorVersion: majorVersion
fs.writeFileSync "docs/v#{majorVersion}/test.html", output
log 'compiled', green, "#{testFile} → docs/v#{majorVersion}/test.html"

fs.watch examplesSourceFolder, interval: 200, ->
renderExamples()
renderIndex()
fs.watch indexFile, interval: 200, renderIndex
fs.watch testFile, interval: 200, renderTest
log 'watching...' , green


Expand Down
1 change: 0 additions & 1 deletion docs/current

This file was deleted.

174 changes: 79 additions & 95 deletions documentation/test.html
Expand Up @@ -3,119 +3,103 @@
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>CoffeeScript Test Suite</title>
<script src="../docs/current/browser-compiler/coffee-script.js"></script>
<script src="browser-compiler/coffee-script.js"></script>
<style>
body, pre {
font-family: Consolas, Menlo, Monaco, monospace;
}
body {
margin: 30px;
font-family: Menlo, Monaco, monospace;
margin: 1em;
}
h1 {
font-size: 20px;
font-size: 1.3em;
}
div {
margin: 0.6em;
}
.good {
color: #22b24c
}
#stdout {
.bad {
color: #eb6864
}
</style>
</head>
<body>

<h1>CoffeeScript Test Suite</h1>
<pre id="stdout"></pre>
<h1>CoffeeScript Test Suite</h1>

<pre id="stdout"></pre>

<script type="text/coffeescript">
stdout = document.getElementById 'stdout'
start = new Date
success = total = done = failed = 0

<script type="text/coffeescript">
say = (msg, emphasis) ->
div = document.createElement 'div'
if emphasis?
div.className = if emphasis then 'good' else 'bad'
div.appendChild document.createTextNode msg
stdout.appendChild div
msg

stdout = document.getElementById 'stdout'
start = new Date
success = total = done = failed = 0
@test = (desc, fn) ->
fn()

say = (msg) ->
div = document.createElement 'div'
div.appendChild document.createTextNode msg
stdout.appendChild div
msg
@ok = (good, msg) ->
++total
if good then ++success else throw Error say msg, no

@test = (desc, fn) ->
fn()
@eq = (x, y, msg) -> ok x is y, msg ? x + ' isnt ' + y

@ok = (good, msg) ->
++total
if good then ++success else throw Error say msg
arrayEqual = (a, b) ->
if a is b
# 0 isnt -0
a isnt 0 or 1/a is 1/b
else if a instanceof Array and b instanceof Array
return no unless a.length is b.length
return no for el, idx in a when not arrayEq el, b[idx]
yes
else
# NaN is NaN
a isnt a and b isnt b

@eq = (x, y, msg) -> ok x is y, msg ? x + ' !== ' + y
@doesNotThrow = (fn) ->
fn()
ok true

arrayEqual = (a, b) ->
if a is b
# 0 isnt -0
a isnt 0 or 1/a is 1/b
else if a instanceof Array and b instanceof Array
return no unless a.length is b.length
return no for el, idx in a when not arrayEq el, b[idx]
yes
@arrayEq = (a, b, msg) -> ok arrayEqual(a,b), msg

@throws = (fun, err, msg) ->
try
fun()
catch e
if err
eq e, err
else
# NaN is NaN
a isnt a and b isnt b

@doesNotThrow = (fn) ->
fn()
ok true

@arrayEq = (a, b, msg) -> ok arrayEqual(a,b), msg

@throws = (fun, err, msg) ->
try
fun()
catch e
if err
eq e, err
else
ok yes
return
ok no

run = (name) ->
CoffeeScript.load "#{name}.coffee", ->
say '\u2714 ' + name
fin() if ++done is names.length

fin = ->
yay = success is total and not failed
sec = (new Date - start) / 1000
msg = "passed #{success} tests in #{ sec.toFixed 2 } seconds"
msg = "failed #{ total - success } tests and #{msg}" unless yay
say msg, yay

run name for name in names = [
'arrays'
'assignment'
'booleans'
'classes'
'cluster'
'comments'
'compilation'
'comprehensions'
'control_flow'
'exception_handling'
'formatting'
'function_invocation'
'functions'
'helpers'
'importing'
'interpolation'
'javascript_literals'
'modules'
'numbers'
'objects'
'operators'
'option_parser'
'ranges'
'regexps'
'scope'
'slicing_and_splicing'
'soaks'
'strings'
]
# allow utf-8 chars in comments
# 智に働けば角が立つ、情に掉させば流される。
</script>
ok yes
return
ok no


# Run the tests
for test in document.getElementsByClassName 'test'
say '\u2714 ' + test.id
try
CoffeeScript.run test.innerHTML
catch exception
console.error exception

# Finish up
yay = success is total and not failed
sec = (new Date - start) / 1000
msg = "passed #{success} tests in #{ sec.toFixed 2 } seconds"
msg = "failed #{ total - success } tests and #{msg}" unless yay
say msg, yay
</script>

<%= tests %>

</body>
</html>
56 changes: 30 additions & 26 deletions test/comments.coffee
Expand Up @@ -293,25 +293,25 @@ test "#3132: Format jsdoc-style block-comment nicely", ->
input = """
###*
# Multiline for jsdoc-"@doctags"
#
#
# @type {Function}
###
fn = () -> 1
"""

result = """
/**
* Multiline for jsdoc-"@doctags"
*
*
* @type {Function}
*/
var fn;
fn = function() {
return 1;
};
"""
eq CoffeeScript.compile(input, bare: on), result

Expand All @@ -321,25 +321,25 @@ test "#3132: Format hand-made (raw) jsdoc-style block-comment nicely", ->
input = """
###*
* Multiline for jsdoc-"@doctags"
*
*
* @type {Function}
###
fn = () -> 1
"""

result = """
/**
* Multiline for jsdoc-"@doctags"
*
*
* @type {Function}
*/
var fn;
fn = function() {
return 1;
};
"""
eq CoffeeScript.compile(input, bare: on), result

Expand All @@ -349,54 +349,54 @@ test "#3132: Place block-comments nicely", ->
input = """
###*
# A dummy class definition
#
#
# @class
###
class DummyClass
###*
# @constructor
###
constructor: ->
###*
# Singleton reference
#
#
# @type {DummyClass}
###
@instance = new DummyClass()
"""

result = """
/**
* A dummy class definition
*
*
* @class
*/
var DummyClass;
DummyClass = (function() {
/**
* @constructor
*/
function DummyClass() {}
/**
* Singleton reference
*
*
* @type {DummyClass}
*/
DummyClass.instance = new DummyClass();
return DummyClass;
})();
"""
eq CoffeeScript.compile(input, bare: on), result

Expand Down Expand Up @@ -427,3 +427,7 @@ test "#3761: Multiline comment at end of an object", ->
###

ok anObject.x is 3

test "#4375: UTF-8 characters in comments", ->
# 智に働けば角が立つ、情に掉させば流される。
ok yes

0 comments on commit 06b3180

Please sign in to comment.