console-poker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,277 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Hand do
4
+ let(:ace) { build(:card, :ace, poker:) }
5
+ let(:three) { build(:card, :three, poker:) }
6
+ let(:five) { build(:card, :five, poker:) }
7
+ let(:seven) { build(:card, :seven, poker:) }
8
+ let(:ten) { build(:card, :ten, poker:) }
9
+ let(:poker) { build(:poker, :with_deck) }
10
+ let(:hand) { build(:hand, poker:) }
11
+
12
+ describe '.new' do
13
+ it 'creates a hand' do
14
+ expect(hand).to be
15
+ end
16
+
17
+ it 'has a bet' do
18
+ expect(hand.bet).to eq(500)
19
+ end
20
+ end
21
+
22
+ describe '#deal_card' do
23
+ it 'adds a card to the hand' do
24
+ expect do
25
+ hand.deal_card
26
+ end.to change { hand.cards.size }.by(1)
27
+ end
28
+ end
29
+
30
+ describe '#pay' do
31
+ context 'when a winning hand is unpayed' do
32
+ let(:hand) { build(:hand, :one_pair, poker:) }
33
+
34
+ it 'adds the result to the poker money' do
35
+ expect do
36
+ hand.pay
37
+ end.to change(poker, :money).by(500)
38
+ end
39
+ end
40
+
41
+ context 'when a losing hand is unpayed' do
42
+ let(:hand) { build(:hand, :unknown, poker:) }
43
+
44
+ it 'subtracts the result from the poker money' do
45
+ expect do
46
+ hand.pay
47
+ end.to change(poker, :money).by(-500)
48
+ end
49
+ end
50
+
51
+ context 'when hand is already payed' do
52
+ before { hand.result = 500 }
53
+
54
+ it 'ignores a hand with a result' do
55
+ expect do
56
+ hand.pay
57
+ end.not_to change(hand, :result)
58
+ end
59
+ end
60
+ end
61
+
62
+ describe '#draw' do
63
+ before do
64
+ hand.cards << ace << ace << ten << ten << ten
65
+ poker.hand = hand
66
+ end
67
+
68
+ it 'draws the hand' do
69
+ expected = " 🂡 🂡 🂪 🂪 🂪 $5.00 \n ⇑ \n\n"
70
+ expect(hand.draw).to eq(expected)
71
+ end
72
+
73
+ context 'with face type 2' do
74
+ before { poker.face_type = 2 }
75
+
76
+ it 'draws the hand' do
77
+ expected = " A♠ A♠ T♠ T♠ T♠ $5.00 \n ⇑ \n\n"
78
+ expect(hand.draw).to eq(expected)
79
+ end
80
+ end
81
+
82
+ context 'with a positive result' do
83
+ before { hand.result = 500 }
84
+
85
+ it 'draws the hand' do
86
+ expected = " 🂡 🂡 🂪 🂪 🂪 +$5.00 \n ⇑ \n\n"
87
+ expect(hand.draw).to eq(expected)
88
+ end
89
+ end
90
+
91
+ context 'with a negative result' do
92
+ before { hand.result = -500 }
93
+
94
+ it 'draws the hand' do
95
+ expected = " 🂡 🂡 🂪 🂪 🂪 -$5.00 \n ⇑ \n\n"
96
+ expect(hand.draw).to eq(expected)
97
+ end
98
+ end
99
+
100
+ context 'with a discarded card' do
101
+ before { hand.discards << 2 }
102
+
103
+ it 'draws the hand' do
104
+ expected = " 🂡 🂡 🂠 🂪 🂪 $5.00 \n ⇑ \n\n"
105
+ expect(hand.draw).to eq(expected)
106
+ end
107
+ end
108
+
109
+ context 'with a rank' do
110
+ before { hand.rank = :full_house }
111
+
112
+ it 'draws the hand' do
113
+ expected = " 🂡 🂡 🂪 🂪 🂪 ⇒ Full House $5.00 \n ⇑ \n\n"
114
+ expect(hand.draw).to eq(expected)
115
+ end
116
+ end
117
+ end
118
+
119
+ describe '#replace_discards' do
120
+ before do
121
+ hand.cards << ace << three << five << seven << ten
122
+ end
123
+
124
+ it 'replaces the discards' do
125
+ hand.discards = [0, 2, 4]
126
+ hand.replace_discards
127
+ expect(hand.discards).to be_empty
128
+ end
129
+ end
130
+
131
+ describe '#normalize_current_card' do
132
+ it 'increases the current card to 0' do
133
+ hand.current_card = -1
134
+ hand.normalize_current_card
135
+ expect(hand.current_card).to eq(0)
136
+ end
137
+
138
+ it 'reduces the current card to 4' do
139
+ hand.current_card = 5
140
+ hand.normalize_current_card
141
+ expect(hand.current_card).to eq(4)
142
+ end
143
+ end
144
+
145
+ describe '#clear_draw_hand_actions' do
146
+ before do
147
+ hand.cards << ace << three << five << seven << ten
148
+ poker.hand = hand
149
+ allow(Poker).to receive(:getc).and_return('x')
150
+ allow(poker).to receive(:puts)
151
+ allow(hand).to receive(:puts)
152
+ end
153
+
154
+ it 'calls normalize_current_card' do
155
+ allow(hand).to receive(:normalize_current_card)
156
+ hand.clear_draw_hand_actions
157
+ expect(hand).to have_received(:normalize_current_card)
158
+ end
159
+
160
+ it 'calls clear' do
161
+ allow(poker).to receive(:clear)
162
+ hand.clear_draw_hand_actions
163
+ expect(poker).to have_received(:clear)
164
+ end
165
+
166
+ it 'calls draw_hand' do
167
+ allow(poker).to receive(:draw_hand)
168
+ hand.clear_draw_hand_actions
169
+ expect(poker).to have_received(:draw_hand)
170
+ end
171
+
172
+ it 'calls ask_hand_action' do
173
+ allow(hand).to receive(:ask_hand_action)
174
+ hand.clear_draw_hand_actions
175
+ expect(hand).to have_received(:ask_hand_action)
176
+ end
177
+ end
178
+
179
+ describe '#ask_hand_action' do
180
+ before do
181
+ hand.cards << ace << three << five << seven << ten
182
+ poker.hand = hand
183
+ allow(poker).to receive(:puts)
184
+ allow(hand).to receive(:puts)
185
+ end
186
+
187
+ context 'when keeping current card' do
188
+ before do
189
+ allow(Poker).to receive(:getc).and_return('k', 'x', 'q')
190
+ allow(hand.discards).to receive(:delete).with(0)
191
+ allow(hand).to receive(:current_card=).with(1)
192
+ allow(hand).to receive(:current_card=).with(nil)
193
+ allow(hand).to receive(:clear_draw_hand_actions).and_call_original
194
+ end
195
+
196
+ it 'increments the current card' do
197
+ hand.ask_hand_action
198
+ expect(hand.discards).to have_received(:delete).with(0)
199
+ expect(hand).to have_received(:current_card=).with(1)
200
+ expect(hand).to have_received(:current_card=).with(nil)
201
+ expect(hand).to have_received(:clear_draw_hand_actions)
202
+ end
203
+ end
204
+
205
+ context 'when discarding current card' do
206
+ before do
207
+ allow(Poker).to receive(:getc).and_return('d', 'x', 'q')
208
+ allow(hand.discards).to receive(:<<).with(0)
209
+ allow(hand).to receive(:current_card=).with(1)
210
+ allow(hand).to receive(:current_card=).with(nil)
211
+ allow(hand).to receive(:clear_draw_hand_actions).and_call_original
212
+ end
213
+
214
+ it 'increments the current card' do
215
+ hand.ask_hand_action
216
+ expect(hand.discards).to have_received(:<<).with(0)
217
+ expect(hand).to have_received(:current_card=).with(1)
218
+ expect(hand).to have_received(:current_card=).with(nil)
219
+ expect(hand).to have_received(:clear_draw_hand_actions)
220
+ end
221
+
222
+ it 'does not add a duplicate discard' do
223
+ allow(hand.discards).to receive(:include?).and_return(true)
224
+ hand.ask_hand_action
225
+ expect(hand.discards).to_not have_received(:<<).with(0)
226
+ expect(hand).to have_received(:current_card=).with(1)
227
+ expect(hand).to have_received(:current_card=).with(nil)
228
+ expect(hand).to have_received(:clear_draw_hand_actions)
229
+ end
230
+ end
231
+
232
+ context 'when skipping the current card' do
233
+ before do
234
+ allow(Poker).to receive(:getc).and_return('n', 'x', 'q')
235
+ allow(hand).to receive(:current_card=).with(1)
236
+ allow(hand).to receive(:current_card=).with(nil)
237
+ allow(hand).to receive(:clear_draw_hand_actions).and_call_original
238
+ end
239
+
240
+ it 'increments the current card' do
241
+ hand.ask_hand_action
242
+ expect(hand).to have_received(:current_card=).with(1)
243
+ expect(hand).to have_received(:current_card=).with(nil)
244
+ expect(hand).to have_received(:clear_draw_hand_actions)
245
+ end
246
+ end
247
+
248
+ context 'when moving to previous card' do
249
+ before do
250
+ hand.current_card = 1
251
+ allow(Poker).to receive(:getc).and_return('p', 'x', 'q')
252
+ allow(hand).to receive(:current_card=).with(0)
253
+ allow(hand).to receive(:current_card=).with(nil)
254
+ allow(hand).to receive(:clear_draw_hand_actions).and_call_original
255
+ end
256
+
257
+ it 'increments the current card' do
258
+ hand.ask_hand_action
259
+ expect(hand).to have_received(:current_card=).with(0)
260
+ expect(hand).to have_received(:current_card=).with(nil)
261
+ expect(hand).to have_received(:clear_draw_hand_actions)
262
+ end
263
+ end
264
+
265
+ context 'when invalid input' do
266
+ before do
267
+ allow(Poker).to receive(:getc).and_return('z', 'x', 'q')
268
+ allow(hand).to receive(:clear_draw_hand_actions).and_call_original
269
+ end
270
+
271
+ it 'increments the current card' do
272
+ hand.ask_hand_action
273
+ expect(hand).to have_received(:clear_draw_hand_actions)
274
+ end
275
+ end
276
+ end
277
+ end