diff options
author | Stig Sandbeck Mathisen <ssm@debian.org> | 2014-09-07 10:14:36 +0200 |
---|---|---|
committer | Stig Sandbeck Mathisen <ssm@debian.org> | 2014-09-07 10:14:36 +0200 |
commit | d4b83be375ac1dead058e091191ee7c7b7c24c8a (patch) | |
tree | dc825687392ae3068de5b764be60c53122d9e02a /spec/lib/puppet_spec/matchers.rb | |
parent | 229cbb976fe0f70f5f30548b83517b415840f9bb (diff) | |
parent | 1681684857c6e39d60d87b0b3520d8783977ceff (diff) | |
download | puppet-upstream/3.7.0.tar.gz |
Imported Upstream version 3.7.0upstream/3.7.0
Diffstat (limited to 'spec/lib/puppet_spec/matchers.rb')
-rw-r--r-- | spec/lib/puppet_spec/matchers.rb | 91 |
1 files changed, 59 insertions, 32 deletions
diff --git a/spec/lib/puppet_spec/matchers.rb b/spec/lib/puppet_spec/matchers.rb index 448bd1811..c01d8e89d 100644 --- a/spec/lib/puppet_spec/matchers.rb +++ b/spec/lib/puppet_spec/matchers.rb @@ -44,63 +44,82 @@ RSpec::Matchers.define :exit_with do |expected| end end -class HavePrintedMatcher - attr_accessor :expected, :actual - def initialize(expected) - case expected +RSpec::Matchers.define :have_printed do |expected| + + case expected when String, Regexp - @expected = expected + expected = expected else - @expected = expected.to_s + expected = expected.to_s + end + + chain :and_exit_with do |code| + @expected_exit_code = code + end + + define_method :matches_exit_code? do |actual| + @expected_exit_code.nil? || @expected_exit_code == actual + end + + define_method :matches_output? do |actual| + return false unless actual + case expected + when String + actual.include?(expected) + when Regexp + expected.match(actual) + else + raise ArgumentError, "No idea how to match a #{actual.class.name}" end end - def matches?(block) + match do |block| + $stderr = $stdout = StringIO.new + $stdout.set_encoding('UTF-8') if $stdout.respond_to?(:set_encoding) + begin - $stderr = $stdout = StringIO.new - $stdout.set_encoding('UTF-8') if $stdout.respond_to?(:set_encoding) block.call + rescue SystemExit => e + raise unless @expected_exit_code + @actual_exit_code = e.status + ensure $stdout.rewind @actual = $stdout.read - ensure + $stdout = STDOUT $stderr = STDERR end - if @actual then - case @expected - when String - @actual.include? @expected - when Regexp - @expected.match @actual - end - else - false - end + matches_output?(@actual) && matches_exit_code?(@actual_exit_code) end - def failure_message_for_should - if @actual.nil? then - "expected #{@expected.inspect}, but nothing was printed" + failure_message_for_should do |actual| + if actual.nil? then + "expected #{expected.inspect}, but nothing was printed" else - "expected #{@expected.inspect} to be printed; got:\n#{@actual}" + if !@expected_exit_code.nil? && matches_output?(actual) + "expected exit with code #{@expected_exit_code} but " + + (@actual_exit_code.nil? ? " exit was not called" : "exited with #{@actual_exit_code} instead") + else + "expected #{expected.inspect} to be printed; got:\n#{actual}" + end end end - def failure_message_for_should_not - "expected #{@expected.inspect} to not be printed; got:\n#{@actual}" + failure_message_for_should_not do |actual| + if @expected_exit_code && matches_exit_code?(@actual_exit_code) + "expected exit code to not be #{@actual_exit_code}" + else + "expected #{expected.inspect} to not be printed; got:\n#{actual}" + end end - def description - "expect #{@expected.inspect} to be printed" + description do + "expect #{expected.inspect} to be printed" + (@expected_exit_code.nil ? '' : " with exit code #{@expected_exit_code}") end end -def have_printed(what) - HavePrintedMatcher.new(what) -end - RSpec::Matchers.define :equal_attributes_of do |expected| match do |actual| actual.instance_variables.all? do |attr| @@ -109,6 +128,14 @@ RSpec::Matchers.define :equal_attributes_of do |expected| end end +RSpec::Matchers.define :equal_resource_attributes_of do |expected| + match do |actual| + actual.keys do |attr| + actual[attr] == expected[attr] + end + end +end + RSpec::Matchers.define :be_one_of do |*expected| match do |actual| expected.include? actual |