Class: ComplainMatcher

Inherits:
Object show all
Defined in:
lib/mspec/matchers/complain.rb

Instance Method Summary collapse

Constructor Details

#initialize(complaint) ⇒ ComplainMatcher

Returns a new instance of ComplainMatcher.



4
5
6
# File 'lib/mspec/matchers/complain.rb', line 4

def initialize(complaint)
  @complaint = complaint
end

Instance Method Details

#failure_messageObject



31
32
33
34
35
36
37
38
39
# File 'lib/mspec/matchers/complain.rb', line 31

def failure_message
  if @complaint.nil?
    ["Expected a warning", "but received none"]
  elsif @complaint.kind_of? Regexp
    ["Expected warning to match:", @complaint.inspect]
  else
    ["Expected warning: #{@complaint.inspect}", "but got: #{@stderr.chomp.inspect}"]
  end
end

#matches?(proc) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mspec/matchers/complain.rb', line 8

def matches?(proc)
  @saved_err = $stderr
  @stderr = $stderr = IOStub.new
  @verbose = $VERBOSE
  $VERBOSE = false

  proc.call

  unless @complaint.nil?
    case @complaint
    when Regexp
      return false unless $stderr =~ @complaint
    else
      return false unless $stderr == @complaint
    end
  end

  return $stderr.empty? ? false : true
ensure
  $VERBOSE = @verbose
  $stderr = @saved_err
end

#negative_failure_messageObject



41
42
43
44
45
46
47
48
49
# File 'lib/mspec/matchers/complain.rb', line 41

def negative_failure_message
  if @complaint.nil?
    ["Unexpected warning: ", @stderr.chomp.inspect]
  elsif @complaint.kind_of? Regexp
    ["Expected warning not to match:", @complaint.inspect]
  else
    ["Expected warning: #{@complaint.inspect}", "but got: #{@stderr.chomp.inspect}"]
  end
end