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-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-01-21 13:02:04 -05:00
|
|
|
Repl.start {input, output}
|
2013-01-26 03:07:56 -05:00
|
|
|
test desc, -> fn input, output
|
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
|
|
|
|
|
|
|
|
|
|
|
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()
|