class Test::Unit::Assertions::AssertionMessage - test-unit: Ruby Standard Library Documentation

# class Test::Unit::Assertions::AssertionMessage

### Constants

MAX_DIFF_TARGET_STRING_SIZE

### Attributes

use_pp[RW]

### Public Class Methods

convert(object) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 1989
    def delayed_literal(&block)
      DelayedLiteral.new(block)
    end

diff_target_string?(string) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 1985
    def literal(value)
      Literal.new(value)
    end

max_diff_target_string_size() click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 1993
    def maybe_container(value, &formatter)
      MaybeContainer.new(value, &formatter)
    end

new(user_message, template_string, parameters) click to toggle source

    # File test-unit-3.6.7/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

prepare_for_diff(from, to) click to toggle source

    # File test-unit-3.6.7/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

### Public Instance Methods

convert(object) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2361
    def convert(object)
      self.class.convert(object)
    end

template() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2365
    def template
      @template ||= Template.create(@template_string)
    end

to_s() click to toggle source

    # File test-unit-3.6.7/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_message() click to toggle source

    # File test-unit-3.6.7/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

### Private Instance Methods

add_period(string) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/assertions.rb, line 2388
    def add_period(string)
      (string =~ /\.\Z/ ? string : string + '.')
    end