Class: Test::Unit::Assertions::AssertionMessage

Inherits:
Object
  • Object
show all
Includes:
Util::BacktraceFilter
Defined in:
lib/test/unit/assertions.rb

Defined Under Namespace

Classes: DelayedLiteral, Literal, MaybeContainer, Template

Constant Summary collapse

MAX_DIFF_TARGET_STRING_SIZE =
1000

Constants included from Util::BacktraceFilter

Util::BacktraceFilter::TESTUNIT_FILE_SEPARATORS, Util::BacktraceFilter::TESTUNIT_PREFIX, Util::BacktraceFilter::TESTUNIT_RB_FILE

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::BacktraceFilter

filter_backtrace

Constructor Details

#initialize(head, template_string, parameters) ⇒ AssertionMessage

Returns a new instance of AssertionMessage.



1084
1085
1086
1087
1088
# File 'lib/test/unit/assertions.rb', line 1084

def initialize(head, template_string, parameters)
  @head = head
  @template_string = template_string
  @parameters = parameters
end

Class Attribute Details

.use_ppObject

Returns the value of attribute use_pp.



926
927
928
# File 'lib/test/unit/assertions.rb', line 926

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
# File 'lib/test/unit/assertions.rb', line 997

def convert(object)
  case object
  when Exception
    <<EOM.chop
Class: <#{convert(object.class)}>
Message: <#{convert(object.message)}>
---Backtrace---
#{Util::BacktraceFilter.filter_backtrace(object.backtrace).join("\n")}
---------------
EOM
  else
    if use_pp
      begin
        require 'pp' unless defined?(PP)
        begin
          return PP.pp(object, '').chomp
        rescue NameError
        end
      rescue LoadError
        self.use_pp = false
      end
    end
    object.inspect
  end
end

.delayed_diff(from, to) ⇒ Object



974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/test/unit/assertions.rb', line 974

def delayed_diff(from, to)
  delayed_literal do
    from, to = prepare_for_diff(from, to)

    diff = "" if from.nil? or to.nil?
    diff ||= Diff.readable(from, to)
    if /^[-+]/ !~ diff
      diff = ""
    elsif /^[ ?]/ =~ diff or /(?:.*\n){2,}/ =~ diff
      diff = "\n\ndiff:\n#{diff}"
    else
      diff = ""
    end

    if Diff.need_fold?(diff)
      folded_diff = Diff.folded_readable(from, to)
      diff << "\n\nfolded diff:\n#{folded_diff}"
    end

    diff
  end
end

.delayed_literal(&block) ⇒ Object



932
933
934
# File 'lib/test/unit/assertions.rb', line 932

def delayed_literal(&block)
  DelayedLiteral.new(block)
end

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


953
954
955
956
957
958
959
# File 'lib/test/unit/assertions.rb', line 953

def diff_target_string?(string)
  if string.respond_to?(:bytesize)
    string.bytesize < max_diff_target_string_size
  else
    string.size < max_diff_target_string_size
  end
end

.literal(value) ⇒ Object



928
929
930
# File 'lib/test/unit/assertions.rb', line 928

def literal(value)
  Literal.new(value)
end

.max_diff_target_string_sizeObject



941
942
943
944
945
946
947
948
949
950
951
# File 'lib/test/unit/assertions.rb', line 941

def max_diff_target_string_size
  size = ENV["TEST_UNIT_MAX_DIFF_TARGET_STRING_SIZE"]
  if size
    begin
      size = Integer(size)
    rescue ArgumentError
      size = nil
    end
  end
  size || MAX_DIFF_TARGET_STRING_SIZE
end

.maybe_container(value, &formatter) ⇒ Object



936
937
938
# File 'lib/test/unit/assertions.rb', line 936

def maybe_container(value, &formatter)
  MaybeContainer.new(value, &formatter)
end

.prepare_for_diff(from, to) ⇒ Object



961
962
963
964
965
966
967
968
969
970
971
972
# File 'lib/test/unit/assertions.rb', line 961

def prepare_for_diff(from, to)
  if !from.is_a?(String) or !to.is_a?(String)
    from = convert(from)
    to = convert(to)
  end

  if diff_target_string?(from) and diff_target_string?(to)
    [from, to]
  else
    [nil, nil]
  end
end

Instance Method Details

#add_period(string) ⇒ Object



1098
1099
1100
# File 'lib/test/unit/assertions.rb', line 1098

def add_period(string)
  (string =~ /\.\Z/ ? string : string + '.')
end

#convert(object) ⇒ Object



1090
1091
1092
# File 'lib/test/unit/assertions.rb', line 1090

def convert(object)
  self.class.convert(object)
end

#templateObject



1094
1095
1096
# File 'lib/test/unit/assertions.rb', line 1094

def template
  @template ||= Template.create(@template_string)
end

#to_sObject



1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'lib/test/unit/assertions.rb', line 1102

def to_s
  message_parts = []
  if (@head)
    head = @head.to_s 
    unless(head.empty?)
      message_parts << add_period(head)
    end
  end
  tail = template.result(@parameters.collect{|e| convert(e)})
  message_parts << tail unless(tail.empty?)
  message_parts.join("\n")
end