Skip to content

Commit

Permalink
completed exercise 1 of objects and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
zackforbing committed Jun 12, 2016
1 parent 38edcc8 commit 0eb3f20
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 19 deletions.
28 changes: 28 additions & 0 deletions objects-and-methods/exercise-1/lib/bag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Bag

def initialize
@candies = []
end

def empty?
candies.empty?
end

def candies
@candies
end

def count
candies.count
end

def <<(candy)
@candies << candy
end

def contains?(type)
candies.any? do |candy|
candy.type == type
end
end
end
6 changes: 6 additions & 0 deletions objects-and-methods/exercise-1/lib/candy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Candy
attr_reader :type
def initialize(type)
@type = type
end
end
6 changes: 6 additions & 0 deletions objects-and-methods/exercise-1/lib/costume.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Costume
attr_reader :style
def initialize(style)
@style = style
end
end
27 changes: 27 additions & 0 deletions objects-and-methods/exercise-1/lib/trick_or_treater.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class TrickOrTreater

def initialize(costume)
@costume = costume
@bag = Bag.new
end

def dressed_up_as
@costume.style
end

def bag
@bag
end

def has_candy?
!bag.empty?
end

def candy_count
bag.count
end

def eat
bag.candies.pop
end
end
15 changes: 7 additions & 8 deletions objects-and-methods/exercise-1/test/bag_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,39 @@ def test_a_new_bag_is_empty
end

def test_count_the_candies_in_an_empty_bag
skip
# skip
assert_equal 0, Bag.new.count
end

def test_empty_bag_has_no_candies
skip
# skip
assert_equal [], Bag.new.candies
end

def test_put_candy_in_the_bag
skip
# skip
bag = Bag.new
candy = Candy.new("Sour frogs")
bag << candy
assert_equal [candy], bag.candies
end

def test_a_bag_with_candies_is_not_empty
skip
# skip
bag = Bag.new
bag << Candy.new("Nerds")
refute bag.empty?
end

def test_bag_counts_candies
skip
# skip
bag = Bag.new
bag << Candy.new("Caramelized Almonds")
assert_equal 1, bag.count
end

def test_bag_contains_candies_and_candies_have_a_type
skip
# skip
bag = Bag.new
bag << Candy.new("Hershey's Kisses")
# You usually don't want to chain a bunch of different
Expand All @@ -54,12 +54,11 @@ def test_bag_contains_candies_and_candies_have_a_type
end

def test_ask_bag_if_it_contains_a_particular_type_of_candy
skip
# skip
bag = Bag.new
bag << Candy.new("Lindt chocolate")

assert bag.contains?("Lindt chocolate")
refute bag.contains?("Hershey's chocolate")
end
end

3 changes: 1 addition & 2 deletions objects-and-methods/exercise-1/test/candy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ def test_candy_type
end

def test_other_type_of_candy
skip
# skip
candy = Candy.new("Mars")
assert_equal "Mars", candy.type
end
end

3 changes: 1 addition & 2 deletions objects-and-methods/exercise-1/test/costume_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ def test_costume_style
end

def test_other_style_of_costume
skip
# skip
costume = Costume.new("Princess")
assert_equal "Princess", costume.style
end
end

13 changes: 6 additions & 7 deletions objects-and-methods/exercise-1/test/trick_or_treater_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ def test_wears_a_costume
end

def test_wears_a_different_costume
skip
# skip
costume = Costume.new("Pony")
trick_or_treater = TrickOrTreater.new(costume)
assert_equal "Pony", trick_or_treater.dressed_up_as
end

def test_has_an_empty_bag_by_default
skip
# skip
trick_or_treater = TrickOrTreater.new(Costume.new("Alien"))
assert trick_or_treater.bag.empty?
end

def test_an_empty_bag_means_no_candy
skip
# skip
trick_or_treater = TrickOrTreater.new(Costume.new("Knight"))
refute trick_or_treater.has_candy?
end

def test_gets_candies
skip
# skip
trick_or_treater = TrickOrTreater.new(Costume.new("Spaceship Mechanic"))
trick_or_treater.bag << Candy.new("Gummy bears")
assert trick_or_treater.has_candy?
end

def test_counts_candies
skip
# skip
trick_or_treater = TrickOrTreater.new(Costume.new("Spaceship Mechanic"))
assert_equal 0, trick_or_treater.candy_count
trick_or_treater.bag << Candy.new("Gummy bears")
assert_equal 1, trick_or_treater.candy_count
end

def test_eats_candies
skip
# skip
trick_or_treater = TrickOrTreater.new(Costume.new("Baron"))
trick_or_treater.bag << Candy.new("Gummy worms")
trick_or_treater.bag << Candy.new("Liquorice")
Expand All @@ -62,4 +62,3 @@ def test_eats_candies
assert_equal 0, trick_or_treater.candy_count
end
end

0 comments on commit 0eb3f20

Please sign in to comment.