Permalink
Browse files

adding runner class which handles command options, current -t, -d - c…

…loses #9 and #10
  • Loading branch information...
1 parent 01abe1a commit b6ea19ec467024687eacbe41297c9d0b5f4ac149 @ryanb committed Nov 4, 2009
View
7 bin/rubywarrior
@@ -1,8 +1,5 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/ruby_warrior'
-RubyWarrior::UI.out_stream = STDOUT
-RubyWarrior::UI.in_stream = STDIN
-RubyWarrior::UI.delay = 0.8 # TODO allow customization
-game = RubyWarrior::Game.new # TODO pass options into game when initializing
-game.start
+runner = RubyWarrior::Runner.new(ARGV, STDIN, STDOUT)
+runner.run
View
14 features/command_options.feature
@@ -0,0 +1,14 @@
+Feature: Command Options
+ In order to play the game the way I want
+ As a player
+ I want to customize ruby-warrior with options
+
+ Background:
+ Given no profile at "tmp"
+
+ Scenario: Run ruby warrior in specified directory with -d 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"
+ And I answer "y" to "next level"
+ Then I should see "directory for the next level"
View
9 features/step_definitions/interaction_steps.rb
@@ -24,6 +24,15 @@
end
end
+When /^I run rubywarrior with options "([^\"]*)"$/ do |options|
+ @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
+
When /^I answer "([^\"]*)" to "([^\"]*)"$/ do |answer, question|
@io.gets_until_include(question)
@io.puts(answer)
View
1 lib/ruby_warrior.rb
@@ -2,6 +2,7 @@
require 'ruby_warrior/core_additions'
+require 'ruby_warrior/runner'
require 'ruby_warrior/game'
require 'ruby_warrior/profile'
require 'ruby_warrior/ui'
View
29 lib/ruby_warrior/runner.rb
@@ -0,0 +1,29 @@
+require 'optparse'
+
+module RubyWarrior
+ class Runner
+ def initialize(arguments, stdin, stdout)
+ @arguments = arguments
+ @stdin = stdin
+ @stdout = stdout
+ @game = RubyWarrior::Game.new
+ end
+
+ def run
+ RubyWarrior::UI.in_stream = @stdin
+ RubyWarrior::UI.out_stream = @stdout
+ RubyWarrior::UI.delay = 0.8 # TODO allow customization
+ parse_options
+ @game.start
+ end
+
+ private
+
+ def parse_options
+ options = OptionParser.new
+ 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.parse!(@arguments)
+ end
+ end
+end
View
1 tmp/rubywarrior

1 comment on commit b6ea19e

@adamakhtar

Im learning ruby so aplogies in advance if this is a stupid question but why do you assign STDIN and STDOUT to instance variables? Is this purely to make testing user input easier or is there another benefit?

Please sign in to comment.