X-Git-Url: https://review.fuel-infra.org/gitweb?a=blobdiff_plain;f=spec%2Fmatchers%2Fexception_matchers.rb;fp=spec%2Fmatchers%2Fexception_matchers.rb;h=db0575122dc07a10e14a1777405c422056a0fb8d;hb=b87d2f4e68281062df1913440ca5753ae63314a9;hp=0000000000000000000000000000000000000000;hpb=ab0ea530b8ac956091f17b104ab2311336cfc250;p=packages%2Fprecise%2Fmcollective.git diff --git a/spec/matchers/exception_matchers.rb b/spec/matchers/exception_matchers.rb new file mode 100644 index 0000000..db05751 --- /dev/null +++ b/spec/matchers/exception_matchers.rb @@ -0,0 +1,75 @@ +module MCollective + module Matchers + def raise_code(*args) + CodedExceptionMatcher.new(args) + end + + class CodedExceptionMatcher + def initialize(args) + @args = args + + raise "Need at least an exception to match" if args.size == 0 + + @expected_code = @args.shift + @expected_data = @args.shift + + @failure_type = nil + @failure_expected = nil + @failure_got = nil + end + + def matches?(actual) + begin + actual.call + rescue => e + unless e.is_a?(MCollective::CodedError) + @failure_type = :type + @failure_expected = "MCollective::CodedError" + @failure_got = e.class + return false + end + + unless [e.code, e.default].include?(@expected_code) + @failure_type = :code + @failure_expected = @expected_code + @failure_got = e.code + return false + end + + if @expected_data + unless e.args == @expected_data + @failure_type = :arguments + @failure_expected = @expected_data.inspect + @failure_got = e.args.inspect + return false + end + end + end + + true + end + + def failure_message + case @failure_type + when :type + "Expected an exception of type %s but got %s" % [@failure_expected, @failure_got] + when :code + "Expected a message code %s but got %s" % [@failure_expected, @failure_got] + when :arguments + "Expected arguments %s but got %s" % [@failure_expected, @failure_got] + end + end + + def negative_failure_message + case @failure_type + when :type + "Expected an exception of type %s but got %s" % [@failure_got, @failure_expected] + when :code + "Expected a message code %s but got %s" % [@failure_got, @failure_expected] + when :arguments + "Expected arguments %s but got %s" % [@failure_got, @failure_expected] + end + end + end + end +end