Skip to content

Commit

Permalink
adding -s option for skipping user input - closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanb committed Nov 4, 2009
1 parent 8b1860f commit 99a0a1a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 37 deletions.
11 changes: 11 additions & 0 deletions features/command_options.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ Feature: Command Options
And I run rubywarrior with options "-d tmp/ruby-warrior/joe-beginner -t 0"
And I answer "y" to "next level"
Then I should see "directory for the next level"

Scenario: Skip user input with -s option
Given a profile named "Joe" on "beginner"
When I copy fixture "walking_player.rb" to "tmp/ruby-warrior/joe-beginner/player.rb"
And I run rubywarrior with options "-d tmp/ruby-warrior/joe-beginner -t 0 -s"
Then I should see "current level"
When I run rubywarrior with options "-d tmp/ruby-warrior/joe-beginner -t 0"
And I answer "y" to "next level"
Then I should see "directory for the next level"
When I run rubywarrior with options "-d tmp/ruby-warrior/joe-beginner -t 0 -s"
Then I should see "failed the level"
7 changes: 3 additions & 4 deletions features/step_definitions/interaction_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
When /^I run rubywarrior$/ do
@io = MockIO.new
@io.start do |io|
RubyWarrior::UI.out_stream = io
RubyWarrior::UI.in_stream = io
RubyWarrior::Config.out_stream = io
RubyWarrior::Config.in_stream = io
RubyWarrior::Game.new.start
end
end

When /^I run rubywarrior with options "([^\"]*)"$/ do |options|
RubyWarrior::Config.reset
@io = MockIO.new
@io.start do |io|
RubyWarrior::UI.out_stream = io
RubyWarrior::UI.in_stream = io
RubyWarrior::Runner.new(options.split, io, io).run
end
end
Expand Down
4 changes: 4 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

require File.dirname(__FILE__) + '/../../lib/ruby_warrior'

Before do
RubyWarrior::Config.reset
end

After do
FileUtils.rm_rf "towers/short"
end
15 changes: 13 additions & 2 deletions lib/ruby_warrior/config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
module RubyWarrior
class Config
class << self
attr_writer :path_prefix
attr_accessor :delay, :in_stream, :out_stream
attr_writer :path_prefix, :skip_input

def path_prefix
@path_prefix || "."
end

def skip_input?
@skip_input
end

def reset
[:@path_prefix, :@skip_input, :@delay, :@in_stream, :@out_stream].each do |i|
remove_instance_variable(i) if instance_variable_defined?(i)
end
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/ruby_warrior/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def start
end

if profile.epic?
UI.delay /= 2 if UI.delay # speed up UI since we're going to be doing a lot here
Config.delay /= 2 if Config.delay # speed up UI since we're going to be doing a lot here
profile.current_epic_score = 0
playing = true
while playing
Expand Down Expand Up @@ -56,15 +56,15 @@ def play_current_level
else
continue = false
UI.puts "Sorry, you failed the level. Change your script and try again."
if current_level.clue && UI.ask("Would you like to read the additional clues for this level?")
if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?")
UI.puts current_level.clue
end
end
continue
end

def request_next_level
if (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?"))
if !Config.skip_input? && (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?"))
if next_level.exists?
prepare_next_level
UI.puts "See the ruby-warrior directory for the next level README."
Expand Down
13 changes: 7 additions & 6 deletions lib/ruby_warrior/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ def initialize(arguments, stdin, stdout)
end

def run
RubyWarrior::UI.in_stream = @stdin
RubyWarrior::UI.out_stream = @stdout
RubyWarrior::UI.delay = 0.8 # TODO allow customization
Config.in_stream = @stdin
Config.out_stream = @stdout
Config.delay = 0.8
parse_options
@game.start
end
Expand All @@ -22,9 +22,10 @@ def run
def parse_options
options = OptionParser.new
options.banner = "Usage: rubywarrior [options]"
options.on('-d', '--directory DIR', 'Run under given directory') { |dir| RubyWarrior::Config.path_prefix = dir }
options.on('-t', '--time SECONDS', 'Delay each turn by seconds') { |seconds| RubyWarrior::UI.delay = seconds.to_f }
options.on('-h', '--help', 'Show this message') { puts(options); exit }
options.on('-d', '--directory DIR', "Run under given directory") { |dir| Config.path_prefix = dir }
options.on('-s', '--skip', "Skip user input") { Config.skip_input = true }
options.on('-t', '--time SECONDS', "Delay each turn by seconds") { |seconds| Config.delay = seconds.to_f }
options.on('-h', '--help', "Show this message") { puts(options); exit }
options.parse!(@arguments)
end
end
Expand Down
18 changes: 4 additions & 14 deletions lib/ruby_warrior/ui.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
module RubyWarrior
class UI
class << self
attr_accessor :delay

def in_stream=(in_stream)
@in = in_stream
end

def out_stream=(stream)
@out = stream
end

def puts(msg)
@out.puts(msg) if @out
Config.out_stream.puts(msg) if Config.out_stream
end

def puts_with_delay(msg)
result = puts(msg)
sleep(@delay) if @delay
sleep(Config.delay) if Config.delay
result
end

def print(msg)
@out.print(msg) if @out
Config.out_stream.print(msg) if Config.out_stream
end

def gets
@in ? @in.gets : ''
Config.in_stream ? Config.in_stream.gets : ''
end

def request(msg)
Expand Down
13 changes: 5 additions & 8 deletions spec/ruby_warrior/ui_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
describe RubyWarrior::UI do
before(:each) do
@ui = RubyWarrior::UI
@config = RubyWarrior::Config
@out = StringIO.new
@in = StringIO.new
@ui.out_stream = @out
@ui.in_stream = @in
end

after(:each) do
@ui.delay = nil
@config.out_stream = @out
@config.in_stream = @in
end

it "should add puts to out stream" do
Expand All @@ -30,7 +27,7 @@
end

it "should gets should return empty string if no input" do
@ui.in_stream = nil
@config.in_stream = nil
@ui.gets.should == ""
end

Expand Down Expand Up @@ -82,7 +79,7 @@
end

it "should delay after puts when specified" do
@ui.delay = 1.3
@config.delay = 1.3
@ui.expects(:puts).with("foo")
@ui.expects(:sleep).with(1.3)
@ui.puts_with_delay("foo")
Expand Down
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@

Spec::Runner.configure do |config|
config.mock_with :mocha
config.before(:each) do
RubyWarrior::Config.reset
end
end

0 comments on commit 99a0a1a

Please sign in to comment.