1 #! /usr/bin/env ruby -S rspec
4 ########################################################################
5 # Backward compatibility for Jenkins outdated environment.
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
17 ########################################################################
19 RSpec::Matchers.define :have_matching_element do |expected|
21 actual.any? { |item| item =~ expected }
26 RSpec::Matchers.define :exit_with do |expected|
31 rescue SystemExit => e
34 actual and actual == expected
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")
40 failure_message_for_should_not do |block|
41 "expected that exit would not be called with #{expected}"
44 "expect exit with #{expected}"
48 class HavePrintedMatcher
49 attr_accessor :expected, :actual
51 def initialize(expected)
56 @expected = expected.to_s
62 $stderr = $stdout = StringIO.new
63 $stdout.set_encoding('UTF-8') if $stdout.respond_to?(:set_encoding)
66 @actual = $stdout.read
75 @actual.include? @expected
77 @expected.match @actual
84 def failure_message_for_should
86 "expected #{@expected.inspect}, but nothing was printed"
88 "expected #{@expected.inspect} to be printed; got:\n#{@actual}"
92 def failure_message_for_should_not
93 "expected #{@expected.inspect} to not be printed; got:\n#{@actual}"
97 "expect #{@expected.inspect} to be printed"
101 def have_printed(what)
102 HavePrintedMatcher.new(what)
105 RSpec::Matchers.define :equal_attributes_of do |expected|
107 actual.instance_variables.all? do |attr|
108 actual.instance_variable_get(attr) == expected.instance_variable_get(attr)
113 RSpec::Matchers.define :be_one_of do |*expected|
115 expected.include? actual
118 failure_message_for_should do |actual|
119 "expected #{actual.inspect} to be one of #{expected.map(&:inspect).join(' or ')}"