Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding profile object and improving game class and tests
- Loading branch information
Showing
9 changed files
with
250 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module RubyWarrior | ||
class Profile | ||
def initialize(tower, name) | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,50 @@ | ||
module RubyWarrior | ||
class UI | ||
def self.in_stream=(in_stream) | ||
@in = in_stream | ||
end | ||
|
||
def self.out_stream=(stream) | ||
@out = stream | ||
end | ||
|
||
def self.out | ||
@out ||= StringIO.new | ||
def self.puts(msg) | ||
@out.puts(msg) if @out | ||
end | ||
|
||
def self.print(msg) | ||
@out.print(msg) if @out | ||
end | ||
|
||
def self.puts(msg) | ||
out.puts(msg) | ||
def self.gets | ||
@in.gets if @in | ||
end | ||
|
||
def self.request(msg) | ||
print(msg) | ||
gets | ||
end | ||
|
||
def self.ask(msg) | ||
request("#{msg} [yn] ") == 'y' | ||
end | ||
|
||
# REFACTORME | ||
def self.choose(options) | ||
options.each_with_index do |option, i| | ||
if option.kind_of? Array | ||
puts "[#{i+1}] #{option.last}" | ||
else | ||
puts "[#{i+1}] #{option}" | ||
end | ||
end | ||
choice = request('Choose an option by typing the number: ') | ||
response = options[choice.to_i-1] | ||
if response.kind_of? Array | ||
response.first | ||
else | ||
response | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe RubyWarrior::Profile do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
require File.dirname(__FILE__) + '/../spec_helper' | ||
|
||
describe RubyWarrior::UI do | ||
before(:each) do | ||
@ui = RubyWarrior::UI | ||
@out = StringIO.new | ||
@in = StringIO.new | ||
@ui.out_stream = @out | ||
@ui.in_stream = @in | ||
end | ||
|
||
it "should add puts to out stream" do | ||
@ui.puts "hello" | ||
@out.string.should == "hello\n" | ||
end | ||
|
||
it "should add print to out stream without newline" do | ||
@ui.print "hello" | ||
@out.string.should == "hello" | ||
end | ||
|
||
it "should fetch gets from in stream" do | ||
@in.puts "bar" | ||
@in.rewind | ||
@ui.gets.should == "bar\n" | ||
end | ||
|
||
it "should request text input" do | ||
@in.puts "bar" | ||
@in.rewind | ||
@ui.request("foo").should == "bar\n" | ||
@out.string.should == "foo" | ||
end | ||
|
||
it "should ask for yes/no and return true when yes" do | ||
@ui.expects(:request).with('foo? [yn] ').returns('y') | ||
@ui.ask("foo?").should be_true | ||
end | ||
|
||
it "should ask for yes/no and return false when no" do | ||
@ui.stubs(:request).returns('n') | ||
@ui.ask("foo?").should be_false | ||
end | ||
|
||
it "should ask for yes/no and return false for any input" do | ||
@ui.stubs(:request).returns('aklhasdf') | ||
@ui.ask("foo?").should be_false | ||
end | ||
|
||
it "should present multiple options and return selected one" do | ||
@ui.stubs(:request).returns('2') | ||
@ui.choose([:foo, :bar, :test]).should == :bar | ||
@out.string.should include('[1] foo') | ||
@out.string.should include('[2] bar') | ||
@out.string.should include('[3] test') | ||
end | ||
|
||
it "choose should accept array as option" do | ||
@ui.stubs(:request).returns('3') | ||
@ui.choose([:foo, :bar, [:tower, 'easy']]).should == :tower | ||
@out.string.should include('[3] easy') | ||
end | ||
end |