games_dice 0.3.12 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/dice_spec.rb CHANGED
@@ -1,34 +1,33 @@
1
- require 'helpers'
2
-
3
- describe GamesDice::Dice do
4
-
5
- describe "dice scheme" do
6
-
7
- before :each do
8
- srand(67809)
9
- end
10
-
11
- describe '1d10+2' do
12
- let(:dice) { GamesDice::Dice.new( [ { :sides => 10, :ndice => 1 } ], 2 ) }
13
-
14
- it "should simulate rolling a ten-sided die, and adding two to each result" do
15
- [5,4,10,10,7,5,9].each do |expected_total|
16
- dice.roll.should == expected_total
17
- dice.result.should == expected_total
18
- end
19
- end
20
- end
21
-
22
- describe '2d6+6' do
23
- let(:dice) { GamesDice::Dice.new( [ { :sides => 6, :ndice => 2 } ], 6) }
24
-
25
- it "should simulate rolling two six-sided dice and adding six to the result" do
26
- [15,12,17,15,13,13,16].each do |expected_total|
27
- dice.roll.should == expected_total
28
- dice.result.should == expected_total
29
- end
30
- end
31
- end
32
-
33
- end
34
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'helpers'
4
+
5
+ describe GamesDice::Dice do
6
+ describe 'dice scheme' do
7
+ before :each do
8
+ srand(67_809)
9
+ end
10
+
11
+ describe '1d10+2' do
12
+ let(:dice) { GamesDice::Dice.new([{ sides: 10, ndice: 1 }], 2) }
13
+
14
+ it 'should simulate rolling a ten-sided die, and adding two to each result' do
15
+ [5, 4, 10, 10, 7, 5, 9].each do |expected_total|
16
+ expect(dice.roll).to eql expected_total
17
+ expect(dice.result).to eql expected_total
18
+ end
19
+ end
20
+ end
21
+
22
+ describe '2d6+6' do
23
+ let(:dice) { GamesDice::Dice.new([{ sides: 6, ndice: 2 }], 6) }
24
+
25
+ it 'should simulate rolling two six-sided dice and adding six to the result' do
26
+ [15, 12, 17, 15, 13, 13, 16].each do |expected_total|
27
+ expect(dice.roll).to eql expected_total
28
+ expect(dice.result).to eql expected_total
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,261 +1,254 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'helpers'
2
4
 
3
5
  describe GamesDice::DieResult do
4
-
5
- describe ".new" do
6
-
6
+ describe '.new' do
7
7
  it "should work without parameters to represent 'no results yet'" do
8
- die_result = GamesDice::DieResult.new()
9
- die_result.value.should == nil
10
- die_result.rolls.should == []
11
- die_result.roll_reasons.should == []
8
+ die_result = GamesDice::DieResult.new
9
+ expect(die_result.value).to eql nil
10
+ expect(die_result.rolls).to eql []
11
+ expect(die_result.roll_reasons).to eql []
12
12
  end
13
13
 
14
- it "should work with a single Integer param to represent an initial result" do
14
+ it 'should work with a single Integer param to represent an initial result' do
15
15
  die_result = GamesDice::DieResult.new(8)
16
- die_result.value.should == 8
17
- die_result.rolls.should == [8]
18
- die_result.roll_reasons.should == [:basic]
16
+ expect(die_result.value).to eql 8
17
+ expect(die_result.rolls).to eql [8]
18
+ expect(die_result.roll_reasons).to eql [:basic]
19
19
  end
20
20
 
21
- it "should not accept a param that cannot be coerced to Integer" do
22
- lambda { GamesDice::DieResult.new([]) }.should raise_error( TypeError )
23
- lambda { GamesDice::DieResult.new('N') }.should raise_error( ArgumentError )
21
+ it 'should not accept a param that cannot be coerced to Integer' do
22
+ expect(-> { GamesDice::DieResult.new([]) }).to raise_error(TypeError)
23
+ expect(-> { GamesDice::DieResult.new('N') }).to raise_error(ArgumentError)
24
24
  end
