Exception: Probatio::AssertionError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/probatio/errors.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assertion, arguments, error_or_message, test, file, line) ⇒ AssertionError

Returns a new instance of AssertionError.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/probatio/errors.rb', line 14

def initialize(assertion, arguments, error_or_message, test, file, line)

  @assertion = assertion
  @arguments = arguments

  @test = test

  @file = file
  @line = line

  if error_or_message.is_a?(String)
    @msg = error_or_message
  else
    @msg = "error while asserting: " + error_or_message.message
    @nested_error = error_or_message
  end

  super(@msg)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



9
10
11
# File 'lib/probatio/errors.rb', line 9

def arguments
  @arguments
end

#assertionObject (readonly)

Returns the value of attribute assertion.



9
10
11
# File 'lib/probatio/errors.rb', line 9

def assertion
  @assertion
end

#fileObject (readonly) Also known as: path

Returns the value of attribute file.



9
10
11
# File 'lib/probatio/errors.rb', line 9

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



9
10
11
# File 'lib/probatio/errors.rb', line 9

def line
  @line
end

#nested_errorObject

Returns the value of attribute nested_error.



10
11
12
# File 'lib/probatio/errors.rb', line 10

def nested_error
  @nested_error
end

#testObject (readonly)

Returns the value of attribute test.



9
10
11
# File 'lib/probatio/errors.rb', line 9

def test
  @test
end

Class Method Details

.select_source_lines(path, line) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/probatio/errors.rb', line 105

def select_source_lines(path, line)

  return [] unless path

  File.readlines(path).each_with_index.to_a[line - 1..-1]
    .map { |l, i| [ i + 1, l.rstrip ] }
    .take_while { |_, l|
      l = l.strip
      l.length > 0 && l != 'end' && l != '}' }
end

Instance Method Details

#locObject



39
40
41
42
# File 'lib/probatio/errors.rb', line 39

def loc

  location.map(&:to_s).join(':')
end

#locationObject



34
35
36
37
# File 'lib/probatio/errors.rb', line 34

def location

  [ @file, @line ]
end

#source_lineObject



55
56
57
58
59
# File 'lib/probatio/errors.rb', line 55

def source_line

  @source_line ||=
    File.readlines(@file)[@line - 1]
end

#source_linesObject



61
62
63
64
65
# File 'lib/probatio/errors.rb', line 61

def source_lines

  @source_lines ||=
    Probatio::AssertionError.select_source_lines(@file, @line)
end

#summary(indent = '') ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/probatio/errors.rb', line 67

def summary(indent='')

  nl = "\n" + indent

  tw = Probatio.term_width - 4 - indent.length

  as =
    @arguments.find { |a| a.inspect.length > tw } ?
      @arguments.collect { |a|
        if (s0 = a.inspect).length < tw
          nl + '    ' + s0 + "\n"
        else
          s1 = StringIO.new; PP.pp(a, s1, tw)
          qualify_argument(a) + "\n" +
          indent + s1.string.gsub(/^(.*)$/) { "    #{$1}" }
        end } :
    @arguments.collect(&:inspect)

  s = StringIO.new
  s << indent << @assertion << ':'

  case @arguments.collect(&:class)
  when [ Hash, Hash ]
    as.each_with_index { |a, i| s << nl << '  %d: %s' % [ i, a ] }
    output_hash_diff(indent, s)
  when [ Array, Array ]
    output_array_diff(indent, s)
  when [ String, String ]
    output_string_diff(indent, s)
  else
    as.each_with_index { |a, i| s << nl << '  %d: %s' % [ i, a ] }
  end

  s.string
end

#to_sObject



44
45
46
47
# File 'lib/probatio/errors.rb', line 44

def to_s

  "#{self.class.name}: #{@msg}"
end

#trailObject



49
50
51
52
53
# File 'lib/probatio/errors.rb', line 49

def trail

  @test.trail + "\n" +
  Probatio.c.red("#{'  ' * (test.depth + 1)}#{loc} --> #{@msg}")
end