acpc_poker_match_state 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Rakefile +7 -11
- data/acpc_poker_match_state.gemspec +5 -4
- data/lib/acpc_poker_match_state/players_at_the_table.rb +3 -6
- data/lib/acpc_poker_match_state/version.rb +1 -1
- data/spec/match_state_transition_spec.rb +38 -38
- data/spec/players_at_the_table_spec.rb +155 -584
- data/spec/support/spec_helper.rb +27 -8
- metadata +43 -239
- data/spec/coverage/assets/0.5.3/app.js +0 -88
- data/spec/coverage/assets/0.5.3/fancybox/blank.gif +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_close.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_loading.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_nav_left.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_nav_right.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_e.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_n.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_ne.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_nw.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_s.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_se.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_sw.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_shadow_w.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_title_left.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_title_main.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_title_over.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancy_title_right.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancybox-x.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancybox-y.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/fancybox.png +0 -0
- data/spec/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.css +0 -363
- data/spec/coverage/assets/0.5.3/fancybox/jquery.fancybox-1.3.1.pack.js +0 -44
- data/spec/coverage/assets/0.5.3/favicon_green.png +0 -0
- data/spec/coverage/assets/0.5.3/favicon_red.png +0 -0
- data/spec/coverage/assets/0.5.3/favicon_yellow.png +0 -0
- data/spec/coverage/assets/0.5.3/highlight.css +0 -129
- data/spec/coverage/assets/0.5.3/highlight.pack.js +0 -1
- data/spec/coverage/assets/0.5.3/jquery-1.6.2.min.js +0 -18
- data/spec/coverage/assets/0.5.3/jquery.dataTables.min.js +0 -152
- data/spec/coverage/assets/0.5.3/jquery.timeago.js +0 -141
- data/spec/coverage/assets/0.5.3/jquery.url.js +0 -174
- data/spec/coverage/assets/0.5.3/loading.gif +0 -0
- data/spec/coverage/assets/0.5.3/magnify.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/spec/coverage/assets/0.5.3/smoothness/jquery-ui-1.8.4.custom.css +0 -295
- data/spec/coverage/assets/0.5.3/stylesheet.css +0 -383
- data/spec/coverage/assets/0.7.1/application.css +0 -1110
- data/spec/coverage/assets/0.7.1/application.js +0 -626
- data/spec/coverage/assets/0.7.1/fancybox/blank.gif +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_close.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_loading.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_left.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_nav_right.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_e.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_n.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_ne.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_nw.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_s.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_se.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_sw.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_shadow_w.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_left.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_main.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_over.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancy_title_right.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox-x.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox-y.png +0 -0
- data/spec/coverage/assets/0.7.1/fancybox/fancybox.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_green.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_red.png +0 -0
- data/spec/coverage/assets/0.7.1/favicon_yellow.png +0 -0
- data/spec/coverage/assets/0.7.1/loading.gif +0 -0
- data/spec/coverage/assets/0.7.1/magnify.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_222222_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_454545_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_888888_256x240.png +0 -0
- data/spec/coverage/assets/0.7.1/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/spec/coverage/index.html +0 -553
- data/spec/players_at_the_table_integ_spec.rb +0 -240
- data/spec/support/model_test_helper.rb +0 -56
- data/spec/support/test_example.rb +0 -74
@@ -1,240 +0,0 @@
|
|
1
|
-
|
2
|
-
require File.expand_path('../support/spec_helper', __FILE__)
|
3
|
-
|
4
|
-
require 'acpc_dealer'
|
5
|
-
require 'acpc_dealer_data'
|
6
|
-
|
7
|
-
require File.expand_path('../../lib/acpc_poker_match_state/players_at_the_table', __FILE__)
|
8
|
-
|
9
|
-
describe PlayersAtTheTable do
|
10
|
-
describe '#update!' do
|
11
|
-
it "keeps track of state for a sequence of match states and actions in Doyle's game" do
|
12
|
-
num_hands = 15
|
13
|
-
match_logs.each do |log_description|
|
14
|
-
@match = PokerMatchData.parse_files(
|
15
|
-
log_description.actions_file_path,
|
16
|
-
log_description.results_file_path,
|
17
|
-
log_description.player_names,
|
18
|
-
AcpcDealer::DEALER_DIRECTORY,
|
19
|
-
num_hands
|
20
|
-
)
|
21
|
-
@match.for_every_seat! do |users_seat|
|
22
|
-
|
23
|
-
@patient = PlayersAtTheTable.seat_players(
|
24
|
-
@match.match_def.game_def,
|
25
|
-
(@match.players.map{ |player| player.name }),
|
26
|
-
users_seat,
|
27
|
-
num_hands
|
28
|
-
)
|
29
|
-
|
30
|
-
check_patient
|
31
|
-
|
32
|
-
@match.for_every_hand! do
|
33
|
-
@match.for_every_turn! do
|
34
|
-
@patient.update! @match.current_hand.current_match_state
|
35
|
-
|
36
|
-
check_patient
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def check_patient(patient=@patient)
|
45
|
-
patient.player_acting_sequence.should == @match.player_acting_sequence
|
46
|
-
patient.number_of_players.should == @match.players.length
|
47
|
-
if @match.current_hand && @match.current_hand.last_action
|
48
|
-
patient.player_who_acted_last.seat.should == @match.current_hand.last_action.seat
|
49
|
-
else
|
50
|
-
patient.player_who_acted_last.should be nil
|
51
|
-
end
|
52
|
-
if @match.current_hand && @match.current_hand.next_action
|
53
|
-
patient.next_player_to_act.seat.should == @match.current_hand.next_action.seat
|
54
|
-
else
|
55
|
-
patient.next_player_to_act.should be nil
|
56
|
-
end
|
57
|
-
if @match.current_hand && @match.current_hand.final_turn?
|
58
|
-
patient.players.players_close_enough?(@match.players).should == true
|
59
|
-
patient.user_player.close_enough?(@match.player).should == true
|
60
|
-
patient.opponents.players_close_enough?(@match.opponents).should == true
|
61
|
-
patient.non_folded_players.players_close_enough?(@match.non_folded_players).should == true
|
62
|
-
patient.active_players.players_close_enough?(@match.active_players).should == true
|
63
|
-
patient.player_with_dealer_button.close_enough?(@match.player_with_dealer_button).should == true
|
64
|
-
check_player_blind_relation(patient)
|
65
|
-
patient.chip_stacks.should == @match.chip_stacks
|
66
|
-
patient.chip_balances.should == @match.chip_balances
|
67
|
-
patient.chip_contributions.sum.should == @match.chip_contributions.sum
|
68
|
-
end
|
69
|
-
patient.opponents_cards_visible?.should == @match.opponents_cards_visible?
|
70
|
-
patient.reached_showdown?.should == @match.opponents_cards_visible?
|
71
|
-
patient.less_than_two_non_folded_players?.should == @match.non_folded_players.length < 2
|
72
|
-
if @match.current_hand
|
73
|
-
patient.hand_ended?.should == @match.current_hand.final_turn?
|
74
|
-
patient.match_ended?.should == (@match.final_hand? && @match.current_hand.final_turn?)
|
75
|
-
end
|
76
|
-
patient.last_hand?.should == if @match.final_hand?.nil?
|
77
|
-
false
|
78
|
-
else
|
79
|
-
@match.final_hand?
|
80
|
-
end
|
81
|
-
patient.player_acting_sequence_string.should == @match.player_acting_sequence_string
|
82
|
-
patient.users_turn_to_act?.should == @match.users_turn_to_act?
|
83
|
-
check_betting_sequence(patient)
|
84
|
-
# @todo Test this eventually
|
85
|
-
# patient.min_wager.to_i.should == @min_wager.to_i
|
86
|
-
end
|
87
|
-
|
88
|
-
def check_player_blind_relation(patient)
|
89
|
-
expected_player_blind_relation = @match.player_blind_relation
|
90
|
-
patient.player_blind_relation.each do |player, blind|
|
91
|
-
expected_player_and_blind = expected_player_blind_relation.to_a.find do |player_and_blind|
|
92
|
-
player_and_blind.first.seat == player.seat
|
93
|
-
end
|
94
|
-
|
95
|
-
expected_player = expected_player_and_blind.first
|
96
|
-
expected_blind = expected_player_and_blind.last
|
97
|
-
|
98
|
-
player.close_enough?(expected_player).should == true
|
99
|
-
blind.should == expected_blind
|
100
|
-
end
|
101
|
-
end
|
102
|
-
def check_betting_sequence(patient)
|
103
|
-
patient_betting_sequence = patient.betting_sequence.map do |actions|
|
104
|
-
actions.map { |action| action.to_low_res_acpc }
|
105
|
-
end
|
106
|
-
expected_betting_sequence = @match.betting_sequence.map do |actions|
|
107
|
-
actions.map { |action| action.to_low_res_acpc }
|
108
|
-
end
|
109
|
-
patient_betting_sequence.should == expected_betting_sequence
|
110
|
-
|
111
|
-
patient.betting_sequence_string.scan(/([a-z]\d*|\/)/).flatten.map do |action|
|
112
|
-
if action.match(/\//)
|
113
|
-
action
|
114
|
-
else
|
115
|
-
PokerAction.new(action).to_low_res_acpc
|
116
|
-
end
|
117
|
-
end.join('').should == @match.betting_sequence_string
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
|
122
|
-
# @todo Move these into utils
|
123
|
-
|
124
|
-
require 'awesome_print'
|
125
|
-
|
126
|
-
# @param [#to_s] message The message to log.
|
127
|
-
def log_message(message)
|
128
|
-
puts message.to_s
|
129
|
-
end
|
130
|
-
|
131
|
-
def debug(variables)
|
132
|
-
log_message variables.awesome_inspect
|
133
|
-
end
|
134
|
-
|
135
|
-
############
|
136
|
-
|
137
|
-
class Array
|
138
|
-
def players_close_enough?(other_players)
|
139
|
-
# puts "length: #{length}, other: #{other_players.length}"
|
140
|
-
|
141
|
-
return false if other_players.length != length
|
142
|
-
each_with_index do |player, index|
|
143
|
-
return false unless player.close_enough?(other_players[index])
|
144
|
-
end
|
145
|
-
true
|
146
|
-
end
|
147
|
-
def reject_empty_elements
|
148
|
-
reject do |elem|
|
149
|
-
elem.empty?
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
# @todo Move these into their respective classes
|
155
|
-
class PokerMatchData
|
156
|
-
# @todo Untested
|
157
|
-
# @return [String] player acting sequence as a string.
|
158
|
-
def player_acting_sequence_string
|
159
|
-
(player_acting_sequence.map { |per_round| per_round.join('') }).join('/')
|
160
|
-
end
|
161
|
-
def users_turn_to_act?
|
162
|
-
return false unless current_hand && current_hand.next_action
|
163
|
-
current_hand.next_action.seat == @seat
|
164
|
-
end
|
165
|
-
def betting_sequence
|
166
|
-
sequence = [[]]
|
167
|
-
|
168
|
-
if (
|
169
|
-
@hand_number.nil? ||
|
170
|
-
current_hand.turn_number.nil? ||
|
171
|
-
current_hand.turn_number < 1
|
172
|
-
)
|
173
|
-
return sequence
|
174
|
-
end
|
175
|
-
|
176
|
-
turns_taken = current_hand.data[0..current_hand.turn_number-1]
|
177
|
-
turns_taken.each_with_index do |turn, turn_index|
|
178
|
-
next unless turn.action_message
|
179
|
-
|
180
|
-
sequence[turn.action_message.state.round] << turn.action_message.action
|
181
|
-
|
182
|
-
if (
|
183
|
-
new_round?(sequence.length - 1 , turn_index) ||
|
184
|
-
players_all_in?(sequence.length - 1, turn_index, turns_taken)
|
185
|
-
)
|
186
|
-
sequence << []
|
187
|
-
end
|
188
|
-
end
|
189
|
-
|
190
|
-
sequence
|
191
|
-
end
|
192
|
-
def betting_sequence_string
|
193
|
-
(betting_sequence.map do |per_round|
|
194
|
-
(per_round.map{|action| action.to_acpc}).join('')
|
195
|
-
end).join('/')
|
196
|
-
end
|
197
|
-
# @todo Test and implement this
|
198
|
-
# def min_wager
|
199
|
-
# return nil unless current_hand
|
200
|
-
|
201
|
-
# @match_def.game_def.min_wagers[current_hand.next_state.round]
|
202
|
-
# ChipStack.new [@min_wager.to_i, action_with_context.amount_to_put_in_pot.to_i].max
|
203
|
-
# end
|
204
|
-
end
|
205
|
-
class PokerAction
|
206
|
-
# @return [Hash] Map of specific to general actions to more specific actions (e.g. check to call and bet to raise).
|
207
|
-
LOW_RESOLUTION_ACTION_CONVERSION = {call: :call, raise: :raise, fold: :fold, check: :call, bet: :raise}
|
208
|
-
|
209
|
-
def to_low_res_acpc
|
210
|
-
LEGAL_ACTIONS[LOW_RESOLUTION_ACTION_CONVERSION[@symbol]] + @modifier.to_s
|
211
|
-
end
|
212
|
-
end
|
213
|
-
class Player
|
214
|
-
def acpc_actions_taken_this_hand
|
215
|
-
acpc_actions = @actions_taken_this_hand.map do |actions_per_turn|
|
216
|
-
actions_per_turn.map { |action| action.to_low_res_acpc }
|
217
|
-
end
|
218
|
-
if acpc_actions.first.empty?
|
219
|
-
acpc_actions
|
220
|
-
else
|
221
|
-
acpc_actions.reject_empty_elements
|
222
|
-
end
|
223
|
-
end
|
224
|
-
|
225
|
-
def close_enough?(other)
|
226
|
-
# puts "name: #{name == other.name}"
|
227
|
-
# puts "seat: #{seat == other.seat}"
|
228
|
-
# puts "chip_stack: #{chip_stack}, other: #{other.chip_stack}"
|
229
|
-
# puts "chip balances: #{chip_balance}, other: #{other.chip_balance}"
|
230
|
-
# puts "actions_taken_this_hand: #{acpc_actions_taken_this_hand}, other: #{other.acpc_actions_taken_this_hand}"
|
231
|
-
# puts "all_in: #{all_in?}, other: #{other.all_in?}"
|
232
|
-
|
233
|
-
|
234
|
-
@name == other.name &&
|
235
|
-
@seat == other.seat &&
|
236
|
-
@chip_stack == other.chip_stack &&
|
237
|
-
@chip_balance == other.chip_balance &&
|
238
|
-
acpc_actions_taken_this_hand == other.acpc_actions_taken_this_hand
|
239
|
-
end
|
240
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
|
2
|
-
# Gems
|
3
|
-
require 'acpc_poker_types'
|
4
|
-
|
5
|
-
# Assortment of methods to support model tests
|
6
|
-
module ModelTestHelper
|
7
|
-
|
8
|
-
# Initialization methods ---------------------------------------------------
|
9
|
-
def create_players(big_blind, small_blind)
|
10
|
-
player_who_submitted_big_blind = mock('Player')
|
11
|
-
player_who_submitted_big_blind.stubs(:current_wager_faced=).with(0)
|
12
|
-
player_who_submitted_big_blind.stubs(:current_wager_faced).returns(0)
|
13
|
-
player_who_submitted_big_blind.stubs(:name).returns('big_blind_player')
|
14
|
-
|
15
|
-
player_who_submitted_small_blind = mock('Player')
|
16
|
-
player_who_submitted_small_blind.stubs(:current_wager_faced=).with(big_blind - small_blind)
|
17
|
-
player_who_submitted_small_blind.stubs(:current_wager_faced).returns(big_blind - small_blind)
|
18
|
-
player_who_submitted_small_blind.stubs(:name).returns('small_blind_player')
|
19
|
-
|
20
|
-
other_player = mock('Player')
|
21
|
-
other_player.stubs(:current_wager_faced=).with(big_blind)
|
22
|
-
other_player.stubs(:current_wager_faced).returns(big_blind)
|
23
|
-
other_player.stubs(:name).returns('other_player')
|
24
|
-
|
25
|
-
[player_who_submitted_big_blind, player_who_submitted_small_blind, other_player]
|
26
|
-
end
|
27
|
-
|
28
|
-
def setup_action_test(match_state, action_type, action_argument = '')
|
29
|
-
action = action_argument + action_type
|
30
|
-
expected_string = raw_match_state match_state, action
|
31
|
-
|
32
|
-
expected_string
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
# Helper methods -----------------------------------------------------------
|
37
|
-
|
38
|
-
def raw_match_state(match_state, action)
|
39
|
-
"#{match_state}:#{action}"
|
40
|
-
end
|
41
|
-
|
42
|
-
# Construct an arbitrary hole card hand.
|
43
|
-
#
|
44
|
-
# @return [Mock Hand] An arbitrary hole card hand.
|
45
|
-
def arbitrary_hole_card_hand
|
46
|
-
hand = mock('Hand')
|
47
|
-
hand_as_string = AcpcPokerTypes::CARD_RANKS[:two]
|
48
|
-
+ AcpcPokerTypes::CARD_SUITS[:spades][:acpc_character]
|
49
|
-
+ AcpcPokerTypes::CARD_RANKS[:three]
|
50
|
-
+ AcpcPokerTypes::CARD_SUITS[:hearts][:acpc_character]
|
51
|
-
hand.stubs(:to_str).returns(hand_as_string)
|
52
|
-
hand.stubs(:to_s).returns(hand_as_string)
|
53
|
-
|
54
|
-
hand
|
55
|
-
end
|
56
|
-
end
|
@@ -1,74 +0,0 @@
|
|
1
|
-
|
2
|
-
class Symbol
|
3
|
-
def to_setter_signature
|
4
|
-
"#{self}=".to_sym
|
5
|
-
end
|
6
|
-
end
|
7
|
-
|
8
|
-
require 'dmorrill10-utils'
|
9
|
-
|
10
|
-
# @todo This was an attempt at organizing tests better but it didn't make tests any nicer.
|
11
|
-
# I would get rid of it, but it's still being used, so it will stay for now.
|
12
|
-
class TestExample
|
13
|
-
|
14
|
-
exceptions :no_properties_for_given, :no_properties_for_then
|
15
|
-
|
16
|
-
attr_reader :description, :given, :then
|
17
|
-
|
18
|
-
def initialize(example_description, example_data_catagories)
|
19
|
-
@description = example_description
|
20
|
-
|
21
|
-
@given = {}
|
22
|
-
@then = {}
|
23
|
-
|
24
|
-
given_properties = example_data_catagories[:given]
|
25
|
-
|
26
|
-
raise NoPropertiesForGiven unless given_properties
|
27
|
-
|
28
|
-
then_properties = example_data_catagories[:then]
|
29
|
-
|
30
|
-
raise NoPropertiesForThen unless then_properties
|
31
|
-
|
32
|
-
given_properties.each do |property|
|
33
|
-
define_getter_and_setter @given, property
|
34
|
-
end
|
35
|
-
|
36
|
-
then_properties.each do |property|
|
37
|
-
define_getter_and_setter @then, property
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def to_s
|
42
|
-
given_as_string = partition_to_string @given
|
43
|
-
then_as_string = partition_to_string @then
|
44
|
-
|
45
|
-
"#{@description}: given: #{given_as_string}, then: #{then_as_string}"
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def partition_to_string(partition)
|
51
|
-
partition.map do |key_value_pair|
|
52
|
-
key_value_pair.join(' is ')
|
53
|
-
end.join(', and')
|
54
|
-
end
|
55
|
-
|
56
|
-
def define_getter_and_setter(instance_on_which_to_define, property)
|
57
|
-
define_getter instance_on_which_to_define, property
|
58
|
-
define_setter instance_on_which_to_define, property
|
59
|
-
end
|
60
|
-
|
61
|
-
def define_getter(instance_on_which_to_define, property)
|
62
|
-
signature = property.to_sym
|
63
|
-
instance_on_which_to_define.singleton_class.send(:define_method, signature) do
|
64
|
-
instance_on_which_to_define[property.to_sym]
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def define_setter(instance_on_which_to_define, property)
|
69
|
-
signature = property.to_sym.to_setter_signature
|
70
|
-
instance_on_which_to_define.singleton_class.send(:define_method, signature) do |to_set|
|
71
|
-
store(property.to_sym, to_set)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|