/
exception.rb
40 lines (35 loc) · 1.61 KB
/
exception.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
# Difference reports between {::Exception}s. The report differs from that of
# {Object} in that it tries to “diff” the exception messages and that it uses
# ‘≉’ instead of ‘≠’ to show that the actual result doesn’t match an expected
# Regexp.
class Lookout::Difference::Exception < Lookout::Difference::Object
# Initializes a difference report between the ACTUAL result and the EXPECTED
# value, where REGEXP is the {::Regexp} found inside EXPECTED, if any.
# @param [::Exception] actual
# @param [::Exception] expected
# @param [::Regexp, nil] regexp
def initialize(actual, expected, regexp = nil)
@regexp = regexp
super actual, expected
end
# @return [Enumerable<String>, super] The result of {String#diff} between
# {#expected}’s message and {#actual}’s message if {#actual} and
# {#expected} are of the same class and {#expected} and {#actual}’s
# messages are Strings, {super} otherwise
def diff
return super unless expected.class == actual.class and
String === expected.message and
not @regexp and
(m = actual.message rescue nil) and
String === m
expected.message.to_lookout_expected.difference(m).diff
end
private
# @return [::String, super] A specially crafted inspection String containing
# the Regexp if {#expected} contains a Regexp, {super} otherwise
def inspect_expected; @regexp ? '#<%s: %p>' % [expected.class, @regexp] : super end
# @return [::String, super] The symbol ‘≉’ if a Regexp was found inside
# {#expected}, {super} otherwise
def symbol; @regexp ? '≉' : super end
end