Skip to content

Commit

Permalink
Add test coverage for RubyWarrior monkeypatches
Browse files Browse the repository at this point in the history
  • Loading branch information
deadprogram committed Dec 24, 2011
1 parent 33cb0a1 commit db9a0b7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/kidsruby/rubywarrior.rb
@@ -1,18 +1,18 @@
require 'ruby_warrior'

# various monkey patches needed to get rubywarrior working within kidsruby environment
module RubyWarrior
Config.in_stream = StdIn.new
Config.out_stream = $stdout
Config.delay = 0.6
Config.path_prefix = File.expand_path(File.dirname(__FILE__) + "../../..")

def self.play
game = RubyWarrior::Game.new
game = Game.new
game.verify_setup
game.start
end

# just generate the .profile file as needed
class Game
def verify_setup
make_game_directory unless File.exist?(Config.path_prefix + '/rubywarrior')
Expand All @@ -21,22 +21,22 @@ def verify_setup
alias_method :prepare_next_level_original, :prepare_next_level
def prepare_next_level
prepare_next_level_original
next_level.display_description
next_level.show_description
end
end

# do not load player class from rubywarrior directory, because it should be in kid's editor
class Level
# do not load player class from rubywarrior directory, because it should be in kidsruby editor
def load_player; end

def display_description
def show_description
load_level
UI.puts PlayerGenerator.new(self).level_description
UI.puts PlayerGenerator.new(self).show_description
end
end

class PlayerGenerator
def level_description
def show_description
read_template(templates_path + '/README.erb')
end
end
Expand Down
56 changes: 56 additions & 0 deletions spec/lib/rubywarrior_spec.rb
@@ -0,0 +1,56 @@
require_relative "../spec_helper"
require_relative "../../lib/kidsruby/stdio"
require_relative "../../lib/kidsruby/rubywarrior"

describe 'rubywarrior' do
describe 'Game' do
it "must be able to start" do
RubyWarrior::Game.any_instance.expects(:verify_setup).returns(nil)
RubyWarrior::Game.any_instance.expects(:start).returns(nil)
RubyWarrior.play
end

it "must be able to verify setup where no game directory exists yet" do
File.expects(:exist?).returns(false)
RubyWarrior::Game.any_instance.expects(:make_game_directory).returns(nil)
RubyWarrior::Game.new.verify_setup
end

it "must be able to prepare the next level" do
@next_level = mock("level")
@next_level.expects(:show_description).returns(nil)
RubyWarrior::Game.any_instance.expects(:next_level).returns(@next_level)
RubyWarrior::Game.any_instance.expects(:prepare_next_level_original).returns(nil)
RubyWarrior::Game.new.prepare_next_level
end
end

describe 'Level' do
before do
@profile = mock('profile')
@level = RubyWarrior::Level.new(@profile, 1)
end

it "must not load player from file" do
@level.expects(:load).never
@level.load_player
end

it "must be able to show description" do
@level.expects(:load_level)
RubyWarrior::PlayerGenerator.any_instance.expects(:show_description)
RubyWarrior::UI.expects(:puts)
@level.show_description
end
end

describe 'PlayerGenerator' do
it "must be able to show description" do
@level = mock('level')
RubyWarrior::PlayerGenerator.any_instance.expects(:read_template)
RubyWarrior::PlayerGenerator.new(@level).show_description
end
end

end

0 comments on commit db9a0b7

Please sign in to comment.