2013-02-25 03:12:22 -05:00
|
|
|
return if global.testingBrowser
|
|
|
|
|
2013-03-25 11:47:34 -04:00
|
|
|
fs = require 'fs'
|
|
|
|
|
2011-01-03 04:28:47 -05:00
|
|
|
# REPL
|
|
|
|
# ----
|
2013-01-21 13:02:04 -05:00
|
|
|
Stream = require 'stream'
|
2011-01-03 04:28:47 -05:00
|
|
|
|
2013-01-21 13:02:04 -05:00
|
|
|
class MockInputStream extends Stream
|
2013-01-26 03:07:56 -05:00
|
|
|
constructor: ->
|
2013-01-21 13:02:04 -05:00
|
|
|
@readable = true
|
|
|
|
|
|
|
|
resume: ->
|
|
|
|
|
|
|
|
emitLine: (val) ->
|
|
|
|
@emit 'data', new Buffer("#{val}\n")
|
|
|
|
|
|
|
|
class MockOutputStream extends Stream
|
2013-01-26 03:07:56 -05:00
|
|
|
constructor: ->
|
2013-01-21 13:02:04 -05:00
|
|
|
@writable = true
|
|
|
|
@written = []
|
|
|
|
|
|
|
|
write: (data) ->
|
|
|
|
#console.log 'output write', arguments
|
|
|
|
@written.push data
|
|
|
|
|
2013-01-26 03:07:56 -05:00
|
|
|
lastWrite: (fromEnd = -1) ->
|
2013-01-21 13:02:04 -05:00
|
|
|
@written[@written.length - 1 + fromEnd].replace /\n$/, ''
|
|
|
|
|
2013-03-25 11:47:34 -04:00
|
|
|
# Create a dummy history file
|
|
|
|
historyFile = '.coffee_history_test'
|
|
|
|
fs.writeFileSync historyFile, '1 + 2\n'
|
2013-01-26 03:07:56 -05:00
|
|
|
|
2013-01-21 13:02:04 -05:00
|
|
|
testRepl = (desc, fn) ->
|
2013-01-26 03:07:56 -05:00
|
|
|
input = new MockInputStream
|
|
|
|
output = new MockOutputStream
|
2013-03-25 11:47:34 -04:00
|
|
|
repl = Repl.start {input, output, historyFile}
|
|
|
|
test desc, -> fn input, output, repl
|
2013-01-21 13:02:04 -05:00
|
|
|
|
2013-01-26 03:07:56 -05:00
|
|
|
ctrlV = { ctrl: true, name: 'v'}
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
|
2013-03-25 11:47:34 -04:00
|
|
|
testRepl 'reads history file', (input, output, repl) ->
|
|
|
|
input.emitLine repl.rli.history[0]
|
|
|
|
eq '3', output.lastWrite()
|
|
|
|
|
2013-01-21 13:02:04 -05:00
|
|
|
testRepl "starts with coffee prompt", (input, output) ->
|
2013-01-26 03:07:56 -05:00
|
|
|
eq 'coffee> ', output.lastWrite(0)
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "writes eval to output", (input, output) ->
|
|
|
|
input.emitLine '1+1'
|
2013-01-26 03:07:56 -05:00
|
|
|
eq '2', output.lastWrite()
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "comments are ignored", (input, output) ->
|
|
|
|
input.emitLine '1 + 1 #foo'
|
2013-01-26 03:07:56 -05:00
|
|
|
eq '2', output.lastWrite()
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "output in inspect mode", (input, output) ->
|
|
|
|
input.emitLine '"1 + 1\\n"'
|
2013-01-26 03:07:56 -05:00
|
|
|
eq "'1 + 1\\n'", output.lastWrite()
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "variables are saved", (input, output) ->
|
|
|
|
input.emitLine "foo = 'foo'"
|
|
|
|
input.emitLine 'foobar = "#{foo}bar"'
|
2013-01-26 03:07:56 -05:00
|
|
|
eq "'foobar'", output.lastWrite()
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "empty command evaluates to undefined", (input, output) ->
|
|
|
|
input.emitLine ''
|
2013-01-26 03:07:56 -05:00
|
|
|
eq 'undefined', output.lastWrite()
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "ctrl-v toggles multiline prompt", (input, output) ->
|
|
|
|
input.emit 'keypress', null, ctrlV
|
2013-01-26 03:07:56 -05:00
|
|
|
eq '------> ', output.lastWrite(0)
|
2013-01-21 13:02:04 -05:00
|
|
|
input.emit 'keypress', null, ctrlV
|
2013-01-26 03:07:56 -05:00
|
|
|
eq 'coffee> ', output.lastWrite(0)
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "multiline continuation changes prompt", (input, output) ->
|
|
|
|
input.emit 'keypress', null, ctrlV
|
|
|
|
input.emitLine ''
|
2013-01-26 03:07:56 -05:00
|
|
|
eq '....... ', output.lastWrite(0)
|
2013-01-21 13:02:04 -05:00
|
|
|
|
|
|
|
testRepl "evaluates multiline", (input, output) ->
|
|
|
|
# Stubs. Could assert on their use.
|
|
|
|
output.cursorTo = (pos) ->
|
|
|
|
output.clearLine = ->
|
|
|
|
|
|
|
|
input.emit 'keypress', null, ctrlV
|
2013-01-26 03:07:56 -05:00
|
|
|
input.emitLine 'do ->'
|
2013-01-21 13:02:04 -05:00
|
|
|
input.emitLine ' 1 + 1'
|
|
|
|
input.emit 'keypress', null, ctrlV
|
2013-01-26 03:07:56 -05:00
|
|
|
eq '2', output.lastWrite()
|
2013-03-11 19:16:48 -04:00
|
|
|
|
|
|
|
testRepl "variables in scope are preserved", (input, output) ->
|
|
|
|
input.emitLine 'a = 1'
|
|
|
|
input.emitLine 'do -> a = 2'
|
|
|
|
input.emitLine 'a'
|
|
|
|
eq '2', output.lastWrite()
|
|
|
|
|
2013-03-11 22:48:21 -04:00
|
|
|
testRepl "existential assignment of previously declared variable", (input, output) ->
|
2013-03-11 19:16:48 -04:00
|
|
|
input.emitLine 'a = null'
|
|
|
|
input.emitLine 'a ?= 42'
|
2013-03-11 22:48:21 -04:00
|
|
|
eq '42', output.lastWrite()
|
2013-03-11 20:31:17 -04:00
|
|
|
|
|
|
|
testRepl "keeps running after runtime error", (input, output) ->
|
|
|
|
input.emitLine 'a = b'
|
|
|
|
eq 0, output.lastWrite().indexOf 'ReferenceError: b is not defined'
|
|
|
|
input.emitLine 'a'
|
|
|
|
eq 'undefined', output.lastWrite()
|
2013-03-25 11:47:34 -04:00
|
|
|
|
2013-03-25 12:49:59 -04:00
|
|
|
process.on 'exit', ->
|
2013-03-25 11:47:34 -04:00
|
|
|
fs.unlinkSync historyFile
|