#$Id: wiki-test.rb 193 2007-04-18 15:27:15Z alpinegizmo $ require 'test/unit' require 'yaml' require 'benchmark' require 'core_ext/file' class WikiTestCases def self.get_input(doc, type) doc['in'].gsub(/\{\{\{(.*?)\}\}\}/) {|sub| subs[$1]} end def initialize(parserKlass, testfiles, targets=[:html]) # if either the testfile or target argument is a single value, turn # it into a single element array testfiles = testfiles.to_a targests = targets.to_a Class.new Test::Unit::TestCase do define_method 'parserClass' do parserKlass end def test_parser(doc, options, type, type_name) input = WikiTestCases.get_input(doc, type) expected \ = doc[type_name].gsub(/\{\{\{(.*?)\}\}\}/) {|sub| subs[$1]} # time limit in seconds time_limit = options['time_limit'] ? options['time_limit'] : 1.0 parser = parserClass.new(input, options) out = nil wtimes = Benchmark.measure do out = parser.to_type(type) end time_elapsed = wtimes.utime + wtimes.stime if $DEBUG || options['debug'] || options[:debug] print "time_limit: #{'%0.3f' % time_limit}\n"+ "time_elapsed: #{'%0.3f' % time_elapsed}" end assert_equal(expected, out) assert time_elapsed <= time_limit, "elapsed time expected <= #{'%0.3f' % time_limit}"+ " but was #{'%0.3f' % time_elapsed}\n" end sequence = 0 globalOptions = (defined? WikiOptions) ? WikiOptions.dup : {} globalSubs = {} testfiles.each do |testfile| file = File.find_file_in_path(testfile) YAML::load_documents( File.open( file ) ) do |doc| next unless doc globalOptions = doc['options.global'] if doc['options.global'] options = globalOptions options = options.merge(doc['options']) if doc['options'] globalSubs = doc['subs.global'] if doc['subs.global'] subs = globalSubs subs = subs.merge(options) subs = subs.merge(doc['subs']) if doc['subs'] next unless doc['in'] title = doc['title'] ? doc['title'] : (sequence += 1).to_s targets.each do |target| target = target.to_sym if target.is_a? String target_name = target.id2name if target.is_a? Symbol next unless doc[target_name] define_method "test_#{target_name}_" + title do test_parser doc, options.dup, target, target_name end # define_method end # targests.each end # YAML::load_documents end # testfiles.each end # Class.new end # intialize end # class WikiTestCases