Class: Wrong::FailureMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/wrong/failure_message.rb

Defined Under Namespace

Classes: Formatter

Constant Summary collapse

@@formatters =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(chunk, valence, explanation) ⇒ FailureMessage

Returns a new instance of FailureMessage.



48
49
50
# File 'lib/wrong/failure_message.rb', line 48

def initialize(chunk, valence, explanation)
  @chunk, @valence, @explanation = chunk, valence, explanation
end

Instance Attribute Details

#chunkObject

Returns the value of attribute chunk.



46
47
48
# File 'lib/wrong/failure_message.rb', line 46

def chunk
  @chunk
end

#explanationObject

Returns the value of attribute explanation.



46
47
48
# File 'lib/wrong/failure_message.rb', line 46

def explanation
  @explanation
end

#valenceObject

Returns the value of attribute valence.



46
47
48
# File 'lib/wrong/failure_message.rb', line 46

def valence
  @valence
end

Class Method Details

.formatter_for(predicate) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/wrong/failure_message.rb', line 16

def self.formatter_for(predicate)
  @@formatters.each do |formatter_class|
    formatter = formatter_class.new(predicate)
    if formatter.match?
      return formatter
    end
  end
  nil
end

.formattersObject



12
13
14
# File 'lib/wrong/failure_message.rb', line 12

def self.formatters
  @@formatters
end

.register_formatter(formatter) ⇒ Object



8
9
10
# File 'lib/wrong/failure_message.rb', line 8

def self.register_formatter(formatter)
  @@formatters << formatter
end

Instance Method Details

#basicObject



52
53
54
# File 'lib/wrong/failure_message.rb', line 52

def basic
  "#{valence == :deny ? "Didn't expect" : "Expected"} #{colored(:blue, chunk.code)}"
end

#detailsObject



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/wrong/failure_message.rb', line 76

def details
  @details ||= begin
    require "wrong/rainbow" if Wrong.config[:color]
    s = ""
    parts = chunk.parts

    parts.shift while parts.first == "()" # the parser adds this sometimes
    parts.shift # remove the first part, since it's the same as the code

    details = []

    if parts.size > 0
      parts.each do |part|
        begin
          value = eval(part, chunk.block.binding)
          unless part == value.inspect # this skips literals or tautologies
            if part =~ /\n/m
              part.gsub!(/\n/, newline(2))
              part += newline(3)
            end
            value = pretty_value(value, (4 + part.length + 4))
            if Wrong.config[:color]
              part = part.color(:blue)
              value = value.color(:magenta)
            end
            details << indent(4, part, " is ", value)
          end
        rescue Exception => e
          raises = "raises #{e.class}"
          if Wrong.config[:color]
            part = part.color(:blue)
            raises = raises.bold.color(:red)
          end
          formatted_exeption = if e.message and e.message != e.class.to_s
                                 indent(4, part, " ", raises, ": ", indent_all(6, e.message))
                               else
                                 indent(4, part, " ", raises)
                               end
          details << formatted_exeption
        end
      end
    end

    details.uniq!
    if details.empty?
      ""
    else
      "\n" + details.join("\n") + "\n"
    end
  end

end

#fullObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/wrong/failure_message.rb', line 56

def full
  message = ""
  message << "#{explanation}: " if explanation
  message << basic

  formatted_message = if predicate && !(predicate.is_a? Predicated::Conjunction)
                        if formatter = FailureMessage.formatter_for(predicate)
                          colored(:bold, formatter.describe)
                        end
                      end

  unless details.empty? and formatted_message.nil?
    message << ", but"
  end

  message << formatted_message if formatted_message
  message << details unless details.empty?
  message
end

#pretty_value(value, starting_col = 0, indent_wrapped_lines = 6, width = Terminal.width) ⇒ Object

todo: use awesome_print



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/wrong/failure_message.rb', line 130

def pretty_value(value, starting_col = 0, indent_wrapped_lines = 6, width = Terminal.width)
  # inspected = value.inspect

  # note that if the first line overflows due to the starting column then pp won't wrap it right
  inspected = PP.pp(value, "", width - starting_col).chomp

  # this bit might be redundant with the pp call now
  indented = indent_all(6, inspected)
  if width
    wrap_and_indent(indented, starting_col, indent_wrapped_lines, width)
  else
    indented
  end
end