25
25
 
26
- it "should not accept unknown reasons for making a roll" do
27
- lambda { GamesDice::DieResult.new(8,'wooo') }.should raise_error( ArgumentError )
28
- lambda { GamesDice::DieResult.new(8,:frabulous) }.should raise_error( ArgumentError )
26
+ it 'should not accept unknown reasons for making a roll' do
27
+ expect(-> { GamesDice::DieResult.new(8, 'wooo') }).to raise_error(ArgumentError)
28
+ expect(-> { GamesDice::DieResult.new(8, :frabulous) }).to raise_error(ArgumentError)
29
29
  end
30
-
31
30
  end
32
31
 
33
- describe "#add_roll" do
34
-
32
+ describe '#add_roll' do
35
33
  context "starting from 'no results yet'" do
36
- let(:die_result) { GamesDice::DieResult.new() }
34
+ let(:die_result) { GamesDice::DieResult.new }
37
35
 
38
- it "should create an initial result" do
36
+ it 'should create an initial result' do
39
37
  die_result.add_roll(5)
40
- die_result.value.should == 5
41
- die_result.rolls.should == [5]
42
- die_result.roll_reasons.should == [:basic]
38
+ expect(die_result.value).to eql 5
39
+ expect(die_result.rolls).to eql [5]
40
+ expect(die_result.roll_reasons).to eql [:basic]
43
41
  end
44
42
 
45
- it "should accept non-basic reasons for the first roll" do
46
- die_result.add_roll(4,:reroll_subtract)
47
- die_result.value.should == -4
48
- die_result.rolls.should == [4]
49
- die_result.roll_reasons.should == [:reroll_subtract]
43
+ it 'should accept non-basic reasons for the first roll' do
44
+ die_result.add_roll(4, :reroll_subtract)
45
+ expect(die_result.value).to eql(-4)
46
+ expect(die_result.rolls).to eql [4]
47
+ expect(die_result.roll_reasons).to eql [:reroll_subtract]
50
48
  end
51
49
 
52
- it "should not accept a first param that cannot be coerced to Integer" do
53
- lambda { die_result.add_roll([]) }.should raise_error( TypeError )
54
- lambda { die_result.add_roll('N') }.should raise_error( ArgumentError )
50
+ it 'should not accept a first param that cannot be coerced to Integer' do
51
+ expect(-> { die_result.add_roll([]) }).to raise_error(TypeError)
52
+ expect(-> { die_result.add_roll('N') }).to raise_error(ArgumentError)
55
53
  end
56
54
 
57
- it "should not accept an unsupported second param" do
58
- lambda { die_result.add_roll(5,[]) }.should raise_error( ArgumentError )
59
- lambda { die_result.add_roll(15,:bam) }.should raise_error( ArgumentError )
55
+ it 'should not accept an unsupported second param' do
56
+ expect(-> { die_result.add_roll(5, []) }).to raise_error(ArgumentError)
57
+ expect(-> { die_result.add_roll(15, :bam) }).to raise_error(ArgumentError)
60
58
  end
61
-
62
59
  end
63
60
 
64
- context "starting with an initial result" do
61
+ context 'starting with an initial result' do
65
62
  let(:die_result) { GamesDice::DieResult.new(7) }
66
63
 
67
- it "should not accept a first param that cannot be coerced to Integer" do
68
- lambda { die_result.add_roll([]) }.should raise_error( TypeError )
69
- lambda { die_result.add_roll('N') }.should raise_error( ArgumentError )
64
+ it 'should not accept a first param that cannot be coerced to Integer' do
65
+ expect(-> { die_result.add_roll([]) }).to raise_error(TypeError)
66
+ expect(-> { die_result.add_roll('N') }).to raise_error(ArgumentError)
70
67
  end
71
68
 
