assert-text-equal.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. ###
  2. ### $Release: 2.7.0 $
  3. ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ###
  5. require 'test/unit'
  6. require 'tempfile'
  7. module Test
  8. module Unit
  9. class TestCase
  10. def assert_text_equal(expected, actual, message=nil, diffopt='-u', flag_cut=true)
  11. if expected == actual
  12. assert(true)
  13. return
  14. end
  15. if expected[-1] != ?\n || actual[-1] != ?\n
  16. expected += "\n"
  17. actual += "\n"
  18. end
  19. begin
  20. expfile = Tempfile.new(".expected.")
  21. expfile.write(expected); expfile.flush()
  22. actfile = Tempfile.new(".actual.")
  23. actfile.write(actual); actfile.flush()
  24. diff = `diff #{diffopt} #{expfile.path} #{actfile.path}`
  25. ensure
  26. expfile.close(true) if expfile
  27. actfile.close(true) if actfile
  28. end
  29. # cut 1st & 2nd lines
  30. message = (flag_cut ? diff.gsub(/\A.*\n.*\n/, '') : diff) unless message
  31. #raise Test::Unit::AssertionFailedError.new(message)
  32. assert_block(message) { false } # or assert(false, message)
  33. end
  34. alias assert_equal_with_diff assert_text_equal # for compatibility
  35. alias assert_text_equals assert_text_equal # for typo
  36. end
  37. end
  38. end