diff options
author | Nan Liu <nan.liu@gmail.com> | 2012-05-01 16:20:12 -0700 |
---|---|---|
committer | Nan Liu <nan.liu@gmail.com> | 2012-05-01 16:20:12 -0700 |
commit | 9bd4362615994e0025d220bfb34609a689720d2b (patch) | |
tree | 2a4f1450709f3559bf2b6cad95c7efe04011161b /spec/puppetlabs_spec/matchers.rb | |
parent | df6c470fd39e6e4e92968d414ae9021b4ec67205 (diff) | |
parent | c83ffff9493443845c1545240f75d8136c0ecdff (diff) | |
download | puppet-tftp-9bd4362615994e0025d220bfb34609a689720d2b.tar.gz puppet-tftp-9bd4362615994e0025d220bfb34609a689720d2b.tar.bz2 |
Merge pull request #1 from nanliu/master
Add support files for initial module release.
Diffstat (limited to 'spec/puppetlabs_spec/matchers.rb')
-rw-r--r-- | spec/puppetlabs_spec/matchers.rb | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/spec/puppetlabs_spec/matchers.rb b/spec/puppetlabs_spec/matchers.rb new file mode 100644 index 0000000..77f5803 --- /dev/null +++ b/spec/puppetlabs_spec/matchers.rb @@ -0,0 +1,87 @@ +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 + + +RSpec::Matchers.define :have_printed do |expected| + match do |block| + $stderr = $stdout = StringIO.new + + begin + block.call + ensure + $stdout.rewind + @actual = $stdout.read + + $stdout = STDOUT + $stderr = STDERR + end + + if @actual then + 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 + end + + 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}" + end + end + + description do + "expect #{expected.inspect} to be printed" + end + + diffable +end |