72
- it "should not accept an unsupported second param" do
73
- lambda { die_result.add_roll(5,[]) }.should raise_error( ArgumentError )
74
- lambda { die_result.add_roll(15,:bam) }.should raise_error( ArgumentError )
69
+ it 'should not accept an unsupported second param' do
70
+ expect(-> { die_result.add_roll(5, []) }).to raise_error(ArgumentError)
71
+ expect(-> { die_result.add_roll(15, :bam) }).to raise_error(ArgumentError)
75
72
  end
76
73
 
77
- context "add another basic roll" do
78
- it "should replace an initial result, as if the die were re-rolled" do
74
+ context 'add another basic roll' do
75
+ it 'should replace an initial result, as if the die were re-rolled' do
79
76
  die_result.add_roll(5)
80
- die_result.value.should == 5
81
- die_result.rolls.should == [7,5]
82
- die_result.roll_reasons.should == [:basic, :basic]
77
+ expect(die_result.value).to eql 5
78
+ expect(die_result.rolls).to eql [7, 5]
79
+ expect(die_result.roll_reasons).to eql %i[basic basic]
83
80
  end
84
81
  end
85
82
 
86
- context "exploding dice" do
87
- it "should add to value when exploding up" do
88
- die_result.add_roll( 6, :reroll_add )
89
- die_result.value.should == 13
90
- die_result.rolls.should == [7,6]
91
- die_result.roll_reasons.should == [:basic, :reroll_add]
83
+ context 'exploding dice' do
84
+ it 'should add to value when exploding up' do
85
+ die_result.add_roll(6, :reroll_add)
86
+ expect(die_result.value).to eql 13
87
+ expect(die_result.rolls).to eql [7, 6]
88
+ expect(die_result.roll_reasons).to eql %i[basic reroll_add]
92
89
  end
93
90
 
94
- it "should subtract from value when exploding down" do
95
- die_result.add_roll( 4, :reroll_subtract )
96
- die_result.value.should == 3
97
- die_result.rolls.should == [7,4]
98
- die_result.roll_reasons.should == [:basic, :reroll_subtract]
91
+ it 'should subtract from value when exploding down' do
92
+ die_result.add_roll(4, :reroll_subtract)
93
+ expect(die_result.value).to eql 3
94
+ expect(die_result.rolls).to eql [7, 4]
95
+ expect(die_result.roll_reasons).to eql %i[basic reroll_subtract]
99
96
  end
100
97
  end
101
98
 
102
- context "re-roll dice" do
103
- it "should optionally replace roll unconditionally" do
104
- die_result.add_roll( 2, :reroll_replace )
105
- die_result.value.should == 2
106
- die_result.rolls.should == [7,2]
107
- die_result.roll_reasons.should == [:basic, :reroll_replace]
108
-
109
- die_result.add_roll( 5, :reroll_replace )
110
- die_result.value.should == 5
111
- die_result.rolls.should == [7,2,5]
112
- die_result.roll_reasons.should == [:basic, :reroll_replace, :reroll_replace]
99
+ context 're-roll dice' do
100
+ it 'should optionally replace roll unconditionally' do
101
+ die_result.add_roll(2, :reroll_replace)
102
+ expect(die_result.value).to eql 2
103
+ expect(die_result.rolls).to eql [7, 2]
104
+ expect(die_result.roll_reasons).to eql %i[basic reroll_replace]
105
+
106
+ die_result.add_roll(5, :reroll_replace)
107
+ expect(die_result.value).to eql 5
108
+ expect(die_result.rolls).to eql [7, 2, 5]
109
+ expect(die_result.roll_reasons).to eql %i[basic reroll_replace reroll_replace]
113
110
  end
114
111
 
115
- it "should optionally use best roll" do
116
- die_result.add_roll( 2, :reroll_use_best )
117
- die_result.value.should == 7
118
- die_result.rolls.should == [7,2]
119
- die_result.roll_reasons.should == [:basic, :reroll_use_best]
112
+ it 'should optionally use best roll' do
113
+ die_result.add_roll(2, :reroll_use_best)
114
+ expect(die_result.value).to eql 7
115
+ expect(die_result.rolls).to eql [7, 2]
116
+ expect(die_result.roll_reasons).to eql %i[basic reroll_use_best]
120
117
 
