From 5163fa4ec49c59f35e85b5405c40f99e14ba8528 Mon Sep 17 00:00:00 2001 From: drammopo Date: Tue, 11 Feb 2014 23:04:12 +0200 Subject: [PATCH 1/7] Initial attempt at both Panda Levels and Tiger L-1 --- minitest/navy.rb | 12 ++++++++++-- minitest/navy_test.rb | 24 ++++++++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index c672e0a..1eb4341 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -7,15 +7,23 @@ def fire_upon_target @battleship.fire! end + def has_a_ship? + true + end + end class Battleship - attr_reader :ammunition + attr_accessor :ammunition def initialize - @ammunition = 100 + @ammunition = 10 end def fire! @ammunition = @ammunition - 1 end + + def more_ammo!(extra_ammunition) + @ammunition += extra_ammunition + end end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index de35ec0..f431b46 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -7,6 +7,8 @@ class TestAdmiral < MiniTest::Unit::TestCase def setup @battleship = MiniTest::Mock.new @admiral = Admiral.new(@battleship) + @another_battleship = Battleship.new + @another_admiral = Admiral.new(@another_battleship) end def test_can_tell_the_battleship_to_fire @@ -14,14 +16,28 @@ def test_can_tell_the_battleship_to_fire @admiral.fire_upon_target @battleship.verify end + + def test_have_a_battleship + assert_equal true, @another_admiral.has_a_ship? + end end class TestBattleship< MiniTest::Unit::TestCase + + def setup + @battleship = Battleship.new + @starting_ammunition = @battleship.ammunition + end + def test_will_decrease_ammunition_when_firing - battleship = Battleship.new - starting_ammunition = battleship.ammunition - battleship.fire! - assert_equal (starting_ammunition - 1), battleship.ammunition + @battleship.fire! + assert_equal (@starting_ammunition - 1), @battleship.ammunition + end + + def test_can_request_more_ammunition + extra_ammunition = 10 + @battleship.more_ammo!(extra_ammunition) + assert_equal (@starting_ammunition + extra_ammunition), @battleship.ammunition end end From 5ec2b9e53300ba0421f28d264e9cf36b9caa5ab3 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 13 Feb 2014 09:03:39 -0800 Subject: [PATCH 2/7] Added test specification to confirm that the Battleship can request more ammunition. --- minitest/navy_test.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index f431b46..99f379b 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -25,6 +25,7 @@ def test_have_a_battleship class TestBattleship< MiniTest::Unit::TestCase def setup + @mock_battleship = MiniTest::Mock.new @battleship = Battleship.new @starting_ammunition = @battleship.ammunition end @@ -35,10 +36,21 @@ def test_will_decrease_ammunition_when_firing end def test_can_request_more_ammunition + @mock_battleship.expect :reload!, nil + @mock_battleship.reload! + @mock_battleship.verify + end + + def test_can_receive_more_ammunition extra_ammunition = 10 - @battleship.more_ammo!(extra_ammunition) + @battleship.reload!(extra_ammunition) assert_equal (@starting_ammunition + extra_ammunition), @battleship.ammunition end + + def test_skip_to_my_loo + skip("reason for skipping the test") + end + end describe Battleship do From c6ac1d9af303d20b4a729e3fa56c1d9c9e055859 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 13 Feb 2014 09:09:58 -0800 Subject: [PATCH 3/7] Corrected Battleship#reload. --- minitest/navy.rb | 2 +- minitest/navy_test.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index 1eb4341..a9abfcb 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -23,7 +23,7 @@ def fire! @ammunition = @ammunition - 1 end - def more_ammo!(extra_ammunition) + def reload!(extra_ammunition) @ammunition += extra_ammunition end end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index 99f379b..7e46af0 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -47,10 +47,6 @@ def test_can_receive_more_ammunition assert_equal (@starting_ammunition + extra_ammunition), @battleship.ammunition end - def test_skip_to_my_loo - skip("reason for skipping the test") - end - end describe Battleship do From d00706563253473164676f38e1ae8ddc6aa21e4e Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 13 Feb 2014 09:45:19 -0800 Subject: [PATCH 4/7] Added Eagle Level features. --- minitest/fate.rb | 2 ++ minitest/navy.rb | 11 +++++++++++ minitest/navy_test.rb | 6 ++++++ 3 files changed, 19 insertions(+) create mode 100644 minitest/fate.rb diff --git a/minitest/fate.rb b/minitest/fate.rb new file mode 100644 index 0000000..93ac265 --- /dev/null +++ b/minitest/fate.rb @@ -0,0 +1,2 @@ +class Fate +end \ No newline at end of file diff --git a/minitest/navy.rb b/minitest/navy.rb index a9abfcb..f1bc814 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -10,16 +10,27 @@ def fire_upon_target def has_a_ship? true end +end + +module Fate + FATE = def hit_or_miss + result = rand(5)+1 + return true if result.even? + return false if result.odd? + end end class Battleship + include Fate attr_accessor :ammunition def initialize @ammunition = 10 + @hit = false end def fire! + @hit = hit_or_miss @ammunition = @ammunition - 1 end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index 7e46af0..63157fe 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -47,6 +47,12 @@ def test_can_receive_more_ammunition assert_equal (@starting_ammunition + extra_ammunition), @battleship.ammunition end + def test_hit_or_miss + @mock_battleship.expect :fire!, [Fate::FATE] + @mock_battleship.fire! + @mock_battleship.verify + end + end describe Battleship do From 59bcd086dc65143dc0a60132af0c9c130bcf06b4 Mon Sep 17 00:00:00 2001 From: drammopo Date: Thu, 13 Feb 2014 20:27:30 +0200 Subject: [PATCH 5/7] Removed FATE constant. Using a Fate class contained in the Fate Module. Cleaned up Fate::Fate#hit_or_miss. --- minitest/navy.rb | 9 +++++---- minitest/navy_test.rb | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index f1bc814..a70a8a7 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -14,11 +14,12 @@ def has_a_ship? module Fate - FATE = def hit_or_miss + class Fate + def hit_or_miss result = rand(5)+1 - return true if result.even? - return false if result.odd? + result.even? end + end end class Battleship @@ -30,7 +31,7 @@ def initialize end def fire! - @hit = hit_or_miss + @hit = Fate.new.hit_or_miss @ammunition = @ammunition - 1 end diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index 63157fe..d8cc1d3 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -48,7 +48,7 @@ def test_can_receive_more_ammunition end def test_hit_or_miss - @mock_battleship.expect :fire!, [Fate::FATE] + @mock_battleship.expect :fire!, [true, false] @mock_battleship.fire! @mock_battleship.verify end From 2e5462016a753a41349da7cc6555ff8034b4cdec Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 03:52:14 +0200 Subject: [PATCH 6/7] Added hit counter. --- minitest/navy.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index a70a8a7..e55b271 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -8,11 +8,10 @@ def fire_upon_target end def has_a_ship? - true + @battleship.max_hits > 0 end end - module Fate class Fate def hit_or_miss @@ -24,14 +23,19 @@ def hit_or_miss class Battleship include Fate - attr_accessor :ammunition + + INITIAL_AMMUNITION = 10 + MAX_HITS = 10 + + attr_accessor :ammunition, :max_hits + def initialize - @ammunition = 10 - @hit = false + @ammunition = INITIAL_AMMUNITION + @max_hits = MAX_HITS end def fire! - @hit = Fate.new.hit_or_miss + @max_hits = @max_hits - 1 if Fate.new.hit_or_miss @ammunition = @ammunition - 1 end From 668735caedccb5bb0f72f6229a59eca84d4b795b Mon Sep 17 00:00:00 2001 From: drammopo Date: Fri, 14 Feb 2014 18:44:28 +0200 Subject: [PATCH 7/7] Added Admiral#battleship reader. Using max_hits counter to check if 'Admiral has a ship'. --- minitest/navy.rb | 7 ++++--- minitest/navy_test.rb | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/minitest/navy.rb b/minitest/navy.rb index e55b271..fb0e7cb 100644 --- a/minitest/navy.rb +++ b/minitest/navy.rb @@ -1,4 +1,5 @@ class Admiral + attr_reader :battleship def initialize(battleship) @battleship = battleship end @@ -7,9 +8,9 @@ def fire_upon_target @battleship.fire! end - def has_a_ship? - @battleship.max_hits > 0 - end + #def has_a_ship? + #@battleship.max_hits > 0 + #end end module Fate diff --git a/minitest/navy_test.rb b/minitest/navy_test.rb index d8cc1d3..06fb229 100644 --- a/minitest/navy_test.rb +++ b/minitest/navy_test.rb @@ -18,7 +18,7 @@ def test_can_tell_the_battleship_to_fire end def test_have_a_battleship - assert_equal true, @another_admiral.has_a_ship? + assert_equal true, @another_admiral.battleship.max_hits > 0 end end