summaryrefslogtreecommitdiff
path: root/spec/mspec/spec/matchers/complain_spec.rb
blob: 90f94c36842b04a6259da5673cc9ee51d4f95ede (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
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/matchers'

describe ComplainMatcher do
  it "matches when executing the proc results in output to $stderr" do
    proc = lambda { warn "I'm gonna tell yo mama" }
    ComplainMatcher.new(nil).matches?(proc).should == true
  end

  it "matches when executing the proc results in the expected output to $stderr" do
    proc = lambda { warn "Que haces?" }
    ComplainMatcher.new("Que haces?\n").matches?(proc).should == true
    ComplainMatcher.new("Que pasa?\n").matches?(proc).should == false
    ComplainMatcher.new(/Que/).matches?(proc).should == true
    ComplainMatcher.new(/Quoi/).matches?(proc).should == false
  end

  it "does not match when there is no output to $stderr" do
    ComplainMatcher.new(nil).matches?(lambda {}).should == false
  end

  it "provides a useful failure message" do
    matcher = ComplainMatcher.new(nil)
    matcher.matches?(lambda { })
    matcher.failure_message.should == ["Expected a warning", "but received none"]
    matcher = ComplainMatcher.new("listen here")
    matcher.matches?(lambda { warn "look out" })
    matcher.failure_message.should ==
      ["Expected warning: \"listen here\"", "but got: \"look out\""]
    matcher = ComplainMatcher.new(/talk/)
    matcher.matches?(lambda { warn "listen up" })
    matcher.failure_message.should ==
      ["Expected warning to match: /talk/", "but got: \"listen up\""]
  end

  it "provides a useful negative failure message" do
    proc = lambda { warn "ouch" }
    matcher = ComplainMatcher.new(nil)
    matcher.matches?(proc)
    matcher.negative_failure_message.should ==
      ["Unexpected warning: ", "\"ouch\""]
    matcher = ComplainMatcher.new("ouchy")
    matcher.matches?(proc)
    matcher.negative_failure_message.should ==
      ["Expected warning: \"ouchy\"", "but got: \"ouch\""]
    matcher = ComplainMatcher.new(/ou/)
    matcher.matches?(proc)
    matcher.negative_failure_message.should ==
      ["Expected warning not to match: /ou/", "but got: \"ouch\""]
  end

  context "`verbose` option specified" do
    before do
      $VERBOSE, @verbose = nil, $VERBOSE
    end

    after do
      $VERBOSE = @verbose
    end

    it "sets $VERBOSE with specified second optional parameter" do
      verbose = nil
      proc = lambda { verbose = $VERBOSE }

      ComplainMatcher.new(nil, verbose: true).matches?(proc)
      verbose.should == true

      ComplainMatcher.new(nil, verbose: false).matches?(proc)
      verbose.should == false
    end

    it "sets $VERBOSE with false by default" do
      verbose = nil
      proc = lambda { verbose = $VERBOSE }

      ComplainMatcher.new(nil).matches?(proc)
      verbose.should == false
    end

    it "does not have side effect" do
      proc = lambda { safe_value = $VERBOSE }

      lambda do
        ComplainMatcher.new(nil, verbose: true).matches?(proc)
      end.should_not change { $VERBOSE }
    end

    it "accepts a verbose level as single argument" do
      verbose = nil
      proc = lambda { verbose = $VERBOSE }

      ComplainMatcher.new(verbose: true).matches?(proc)
      verbose.should == true
    end
  end
end