2013-02-25 23:30:23 -05:00
|
|
|
# Error Formating
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
# Ensure that errors of different kinds (lexer, parser and compiler) are shown
|
|
|
|
# in a consistent way.
|
|
|
|
|
|
|
|
assertErrorFormat = (code, expectedErrorFormat) ->
|
2013-03-21 02:11:31 -04:00
|
|
|
throws (-> CoffeeScript.run code), (err) ->
|
2013-07-31 07:27:49 -04:00
|
|
|
err.colorful = no
|
|
|
|
eq expectedErrorFormat, "#{err}"
|
2013-02-25 23:30:23 -05:00
|
|
|
yes
|
|
|
|
|
|
|
|
test "lexer errors formating", ->
|
|
|
|
assertErrorFormat '''
|
|
|
|
normalObject = {}
|
|
|
|
insideOutObject = }{
|
|
|
|
''',
|
|
|
|
'''
|
2013-07-31 07:27:49 -04:00
|
|
|
[stdin]:2:19: error: unmatched }
|
2013-02-25 23:30:23 -05:00
|
|
|
insideOutObject = }{
|
|
|
|
^
|
|
|
|
'''
|
|
|
|
|
|
|
|
test "parser error formating", ->
|
|
|
|
assertErrorFormat '''
|
|
|
|
foo in bar or in baz
|
|
|
|
''',
|
|
|
|
'''
|
2014-01-21 21:44:50 -05:00
|
|
|
[stdin]:1:15: error: unexpected in
|
2013-02-25 23:30:23 -05:00
|
|
|
foo in bar or in baz
|
|
|
|
^^
|
|
|
|
'''
|
|
|
|
|
|
|
|
test "compiler error formatting", ->
|
|
|
|
assertErrorFormat '''
|
|
|
|
evil = (foo, eval, bar) ->
|
|
|
|
''',
|
|
|
|
'''
|
2013-07-31 07:27:49 -04:00
|
|
|
[stdin]:1:14: error: parameter name "eval" is not allowed
|
2013-02-25 23:30:23 -05:00
|
|
|
evil = (foo, eval, bar) ->
|
|
|
|
^^^^
|
2013-03-21 02:11:31 -04:00
|
|
|
'''
|
|
|
|
|
2013-06-13 15:54:20 -04:00
|
|
|
test "patchStackTrace line patching", ->
|
|
|
|
err = new Error 'error'
|
2013-06-22 08:57:23 -04:00
|
|
|
ok err.stack.match /test[\/\\]error_messages\.coffee:\d+:\d+\b/
|
2013-06-13 15:54:20 -04:00
|
|
|
|
2013-06-22 08:57:23 -04:00
|
|
|
fs = require 'fs'
|
|
|
|
path = require 'path'
|
2013-03-21 02:11:31 -04:00
|
|
|
|
|
|
|
test "#2849: compilation error in a require()d file", ->
|
|
|
|
# Create a temporary file to require().
|
|
|
|
ok not fs.existsSync 'test/syntax-error.coffee'
|
|
|
|
fs.writeFileSync 'test/syntax-error.coffee', 'foo in bar or in baz'
|
|
|
|
|
|
|
|
try
|
|
|
|
assertErrorFormat '''
|
|
|
|
require './test/syntax-error'
|
|
|
|
''',
|
|
|
|
"""
|
2014-01-21 21:44:50 -05:00
|
|
|
#{path.join __dirname, 'syntax-error.coffee'}:1:15: error: unexpected in
|
2013-03-21 02:11:31 -04:00
|
|
|
foo in bar or in baz
|
|
|
|
^^
|
|
|
|
"""
|
|
|
|
finally
|
2014-01-21 21:44:50 -05:00
|
|
|
fs.unlink 'test/syntax-error.coffee'
|
|
|
|
|
|
|
|
test "#1096: unexpected generated tokens", ->
|
|
|
|
# Unexpected interpolation
|
|
|
|
assertErrorFormat '{"#{key}": val}', '''
|
|
|
|
[stdin]:1:3: error: unexpected string interpolation
|
|
|
|
{"#{key}": val}
|
|
|
|
^^
|
|
|
|
'''
|
|
|
|
# Implicit ends
|
|
|
|
assertErrorFormat 'a:, b', '''
|
|
|
|
[stdin]:1:3: error: unexpected ,
|
|
|
|
a:, b
|
|
|
|
^
|
|
|
|
'''
|
|
|
|
# Explicit ends
|
|
|
|
assertErrorFormat '(a:)', '''
|
|
|
|
[stdin]:1:4: error: unexpected )
|
|
|
|
(a:)
|
|
|
|
^
|
|
|
|
'''
|
|
|
|
# Unexpected end of file
|
|
|
|
assertErrorFormat 'a:', '''
|
|
|
|
[stdin]:1:3: error: unexpected end of input
|
|
|
|
a:
|
|
|
|
^
|
|
|
|
'''
|
|
|
|
# Unexpected implicit object
|
|
|
|
assertErrorFormat '''
|
|
|
|
for i in [1]:
|
|
|
|
1
|
|
|
|
''', '''
|
|
|
|
[stdin]:1:13: error: unexpected :
|
|
|
|
for i in [1]:
|
|
|
|
^
|
|
|
|
'''
|
2014-01-26 00:25:13 -05:00
|
|
|
|
|
|
|
test "#3325: implicit indentation errors", ->
|
|
|
|
assertErrorFormat '''
|
|
|
|
i for i in a then i
|
|
|
|
''', '''
|
|
|
|
[stdin]:1:14: error: unexpected then
|
|
|
|
i for i in a then i
|
|
|
|
^^^^
|
|
|
|
'''
|
|
|
|
|
|
|
|
test "explicit indentation errors", ->
|
|
|
|
assertErrorFormat '''
|
|
|
|
a = b
|
|
|
|
c
|
|
|
|
''', '''
|
|
|
|
[stdin]:2:1: error: unexpected indentation
|
|
|
|
c
|
|
|
|
^^
|
|
|
|
'''
|