Skip to content

Commit

Permalink
Added a method for testing the levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Rennie committed Mar 18, 2012
1 parent 374349f commit b88759e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 18 deletions.
23 changes: 20 additions & 3 deletions lib/githug/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ def play
game.play_level
end

desc :test, "Test a level from a file path"

def test(path = nil)
UI.word_box("Githug")
make_directory
level = Level.load_from_file(path)
game = Game.new
game.test_level(level)
end

desc :hint, "Get a hint for the current level"

def hint
Expand All @@ -25,12 +35,19 @@ def hint

desc :reset, "Reset the current level"

def reset
if level = load_level
UI.word_box("Githug")
def reset(path = nil)
if path
level = Level.load_from_file(path)
else
level = load_level
end
UI.word_box("Githug")
if level
UI.puts("resetting level")
level.setup_level
level.full_description
else
UI.error("Level does not exist")
end
end

Expand Down
10 changes: 7 additions & 3 deletions lib/githug/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ def play_level
end
end

def dry_run(level)
level = Level.load(level)

def test_level(level)
UI.puts level.full_description
if level.test
UI.success "Valid solution"
else
UI.error "Invalid solution"
end
end

def level_bump
Expand Down
24 changes: 19 additions & 5 deletions lib/githug/level.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ class Level
class << self

def load(level_name)
path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}.rb"
setup(path)
end

def load_from_file(path)
setup(path)
end

def setup(path)
level_name = File.basename(path, File.extname(path))
#Remove .rb extension, WTB a better way to do this
level_path = path[0..-4]
level = new
level_path = "#{File.dirname(__FILE__)}/../../levels/#{level_name}"
location = "#{level_path}.rb"
return false unless File.exists?(location)
level.instance_eval(File.read(location))
level.level_no = LEVELS.index(level_name)
return false unless File.exists?(path)
level.instance_eval(File.read(path))
level.level_no = LEVELS.index(level_name) || 1
level.level_path = level_path
level
end
Expand Down Expand Up @@ -78,6 +88,10 @@ def solve
false
end

def test
@solution.call
end

def show_hint
UI.word_box("Githug")
profile = Profile.load
Expand Down
24 changes: 22 additions & 2 deletions spec/githug/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,26 @@
lambda {@cli.make_directory}.should raise_error(SystemExit)
end

describe "test" do
it "should perform a test run of the level" do
level = mock
game = mock
@cli.stub(:make_directory)
Githug::Level.should_receive(:load_from_file).with("/foo/bar/test/level.rb").and_return(level)
Githug::Game.stub(:new).and_return(game)
game.should_receive(:test_level).with(level)
@cli.test("/foo/bar/test/level.rb")
end
end

describe "level methods" do
before(:each) do
@level = mock
@profile = mock
@profile.stub(:level).and_return(1)
Githug::Profile.stub(:load).and_return(@profile)
Githug::Level.stub(:load).and_return(@level)
Githug::Level.stub(:load_from_file).with("/foo/bar/level.rb").and_return(@level)
end

it "should call the hint method on the level" do
Expand All @@ -69,10 +82,17 @@
Githug::Level.stub(:load).and_return(false)
@level.should_not_receive(:setup_level)
@level.should_not_receive(:full_description)
Githug::UI.should_not_receive(:word_box).with("Githug")
Githug::UI.should_not_receive(:puts).with("resetting level")
Githug::UI.should_receive(:error).with("Level does not exist")
@cli.reset
end

it "should reset the level with a path" do
@level.should_receive(:setup_level)
@level.should_receive(:full_description)
Githug::UI.should_receive(:word_box).with("Githug")
Githug::UI.should_receive(:puts).with("resetting level")
@cli.reset("/foo/bar/level.rb")
end
end

end
Expand Down
16 changes: 12 additions & 4 deletions spec/githug/game_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
@game.play_level
end

describe "solve" do
describe "play_level" do

it "should echo congratulations if the level is solved" do
@level.stub(:solve).and_return(true)
Expand Down Expand Up @@ -62,9 +62,17 @@
end


describe "dry run" do
it "should play the game without altering the profile" do
pending
describe "test_level" do
it "Should output Valid solution if the solution is valid" do
@level.stub(:test).and_return(true)
Githug::UI.should_receive(:success).with("Valid solution")
@game.test_level(@level)
end

it "Should output Invalid solution if the solution is invalid" do
@level.stub(:test).and_return(false)
Githug::UI.should_receive(:error).with("Invalid solution")
@game.test_level(@level)
end
end

Expand Down
33 changes: 32 additions & 1 deletion spec/githug/level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@

end

describe "load_from_file" do
it "should load the level" do
File.stub(:dirname).and_return("")
File.should_receive(:read).with('/foo/bar/test/level.rb').and_return(@file)
level = Githug::Level.load_from_file("/foo/bar/test/level.rb")
level.instance_variable_get("@difficulty").should eql(1)
level.instance_variable_get("@description").should eql("A test description")
end

it "should return false if the level does not exist" do
File.stub(:exists?).and_return(false)
Githug::Level.load_from_file("/foo/bar/test/level.rb").should eql(false)
end
end

describe "setup" do

it "should return false if the level does not exist" do
File.stub(:exists?).and_return(false)
Githug::Level.setup("/foo/bar/test/level.rb").should eql(false)
end

end


describe "solve" do

Expand All @@ -68,6 +92,13 @@

end

describe "test" do
it "should call solve" do
@level.instance_variable_get("@solution").should_receive(:call)
@level.test
end
end


describe "full_description" do

Expand Down Expand Up @@ -148,4 +179,4 @@
@level.init_from_level
end
end
end
end

0 comments on commit b88759e

Please sign in to comment.