1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00

final tweaks to REPL rewrite

We can still use some more extensive tests, but it's already much better
tested than the current REPL.
This commit is contained in:
Michael Ficarra 2013-01-26 02:07:56 -06:00
parent 702071553f
commit be9707f8d2
4 changed files with 43 additions and 48 deletions

View file

@ -200,8 +200,8 @@ runTests = (CoffeeScript) ->
return no for el, idx in a when not arrayEgal el, b[idx]
yes
global.eq = (a, b, msg) -> ok egal(a, b), msg
global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg
global.eq = (a, b, msg) -> ok egal(a, b), msg ? "Expected #{a} to eqaul #{b}"
global.arrayEq = (a, b, msg) -> ok arrayEgal(a,b), msg ? "Expected #{a} to deep eqaul #{b}"
# When all the tests have run, collect and print errors.
# If a stacktrace is available, output the compiled function source.

View file

@ -14,21 +14,20 @@
prompt: 'coffee> ',
"eval": function(input, context, filename, cb) {
var js;
input = input.replace(/\uFF00/g, '\n');
input = input.replace(/(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3');
if (/^\s*$/.test(input)) {
return cb(null);
}
try {
input = input.replace(/\uFF00/g, '\n');
input = input.slice(1, -1);
input = input.replace(/(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3');
if (/^\s*$/.test(input)) {
return cb(null);
}
js = CoffeeScript.compile("[]=(" + input + "\n)", {
js = CoffeeScript.compile("_=(" + input + "\n)", {
filename: filename,
bare: true
});
return cb(null, vm.runInContext(js, context, filename));
} catch (err) {
return cb(err);
cb(err);
}
return cb(null, vm.runInContext(js, context, filename));
}
};
@ -67,7 +66,7 @@
rli.prompt(true);
return;
}
if (!((rli.line == null) || rli.line.match(/^\s*$/))) {
if ((rli.line != null) && !rli.line.match(/^\s*$/)) {
return;
}
multiline.enabled = !multiline.enabled;

View file

@ -6,20 +6,18 @@ CoffeeScript = require './coffee-script'
replDefaults =
prompt: 'coffee> ',
eval: (input, context, filename, cb) ->
# XXX: multiline hack
input = input.replace /\uFF00/g, '\n'
# strip single-line comments
input = input.replace /(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3'
# empty command
return cb null if /^\s*$/.test input
# TODO: fix #1829: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations
try
# XXX: multiline hack
input = input.replace /\uFF00/g, '\n'
# strip node-added parens
input = input[1...-1]
# strip single-line comments
input = input.replace /(^|[\r\n]+)(\s*)##?(?:[^#\r\n][^\r\n]*|)($|[\r\n])/, '$1$2$3'
# empty command
return cb null if /^\s*$/.test input
# TODO: fix #1829: pass in-scope vars and avoid accidentally shadowing them by omitting those declarations
js = CoffeeScript.compile "[]=(#{input}\n)", {filename, bare: yes}
cb null, vm.runInContext js, context, filename
js = CoffeeScript.compile "_=(#{input}\n)", {filename, bare: yes}
catch err
cb err
cb null, vm.runInContext(js, context, filename)
addMultilineHandler = (repl) ->
{rli, inputStream, outputStream} = repl
@ -39,7 +37,7 @@ addMultilineHandler = (repl) ->
rli.setPrompt multiline.prompt
rli.prompt true
else
nodeLineListener(cmd)
nodeLineListener cmd
return
# Handle Ctrl-v
@ -53,7 +51,7 @@ addMultilineHandler = (repl) ->
rli.prompt true
return
# no-op unless the current line is empty
return unless not rli.line? or rli.line.match /^\s*$/
return if rli.line? and not rli.line.match /^\s*$/
# eval, print, loop
multiline.enabled = not multiline.enabled
rli.line = ''

View file

@ -3,7 +3,7 @@
Stream = require 'stream'
class MockInputStream extends Stream
constructor: () ->
constructor: ->
@readable = true
resume: ->
@ -12,7 +12,7 @@ class MockInputStream extends Stream
@emit 'data', new Buffer("#{val}\n")
class MockOutputStream extends Stream
constructor: () ->
constructor: ->
@writable = true
@written = []
@ -20,54 +20,53 @@ class MockOutputStream extends Stream
#console.log 'output write', arguments
@written.push data
lastWrite: (fromEnd=-1) ->
lastWrite: (fromEnd = -1) ->
@written[@written.length - 1 + fromEnd].replace /\n$/, ''
testRepl = (desc, fn) ->
input = new MockInputStream()
output = new MockOutputStream()
Repl.start {input, output}
fn input, output
assertEqual = (expected, value) ->
eq expected, value, "Expected '#{value}' to equal '#{expected}'"
testRepl = (desc, fn) ->
input = new MockInputStream
output = new MockOutputStream
Repl.start {input, output}
test desc, -> fn input, output
ctrlV = { ctrl: true, name: 'v'}
testRepl "starts with coffee prompt", (input, output) ->
assertEqual 'coffee> ', output.lastWrite(0)
eq 'coffee> ', output.lastWrite(0)
testRepl "writes eval to output", (input, output) ->
input.emitLine '1+1'
assertEqual '2', output.lastWrite()
eq '2', output.lastWrite()
testRepl "comments are ignored", (input, output) ->
input.emitLine '1 + 1 #foo'
assertEqual '2', output.lastWrite()
eq '2', output.lastWrite()
testRepl "output in inspect mode", (input, output) ->
input.emitLine '"1 + 1\\n"'
assertEqual "'1 + 1\\n'", output.lastWrite()
eq "'1 + 1\\n'", output.lastWrite()
testRepl "variables are saved", (input, output) ->
input.emitLine "foo = 'foo'"
input.emitLine 'foobar = "#{foo}bar"'
assertEqual "'foobar'", output.lastWrite()
eq "'foobar'", output.lastWrite()
testRepl "empty command evaluates to undefined", (input, output) ->
input.emitLine ''
assertEqual 'undefined', output.lastWrite()
eq 'undefined', output.lastWrite()
ctrlV = { ctrl: true, name: 'v'}
testRepl "ctrl-v toggles multiline prompt", (input, output) ->
input.emit 'keypress', null, ctrlV
assertEqual '------> ', output.lastWrite(0)
eq '------> ', output.lastWrite(0)
input.emit 'keypress', null, ctrlV
assertEqual 'coffee> ', output.lastWrite(0)
eq 'coffee> ', output.lastWrite(0)
testRepl "multiline continuation changes prompt", (input, output) ->
input.emit 'keypress', null, ctrlV
input.emitLine ''
assertEqual '....... ', output.lastWrite(0)
eq '....... ', output.lastWrite(0)
testRepl "evaluates multiline", (input, output) ->
# Stubs. Could assert on their use.
@ -75,8 +74,7 @@ testRepl "evaluates multiline", (input, output) ->
output.clearLine = ->
input.emit 'keypress', null, ctrlV
input.emitLine '(->'
input.emitLine 'do ->'
input.emitLine ' 1 + 1'
input.emitLine ')()'
input.emit 'keypress', null, ctrlV
assertEqual '2', output.lastWrite()
eq '2', output.lastWrite()