Created
March 17, 2011 09:09
-
-
Save grahamg/874037 to your computer and use it in GitHub Desktop.
CoffeePot example class in Ruby complete with Test::Unit tests.
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
#!/usr/bin/env ruby | |
=begin | |
1) When I create a "CoffeePot" object, it should be empty and I will know this because it returns true when I ask it "coffee_pot.is_empty?", and it tells me 0 when I ask it "coffee_pot.cups_left" | |
2) When I call the "coffee_pot.fill" method it should change state to have 10 cups left in it, so when I call the "coffee_pot.cups_left" method it returns 10. If I ask "coffee_pot.is_empty?", it should return false. | |
3) every time I call the "coffee_pot.pour_cup" method, there should be one less cup remaining. | |
4) if I call "coffee_pot.pour_cup" when the pot is empty, it should raise an exception. | |
=end | |
require 'test/unit' | |
class CoffeePot | |
@number_cups | |
def initialize | |
@number_cups = 0 | |
end | |
def is_empty? | |
if @number_cups == 0 | |
return true | |
else | |
return false | |
end | |
end | |
def cups_left | |
return @number_cups | |
end | |
def pour_cup | |
if @number_cups == 0 | |
raise 'no cups remaining' | |
else | |
@number_cups -= 1 | |
end | |
end | |
def fill | |
@number_cups = 10 | |
end | |
end | |
class CoffeePotTest < Test::Unit::TestCase | |
def setup | |
@empty_coffee_pot = CoffeePot.new | |
@full_coffee_pot = CoffeePot.new | |
@full_coffee_pot.fill | |
end | |
def test_is_empty? | |
assert_equal true, @empty_coffee_pot.is_empty? | |
assert_equal false, @full_coffee_pot.is_empty? | |
end | |
def test_cups_left | |
assert_equal 0, @empty_coffee_pot.cups_left | |
assert_equal 10, @full_coffee_pot.cups_left | |
end | |
def test_pour_cup | |
@coffee_pot = CoffeePot.new | |
@coffee_pot.fill | |
@coffee_pot.pour_cup | |
assert_equal 9, @coffee_pot.cups_left | |
assert_raise(RuntimeError) { @empty_coffee_pot.pour_cup } | |
end | |
def test_fill | |
@coffee_pot = CoffeePot.new | |
@coffee_pot.fill | |
assert_equal 10, @coffee_pot.cups_left | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment