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

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

Defined Under Namespace

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

Constant Summary

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(head, template_string, parameters) ⇒ AssertionMessage

Returns a new instance of AssertionMessage



2044
2045
2046
2047
2048
# File 'lib/test/unit/assertions.rb', line 2044

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



1659
1660
1661
# File 'lib/test/unit/assertions.rb', line 1659

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
# File 'lib/test/unit/assertions.rb', line 1760

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, '').chomp
      rescue NameError
      end
    rescue LoadError
      self.use_pp = false
    end
  end
  inspector.inspect
end

.delayed_diff(from, to) ⇒ Object



1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
# File 'lib/test/unit/assertions.rb', line 1737

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



1665
1666
1667
# File 'lib/test/unit/assertions.rb', line 1665

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


1706
1707
1708
1709
1710
1711
1712
# File 'lib/test/unit/assertions.rb', line 1706

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



1714
1715
1716
1717
1718
1719
1720
# File 'lib/test/unit/assertions.rb', line 1714

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



1661
1662
1663
# File 'lib/test/unit/assertions.rb', line 1661

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

.max_diff_target_string_sizeObject



1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
# File 'lib/test/unit/assertions.rb', line 1687

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



1702
1703
1704
# File 'lib/test/unit/assertions.rb', line 1702

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

.maybe_container(value, &formatter) ⇒ Object



1669
1670
1671
# File 'lib/test/unit/assertions.rb', line 1669

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

.normalize_tag(tag) ⇒ Object



1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
# File 'lib/test/unit/assertions.rb', line 1673

def normalize_tag(tag)
  case tag
  when /\A:/
    tag[1..-1].intern
  when /\A`(.+)'\z/
    $1.intern
  when String
    tag.intern
  else
    tag
  end
end

.prepare_for_diff(from, to) ⇒ Object



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

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

#add_period(string) ⇒ Object



2058
2059
2060
# File 'lib/test/unit/assertions.rb', line 2058

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

#convert(object) ⇒ Object



2050
2051
2052
# File 'lib/test/unit/assertions.rb', line 2050

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

#templateObject



2054
2055
2056
# File 'lib/test/unit/assertions.rb', line 2054

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

#to_sObject



2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
# File 'lib/test/unit/assertions.rb', line 2062

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