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.



2263
2264
2265
2266
2267
# File 'lib/test/unit/assertions.rb', line 2263

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.



1891
1892
1893
# File 'lib/test/unit/assertions.rb', line 1891

def use_pp
  @use_pp
end

Class Method Details

.convert(object) ⇒ Object



1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
# File 'lib/test/unit/assertions.rb', line 1979

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



1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
# File 'lib/test/unit/assertions.rb', line 1956

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



1897
1898
1899
# File 'lib/test/unit/assertions.rb', line 1897

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

.diff_target_string?(string) ⇒ Boolean

Returns:

  • (Boolean)


1925
1926
1927
1928
1929
1930
1931
# File 'lib/test/unit/assertions.rb', line 1925

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



1933
1934
1935
1936
1937
1938
1939
# File 'lib/test/unit/assertions.rb', line 1933

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



1893
1894
1895
# File 'lib/test/unit/assertions.rb', line 1893

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

.max_diff_target_string_sizeObject



1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
# File 'lib/test/unit/assertions.rb', line 1906

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



1921
1922
1923
# File 'lib/test/unit/assertions.rb', line 1921

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

.maybe_container(value, &formatter) ⇒ Object



1901
1902
1903
# File 'lib/test/unit/assertions.rb', line 1901

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

.prepare_for_diff(from, to) ⇒ Object



1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
# File 'lib/test/unit/assertions.rb', line 1941

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



2269
2270
2271
# File 'lib/test/unit/assertions.rb', line 2269

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

#templateObject



2273
2274
2275
# File 'lib/test/unit/assertions.rb', line 2273

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

#to_sObject



2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
# File 'lib/test/unit/assertions.rb', line 2284

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



2277
2278
2279
2280
2281
2282
# File 'lib/test/unit/assertions.rb', line 2277

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