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.



1188
1189
1190
1191
1192
# File 'lib/test/unit/assertions.rb', line 1188

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.



1030
1031
1032
# File 'lib/test/unit/assertions.rb', line 1030

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
# File 'lib/test/unit/assertions.rb', line 1101

def convert(object)
  case object
  when Exception
    "Class: <\#{convert(object.class)}>\nMessage: <\#{convert(object.message)}>\n---Backtrace---\n\#{Util::BacktraceFilter.filter_backtrace(object.backtrace).join(\"\\n\")}\n---------------\n".chop
  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



1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'lib/test/unit/assertions.rb', line 1078

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



1036
1037
1038
# File 'lib/test/unit/assertions.rb', line 1036

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


1057
1058
1059
1060
1061
1062
1063
# File 'lib/test/unit/assertions.rb', line 1057

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



1032
1033
1034
# File 'lib/test/unit/assertions.rb', line 1032

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

.max_diff_target_string_sizeObject



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
# File 'lib/test/unit/assertions.rb', line 1045

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



1040
1041
1042
# File 'lib/test/unit/assertions.rb', line 1040

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

.prepare_for_diff(from, to) ⇒ Object



1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/test/unit/assertions.rb', line 1065

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



1202
1203
1204
# File 'lib/test/unit/assertions.rb', line 1202

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

#convert(object) ⇒ Object



1194
1195
1196
# File 'lib/test/unit/assertions.rb', line 1194

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

#templateObject



1198
1199
1200
# File 'lib/test/unit/assertions.rb', line 1198

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

#to_sObject



1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/test/unit/assertions.rb', line 1206

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