diff --git a/spec/lib/puppet_spec/matchers.rb b/spec/lib/puppet_spec/matchers.rb index 77f580330..8c7d1b004 100644 --- a/spec/lib/puppet_spec/matchers.rb +++ b/spec/lib/puppet_spec/matchers.rb @@ -1,87 +1,97 @@ require 'stringio' ######################################################################## # Backward compatibility for Jenkins outdated environment. module RSpec module Matchers module BlockAliases alias_method :to, :should unless method_defined? :to alias_method :to_not, :should_not unless method_defined? :to_not alias_method :not_to, :should_not unless method_defined? :not_to end end end ######################################################################## # Custom matchers... RSpec::Matchers.define :have_matching_element do |expected| match do |actual| actual.any? { |item| item =~ expected } end end RSpec::Matchers.define :exit_with do |expected| actual = nil match do |block| begin block.call rescue SystemExit => e actual = e.status end actual and actual == expected end failure_message_for_should do |block| "expected exit with code #{expected} but " + (actual.nil? ? " exit was not called" : "we exited with #{actual} instead") end failure_message_for_should_not do |block| "expected that exit would not be called with #{expected}" end description do "expect exit with #{expected}" end end +class HavePrintedMatcher + attr_accessor :expected, :actual -RSpec::Matchers.define :have_printed do |expected| - match do |block| - $stderr = $stdout = StringIO.new + def initialize(expected) + case expected + when String, Regexp + @expected = expected + else + @expected = expected.to_s + end + end + def matches?(block) begin + $stderr = $stdout = StringIO.new block.call - ensure $stdout.rewind @actual = $stdout.read - + ensure $stdout = STDOUT $stderr = STDERR end if @actual then - case expected + case @expected when String - @actual.include? expected + @actual.include? @expected when Regexp - expected.match @actual - else - raise ArgumentError, "No idea how to match a #{@actual.class.name}" + @expected.match @actual end + else + false end end - failure_message_for_should do |actual| - if actual.nil? then - "expected #{expected.inspect}, but nothing was printed" + def failure_message_for_should + if @actual.nil? then + "expected #{@expected.inspect}, but nothing was printed" else - "expected #{expected.inspect} to be printed; got:\n#{actual}" + "expected #{@expected.inspect} to be printed; got:\n#{@actual}" end end - description do - "expect #{expected.inspect} to be printed" + def description + "expect #{@expected.inspect} to be printed" end +end - diffable +def have_printed(what) + HavePrintedMatcher.new(what) end