121
- die_result.add_roll( 9, :reroll_use_best )
122
- die_result.value.should == 9
123
- die_result.rolls.should == [7,2,9]
124
- die_result.roll_reasons.should == [:basic, :reroll_use_best, :reroll_use_best]
118
+ die_result.add_roll(9, :reroll_use_best)
119
+ expect(die_result.value).to eql 9
120
+ expect(die_result.rolls).to eql [7, 2, 9]
121
+ expect(die_result.roll_reasons).to eql %i[basic reroll_use_best reroll_use_best]
125
122
  end
126
123
 
127
- it "should optionally use worst roll" do
128
- die_result.add_roll( 4, :reroll_use_worst )
129
- die_result.value.should == 4
130
- die_result.rolls.should == [7,4]
131
- die_result.roll_reasons.should == [:basic, :reroll_use_worst]
124
+ it 'should optionally use worst roll' do
125
+ die_result.add_roll(4, :reroll_use_worst)
126
+ expect(die_result.value).to eql 4
127
+ expect(die_result.rolls).to eql [7, 4]
128
+ expect(die_result.roll_reasons).to eql %i[basic reroll_use_worst]
132
129
 
133
- die_result.add_roll( 5, :reroll_use_worst )
134
- die_result.value.should == 4
135
- die_result.rolls.should == [7,4,5]
136
- die_result.roll_reasons.should == [:basic, :reroll_use_worst, :reroll_use_worst]
130
+ die_result.add_roll(5, :reroll_use_worst)
131
+ expect(die_result.value).to eql 4
132
+ expect(die_result.rolls).to eql [7, 4, 5]
133
+ expect(die_result.roll_reasons).to eql %i[basic reroll_use_worst reroll_use_worst]
137
134
  end
138
135
  end
139
136
 
140
- context "combinations of reroll reasons" do
141
- it "should correctly handle valid reasons for extra rolls in combination" do
142
- die_result.add_roll( 10, :reroll_add )
143
- die_result.add_roll( 3, :reroll_subtract)
144
- die_result.value.should == 14
145
- die_result.rolls.should == [7,10,3]
146
- die_result.roll_reasons.should == [:basic, :reroll_add, :reroll_subtract]
147
-
148
- die_result.add_roll( 12, :reroll_replace )
149
- die_result.value.should == 12
150
- die_result.rolls.should == [7,10,3,12]
151
- die_result.roll_reasons.should == [:basic, :reroll_add, :reroll_subtract, :reroll_replace]
152
-
153
- die_result.add_roll( 9, :reroll_use_best )
154
- die_result.value.should == 12
155
- die_result.rolls.should == [7,10,3,12,9]
156
- die_result.roll_reasons.should == [:basic, :reroll_add, :reroll_subtract, :reroll_replace, :reroll_use_best]
157
-
158
- die_result.add_roll( 15, :reroll_add)
159
- die_result.value.should == 27
160
- die_result.rolls.should == [7,10,3,12,9,15]
161
- die_result.roll_reasons.should == [:basic, :reroll_add, :reroll_subtract, :reroll_replace, :reroll_use_best, :reroll_add]
137
+ context 'combinations of reroll reasons' do
138
+ it 'should correctly handle valid reasons for extra rolls in combination' do
139
+ die_result.add_roll(10, :reroll_add)
140
+ die_result.add_roll(3, :reroll_subtract)
141
+ expect(die_result.value).to eql 14
142
+ expect(die_result.rolls).to eql [7, 10, 3]
143
+ expect(die_result.roll_reasons).to eql %i[basic reroll_add reroll_subtract]
144
+
145
+ die_result.add_roll(12, :reroll_replace)
146
+ expect(die_result.value).to eql 12
147
+ expect(die_result.rolls).to eql [7, 10, 3, 12]
148
+ expect(die_result.roll_reasons).to eql %i[basic reroll_add reroll_subtract reroll_replace]
149
+
150
+ die_result.add_roll(9, :reroll_use_best)
151
+ expect(die_result.value).to eql 12
152
+ expect(die_result.rolls).to eql [7, 10, 3, 12, 9]
153
+ expect(die_result.roll_reasons).to eql %i[basic reroll_add reroll_subtract reroll_replace
154
+ reroll_use_best]
155
+
156
+ die_result.add_roll(15, :reroll_add)
157
+ expect(die_result.value).to eql 27
158
+ expect(die_result.rolls).to eql [7, 10, 3, 12, 9, 15]
159
+ expect(die_result.roll_reasons).to eql %i[basic reroll_add reroll_subtract reroll_replace
160
+ reroll_use_best reroll_add]
162
161
  end
