Skip to content

Instantly share code, notes, and snippets.

@phildionne
Created September 28, 2012 22:17
Show Gist options
  • Save phildionne/3802352 to your computer and use it in GitHub Desktop.
Save phildionne/3802352 to your computer and use it in GitHub Desktop.
# A triangle method
def triangle(a, b, c)
if (a <= 0) || (b <= 0) || (c <= 0)
raise TriangleError, "a or b or c can't be 0 or negative"
elsif ((a + b) <= c) || ((a + c) <= b) || ((c + b) <= a)
raise TriangleError, "sum of first and second smallest must be > third"
elsif (a == b) && (b == c)
:equilateral
elsif (a == b && a != c) || (a == c && c != b) || (b == c && b != a)
:isosceles
elsif (a != b) && (a != c) && (b != c)
:scalene
end
end
# A dice class with some requirements (see below)
class DiceSet
def initialize
@h = {}
end
def roll(number)
@h.clear
number.times do |i|
@h[i + 1] = rand(6) + 1
end
@h
end
def values
@h.values
end
end
# Not written by me
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"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment