#!/usr/bin/env ruby require 'maruku' #require 'maruku/textile2' require 'maruku/input_textile2/t2_parser' $marutest_language = :markdown #MARKER = "\n***EOF***\n" SPLIT = %r{\n\*\*\*[^\*]+\*\*\*\n}m def marker(x) "\n*** Output of #{x} ***\n" end def write_lines(i, j, lines, prefix, i_star) i = [i, 0].max j = [j, lines.size-1].min for a in i..j l = lines[a].gsub(/\t/,' ') puts( ("%s %3d" % [prefix, a]) + (a==i_star ? " -->" : " ")+lines[a]) end end # a = expected b = found def equals(a, b) a = a.split("\n") b = b.split("\n") for i in 0..([a.size-1,b.size-1].max) la = a[i] lb = b[i] if la != lb puts "\n" write_lines(i-3, i+3, a, "expected", i ) write_lines(i-3, i+3, b, " found", i ) return false end end return true end TOTEST = [:inspect,:to_html,:to_latex,:to_md,:to_s] def run_test(filename, its_ok, verbose=true) # read file content input = (f=File.open(filename,'r')).read; f.close output_html = File.join(File.dirname(filename), File.basename(filename, File.extname(filename)) + ".html") # split the input in sections stuff = input.split(SPLIT) if stuff.size == 1 stuff[2] = stuff[0] stuff[0] = "Write a comment here" stuff[1] = "{} # params " end comment = stuff.shift params_s = stuff.shift params = eval(params_s||'{}') if params == nil raise "Null params? #{params_s.inspect}" end markdown = stuff.shift # puts "comment: #{markdown.inspect}" # puts "markdown: #{markdown.inspect}" failed = [] relaxed = [] crashed = [] actual = {} doc = if $marutest_language == :markdown Maruku.new(markdown, params) else MaRuKu.textile2(markdown, params) end for s in TOTEST begin if s==:to_html actual[s] = doc.to_html else actual[s] = doc.send s raise "Methods #{s} gave nil" if not actual[s] end rescue Exception => e crashed << s actual[s] = e.inspect+ "\n"+ e.backtrace.join("\n") puts actual[s] end end File.open(output_html, 'w') do |f| f.write doc.to_html_document end begin m = Maruku.new d = m.instance_eval(actual[:inspect]) rescue Exception => e s = e.inspect + e.backtrace.join("\n") raise "Inspect is not correct:\n ========\n#{actual[:inspect]}"+ "============\n #{s}" end expected = {} if (stuff.size < TOTEST.size) $stdout.write " (first time!) " TOTEST.each do |x| expected[x] = actual[x] end else TOTEST.each_index do |i| symbol = TOTEST[i] expected[symbol] = stuff[i] # puts "symbol: #{symbol.inspect} = #{stuff[i].inspect}" end end m = Maruku.new if not its_ok.include? :inspect begin d = m.instance_eval(expected[:inspect]) # puts "Eval: #{d.inspect}" expected[:inspect] = d.inspect rescue Exception => e s = e.inspect + e.backtrace.join("\n") raise "Cannot eval user-provided string:\n #{expected[:inspect].to_s}"+ "\n #{s}" end end # m.instance_eval(actual[:inspect]) != m.instance_eval(expected[:inspect]) # actual[:inspect] = m.instance_eval(actual[:inspect]) # expected[:inspect] = m.instance_eval(expected[:inspect]) TOTEST.each do |x| expected[x].strip! actual[x].strip! if not equals(expected[x], actual[x]) if its_ok.include? x expected[x] = actual[x] $stdout.write " relax:#{x} " relaxed << x else actual[x] = "-----| WARNING | -----\n" + actual[x].to_s failed << x end end end f = File.open(filename, 'w') f.write comment f.write "\n*** Parameters: ***\n" f.write params_s f.write "\n*** Markdown input: ***\n" f.write markdown TOTEST.each do |x| f.write marker(x) f.write expected[x] end f.write "\n*** EOF ***\n" if not failed.empty? or not crashed.empty? f.puts "\n\n\n\nFailed tests: #{failed.inspect} \n" TOTEST.each do |x| f.write marker(x) f.write actual[x] end else f.puts "\n\n\n\tOK!\n\n\n" end md_pl = markdown_pl(markdown) f.write "\n*** Output of Markdown.pl ***\n" f.write md_pl f.write "\n*** Output of Markdown.pl (parsed) ***\n" begin doc = REXML::Document.new("