163
162
  end
164
-
165
163
  end
166
-
167
164
  end
168
165
 
169
- describe "#explain_value" do
170
- let(:die_result) { GamesDice::DieResult.new() }
166
+ describe '#explain_value' do
167
+ let(:die_result) { GamesDice::DieResult.new }
171
168
 
172
169
  it "should be empty string for 'no results yet'" do
173
- die_result.explain_value.should == ''
170
+ expect(die_result.explain_value).to eql ''
174
171
  end
175
172
 
176
- it "should be a simple stringified number when there is one die roll" do
173
+ it 'should be a simple stringified number when there is one die roll' do
177
174
  die_result.add_roll(3)
178
- die_result.explain_value.should == '3'
175
+ expect(die_result.explain_value).to eql '3'
179
176
  end
180
177
 
181
- it "should describe all single rolls made and how they combine" do
178
+ it 'should describe all single rolls made and how they combine' do
182
179
  die_result.add_roll(6)
183
- die_result.explain_value.should == '6'
184
-
185
- die_result.add_roll(5,:reroll_add)
186
- die_result.explain_value.should == '[6+5] 11'
180
+ expect(die_result.explain_value).to eql '6'
187
181
 
188
- die_result.add_roll(2,:reroll_replace)
189
- die_result.explain_value.should == '[6+5|2] 2'
182
+ die_result.add_roll(5, :reroll_add)
183
+ expect(die_result.explain_value).to eql '[6+5] 11'
190
184
 
191
- die_result.add_roll(7,:reroll_subtract)
192
- die_result.explain_value.should == '[6+5|2-7] -5'
185
+ die_result.add_roll(2, :reroll_replace)
186
+ expect(die_result.explain_value).to eql '[6+5|2] 2'
193
187
 
194
- die_result.add_roll(4,:reroll_use_worst)
195
- die_result.explain_value.should == '[6+5|2-7\\4] -5'
188
+ die_result.add_roll(7, :reroll_subtract)
189
+ expect(die_result.explain_value).to eql '[6+5|2-7] -5'
196
190
 
197
- die_result.add_roll(3,:reroll_use_best)
198
- die_result.explain_value.should == '[6+5|2-7\\4/3] 3'
191
+ die_result.add_roll(4, :reroll_use_worst)
192
+ expect(die_result.explain_value).to eql '[6+5|2-7\\4] -5'
199
193
 
194
+ die_result.add_roll(3, :reroll_use_best)
195
+ expect(die_result.explain_value).to eql '[6+5|2-7\\4/3] 3'
200
196
  end
201
-
202
197
  end
203
198
 
204
- it "should combine via +,- and * intuitively based on #value" do
199
+ it 'should combine via +,- and * intuitively based on #value' do
205
200
  die_result = GamesDice::DieResult.new(7)
206
- (die_result + 3).should == 10
207
- (4 + die_result).should == 11
208
- (die_result - 2).should == 5
209
- (9 - die_result).should == 2
201
+ expect((die_result + 3)).to eql 10
202
+ expect((4 + die_result)).to eql 11
203
+ expect((die_result - 2)).to eql 5
204
+ expect((9 - die_result)).to eql 2
210
205
 
