#$Id: wiki-test.rb 207 2007-06-04 15:45:31Z bitherder $ 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 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| if target.kind_of? Array target = target.find {|t| doc[t.to_s]} end next unless doc[target.to_s] define_method "test_#{target.to_s}_" + title do test_parser doc, options.dup, target.to_sym, target.to_s end # define_method end # targets.each end # YAML::load_documents end # testfiles.each end # Class.new end # intialize end # class WikiTestCases