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.



2077
2078
2079
2080
2081
# File 'lib/test/unit/assertions.rb', line 2077

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.



1705
1706
1707
# File 'lib/test/unit/assertions.rb', line 1705

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
# File 'lib/test/unit/assertions.rb', line 1793

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



1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
# File 'lib/test/unit/assertions.rb', line 1770

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



1711
1712
1713
# File 'lib/test/unit/assertions.rb', line 1711

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


1739
1740
1741
1742
1743
1744
1745
# File 'lib/test/unit/assertions.rb', line 1739

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



1747
1748
1749
1750
1751
1752
1753
# File 'lib/test/unit/assertions.rb', line 1747

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



1707
1708
1709
# File 'lib/test/unit/assertions.rb', line 1707

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

.max_diff_target_string_sizeObject



1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
# File 'lib/test/unit/assertions.rb', line 1720

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



1735
1736
1737
# File 'lib/test/unit/assertions.rb', line 1735

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

.maybe_container(value, &formatter) ⇒ Object



1715
1716
1717
# File 'lib/test/unit/assertions.rb', line 1715

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

.prepare_for_diff(from, to) ⇒ Object



1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
# File 'lib/test/unit/assertions.rb', line 1755

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



2083
2084
2085
# File 'lib/test/unit/assertions.rb', line 2083

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

#templateObject



2087
2088
2089
# File 'lib/test/unit/assertions.rb', line 2087

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

#to_sObject



2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
# File 'lib/test/unit/assertions.rb', line 2098

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



2091
2092
2093
2094
2095
2096
# File 'lib/test/unit/assertions.rb', line 2091

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