211
- (die_result + 7.7).should == 14.7
212
- (4.1 + die_result).should == 11.1
206
+ expect((die_result + 7.7)).to eql 14.7
207
+ expect((4.1 + die_result)).to eql 11.1
213
208
 
214
- (die_result * 2).should == 14
215
- (1 * die_result).should == 7
209
+ expect((die_result * 2)).to eql 14
210
+ expect((1 * die_result)).to eql 7
216
211
 
217
212
  other_die_result = GamesDice::DieResult.new(6)
218
- other_die_result.add_roll(6,:reroll_add)
219
- (die_result + other_die_result).should == 19
220
- (other_die_result - die_result).should == 5
213
+ other_die_result.add_roll(6, :reroll_add)
214
+ expect((die_result + other_die_result)).to eql 19
215
+ expect((other_die_result - die_result)).to eql 5
221
216
  end
222
217
 
223
- it "should support comparison with >,<,>=,<= as if it were an integer, based on #value" do
218
+ it 'should support comparison with >,<,>=,<= as if it were an integer, based on #value' do
224
219
  die_result = GamesDice::DieResult.new(7)
225
220
 
226
- (die_result > 3).should == true
227
- (14 > die_result).should == true
228
- (die_result >= 7).should == true
229
- (9.5 >= die_result).should == true
230
- (die_result < 3).should == false
231
- (14 < die_result).should == false
232
- (die_result <= 8).should == true
233
- (14 <= die_result).should == false
221
+ expect((die_result > 3)).to eql true
222
+ expect((die_result < 14)).to eql true
223
+ expect((die_result >= 7)).to eql true
224
+ expect((die_result <= 9.5)).to eql true
225
+ expect((die_result < 3)).to eql false
226
+ expect((die_result > 14)).to eql false
227
+ expect((die_result <= 8)).to eql true
228
+ expect((die_result >= 14)).to eql false
234
229
 
235
230
  other_die_result = GamesDice::DieResult.new(6)
236
- other_die_result.add_roll(6,:reroll_add)
237
- (die_result > other_die_result).should == false
238
- (other_die_result > die_result).should == true
239
- (die_result >= other_die_result).should == false
240
- (other_die_result >= die_result).should == true
241
- (die_result < other_die_result).should == true
242
- (other_die_result < die_result).should == false
243
- (die_result <= other_die_result).should == true
244
- (other_die_result <= die_result).should == false
245
-
231
+ other_die_result.add_roll(6, :reroll_add)
232
+ expect((die_result > other_die_result)).to eql false
233
+ expect((other_die_result > die_result)).to eql true
234
+ expect((die_result >= other_die_result)).to eql false
235
+ expect((other_die_result >= die_result)).to eql true
236
+ expect((die_result < other_die_result)).to eql true
237
+ expect((other_die_result < die_result)).to eql false
238
+ expect((die_result <= other_die_result)).to eql true
239
+ expect((other_die_result <= die_result)).to eql false
246
240
  end
247
241
 
248
- it "should sort, based on #value" do
242
+ it 'should sort, based on #value' do
249
243
  die_results = [
250
244
  GamesDice::DieResult.new(7), GamesDice::DieResult.new(5), GamesDice::DieResult.new(8), GamesDice::DieResult.new(3)
251
245
  ]
252
246
 
253
247
  die_results.sort!
254
248
 
255
- die_results[0].value.should == 3
256
- die_results[1].value.should == 5
257
- die_results[2].value.should == 7
258
- die_results[3].value.should == 8
249
+ expect(die_results[0].value).to eql 3
250
+ expect(die_results[1].value).to eql 5
251
+ expect(die_results[2].value).to eql 7
252
+ expect(die_results[3].value).to eql 8
259
253
  end
260
-
261
254
  end