summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/guards/guard_spec.rb
blob: 5c3dae4b3f3cccbf2ece117e3b8c8bcf8bebeaf3 (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
require 'spec_helper'
require 'mspec/guards'
require 'rbconfig'

describe SpecGuard, ".ruby_version" do
  before :each do
    stub_const "RUBY_VERSION", "8.2.3"
  end

  it "returns the full version for :full" do
    SpecGuard.ruby_version(:full).should == "8.2.3"
  end

  it "returns major.minor.tiny for :tiny" do
    SpecGuard.ruby_version(:tiny).should == "8.2.3"
  end

  it "returns major.minor.tiny for :teeny" do
    SpecGuard.ruby_version(:tiny).should == "8.2.3"
  end

  it "returns major.minor for :minor" do
    SpecGuard.ruby_version(:minor).should == "8.2"
  end

  it "defaults to :minor" do
    SpecGuard.ruby_version.should == "8.2"
  end

  it "returns major for :major" do
    SpecGuard.ruby_version(:major).should == "8"
  end
end

describe SpecGuard, "#yield?" do
  before :each do
    MSpec.clear_modes
    @guard = SpecGuard.new
    @guard.stub(:match?).and_return(false)
  end

  after :each do
    MSpec.unregister :add, @guard
    MSpec.clear_modes
    SpecGuard.clear_guards
  end

  it "returns true if MSpec.mode?(:unguarded) is true" do
    MSpec.register_mode :unguarded
    @guard.yield?.should == true
  end

  it "returns true if MSpec.mode?(:verify) is true" do
    MSpec.register_mode :verify
    @guard.yield?.should == true
  end

  it "returns true if MSpec.mode?(:verify) is true regardless of invert being true" do
    MSpec.register_mode :verify
    @guard.yield?(true).should == true
  end

  it "returns true if MSpec.mode?(:report) is true" do
    MSpec.register_mode :report
    @guard.yield?.should == true
  end

  it "returns true if MSpec.mode?(:report) is true regardless of invert being true" do
    MSpec.register_mode :report
    @guard.yield?(true).should == true
  end

  it "returns true if MSpec.mode?(:report_on) is true and SpecGuards.guards contains the named guard" do
    MSpec.register_mode :report_on
    SpecGuard.guards << :guard_name
    @guard.yield?.should == false
    @guard.name = :guard_name
    @guard.yield?.should == true
  end

  it "returns #match? if neither report nor verify mode are true" do
    @guard.stub(:match?).and_return(false)
    @guard.yield?.should == false
    @guard.stub(:match?).and_return(true)
    @guard.yield?.should == true
  end

  it "returns #match? if invert is true and neither report nor verify mode are true" do
    @guard.stub(:match?).and_return(false)
    @guard.yield?(true).should == true
    @guard.stub(:match?).and_return(true)
    @guard.yield?(true).should == false
  end
end

describe SpecGuard, "#match?" do
  before :each do
    @guard = SpecGuard.new
  end

  it "must be implemented in subclasses" do
    lambda {
      @guard.match?
    }.should raise_error("must be implemented by the subclass")
  end
end

describe SpecGuard, "#unregister" do
  before :each do
    MSpec.stub(:unregister)
    @guard = SpecGuard.new
  end

  it "unregisters from MSpec :add actions" do
    MSpec.should_receive(:unregister).with(:add, @guard)
    @guard.unregister
  end
end

describe SpecGuard, "#record" do
  after :each do
    SpecGuard.clear
  end

  it "saves the name of the guarded spec under the name of the guard" do
    guard = SpecGuard.new "a", "1.8"..."1.9"
    guard.name = :named_guard
    guard.record "SomeClass#action returns true"
    SpecGuard.report.should == {
      'named_guard a, 1.8...1.9' => ["SomeClass#action returns true"]
    }
  end
end

describe SpecGuard, ".guards" do
  it "returns an Array" do
    SpecGuard.guards.should be_kind_of(Array)
  end
end

describe SpecGuard, ".clear_guards" do
  it "resets the array to empty" do
    SpecGuard.guards << :guard
    SpecGuard.guards.should == [:guard]
    SpecGuard.clear_guards
    SpecGuard.guards.should == []
  end
end

describe SpecGuard, ".finish" do
  before :each do
    $stdout = @out = IOStub.new
  end

  after :each do
    $stdout = STDOUT
    SpecGuard.clear
  end

  it "prints the descriptions of the guarded specs" do
    guard = SpecGuard.new "a", "1.8"..."1.9"
    guard.name = :named_guard
    guard.record "SomeClass#action returns true"
    guard.record "SomeClass#reverse returns false"
    SpecGuard.finish
    $stdout.should == %[

2 specs omitted by guard: named_guard a, 1.8...1.9:

SomeClass#action returns true
SomeClass#reverse returns false

]
  end
end

describe SpecGuard, ".run_if" do
  before :each do
    @guard = SpecGuard.new
    ScratchPad.clear
  end

  it "yields if match? returns true" do
    @guard.stub(:match?).and_return(true)
    @guard.run_if(:name) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "does not yield if match? returns false" do
    @guard.stub(:match?).and_return(false)
    @guard.run_if(:name) { fail }
  end

  it "returns the result of the block if match? is true" do
    @guard.stub(:match?).and_return(true)
    @guard.run_if(:name) { 42 }.should == 42
  end

  it "returns nil if given a block and match? is false" do
    @guard.stub(:match?).and_return(false)
    @guard.run_if(:name) { 42 }.should == nil
  end

  it "returns what #match? returns when no block is given" do
    @guard.stub(:match?).and_return(true)
    @guard.run_if(:name).should == true
    @guard.stub(:match?).and_return(false)
    @guard.run_if(:name).should == false
  end
end

describe SpecGuard, ".run_unless" do
  before :each do
    @guard = SpecGuard.new
    ScratchPad.clear
  end

  it "yields if match? returns false" do
    @guard.stub(:match?).and_return(false)
    @guard.run_unless(:name) { ScratchPad.record :yield }
    ScratchPad.recorded.should == :yield
  end

  it "does not yield if match? returns true" do
    @guard.stub(:match?).and_return(true)
    @guard.run_unless(:name) { fail }
  end

  it "returns the result of the block if match? is false" do
    @guard.stub(:match?).and_return(false)
    @guard.run_unless(:name) { 42 }.should == 42
  end

  it "returns nil if given a block and match? is true" do
    @guard.stub(:match?).and_return(true)
    @guard.run_unless(:name) { 42 }.should == nil
  end

  it "returns the opposite of what #match? returns when no block is given" do
    @guard.stub(:match?).and_return(true)
    @guard.run_unless(:name).should == false
    @guard.stub(:match?).and_return(false)
    @guard.run_unless(:name).should == true
  end
end

describe Object, "#guard" do
  before :each do
    ScratchPad.clear
  end

  after :each do
    MSpec.clear_modes
  end

  it "allows to combine guards" do
    guard1 = VersionGuard.new 'x.x.x'
    VersionGuard.stub(:new).and_return(guard1)
    guard2 = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(guard2)

    guard1.stub(:match?).and_return(true)
    guard2.stub(:match?).and_return(true)
    guard -> { ruby_version_is "2.4" and platform_is :linux } do
      ScratchPad.record :yield
    end
    ScratchPad.recorded.should == :yield

    guard1.stub(:match?).and_return(false)
    guard2.stub(:match?).and_return(true)
    guard -> { ruby_version_is "2.4" and platform_is :linux } do
      fail
    end

    guard1.stub(:match?).and_return(true)
    guard2.stub(:match?).and_return(false)
    guard -> { ruby_version_is "2.4" and platform_is :linux } do
      fail
    end

    guard1.stub(:match?).and_return(false)
    guard2.stub(:match?).and_return(false)
    guard -> { ruby_version_is "2.4" and platform_is :linux } do
      fail
    end
  end

  it "yields when the Proc returns true" do
    guard -> { true } do
      ScratchPad.record :yield
    end
    ScratchPad.recorded.should == :yield
  end

  it "does not yield when the Proc returns false" do
    guard -> { false } do
      fail
    end
  end

  it "yields if MSpec.mode?(:unguarded) is true" do
    MSpec.register_mode :unguarded

    guard -> { false } do
      ScratchPad.record :yield1
    end
    ScratchPad.recorded.should == :yield1

    guard -> { true } do
      ScratchPad.record :yield2
    end
    ScratchPad.recorded.should == :yield2
  end

  it "yields if MSpec.mode?(:verify) is true" do
    MSpec.register_mode :verify

    guard -> { false } do
      ScratchPad.record :yield1
    end
    ScratchPad.recorded.should == :yield1

    guard -> { true } do
      ScratchPad.record :yield2
    end
    ScratchPad.recorded.should == :yield2
  end

  it "yields if MSpec.mode?(:report) is true" do
    MSpec.register_mode :report

    guard -> { false } do
      ScratchPad.record :yield1
    end
    ScratchPad.recorded.should == :yield1

    guard -> { true } do
      ScratchPad.record :yield2
    end
    ScratchPad.recorded.should == :yield2
  end

  it "raises an error if no Proc is given" do
    -> { guard :foo }.should raise_error(RuntimeError)
  end

  it "requires a block" do
    -> {
      guard(-> { true })
    }.should raise_error(LocalJumpError)
    -> {
      guard(-> { false })
    }.should raise_error(LocalJumpError)
  end
end

describe Object, "#guard_not" do
  before :each do
    ScratchPad.clear
  end

  it "allows to combine guards" do
    guard1 = VersionGuard.new 'x.x.x'
    VersionGuard.stub(:new).and_return(guard1)
    guard2 = PlatformGuard.new :dummy
    PlatformGuard.stub(:new).and_return(guard2)

    guard1.stub(:match?).and_return(true)
    guard2.stub(:match?).and_return(true)
    guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
      fail
    end

    guard1.stub(:match?).and_return(false)
    guard2.stub(:match?).and_return(true)
    guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
      ScratchPad.record :yield1
    end
    ScratchPad.recorded.should == :yield1

    guard1.stub(:match?).and_return(true)
    guard2.stub(:match?).and_return(false)
    guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
      ScratchPad.record :yield2
    end
    ScratchPad.recorded.should == :yield2

    guard1.stub(:match?).and_return(false)
    guard2.stub(:match?).and_return(false)
    guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
      ScratchPad.record :yield3
    end
    ScratchPad.recorded.should == :yield3
  end

  it "yields when the Proc returns false" do
    guard_not -> { false } do
      ScratchPad.record :yield
    end
    ScratchPad.recorded.should == :yield
  end

  it "does not yield when the Proc returns true" do
    guard_not -> { true } do
      fail
    end
  end

  it "raises an error if no Proc is given" do
    -> { guard_not :foo }.should raise_error(RuntimeError)
  end

  it "requires a block" do
    -> {
      guard_not(-> { true })
    }.should raise_error(LocalJumpError)
    -> {
      guard_not(-> { false })
    }.should raise_error(LocalJumpError)
  end
end