File: result_spec.rb

package info (click to toggle)
ruby-cucumber-core 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 580 kB
  • sloc: ruby: 5,763; makefile: 2
file content (349 lines) | stat: -rw-r--r-- 12,511 bytes parent folder | download | duplicates (3)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# -*- encoding: utf-8 -*-
require 'cucumber/core/test/result'
require 'cucumber/core/test/duration_matcher'

module Cucumber::Core::Test
  describe Result do

    let(:visitor) { double('visitor') }
    let(:args)    { double('args')    }

    describe Result::Passed do
      subject(:result) { Result::Passed.new(duration) }
      let(:duration)   { Result::Duration.new(1 * 1000 * 1000) }

      it "describes itself to a visitor" do
        expect( visitor ).to receive(:passed).with(args)
        expect( visitor ).to receive(:duration).with(duration, args)
        result.describe_to(visitor, args)
      end

      it "converts to a string" do
        expect( result.to_s ).to eq "✓"
      end

      it "has a duration" do
        expect( result.duration ).to eq duration
      end

      it "requires the constructor argument" do
        expect { Result::Passed.new }.to raise_error(ArgumentError)
      end

      it "does nothing when appending the backtrace" do
        expect( result.with_appended_backtrace(double) ).to equal result
      end

      it "does nothing when filtering the backtrace" do
        expect( result.with_filtered_backtrace(double) ).to equal result
      end

      specify { expect( result.to_sym ).to eq :passed }

      specify { expect( result ).to     be_passed    }
      specify { expect( result ).not_to be_failed    }
      specify { expect( result ).not_to be_undefined }
      specify { expect( result ).not_to be_unknown   }
      specify { expect( result ).not_to be_skipped   }

      specify { expect( result ).to be_ok }
      specify { expect( result.ok?(false) ).to be_truthy }
      specify { expect( result.ok?(true) ).to be_truthy }
    end

    describe Result::Failed do
      subject(:result) { Result::Failed.new(duration, exception) }
      let(:duration)   { Result::Duration.new(1 * 1000 * 1000) }
      let(:exception)  { StandardError.new("error message") }

      it "describes itself to a visitor" do
        expect( visitor ).to receive(:failed).with(args)
        expect( visitor ).to receive(:duration).with(duration, args)
        expect( visitor ).to receive(:exception).with(exception, args)
        result.describe_to(visitor, args)
      end

      it "has a duration" do
        expect( result.duration ).to eq duration
      end

      it "requires both constructor arguments" do
        expect { Result::Failed.new }.to raise_error(ArgumentError)
        expect { Result::Failed.new(duration) }.to raise_error(ArgumentError)
      end

      it "does nothing if step has no backtrace line" do
        result.exception.set_backtrace("exception backtrace")
        step = "does not respond_to?(:backtrace_line)"

        expect( result.with_appended_backtrace(step).exception.backtrace ).to eq(["exception backtrace"])
      end

      it "appends the backtrace line of the step" do
        result.exception.set_backtrace("exception backtrace")
        step = double
        expect( step ).to receive(:backtrace_line).and_return("step_line")

        expect( result.with_appended_backtrace(step).exception.backtrace ).to eq(["exception backtrace", "step_line"])
      end

      it "apply filters to the exception" do
        filter_class = double
        filter = double
        filtered_exception = double
        expect( filter_class ).to receive(:new).with(result.exception).and_return(filter)
        expect( filter ).to receive(:exception).and_return(filtered_exception)

        expect( result.with_filtered_backtrace(filter_class).exception ).to equal filtered_exception
      end

      specify { expect( result.to_sym ).to eq :failed }

      specify { expect( result ).not_to be_passed    }
      specify { expect( result ).to     be_failed    }
      specify { expect( result ).not_to be_undefined }
      specify { expect( result ).not_to be_unknown   }
      specify { expect( result ).not_to be_skipped   }

      specify { expect( result ).to_not be_ok }
      specify { expect( result.ok?(false) ).to be_falsey }
      specify { expect( result.ok?(true) ).to be_falsey }
    end

    describe Result::Unknown do
      subject(:result) { Result::Unknown.new }

      it "doesn't describe itself to a visitor" do
        visitor = double('never receives anything')
        result.describe_to(visitor, args)
      end

      specify { expect( result.to_sym ).to eq :unknown }

      specify { expect( result ).not_to be_passed    }
      specify { expect( result ).not_to be_failed    }
      specify { expect( result ).not_to be_undefined }
      specify { expect( result ).to     be_unknown   }
      specify { expect( result ).not_to be_skipped   }
    end

    describe Result::Raisable do
      context "with or without backtrace" do
        subject(:result) { Result::Raisable.new }

        it "does nothing if step has no backtrace line" do
          step = "does not respond_to?(:backtrace_line)"

          expect( result.with_appended_backtrace(step).backtrace ).to eq(nil)
        end
      end

      context "without backtrace" do
        subject(:result) { Result::Raisable.new }

        it "set the backtrace to the backtrace line of the step" do
          step = double
          expect( step ).to receive(:backtrace_line).and_return("step_line")

          expect( result.with_appended_backtrace(step).backtrace ).to eq(["step_line"])
        end

        it "does nothing when filtering the backtrace" do
          expect( result.with_filtered_backtrace(double) ).to equal result
        end
      end

      context "with backtrace" do
        subject(:result) { Result::Raisable.new("message", 0, "backtrace") }

        it "appends the backtrace line of the step" do
          step = double
          expect( step ).to receive(:backtrace_line).and_return("step_line")

          expect( result.with_appended_backtrace(step).backtrace ).to eq(["backtrace", "step_line"])
        end

        it "apply filters to the backtrace" do
          filter_class = double
          filter = double
          filtered_result = double
          expect( filter_class ).to receive(:new).with(result.exception).and_return(filter)
          expect( filter ).to receive(:exception).and_return(filtered_result)

          expect( result.with_filtered_backtrace(filter_class) ).to equal filtered_result
        end
      end
    end

    describe Result::Undefined do
      subject(:result) { Result::Undefined.new }

      it "describes itself to a visitor" do
        expect( visitor ).to receive(:undefined).with(args)
        expect( visitor ).to receive(:duration).with(an_unknown_duration, args)
        result.describe_to(visitor, args)
      end

      specify { expect( result.to_sym ).to eq :undefined }

      specify { expect( result ).not_to be_passed    }
      specify { expect( result ).not_to be_failed    }
      specify { expect( result ).to     be_undefined }
      specify { expect( result ).not_to be_unknown   }
      specify { expect( result ).not_to be_skipped   }

      specify { expect( result ).to be_ok }
      specify { expect( result.ok?(false) ).to be_truthy }
      specify { expect( result.ok?(true) ).to be_falsey }
    end

    describe Result::Skipped do
      subject(:result) { Result::Skipped.new }

      it "describes itself to a visitor" do
        expect( visitor ).to receive(:skipped).with(args)
        expect( visitor ).to receive(:duration).with(an_unknown_duration, args)
        result.describe_to(visitor, args)
      end

      specify { expect( result.to_sym ).to eq :skipped }

      specify { expect( result ).not_to be_passed    }
      specify { expect( result ).not_to be_failed    }
      specify { expect( result ).not_to be_undefined }
      specify { expect( result ).not_to be_unknown   }
      specify { expect( result ).to     be_skipped   }

      specify { expect( result ).to be_ok }
      specify { expect( result.ok?(false) ).to be_truthy }
      specify { expect( result.ok?(true) ).to be_truthy }
    end

    describe Result::Pending do
      subject(:result) { Result::Pending.new }

      it "describes itself to a visitor" do
        expect( visitor ).to receive(:pending).with(result, args)
        expect( visitor ).to receive(:duration).with(an_unknown_duration, args)
        result.describe_to(visitor, args)
      end

      specify { expect( result.to_sym ).to eq :pending }

      specify { expect( result ).not_to be_passed    }
      specify { expect( result ).not_to be_failed    }
      specify { expect( result ).not_to be_undefined }
      specify { expect( result ).not_to be_unknown   }
      specify { expect( result ).not_to be_skipped   }
      specify { expect( result ).to     be_pending   }

      specify { expect( result ).to be_ok }
      specify { expect( result.ok?(false) ).to be_truthy }
      specify { expect( result.ok?(true) ).to be_falsey }
    end

    describe Result::Summary do
      let(:summary)   { Result::Summary.new }
      let(:failed)    { Result::Failed.new(Result::Duration.new(10), exception) }
      let(:passed)    { Result::Passed.new(Result::Duration.new(11)) }
      let(:skipped)   { Result::Skipped.new }
      let(:unknown)   { Result::Unknown.new }
      let(:undefined) { Result::Undefined.new }
      let(:exception) { StandardError.new }

      it "counts failed results" do
        failed.describe_to summary
        expect( summary.total_failed   ).to eq 1
        expect( summary.total(:failed) ).to eq 1
        expect( summary.total          ).to eq 1
      end

      it "counts passed results" do
        passed.describe_to summary
        expect( summary.total_passed   ).to eq 1
        expect( summary.total(:passed) ).to eq 1
        expect( summary.total          ).to eq 1
      end

      it "counts skipped results" do
        skipped.describe_to summary
        expect( summary.total_skipped   ).to eq 1
        expect( summary.total(:skipped) ).to eq 1
        expect( summary.total           ).to eq 1
      end

      it "counts undefined results" do
        undefined.describe_to summary
        expect( summary.total_undefined   ).to eq 1
        expect( summary.total(:undefined) ).to eq 1
        expect( summary.total             ).to eq 1
      end

      it "counts abitrary raisable results" do
        flickering = Class.new(Result::Raisable) do
          def describe_to(visitor, *args)
            visitor.flickering(*args)
          end
        end

        flickering.new.describe_to summary
        expect( summary.total_flickering   ).to eq 1
        expect( summary.total(:flickering) ).to eq 1
        expect( summary.total              ).to eq 1
      end

      it "returns zero for a status where no messges have been received" do
        expect( summary.total_passed   ).to eq 0
        expect( summary.total(:passed) ).to eq 0
        expect( summary.total_ponies   ).to eq 0
        expect( summary.total(:ponies) ).to eq 0
      end

      it "doesn't count unknown results" do
        unknown.describe_to summary
        expect( summary.total ).to eq 0
      end

      it "counts combinations" do
        [passed, passed, failed, skipped, undefined].each { |r| r.describe_to summary }
        expect( summary.total           ).to eq 5
        expect( summary.total_passed    ).to eq 2
        expect( summary.total_failed    ).to eq 1
        expect( summary.total_skipped   ).to eq 1
        expect( summary.total_undefined ).to eq 1
      end

      it "records durations" do
        [passed, failed].each { |r| r.describe_to summary }
        expect( summary.durations[0] ).to be_duration 11
        expect( summary.durations[1] ).to be_duration 10
      end

      it "records exceptions" do
        [passed, failed].each { |r| r.describe_to summary }
        expect( summary.exceptions ).to eq [exception]
      end
    end

    describe Result::Duration do
      subject(:duration) { Result::Duration.new(10) }

      it "#nanoseconds can be accessed in #tap" do
        expect( duration.tap { |duration| @duration = duration.nanoseconds } ).to eq duration
        expect( @duration ).to eq 10
      end
    end

    describe Result::UnknownDuration do
      subject(:duration) { Result::UnknownDuration.new }

      it "#tap does not execute the passed block" do
        expect( duration.tap { raise "tap executed block" } ).to eq duration
      end

      it "accessing #nanoseconds outside #tap block raises exception" do
        expect { duration.nanoseconds }.to raise_error(RuntimeError)
      end
    end
  end
end