Skip to content

Commit b2311bc

Browse files
committed
Make the change backwards-compatible
This supports both the old version (single value) and new version (hash-based). I've added back the tests from the old version to check that the code stills works when we provide a single value. I've also adapted the README a bit to explain both versions.
1 parent cb642e8 commit b2311bc

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ class MyWidget
121121
e.try { UserService.slug_from_login login } # returns String instance or ArgumentError
122122

123123
compare_error_message_and_class = -> (control, candidate) do
124-
control.class == candidate.class &&
124+
control.class == candidate.class &&
125125
control.message == candidate.message
126126
end
127127

128128
compare_argument_errors = -> (control, candidate) do
129129
control.class == ArgumentError &&
130130
candidate.class == ArgumentError &&
131131
control.message.start_with?("Input has invalid characters") &&
132-
candidate.message.start_with?("Invalid characters in input")
132+
candidate.message.start_with?("Invalid characters in input")
133133
end
134134

135135
e.compare_errors do |control, candidate|
@@ -550,7 +550,25 @@ end
550550

551551
#### Providing fake timing data
552552

553-
If you're writing tests that depend on specific timing values, you can provide canned durations using the `fabricate_durations_for_testing_purposes` method, and Scientist will report these in `Scientist::Observation#duration` and `Scientist::Observation#cpu_time` instead of the actual execution times.
553+
If you're writing tests that depend on specific timing values, you can provide canned durations using the `fabricate_durations_for_testing_purposes` method. This can be done using either the old version (single value) or the new version (hash-based) to include both duration and cpu_time.
554+
555+
##### Old version (Single Value)
556+
557+
In the old version, you can provide a single value for the duration:
558+
559+
```ruby
560+
science "absolutely-nothing-suspicious-happening-here" do |e|
561+
e.use { ... } # "control"
562+
e.try { ... } # "candidate"
563+
e.fabricate_durations_for_testing_purposes( "control" => 1.0, "candidate" => 0.5 )
564+
end
565+
```
566+
567+
`fabricate_durations_for_testing_purposes` takes a Hash of duration values, keyed by behavior names. (By default, Scientist uses `"control"` and `"candidate"`, but if you override these as shown in [Trying more than one thing](#trying-more-than-one-thing) or [No control, just candidates](#no-control-just-candidates), use matching names here.) If a name is not provided, the actual execution time will be reported instead.
568+
569+
##### New version (Hash-based)
570+
571+
Scientist will report these in `Scientist::Observation#duration` and `Scientist::Observation#cpu_time` instead of the actual execution times.
554572

555573
```ruby
556574
science "absolutely-nothing-suspicious-happening-here" do |e|

lib/scientist/observation.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ def initialize(name, experiment, fabricated_duration: nil, &block)
3535
@exception = e
3636
end
3737

38-
if fabricated_duration
38+
if fabricated_duration.is_a?(Hash)
3939
@duration = fabricated_duration["duration"]
4040
@cpu_time = fabricated_duration["cpu_time"]
41+
elsif fabricated_duration
42+
@duration = fabricated_duration
43+
@cpu_time = 0.0 # setting a default value
4144
else
4245
end_wall_time, end_cpu_time = capture_times
4346
@duration = end_wall_time - start_wall_time

test/scientist/experiment_test.rb

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,20 @@ def @ex.enabled?
671671
end
672672

673673
describe "testing hooks for extending code" do
674-
it "allows a user to provide fabricated durations for testing purposes" do
674+
it "allows a user to provide fabricated durations for testing purposes (old version)" do
675+
@ex.use { true }
676+
@ex.try { true }
677+
@ex.fabricate_durations_for_testing_purposes( "control" => 0.5, "candidate" => 1.0 )
678+
679+
@ex.run
680+
681+
cont = @ex.published_result.control
682+
cand = @ex.published_result.candidates.first
683+
assert_in_delta 0.5, cont.duration, 0.01
684+
assert_in_delta 1.0, cand.duration, 0.01
685+
end
686+
687+
it "allows a user to provide fabricated durations for testing purposes (new version)" do
675688
@ex.use { true }
676689
@ex.try { true }
677690
@ex.fabricate_durations_for_testing_purposes({
@@ -692,7 +705,20 @@ def @ex.enabled?
692705
assert_equal 0.9, cand.cpu_time
693706
end
694707

695-
it "returns actual durations if fabricated ones are omitted for some blocks" do
708+
it "returns actual durations if fabricated ones are omitted for some blocks (old version)" do
709+
@ex.use { true }
710+
@ex.try { sleep 0.1; true }
711+
@ex.fabricate_durations_for_testing_purposes( "control" => 0.5 )
712+
713+
@ex.run
714+
715+
cont = @ex.published_result.control
716+
cand = @ex.published_result.candidates.first
717+
assert_in_delta 0.5, cont.duration, 0.01
718+
assert_in_delta 0.1, cand.duration, 0.01
719+
end
720+
721+
it "returns actual durations if fabricated ones are omitted for some blocks (new version)" do
696722
@ex.use { true }
697723
@ex.try do
698724
start_time = Time.now

0 commit comments

Comments
 (0)