Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/kwatable/util/assert-text-equal.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#assert_text_equal(expected, actual, message = nil, options = {}) ⇒ Object Also known as: assert_equal_with_diff, assert_text_equals

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kwatable/util/assert-text-equal.rb', line 14

def assert_text_equal(expected, actual, message=nil, options={}) # :nodoc:
  diffopt  = options[:diffopt] || '-u'
  flag_cut = options.key?(:cut) ? options[:key] : true
  prefix   = options[:prefix] || ''
  #prefix   = prefix + "\n" if !prefix.empty? && prefix[-1] != ?\n

  if expected == actual
    assert(true)
    return
  end
  if expected[-1] != ?\n || actual[-1] != ?\n
    expected += "\n"
    actual   += "\n"
  end
  begin
    expfile = Tempfile.new(".expected.")
    expfile.write(expected); expfile.flush()
    actfile = Tempfile.new(".actual.")
    actfile.write(actual);   actfile.flush()
    diff = `diff #{diffopt} #{expfile.path} #{actfile.path}`
  ensure
    expfile.close(true) if expfile
    actfile.close(true) if actfile
  end
  diff.sub!(/\A.*\n.*\n/, '') if flag_cut  # cut 1st & 2nd lines
  msg = prefix + (message || diff)
  assert_block(msg) { false }  # or assert(false, msg)
end