-
Notifications
You must be signed in to change notification settings - Fork 1
/
about_dice_project.rb
84 lines (65 loc) · 1.97 KB
/
about_dice_project.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require File.expand_path(File.dirname(__FILE__) + '/neo')
# Implement a DiceSet Class here:
#
class DiceSet
attr_accessor :values
def initialize
self.values ||= []
end
# roll(x) takes in the number of dice to be rolled,
# and assigns a random value (from 1 - 6) to each die.
def roll(number_of_dice)
@values = (1..number_of_dice).map { rand(6) + 1 }
# Discovered the above shorter and cleaner way on GitHub.
# number_of_dice.times.each do
# self.values << 1 + rand(6)
# end
# self.values
end
# NOTE: An @ before a value is an instance variable.
# "values" is the instance variable, and an array when being computed upon
# by the roll method.
end
class AboutDiceProject < Neo::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