Skip to content

Commit

Permalink
simulator raises an error when #enter called and no way to process it…
Browse files Browse the repository at this point in the history
…s input
  • Loading branch information
sinisterchipmunk committed Mar 15, 2012
1 parent 5e0709d commit ce48882
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 9 deletions.
8 changes: 6 additions & 2 deletions lib/ambrosia/cucumber/steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
simulator.press key
end

When /^I should see "([^"]*)"$/ do |content|
simulator.state[:display].should include(content)
end

Then /^I should see:$/ do |table|
table.hashes.each do |hash|
hash.each do |caption, value|
# this will produce some redundant checks but I don't think that's a problem
simulator.state[:display].should include(caption)
simulator.state[:display].should include(value)
step 'I should see "%s"' % caption
step 'I should see "%s"' % value
end
end
end
Expand Down
11 changes: 10 additions & 1 deletion lib/assets/javascripts/ambrosia/src/simulator.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions public/javascripts/Player.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function Player() {
}
Player.prototype.play = function(song) {
this.currentlyPlayingSong = song;
this.isPlaying = true;
};

Player.prototype.pause = function() {
this.isPlaying = false;
};

Player.prototype.resume = function() {
if (this.isPlaying) {
throw new Error("song is already playing");
}

this.isPlaying = true;
};

Player.prototype.makeFavorite = function() {
this.currentlyPlayingSong.persistFavoriteStatus(true);
};
7 changes: 7 additions & 0 deletions public/javascripts/Song.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function Song() {
}

Song.prototype.persistFavoriteStatus = function(value) {
// something complicated
throw new Error("not yet implemented");
};
9 changes: 9 additions & 0 deletions spec/javascripts/helpers/SpecHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
beforeEach(function() {
this.addMatchers({
toBePlaying: function(expectedSong) {
var player = this.actual;
return player.currentlyPlayingSong === expectedSong &&
player.isPlaying;
}
});
});
5 changes: 5 additions & 0 deletions spec/lib/simulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
end

describe "entering input" do
it "should raise an error if a keypress goes unacknowledged" do
simulate "a = 0"
proc { simulator.enter "1" }.should raise_error("Error: No handler for keypress '1' on this screen")
end

it "should trigger keypresses" do
simulate "a = 0\nswitch getch '1'\n when '1' then a = 1"
simulator.enter "1", false
Expand Down
13 changes: 7 additions & 6 deletions spec/spec_helper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ fs = require 'fs'
lexer = require 'lexer'
builder = require 'builder'
path = require 'path'

{spawn, exec} = require 'child_process'
child_process = require 'child_process'
run = (cmd, args...) ->
done = false
errors = ""
waitsFor -> done
proc = spawn cmd, args
proc = child_process.spawn cmd, args
proc.stderr.on 'data', (buffer) -> errors += buffer
proc.stdout.on 'data', (buffer) -> process.stdout.write buffer.toString()
proc.on 'exit', (status) ->
Expand Down Expand Up @@ -41,9 +41,10 @@ global.dom = (script) ->

validation_index = 0
global.validate = (dom, cb) ->
tmpfile = "tmp/#{validation_index++}.xml"
fs.writeFileSync tmpfile, dom.toString()
run "xmllint", "--noout", "--schema", path.join(process.env['AMBROSIA_PATH'], 'lib/tml.xsd'), tmpfile
if child_process
tmpfile = "tmp/#{validation_index++}.xml"
fs.writeFileSync tmpfile, dom.toString()
run "xmllint", "--noout", "--schema", path.join(process.env['AMBROSIA_PATH'], 'lib/tml.xsd'), tmpfile
dom

global.simulate = (dom, callback) ->
Expand Down
8 changes: 8 additions & 0 deletions src/ambrosia/src/simulator.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ exports.Simulator = class Simulator
type = if field.attrs.type == 'text' then 'string' else field.attrs.type
@evaluate variable, type, lo: "tmlvar:#{field.attrs.name}", op: "plus", ro: char
else
# normally #press will not raise an error on a useless keypress, but in this
# case we want it to because the developer at this stage very likely is not
# on the screen they think they are.
found = false
for variant in @state.screen.element.search('variant')
if variant.attrs['key'] == char
found = true
throw new Error "No handler for keypress '#{char}' on this screen" unless found
@press char

if text.length > 1
Expand Down

0 comments on commit ce48882

Please sign in to comment.