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, HashInspector, 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.



1641
1642
1643
1644
1645
# File 'lib/test/unit/assertions.rb', line 1641

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.



1418
1419
1420
# File 'lib/test/unit/assertions.rb', line 1418

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/test/unit/assertions.rb', line 1499

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)
        if HashInspector.target?(object)
          pp_target = HashInspector.new(object)
        else
          pp_target = object
        end
        begin
          return PP.pp(pp_target, '').chomp
        rescue NameError
        end
      rescue LoadError
        self.use_pp = false
      end
    end
    object.inspect
  end
end

.delayed_diff(from, to) ⇒ Object



1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
# File 'lib/test/unit/assertions.rb', line 1476

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



1424
1425
1426
# File 'lib/test/unit/assertions.rb', line 1424

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


1445
1446
1447
1448
1449
1450
1451
# File 'lib/test/unit/assertions.rb', line 1445

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



1453
1454
1455
1456
1457
1458
1459
# File 'lib/test/unit/assertions.rb', line 1453

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



1420
1421
1422
# File 'lib/test/unit/assertions.rb', line 1420

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

.max_diff_target_string_sizeObject



1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
# File 'lib/test/unit/assertions.rb', line 1433

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



1428
1429
1430
# File 'lib/test/unit/assertions.rb', line 1428

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

.prepare_for_diff(from, to) ⇒ Object



1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
# File 'lib/test/unit/assertions.rb', line 1461

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



1655
1656
1657
# File 'lib/test/unit/assertions.rb', line 1655

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

#convert(object) ⇒ Object



1647
1648
1649
# File 'lib/test/unit/assertions.rb', line 1647

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

#templateObject



1651
1652
1653
# File 'lib/test/unit/assertions.rb', line 1651

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

#to_sObject



1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
# File 'lib/test/unit/assertions.rb', line 1659

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