Class: Test::Unit::TestCase

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

Overview

:nodoc:

Constant Summary collapse

SPECIAL_KEYS =
%[exception errormsg]

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
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kwartz/util/testcase-helper.rb', line 14

def self._untabify(str, width=8)         # :nodoc:
  list = str.split(/\t/, -1)
  return list.first if list.length == 1
  last = list.pop
  buf = []
  list.each do |s|
    column = (pos = s.rindex(?\n)) ? s.length - pos - 1 : s.length
    n = width - (column % width)
    buf << s << (" " * n)
  end
  buf << last
  return buf.join
  #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:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kwartz/util/testcase-helper.rb', line 36

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
  end
  #
  target = $target || ENV['TEST']
  if target
     table[target] or raise "*** target '#{target}' not found."
     list = [ table[target] ]
  end
  #
  list.each do |ydoc| yield(ydoc) end if block_given?
  #
  return list
end

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

:nodoc:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kwartz/util/testcase-helper.rb', line 78

def self.load_yaml_testdata(filename, options={})   # :nodoc:
  identkey   = options[:identkey]   || 'name'
  testmethod = options[:testmethod] || '_test'
  lang       = options[:lang]
  special_keys = options[:special_keys] || SPECIAL_KEYS
  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]
        k = special_keys.include?(key) ? 'ruby' : lang
        val = val[k]
      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:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/kwartz/util/testcase-helper.rb', line 107

def self.load_yaml_testdata_with_each_lang(filename, options={})   # :nodoc:
  identkey   = options[:identkey]   || 'name'
  testmethod = options[:testmethod] || '_test'
  special_keys = options[:special_keys] || SPECIAL_KEYS
  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]
          k = special_keys.include?(key) ? 'ruby' : lang
          val = val[k]
        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:



102
103
104
# File 'lib/kwartz/util/testcase-helper.rb', line 102

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
# File 'lib/kwartz/util/assert-text-equal.rb', line 13

def assert_text_equal(expected, actual, message=nil, options={}) # :nodoc:
  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