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

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

Defined Under Namespace

Classes: ArrayInspector, DelayedLiteral, HashInspector, Inspector, Literal, MaybeContainer, NumericInspector, Template

Constant Summary collapse

MAX_DIFF_TARGET_STRING_SIZE =
1000
@@max_diff_target_string_size =
nil

Constants included from Util::BacktraceFilter

Util::BacktraceFilter::POWERASSERT_PREFIX, 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(user_message, template_string, parameters) ⇒ AssertionMessage

Returns a new instance of AssertionMessage.



2355
2356
2357
2358
2359
# File 'lib/test/unit/assertions.rb', line 2355

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

Class Attribute Details

.use_ppObject

Returns the value of attribute use_pp.



1983
1984
1985
# File 'lib/test/unit/assertions.rb', line 1983

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
# File 'lib/test/unit/assertions.rb', line 2071

def convert(object)
  if object.is_a?(Exception)
    object = AssertExceptionHelper::WrappedException.new(object)
  end
  inspector = Inspector.new(object)
  if use_pp
    begin
      require "pp" unless defined?(PP)
      begin
        return PP.pp(inspector, String.new).chomp
      rescue NameError
      end
    rescue LoadError
      self.use_pp = false
    end
  end
  inspector.inspect
end

.delayed_diff(from, to) ⇒ Object



2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
# File 'lib/test/unit/assertions.rb', line 2048

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



1989
1990
1991
# File 'lib/test/unit/assertions.rb', line 1989

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


2017
2018
2019
2020
2021
2022
2023
# File 'lib/test/unit/assertions.rb', line 2017

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

.ensure_diffable_string(string) ⇒ Object



2025
2026
2027
2028
2029
2030
2031
# File 'lib/test/unit/assertions.rb', line 2025

def ensure_diffable_string(string)
  if string.respond_to?(:encoding) and
      !string.encoding.ascii_compatible?
    string = string.dup.force_encoding("ASCII-8BIT")
  end
  string
end

.literal(value) ⇒ Object



1985
1986
1987
# File 'lib/test/unit/assertions.rb', line 1985

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

.max_diff_target_string_sizeObject



1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
# File 'lib/test/unit/assertions.rb', line 1998

def max_diff_target_string_size
  return @@max_diff_target_string_size if @@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

.max_diff_target_string_size=(size) ⇒ Object



2013
2014
2015
# File 'lib/test/unit/assertions.rb', line 2013

def max_diff_target_string_size=(size)
  @@max_diff_target_string_size = size
end

.maybe_container(value, &formatter) ⇒ Object



1993
1994
1995
# File 'lib/test/unit/assertions.rb', line 1993

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

.prepare_for_diff(from, to) ⇒ Object



2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
# File 'lib/test/unit/assertions.rb', line 2033

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 = ensure_diffable_string(from)
    to = ensure_diffable_string(to)
    [from, to]
  else
    [nil, nil]
  end
end

Instance Method Details

#convert(object) ⇒ Object



2361
2362
2363
# File 'lib/test/unit/assertions.rb', line 2361

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

#templateObject



2365
2366
2367
# File 'lib/test/unit/assertions.rb', line 2365

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

#to_sObject



2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
# File 'lib/test/unit/assertions.rb', line 2376

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

#user_messageObject



2369
2370
2371
2372
2373
2374
# File 'lib/test/unit/assertions.rb', line 2369

def user_message
  return nil unless @user_message
  message = @user_message
  message = message.call if message.respond_to?(:call)
  message.to_s
end