]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/spec/lib/puppet_spec/matchers.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / spec / lib / puppet_spec / matchers.rb
1 #! /usr/bin/env ruby -S rspec
2 require 'stringio'
3
4 ########################################################################
5 # Backward compatibility for Jenkins outdated environment.
6 module RSpec
7   module Matchers
8     module BlockAliases
9       alias_method :to,     :should      unless method_defined? :to
10       alias_method :to_not, :should_not  unless method_defined? :to_not
11       alias_method :not_to, :should_not  unless method_defined? :not_to
12     end
13   end
14 end
15
16
17 ########################################################################
18 # Custom matchers...
19 RSpec::Matchers.define :have_matching_element do |expected|
20   match do |actual|
21     actual.any? { |item| item =~ expected }
22   end
23 end
24
25
26 RSpec::Matchers.define :exit_with do |expected|
27   actual = nil
28   match do |block|
29     begin
30       block.call
31     rescue SystemExit => e
32       actual = e.status
33     end
34     actual and actual == expected
35   end
36   failure_message_for_should do |block|
37     "expected exit with code #{expected} but " +
38       (actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
39   end
40   failure_message_for_should_not do |block|
41     "expected that exit would not be called with #{expected}"
42   end
43   description do
44     "expect exit with #{expected}"
45   end
46 end
47
48 class HavePrintedMatcher
49   attr_accessor :expected, :actual
50
51   def initialize(expected)
52     case expected
53     when String, Regexp
54       @expected = expected
55     else
56       @expected = expected.to_s
57     end
58   end
59
60   def matches?(block)
61     begin
62       $stderr = $stdout = StringIO.new
63       $stdout.set_encoding('UTF-8') if $stdout.respond_to?(:set_encoding)
64       block.call
65       $stdout.rewind
66       @actual = $stdout.read
67     ensure
68       $stdout = STDOUT
69       $stderr = STDERR
70     end
71
72     if @actual then
73       case @expected
74       when String
75         @actual.include? @expected
76       when Regexp
77         @expected.match @actual
78       end
79     else
80       false
81     end
82   end
83
84   def failure_message_for_should
85     if @actual.nil? then
86       "expected #{@expected.inspect}, but nothing was printed"
87     else
88       "expected #{@expected.inspect} to be printed; got:\n#{@actual}"
89     end
90   end
91
92   def failure_message_for_should_not
93     "expected #{@expected.inspect} to not be printed; got:\n#{@actual}"
94   end
95
96   def description
97     "expect #{@expected.inspect} to be printed"
98   end
99 end
100
101 def have_printed(what)
102   HavePrintedMatcher.new(what)
103 end
104
105 RSpec::Matchers.define :equal_attributes_of do |expected|
106   match do |actual|
107     actual.instance_variables.all? do |attr|
108       actual.instance_variable_get(attr) == expected.instance_variable_get(attr)
109     end
110   end
111 end
112
113 RSpec::Matchers.define :be_one_of do |*expected|
114   match do |actual|
115     expected.include? actual
116   end
117
118   failure_message_for_should do |actual|
119     "expected #{actual.inspect} to be one of #{expected.map(&:inspect).join(' or ')}"
120   end
121 end