Skip to content

Commit 7c561d5

Browse files
committed
Revert the Trial#complete! public API to support noargs.
Also support passing Goals and a single goal
1 parent 371754a commit 7c561d5

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/split/trial.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ def alternative=(alternative)
2626
end
2727
end
2828

29-
def complete!(goals, context = nil)
30-
goals = goals || []
31-
29+
def complete!(goals=[], context = nil)
3230
if alternative
33-
if goals.empty?
31+
if Array(goals).empty?
3432
alternative.increment_completion
3533
else
36-
goals.each {|g| alternative.increment_completion(g) }
34+
Array(goals).each {|g| alternative.increment_completion(g) }
3735
end
3836

3937
context.send(Split.configuration.on_trial_complete, self) \

spec/trial_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,42 @@ def expect_alternative(trial, alternative_name)
109109
end
110110
end
111111

112+
describe "#complete!" do
113+
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
114+
context 'when there are no goals' do
115+
it 'should complete the trial' do
116+
trial.choose!
117+
old_completed_count = trial.alternative.completed_count
118+
trial.complete!
119+
expect(trial.alternative.completed_count).to be(old_completed_count+1)
120+
end
121+
end
122+
123+
context 'when there are many goals' do
124+
let(:goals) { ['first', 'second'] }
125+
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
126+
shared_examples_for "goal completion" do
127+
it 'should not complete the trial' do
128+
trial.choose!
129+
old_completed_count = trial.alternative.completed_count
130+
trial.complete!(goal)
131+
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
132+
end
133+
end
134+
135+
describe 'Array of Goals' do
136+
let(:goal) { [goals.first] }
137+
it_behaves_like 'goal completion'
138+
end
139+
140+
describe 'String of Goal' do
141+
let(:goal) { goals.first }
142+
it_behaves_like 'goal completion'
143+
end
144+
145+
end
146+
end
147+
112148
describe "alternative recording" do
113149
before(:each) { Split.configuration.store_override = false }
114150

0 commit comments

Comments
 (0)