Created
October 11, 2011 17:33
-
-
Save andrewhl/1278774 to your computer and use it in GitHub Desktop.
Ruby Koan
This file contains hidden or 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
# Will that satisfy the following test cases? | |
require File.expand_path(File.dirname(__FILE__) + '/edgecase') | |
# Implement a DiceSet Class here: | |
# | |
# class DiceSet | |
# code ... | |
# end | |
class AboutDiceProject < EdgeCase::Koan | |
def test_can_create_a_dice_set | |
dice = DiceSet.new | |
assert_not_nil dice | |
end | |
def test_rolling_the_dice_returns_a_set_of_integers_between_1_and_6 | |
dice = DiceSet.new | |
dice.roll(5) | |
assert dice.values.is_a?(Array), "should be an array" | |
assert_equal 5, dice.values.size | |
dice.values.each do |value| | |
assert value >= 1 && value <= 6, "value #{value} must be between 1 and 6" | |
end | |
end | |
def test_dice_values_do_not_change_unless_explicitly_rolled | |
dice = DiceSet.new | |
dice.roll(5) | |
first_time = dice.values | |
second_time = dice.values | |
assert_equal first_time, second_time | |
end | |
def test_dice_values_should_change_between_rolls | |
dice = DiceSet.new | |
dice.roll(5) | |
first_time = dice.values | |
dice.roll(5) | |
second_time = dice.values | |
assert_not_equal first_time, second_time, | |
"Two rolls should not be equal" | |
# THINK ABOUT IT: | |
# | |
# If the rolls are random, then it is possible (although not | |
# likely) that two consecutive rolls are equal. What would be a | |
# better way to test this. | |
end | |
def test_you_can_roll_different_numbers_of_dice | |
dice = DiceSet.new | |
dice.roll(3) | |
assert_equal 3, dice.values.size | |
dice.roll(1) | |
assert_equal 1, dice.values.size | |
end | |
end |
This file contains hidden or 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
class DiceSet < Array | |
attr_reader :values | |
def initialize | |
@values = Array.new | |
end | |
def roll(number) | |
number.times do | |
@values << (1 + rand(6)) | |
puts self.to_s | |
puts "Numbers = #{self}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment