lib/assert/result.rb in assert-2.15.2 vs lib/assert/result.rb in assert-2.16.0

- old
+ new

@@ -1,5 +1,7 @@ +require 'assert/file_line' + module Assert; end module Assert::Result class Base; end class Pass < Base; end @@ -28,54 +30,80 @@ def self.type; :unknown; end def self.name; ''; end def self.for_test(test, message, bt) self.new({ - :test_name => test.name, - :test_id => test.file_line.to_s, - :message => message, - :output => test.output, - :backtrace => Backtrace.new(bt) + :test_name => test.name, + :test_file_line => test.file_line, + :message => message, + :output => test.output, + :backtrace => Backtrace.new(bt) }) end def initialize(build_data) @build_data = build_data end - def type; @type ||= (@build_data[:type] || self.class.type).to_sym; end - def name; @name ||= (@build_data[:name] || self.class.name.to_s); end - def test_name; @test_name ||= (@build_data[:test_name] || ''); end - def test_id; @test_id ||= (@build_data[:test_id] || ''); end - def message; @message ||= (@build_data[:message] || ''); end - def output; @output ||= (@build_data[:output] || ''); end - def backtrace; @backtrace ||= (@build_data[:backtrace] || Backtrace.new([])); end - def trace; @trace ||= (@build_data[:trace] || build_trace(self.backtrace)); end + def type + @type ||= (@build_data[:type] || self.class.type).to_sym + end - Assert::Result.types.keys.each do |type| - define_method("#{type}?"){ self.type == type } + def name + @name ||= (@build_data[:name] || self.class.name.to_s) end + def test_name + @test_name ||= (@build_data[:test_name] || '') + end + + def test_file_line + @test_file_line ||= (@build_data[:test_file_line] || Assert::FileLine.parse('')) + end + + def test_file_name; self.test_file_line.file; end + def test_line_num; self.test_file_line.line.to_i; end + + def test_id + self.test_file_line.to_s + end + + def message + @message ||= (@build_data[:message] || '') + end + + def output + @output ||= (@build_data[:output] || '') + end + + def backtrace + @backtrace ||= (@build_data[:backtrace] || Backtrace.new([])) + end + + def trace + @trace ||= (@build_data[:trace] || build_trace(self.backtrace)) + end + # we choose to implement this way instead of using an `attr_writer` to be # consistant with how you override exception backtraces. def set_backtrace(bt) @backtrace = Backtrace.new(bt) @trace = build_trace(@backtrace) + @file_line = Assert::FileLine.parse(first_filtered_bt_line(@backtrace)) end - def data - { :type => self.type, - :name => self.name, - :test_name => self.test_name, - :test_id => self.test_id, - :message => self.message, - :output => self.output, - :backtrace => self.backtrace, - :trace => self.trace, - } + def file_line + @file_line ||= Assert::FileLine.parse(first_filtered_bt_line(self.backtrace)) end + def file_name; self.file_line.file; end + def line_num; self.file_line.line.to_i; end + + Assert::Result.types.keys.each do |type| + define_method("#{type}?"){ self.type == type } + end + def to_sym; self.type; end def to_s [ "#{self.name.upcase}: #{self.test_name}", self.message, @@ -86,17 +114,29 @@ def ==(other_result) self.type == other_result.type && self.message == other_result.message end def inspect - "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @message=#{self.message.inspect}>" + "#<#{self.class}:#{'0x0%x' % (object_id << 1)} "\ + "@message=#{self.message.inspect} "\ + "@file_line=#{self.file_line.to_s.inspect} "\ + "@test_file_line=#{self.test_file_line.to_s.inspect}>" end private # by default, a result's trace is the first line of its filtered backtrace - def build_trace(backtrace); backtrace.filtered.first.to_s; end + # if the filtered backtrace is empty, just use the backtrace itself (this + # should only occur if the result is an error from a line in assert's + # non-test code). This is overridden for error results as they always show + # the entire backtrace + def build_trace(backtrace) + first_filtered_bt_line(backtrace) + end + def first_filtered_bt_line(backtrace) + ((fbt = backtrace.filtered).empty? ? backtrace : fbt).first.to_s + end end class Pass < Base def self.type; :pass; end