Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/kwalify/util/assert-text-equal.rb,
lib/kwalify/util/testcase-helper.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._untabify(str, width = 8) ⇒ Object

:nodoc:



14
15
16
17
18
19
20
21
22
# File 'lib/kwalify/util/testcase-helper.rb', line 14

def self._untabify(str, width=8)         # :nodoc:
  sb = []
  str.scan(/(.*?)\t/m) do |s, |
    len = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
    sb << s << (" " * (width - len % width))
  end
  str = (sb << $').join if $'
  return str
end

.load_yaml_documents(filename, options = {}) ⇒ Object

:nodoc:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kwalify/util/testcase-helper.rb', line 25

def self.load_yaml_documents(filename, options={})   # :nodoc:
  str = File.read(filename)
  if filename =~ /\.rb$/
    str =~ /^__END__$/   or raise "*** error: __END__ is not found in '#{filename}'."
    str = $'
  end
  str = _untabify(str) unless options[:tabify] == false
  #
  identkey = options[:identkey] || 'name'
  list = []
  table = {}
  YAML.load_documents(str) do |ydoc|
    if ydoc.is_a?(Hash)
      list << ydoc
    elsif ydoc.is_a?(Array)
      list += ydoc
    else
      raise "*** invalid ydoc: #{ydoc.inspect}"
    end
  end
  #
  list.each do |ydoc|
    ident = ydoc[identkey]
    ident         or  raise "*** #{identkey} is not found."
    table[ident]  and raise "*** #{identkey} '#{ident}' is duplicated."
    table[ident] = ydoc
    yield(ydoc) if block_given?
  end
  #
  return list
end

.load_yaml_testdata(filename, options = {}) ⇒ Object

:nodoc:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kwalify/util/testcase-helper.rb', line 58

def self.load_yaml_testdata(filename, options={})   # :nodoc:
  identkey   = options[:identkey]   || 'name'
  testmethod = options[:testmethod] || '_test'
  lang       = options[:lang]
  load_yaml_documents(filename, options) do |ydoc|
    ident = ydoc[identkey]
    s  =   "def test_#{ident}\n"
    ydoc.each do |key, val|
      if key[-1] == ?*
        key = key[0, key.length-1]
        val = val[lang]
      end
      s << "  @#{key} = #{val.inspect}\n"
    end
    s  <<  "  #{testmethod}\n"
    s  <<  "end\n"
    #$stderr.puts "*** #{method_name()}(): eval_str=<<'END'\n#{s}END" if $DEBUG
    module_eval s   # not eval!
  end
end

.load_yaml_testdata_with_each_lang(filename, options = {}) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kwalify/util/testcase-helper.rb', line 85

def self.load_yaml_testdata_with_each_lang(filename, options={})   # :nodoc:
  identkey   = options[:identkey]   || 'name'
  testmethod = options[:testmethod] || '_test'
  langs = defined?($lang) && $lang ? [ $lang ] : options[:langs]
  langs or raise "*** #{method_name()}(): option ':langs' is required."
  #
  load_yaml_documents(filename, options) do |ydoc|
    ident = ydoc[identkey]
    langs.each do |lang|
      s  =   "def test_#{ident}_#{lang}\n"
      s  <<  "  @lang = #{lang.inspect}\n"
      ydoc.each do |key, val|
        if key[-1] == ?*
          key = key[0, key.length-1]
          val = val[lang]
        end
        s << "  @#{key} = #{val.inspect}\n"
      end
      s  <<  "  #{testmethod}\n"
      s  <<  "end\n"
      #$stderr.puts "*** #{method_name()}(): eval_str=<<'END'\n#{s}END" if $DEBUG
      module_eval s   # not eval!
    end
  end
end

.method_nameObject

:nodoc:



80
81
82
# File 'lib/kwalify/util/testcase-helper.rb', line 80

def self.method_name   # :nodoc:
  return (caller[0] =~ /in `(.*?)'/) && $1
end

Instance Method Details

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

:nodoc:



13
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/kwalify/util/assert-text-equal.rb', line 13

def assert_text_equal(expected, actual, message=nil, options={}) # :nodoc:
  expected = expected.to_s
  actual   = actual.to_s
  diffopt  = options[:diffopt] || '-u'
  flag_cut = options.key?(:cut) ? options[:key] : true

  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
  # cut 1st & 2nd lines
  message = (flag_cut ? diff.gsub(/\A.*\n.*\n/, '') : diff) unless message
  #raise Test::Unit::AssertionFailedError.new(message)
  assert_block(message) { false }  # or assert(false, message)
end