From 998f24bb7321708fb04dd9c1b47de7dda2e6e317 Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Sat, 8 Jun 2013 01:44:54 -0400 Subject: [PATCH 1/4] Panda complete --- minitest/navy.rb | 4 +++- minitest/navy_test.rb | 35 +++++++++++++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index c672e0a..0ef0115 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -1,4 +1,6 @@ class Admiral + attr_reader :battleship + def initialize(battleship) @battleship = battleship end @@ -12,7 +14,7 @@ def fire_upon_target class Battleship attr_reader :ammunition def initialize - @ammunition = 100 + @ammunition = 10 end def fire! diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index de35ec0..795626a 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -9,6 +9,10 @@ def setup @admiral = Admiral.new(@battleship) end + def test_the_admiral_has_a_battleship + assert_equal @battleship.object_id, @admiral.battleship.object_id + end + def test_can_tell_the_battleship_to_fire @battleship.expect :fire!, nil @admiral.fire_upon_target @@ -17,19 +21,26 @@ def test_can_tell_the_battleship_to_fire end class TestBattleship< MiniTest::Unit::TestCase - def test_will_decrease_ammunition_when_firing - battleship = Battleship.new - starting_ammunition = battleship.ammunition - battleship.fire! - assert_equal (starting_ammunition - 1), battleship.ammunition + def setup + @battleship = Battleship.new + @starting_ammunition = @battleship.ammunition end -end -describe Battleship do - it "should decrease ammo" do - battleship = Battleship.new - starting_ammunition = battleship.ammunition - battleship.fire! - battleship.ammunition.must_equal (starting_ammunition -1) + def test_starts_with_ten_rounds_of_ammunition + assert_equal 10, @starting_ammunition + end + + def test_will_decrease_ammunition_when_firing + @battleship.fire! + assert_equal (@starting_ammunition - 1), @battleship.ammunition end end + +#describe Battleship do +# it "should decrease ammo" do +# battleship = Battleship.new +# starting_ammunition = battleship.ammunition +# battleship.fire! +# battleship.ammunition.must_equal (starting_ammunition -1) +# end +#end From db0cf919e3c7b30ed74f9d6825240ecd8bccbe36 Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Sun, 9 Jun 2013 02:26:26 -0400 Subject: [PATCH 2/4] Tiger complete --- minitest/navy.rb | 30 ++++++++++++++++++-- minitest/navy_test.rb | 64 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 83 insertions(+), 11 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index 0ef0115..e18be55 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -12,12 +12,38 @@ def fire_upon_target end class Battleship - attr_reader :ammunition - def initialize + attr_reader :ammunition, :armory + def initialize(armory) @ammunition = 10 + @armory = armory end def fire! @ammunition = @ammunition - 1 + reload! if @ammunition == 0 end + + def reload! + @ammunition += @armory.request_ammunition(10) + end +end + +class Armory + attr_reader :ammunition_reserves + + def initialize(reserves) + @ammunition_reserves = reserves + end + + def request_ammunition(amount) + if amount <= @ammunition_reserves + @ammunition_reserves = @ammunition_reserves - amount + amount + else + available = @ammunition_reserves + @ammunition_reserves = 0 + available + end + end + end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index 795626a..b5f42df 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -22,7 +22,8 @@ def test_can_tell_the_battleship_to_fire class TestBattleship< MiniTest::Unit::TestCase def setup - @battleship = Battleship.new + @armory = MiniTest::Mock.new + @battleship = Battleship.new(@armory) @starting_ammunition = @battleship.ammunition end @@ -36,11 +37,56 @@ def test_will_decrease_ammunition_when_firing end end -#describe Battleship do -# it "should decrease ammo" do -# battleship = Battleship.new -# starting_ammunition = battleship.ammunition -# battleship.fire! -# battleship.ammunition.must_equal (starting_ammunition -1) -# end -#end +describe Battleship do + + it "should decrease ammo" do + mock_armory = MiniTest::Mock.new + ship = Battleship.new(mock_armory) + starting_ammo = ship.ammunition + ship.fire! + ship.ammunition.must_equal (starting_ammo - 1) + end + + it "can request more ammo" do + mock_armory = MiniTest::Mock.new + ship = Battleship.new(mock_armory) + + mock_armory.expect(:request_ammunition, 10, [Integer]) + while ship.ammunition >= 2 + ship.fire! + end + ship.fire! + mock_armory.verify + end + + it "should recieve more ammo" do + mock_armory = MiniTest::Mock.new + ship = Battleship.new(mock_armory) + + mock_armory.expect(:request_ammunition, 10, [Integer]) + starting_ammo = ship.ammunition + ship.reload! + ship.ammunition.must_equal (starting_ammo + 10) + end + +end + +describe Armory do + + subject { Armory.new(100) } + + it "should have reserve ammunition" do + subject.ammunition_reserves.must_equal(100) + end + + it "should provide ammunition if it has enough" do + ammo = subject.request_ammunition(10) + ammo.must_equal 10 + end + + it "should provide partial ammunition if it doesn't have enough" do + available = subject.ammunition_reserves + ammo = subject.request_ammunition(available + 10) + ammo.must_equal available + end +end From 833c95aa34863939757a04938a64246ead100458 Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Sun, 9 Jun 2013 17:35:50 -0400 Subject: [PATCH 3/4] Eagle Complete --- minitest/navy.rb | 17 +++++++++++-- minitest/navy_test.rb | 55 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index e18be55..cc73f2e 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -12,15 +12,17 @@ def fire_upon_target end class Battleship - attr_reader :ammunition, :armory - def initialize(armory) + attr_reader :ammunition, :armory, :fate + def initialize(armory, fate) @ammunition = 10 @armory = armory + @fate = fate end def fire! @ammunition = @ammunition - 1 reload! if @ammunition == 0 + fate.report end def reload! @@ -47,3 +49,14 @@ def request_ammunition(amount) end end + +class Fate + + def initialize(randomizer=Random) + @randomizer = randomizer + end + + def report + @randomizer.rand(11).even? + end +end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index b5f42df..50a338f 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -23,7 +23,8 @@ def test_can_tell_the_battleship_to_fire class TestBattleship< MiniTest::Unit::TestCase def setup @armory = MiniTest::Mock.new - @battleship = Battleship.new(@armory) + @fate = MiniTest::Mock.new + @battleship = Battleship.new(@armory, @fate) @starting_ammunition = @battleship.ammunition end @@ -32,8 +33,9 @@ def test_starts_with_ten_rounds_of_ammunition end def test_will_decrease_ammunition_when_firing + @fate.expect(:report, true) @battleship.fire! - assert_equal (@starting_ammunition - 1), @battleship.ammunition + assert_equal @starting_ammunition - 1, @battleship.ammunition end end @@ -41,16 +43,21 @@ def test_will_decrease_ammunition_when_firing it "should decrease ammo" do mock_armory = MiniTest::Mock.new - ship = Battleship.new(mock_armory) + mock_fate = MiniTest::Mock.new + ship = Battleship.new(mock_armory, mock_fate) + + mock_fate.expect(:report, true) starting_ammo = ship.ammunition ship.fire! - ship.ammunition.must_equal (starting_ammo - 1) + ship.ammunition.must_equal(starting_ammo - 1) end it "can request more ammo" do mock_armory = MiniTest::Mock.new - ship = Battleship.new(mock_armory) + mock_fate = MiniTest::Mock.new + ship = Battleship.new(mock_armory, mock_fate) + mock_fate.expect(:report, true) mock_armory.expect(:request_ammunition, 10, [Integer]) while ship.ammunition >= 2 ship.fire! @@ -61,12 +68,23 @@ def test_will_decrease_ammunition_when_firing it "should recieve more ammo" do mock_armory = MiniTest::Mock.new - ship = Battleship.new(mock_armory) + mock_fate = MiniTest::Mock.new + ship = Battleship.new(mock_armory, mock_fate) + mock_fate.expect(:report, true) mock_armory.expect(:request_ammunition, 10, [Integer]) starting_ammo = ship.ammunition ship.reload! - ship.ammunition.must_equal (starting_ammo + 10) + ship.ammunition.must_equal(starting_ammo + 10) + end + + it "should know if there was a hit or miss after firing" do + mock_armory = MiniTest::Mock.new + mock_fate = MiniTest::Mock.new + ship = Battleship.new(mock_armory, mock_fate) + + mock_fate.expect(:report, true) + ship.fire!.must_equal(true) end end @@ -80,13 +98,28 @@ def test_will_decrease_ammunition_when_firing end it "should provide ammunition if it has enough" do - ammo = subject.request_ammunition(10) - ammo.must_equal 10 + subject.request_ammunition(10).must_equal 10 end it "should provide partial ammunition if it doesn't have enough" do available = subject.ammunition_reserves - ammo = subject.request_ammunition(available + 10) - ammo.must_equal available + subject.request_ammunition(available + 10).must_equal available + end +end + +describe Fate do + + it "should return true if rand is even" do + mock_rand = MiniTest::Mock.new + fate = Fate.new(mock_rand) + mock_rand.expect(:rand, 6, [Integer]) + fate.report.must_equal true + end + + it "should return false if rand is odd" do + mock_rand = MiniTest::Mock.new + fate = Fate.new(mock_rand) + mock_rand.expect(:rand, 5, [Integer]) + fate.report.must_equal false end end From 23bd535c2553c100c1339fcfa78da21e1fd9e70e Mon Sep 17 00:00:00 2001 From: Melissa Holmes Date: Sun, 9 Jun 2013 17:41:13 -0400 Subject: [PATCH 4/4] Clean up Battleship tests --- minitest/navy_test.rb | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index 50a338f..123488d 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -41,49 +41,37 @@ def test_will_decrease_ammunition_when_firing describe Battleship do - it "should decrease ammo" do - mock_armory = MiniTest::Mock.new - mock_fate = MiniTest::Mock.new - ship = Battleship.new(mock_armory, mock_fate) + let(:armory) { MiniTest::Mock.new } + let(:fate) { MiniTest::Mock.new } + let(:ship) { Battleship.new(armory, fate) } - mock_fate.expect(:report, true) + it "should decrease ammo" do + fate.expect(:report, true) starting_ammo = ship.ammunition ship.fire! ship.ammunition.must_equal(starting_ammo - 1) end it "can request more ammo" do - mock_armory = MiniTest::Mock.new - mock_fate = MiniTest::Mock.new - ship = Battleship.new(mock_armory, mock_fate) - - mock_fate.expect(:report, true) - mock_armory.expect(:request_ammunition, 10, [Integer]) + fate.expect(:report, true) + armory.expect(:request_ammunition, 10, [Integer]) while ship.ammunition >= 2 ship.fire! end ship.fire! - mock_armory.verify + armory.verify end it "should recieve more ammo" do - mock_armory = MiniTest::Mock.new - mock_fate = MiniTest::Mock.new - ship = Battleship.new(mock_armory, mock_fate) - - mock_fate.expect(:report, true) - mock_armory.expect(:request_ammunition, 10, [Integer]) + fate.expect(:report, true) + armory.expect(:request_ammunition, 10, [Integer]) starting_ammo = ship.ammunition ship.reload! ship.ammunition.must_equal(starting_ammo + 10) end it "should know if there was a hit or miss after firing" do - mock_armory = MiniTest::Mock.new - mock_fate = MiniTest::Mock.new - ship = Battleship.new(mock_armory, mock_fate) - - mock_fate.expect(:report, true) + fate.expect(:report, true) ship.fire!.must_equal(true) end