Skip to content

Commit

Permalink
merge from master.
Browse files Browse the repository at this point in the history
  • Loading branch information
woodhull committed Jan 3, 2013
2 parents eef203e + c302857 commit ed9ee3f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 91 deletions.
60 changes: 30 additions & 30 deletions lib/split/helper.rb
Expand Up @@ -173,35 +173,35 @@ def start_trial(trial)
def keys_without_experiment(keys, experiment_key)
keys.reject { |k| k.match(Regexp.new("^#{experiment_key}(:finished)?$")) }
end
#
#def normalize_variants(variants)
# given_probability, num_with_probability = variants.inject([0,0]) do |a,v|
# p, n = a
# if v.kind_of?(Hash) && v[:percent]
# [p + v[:percent], n + 1]
# else
# a
# end
# end
#
# num_without_probability = variants.length - num_with_probability
# unassigned_probability = ((100.0 - given_probability) / num_without_probability / 100.0)
#
# if num_with_probability.nonzero?
# variants = variants.map do |v|
# if v.kind_of?(Hash) && v[:name] && v[:percent]
# { v[:name] => v[:percent] / 100.0 }
# elsif v.kind_of?(Hash) && v[:name]
# { v[:name] => unassigned_probability }
# else
# { v => unassigned_probability }
# end
# end
# [variants.shift, variants.inject({}, :merge)]
# else
# [variants.shift, variants]
# end
#end
end

# def normalize_variants(variants)
# given_probability, num_with_probability = variants.inject([0,0]) do |a,v|
# p, n = a
# if v.kind_of?(Hash) && v[:percent]
# [p + v[:percent], n + 1]
# else
# a
# end
# end
#
# num_without_probability = variants.length - num_with_probability
# unassigned_probability = ((100.0 - given_probability) / num_without_probability / 100.0)
#
# if num_with_probability.nonzero?
# variants = variants.map do |v|
# if v.kind_of?(Hash) && v[:name] && v[:percent]
# { v[:name] => v[:percent] / 100.0 }
# elsif v.kind_of?(Hash) && v[:name]
# { v[:name] => unassigned_probability }
# else
# { v => unassigned_probability }
# end
# end
# [variants.shift, variants]
# else
# variants = variants.dup
# [variants.shift, variants]
# end
# end
end
end
142 changes: 82 additions & 60 deletions spec/helper_spec.rb
Expand Up @@ -576,66 +576,88 @@ def should_finish_experiment(experiment_name, should_finish=true)

end

context "with preloaded config" do
before { Split.configuration.experiments = {} }

#it "pulls options from config file" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [ "control_opt", "other_opt" ],
# }
#
# trial = mock('trial')
# Split::Alternative.stub(:trial).with(experiment_name: :my_experiment).and_return(trial)
# should_receive(:start_trial).with(trial)
#
# ab_test :my_experiment
#end
#
#it "accepts multiple variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [ "control_opt", "second_opt", "third_opt" ],
# }
# should_receive(:experiment_variable).with(["second_opt", "third_opt"], "control_opt", :my_experiment)
# ab_test :my_experiment
#end

#it "accepts probability on variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt", :percent => 67 },
# { :name => "second_opt", :percent => 10 },
# { :name => "third_opt", :percent => 23 },
# ],
# }
# should_receive(:experiment_variable).with({"second_opt" => 0.1, "third_opt" => 0.23}, {"control_opt" => 0.67}, :my_experiment)
# ab_test :my_experiment
#end
#
#it "accepts probability on some variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt", :percent => 34 },
# "second_opt",
# { :name => "third_opt", :percent => 23 },
# "fourth_opt",
# ],
# }
# should_receive(:experiment_variable).with({"second_opt" => 0.215, "third_opt" => 0.23, "fourth_opt" => 0.215}, {"control_opt" => 0.34}, :my_experiment)
# ab_test :my_experiment
#end
#
#it "allows name param without probability" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt" },
# "second_opt",
# { :name => "third_opt", :percent => 64 },
# ],
# }
# should_receive(:experiment_variable).with({"second_opt" => 0.18, "third_opt" => 0.64}, {"control_opt" => 0.18}, :my_experiment)
# ab_test :my_experiment
#end
end
# context "with preloaded config" do
# before { Split.configuration.experiments = {} }
#
# subject { self }
# RSpec::Matchers.define :start_experiment do |name|
# match do |actual|
# @control ||= anything
# @alternatives ||= anything
# @times ||= 1
# actual.should_receive(:experiment_variable).with(@alternatives, @control, name).exactly(@times).times
# end
# chain :with do |control, *alternatives|
# @control = control
# @alternatives = alternatives.flatten
# end
# chain :exactly do |times|
# @times = times
# end
# chain :times do end
# end
#
# it "pulls options from config file" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [ "control_opt", "other_opt" ],
# }
# should start_experiment(:my_experiment).with("control_opt", ["other_opt"])
# ab_test :my_experiment
# end
#
# it "can be called multiple times" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [ "control_opt", "other_opt" ],
# }
# should start_experiment(:my_experiment).with("control_opt", ["other_opt"]).exactly(5).times
# 5.times { ab_test :my_experiment }
# end
#
# it "accepts multiple variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [ "control_opt", "second_opt", "third_opt" ],
# }
# should start_experiment(:my_experiment).with("control_opt", ["second_opt", "third_opt"])
# ab_test :my_experiment
# end
#
# it "accepts probability on variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt", :percent => 67 },
# { :name => "second_opt", :percent => 10 },
# { :name => "third_opt", :percent => 23 },
# ],
# }
# should start_experiment(:my_experiment).with({"control_opt" => 0.67}, {"second_opt" => 0.1}, {"third_opt" => 0.23})
# ab_test :my_experiment
# end
#
# it "accepts probability on some variants" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt", :percent => 34 },
# "second_opt",
# { :name => "third_opt", :percent => 23 },
# "fourth_opt",
# ],
# }
# should start_experiment(:my_experiment).with({"control_opt" => 0.34}, {"second_opt" => 0.215}, {"third_opt" => 0.23}, {"fourth_opt" => 0.215})
# ab_test :my_experiment
# end
#
# it "allows name param without probability" do
# Split.configuration.experiments[:my_experiment] = {
# :variants => [
# { :name => "control_opt" },
# "second_opt",
# { :name => "third_opt", :percent => 64 },
# ],
# }
# should start_experiment(:my_experiment).with({"control_opt" => 0.18}, {"second_opt" => 0.18}, {"third_opt" => 0.64})
# ab_test :my_experiment
# end
# end

it 'should handle multiple experiments correctly' do
experiment = Split::Experiment.find_or_create('link_color', 'blue', 'red')
Expand Down
3 changes: 2 additions & 1 deletion spec/trial_spec.rb
Expand Up @@ -19,8 +19,9 @@

it "should call select_alternative if nil" do
trial = Split::Trial.new(experiment: experiment = mock('experiment'))
trial.should_receive(:choose).and_return(alternative = mock('alternative'))
experiment.should_receive(:next_alternative).and_return(alternative = mock('alternative'))
alternative.should_receive(:increment_participation)
experiment.should_receive(:winner).and_return nil
trial.choose!

trial.alternative.should == alternative
Expand Down

0 comments on commit ed9ee3f

Please sign in to comment.