testutil.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. ###
  2. ### $Release: 2.7.0 $
  3. ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
  4. ###
  5. require 'yaml'
  6. require 'test/unit/testcase'
  7. def ruby18? # :nodoc:
  8. RUBY_VERSION =~ /\A1.8/
  9. end
  10. def ruby19? # :nodoc:
  11. RUBY_VERSION =~ /\A1.9/
  12. end
  13. def rubinius? # :nodoc:
  14. defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
  15. end
  16. class Test::Unit::TestCase
  17. def self.load_yaml_datafile(filename, options={}, &block) # :nodoc:
  18. # read datafile
  19. s = File.read(filename)
  20. if filename =~ /\.rb$/
  21. s =~ /^__END__$/ or raise "*** error: __END__ is not found in '#{filename}'."
  22. s = $'
  23. end
  24. # untabify
  25. s = _untabify(s) unless options[:tabify] == false
  26. # load yaml document
  27. testdata_list = []
  28. YAML.load_documents(s) do |ydoc|
  29. if ydoc.is_a?(Hash)
  30. testdata_list << ydoc
  31. elsif ydoc.is_a?(Array)
  32. ydoc.each do |hash|
  33. raise "testdata should be a mapping." unless hash.is_a?(Hash)
  34. testdata_list << hash
  35. end
  36. else
  37. raise "testdata should be a mapping."
  38. end
  39. end
  40. # data check
  41. identkey = options[:identkey] || 'name'
  42. table = {}
  43. testdata_list.each do |hash|
  44. ident = hash[identkey]
  45. ident or raise "*** key '#{identkey}' is required but not found."
  46. table[ident] and raise "*** #{identkey} '#{ident}' is duplicated."
  47. table[ident] = hash
  48. yield(hash) if block
  49. end
  50. #
  51. return testdata_list
  52. end
  53. def self.define_testmethods(testdata_list, options={}, &block)
  54. identkey = options[:identkey] || 'name'
  55. testmethod = options[:testmethod] || '_test'
  56. testdata_list.each do |hash|
  57. yield(hash) if block
  58. ident = hash[identkey]
  59. s = "def test_#{ident}\n"
  60. hash.each do |key, val|
  61. s << " @#{key} = #{val.inspect}\n"
  62. end
  63. s << " #{testmethod}\n"
  64. s << "end\n"
  65. $stderr.puts "*** load_yaml_testdata(): eval_str=<<'END'\n#{s}END" if $DEBUG
  66. self.module_eval s
  67. end
  68. end
  69. def self.post_definition
  70. if ENV['TEST']
  71. target = "test_#{ENV['TEST']}"
  72. self.instance_methods.each do |method_name|
  73. m = method_name.to_s
  74. private m if m =~ /\Atest_/ && m != target
  75. end
  76. end
  77. end
  78. def self._untabify(str, width=8)
  79. return str if str.nil?
  80. list = str.split(/\t/, -1) # if 2nd arg is negative then split() doesn't remove tailing empty strings
  81. last = list.pop
  82. sb = ''
  83. list.each do |s|
  84. column = (n = s.rindex(?\n)) ? s.length - n - 1 : s.length
  85. n = width - (column % width)
  86. sb << s << (' ' * n)
  87. end
  88. sb << last if last
  89. return sb
  90. end
  91. end