Class: OutputMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(stdout, stderr) ⇒ OutputMatcher


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

def initialize(stdout, stderr)
  @out = stdout
  @err = stderr
end

Instance Method Details

#failure_messageObject


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mspec/matchers/output.rb', line 41

def failure_message
  expected_out = "\n"
  actual_out = "\n"
  unless @out.nil?
    expected_out += "  $stdout: #{@out.inspect}\n"
    actual_out += "  $stdout: #{@stdout.inspect}\n"
  end
  unless @err.nil?
    expected_out += "  $stderr: #{@err.inspect}\n"
    actual_out += "  $stderr: #{@stderr.inspect}\n"
  end
  ["Expected:#{expected_out}", "     got:#{actual_out}"]
end

#matches?(proc) ⇒ Boolean


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
# File 'lib/mspec/matchers/output.rb', line 9

def matches?(proc)
  @saved_out = $stdout
  @saved_err = $stderr
  @stdout = $stdout = IOStub.new
  @stderr = $stderr = IOStub.new

  proc.call

  unless @out.nil?
    case @out
    when Regexp
      return false unless $stdout =~ @out
    else
      return false unless $stdout == @out
    end
  end

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

  return true
ensure
  $stdout = @saved_out
  $stderr = @saved_err
end

#negative_failure_messageObject


55
56
57
58
59
60
# File 'lib/mspec/matchers/output.rb', line 55

def negative_failure_message
  out = ""
  out += "  $stdout: #{@stdout.chomp.dump}\n" unless @out.nil?
  out += "  $stderr: #{@stderr.chomp.dump}\n" unless @err.nil?
  ["Expected output not to be:\n", out]
end