1 | suite 'REPL', ->
|
2 |
|
3 | Stream = require 'stream'
|
4 |
|
5 | class MockInputStream extends Stream
|
6 | constructor: ->
|
7 |
|
8 | readable: true
|
9 |
|
10 | resume: ->
|
11 |
|
12 | emitLine: (val) ->
|
13 | @emit 'data', new Buffer "#{val}\n"
|
14 |
|
15 | class MockOutputStream extends Stream
|
16 | constructor: ->
|
17 | @written = []
|
18 |
|
19 | writable: true
|
20 |
|
21 | write: (data) ->
|
22 | @written.push data
|
23 |
|
24 | lastWrite: (fromEnd = -1) ->
|
25 | @written[@written.length - 1 + fromEnd].replace /\n$/, ''
|
26 |
|
27 |
|
28 | testRepl = (desc, fn) ->
|
29 | input = new MockInputStream
|
30 | output = new MockOutputStream
|
31 | Repl.start {input, output}
|
32 | test desc, -> fn input, output
|
33 |
|
34 | ctrlV = { ctrl: true, name: 'v'}
|
35 |
|
36 |
|
37 | testRepl "starts with coffee prompt", (input, output) ->
|
38 | eq 'coffee> ', output.lastWrite 0
|
39 |
|
40 | testRepl "writes eval to output", (input, output) ->
|
41 | input.emitLine '1+1'
|
42 | eq '2', output.lastWrite()
|
43 |
|
44 | testRepl "comments are ignored", (input, output) ->
|
45 | input.emitLine '1 + 1 #foo'
|
46 | eq '2', output.lastWrite()
|
47 |
|
48 | testRepl "output in inspect mode", (input, output) ->
|
49 | input.emitLine '"1 + 1\\n"'
|
50 | eq "'1 + 1\\n'", output.lastWrite()
|
51 |
|
52 | testRepl "variables are saved", (input, output) ->
|
53 | input.emitLine 'foo = "foo"'
|
54 | input.emitLine 'foobar = "#{foo}bar"'
|
55 | eq "'foobar'", output.lastWrite()
|
56 |
|
57 | testRepl "empty command evaluates to undefined", (input, output) ->
|
58 | input.emitLine ''
|
59 | eq 'undefined', output.lastWrite()
|
60 |
|
61 | testRepl "ctrl-v toggles multiline prompt", (input, output) ->
|
62 | input.emit 'keypress', null, ctrlV
|
63 | eq '------> ', output.lastWrite 0
|
64 | input.emit 'keypress', null, ctrlV
|
65 | eq 'coffee> ', output.lastWrite 0
|
66 |
|
67 | testRepl "multiline continuation changes prompt", (input, output) ->
|
68 | input.emit 'keypress', null, ctrlV
|
69 | input.emitLine ''
|
70 | eq '....... ', output.lastWrite 0
|
71 |
|
72 | testRepl "evaluates multiline", (input, output) ->
|
73 |
|
74 | output.cursorTo = output.clearLine = ->
|
75 |
|
76 | input.emit 'keypress', null, ctrlV
|
77 | input.emitLine 'do ->'
|
78 | input.emitLine ' 1 + 1'
|
79 | input.emit 'keypress', null, ctrlV
|
80 | eq '2', output.lastWrite()
|