Class | Spec::Runner::SpecShouldRaiseHandler |
In: |
lib/spec/runner/spec_should_raise_handler.rb
|
Parent: | Object |
# File lib/spec/runner/spec_should_raise_handler.rb, line 4 4: def initialize(file_and_line_number, opts) 5: @file_and_line_number = file_and_line_number 6: @options = opts 7: @expected_error_class = determine_error_class(opts) 8: @expected_error_message = determine_error_message(opts) 9: end
# File lib/spec/runner/spec_should_raise_handler.rb, line 32 32: def build_message(exception=nil) 33: if @expected_error_message.nil? 34: message = "specify block expected #{@expected_error_class.to_s}" 35: else 36: message = "specify block expected #{@expected_error_class.new(@expected_error_message.to_s).inspect}" 37: end 38: message << " but raised #{exception.inspect}" if exception 39: message << " but nothing was raised" unless exception 40: message << "\n" 41: message << @file_and_line_number 42: end
# File lib/spec/runner/spec_should_raise_handler.rb, line 11 11: def determine_error_class(opts) 12: if candidate = opts[:should_raise] 13: if candidate.is_a?(Class) 14: return candidate 15: elsif candidate.is_a?(Array) 16: return candidate[0] 17: else 18: return Exception 19: end 20: end 21: end
# File lib/spec/runner/spec_should_raise_handler.rb, line 23 23: def determine_error_message(opts) 24: if candidate = opts[:should_raise] 25: if candidate.is_a?(Array) 26: return candidate[1] 27: end 28: end 29: return nil 30: end
# File lib/spec/runner/spec_should_raise_handler.rb, line 44 44: def error_matches?(error) 45: return false unless error.kind_of?(@expected_error_class) 46: unless @expected_error_message.nil? 47: if @expected_error_message.is_a?(Regexp) 48: return false unless error.message =~ @expected_error_message 49: else 50: return false unless error.message == @expected_error_message 51: end 52: end 53: return true 54: end
# File lib/spec/runner/spec_should_raise_handler.rb, line 56 56: def handle(errors) 57: if @expected_error_class 58: if errors.empty? 59: errors << Spec::Expectations::ExpectationNotMetError.new(build_message) 60: else 61: error_to_remove = errors.detect do |error| 62: error_matches?(error) 63: end 64: if error_to_remove.nil? 65: errors.insert(0,Spec::Expectations::ExpectationNotMetError.new(build_message(errors[0]))) 66: else 67: errors.delete(error_to_remove) 68: end 69: end 70: end 71: end