forked from github/scientist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservation_test.rb
More file actions
170 lines (131 loc) · 4.87 KB
/
observation_test.rb
File metadata and controls
170 lines (131 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
describe Scientist::Observation do
before do
@experiment = Scientist::Experiment.new "test"
end
it "observes and records the execution of a block" do
ob = Scientist::Observation.new("test", @experiment) do
start_time = Time.now
while Time.now - start_time < 0.1
# Perform some CPU-intensive work
(1..1000).each { |i| i * i }
end
"ret"
end
assert_equal "ret", ob.value
refute ob.raised?
assert_in_delta 0.1, ob.duration, 0.01
assert_in_delta 0.1, ob.cpu_time, 0.01
end
it "stashes exceptions" do
ob = Scientist::Observation.new("test", @experiment) do
raise "exception"
end
assert ob.raised?
assert_equal "exception", ob.exception.message
assert_nil ob.value
end
describe "::RESCUES" do
before do
@original = Scientist::Observation::RESCUES.dup
end
after do
Scientist::Observation::RESCUES.replace(@original)
end
it "includes all exception types by default" do
ob = Scientist::Observation.new("test", @experiment) do
raise Exception.new("not a StandardError")
end
assert ob.raised?
assert_instance_of Exception, ob.exception
end
it "can customize rescued types" do
Scientist::Observation::RESCUES.replace [StandardError]
ex = assert_raises Exception do
Scientist::Observation.new("test", @experiment) do
raise Exception.new("not a StandardError")
end
end
assert_equal "not a StandardError", ex.message
end
end
it "compares values" do
a = Scientist::Observation.new("test", @experiment) { 1 }
b = Scientist::Observation.new("test", @experiment) { 1 }
assert a.equivalent_to?(b)
x = Scientist::Observation.new("test", @experiment) { 1 }
y = Scientist::Observation.new("test", @experiment) { 2 }
refute x.equivalent_to?(y)
end
it "compares exception messages" do
a = Scientist::Observation.new("test", @experiment) { raise "error" }
b = Scientist::Observation.new("test", @experiment) { raise "error" }
assert a.equivalent_to?(b)
x = Scientist::Observation.new("test", @experiment) { raise "error" }
y = Scientist::Observation.new("test", @experiment) { raise "ERROR" }
refute x.equivalent_to?(y)
end
FirstError = Class.new(StandardError)
SecondError = Class.new(StandardError)
it "compares exception classes" do
x = Scientist::Observation.new("test", @experiment) { raise FirstError, "error" }
y = Scientist::Observation.new("test", @experiment) { raise SecondError, "error" }
z = Scientist::Observation.new("test", @experiment) { raise FirstError, "error" }
assert x.equivalent_to?(z)
refute x.equivalent_to?(y)
end
it "compares values using a comparator proc" do
a = Scientist::Observation.new("test", @experiment) { 1 }
b = Scientist::Observation.new("test", @experiment) { "1" }
refute a.equivalent_to?(b)
compare_on_string = -> (x, y) { x.to_s == y.to_s }
assert a.equivalent_to?(b, compare_on_string)
yielded = []
compare_appends = -> (x, y) do
yielded << x
yielded << y
true
end
a.equivalent_to?(b, compare_appends)
assert_equal [a.value, b.value], yielded
end
it "compares exceptions using an error comparator proc" do
x = Scientist::Observation.new("test", @experiment) { raise FirstError, "error" }
y = Scientist::Observation.new("test", @experiment) { raise SecondError, "error" }
z = Scientist::Observation.new("test", @experiment) { raise FirstError, "ERROR" }
refute x.equivalent_to?(z)
refute x.equivalent_to?(y)
compare_on_class = -> (error, other_error) {
error.class == other_error.class
}
compare_on_message = -> (error, other_error) {
error.message == other_error.message
}
assert x.equivalent_to?(z, nil, compare_on_class)
assert x.equivalent_to?(y, nil, compare_on_message)
end
describe "#cleaned_value" do
it "returns the observation's value by default" do
a = Scientist::Observation.new("test", @experiment) { 1 }
assert_equal 1, a.cleaned_value
end
it "uses the experiment's clean block to clean a value when configured" do
@experiment.clean { |value| value.upcase }
a = Scientist::Observation.new("test", @experiment) { "test" }
assert_equal "TEST", a.cleaned_value
end
it "doesn't clean nil values" do
@experiment.clean { |value| "foo" }
a = Scientist::Observation.new("test", @experiment) { nil }
assert_nil a.cleaned_value
end
it "returns false boolean values" do
a = Scientist::Observation.new("test", @experiment) { false }
assert_equal false, a.cleaned_value
end
it "cleans false values" do
@experiment.clean { |value| value.to_s.upcase }
a = Scientist::Observation.new("test", @experiment) { false }
assert_equal "FALSE", a.cleaned_value
end
end
end