## ## usage: ruby -s test-eruby.rb -target=eruby1 ## # (setq ed::*ruby-indent-column* 2) currdir = File.dirname(File.expand_path(__FILE__)) testdir = File.dirname(currdir) + "/test" $: << testdir raise "*** target is required." unless $target require 'test/unit' #require 'test/unit/ui/console/testrunner' require 'assert-diff' require 'yaml' require $target class ErubyTest < Test::Unit::TestCase #str = DATA.read() str = File.read(__FILE__) str.gsub!(/.*^__END__$/m, '') @@ydocs = {} YAML.load_documents(str) do |ydoc| name = ydoc['name'] raise "*** test name '#{name}' is duplicated." if @@ydocs[name] ydoc.each do |key, val| if key[-1] == ?* key = key.sub(/\*\z/, '') val = val[$target] ydoc[key] = val end end target = ydoc['target'] next unless target && target.include?($target) @@ydocs[name] = ydoc s = <<-END def test_#{name} @name = #{name.dump} _test() end END eval s end def _test() ydoc = @@ydocs[@name] input = ydoc['input'] src = ydoc['src'].gsub(/@/, ' ') output = ydoc['output'].gsub(/@/, ' ') options = ydoc['options'] # require ydoc['require'] if ydoc['require'] klass = eval(ydoc['class'] || 'Eruby') if options eruby = klass.new(input, options) else eruby = klass.new(input) end assert_equal_with_diff(src, eruby.src) list = ['', 'b&b', '"ccc"'] assert_equal_with_diff(output, eruby.result(binding())) end end __END__ --- name: basic1 target: [ eruby1, eruby2 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: basic2 target: [ eruby1, eruby2 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: basic3 target: [ eruby1, eruby2, eruby3 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: fast1 target: [ eruby2 ] require: fast-eruby class: FastEruby input: | <% for item in list %> <% end %>
<%= item %>
src: | _out = ''; _out << "\n "; for item in list ; _out << "\n \n \n \n "; end ; _out << "\n
"; _out << ( item ).to_s; _out << "
\n"; _out output: | @ @ @ @
b&b
"ccc"
## --- name: trim1 target: [ eruby3 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: trim2 target: [ eruby3 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: escape1 target: [ eruby4 ] input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: escape2 target: [ eruby4 ] require: xml-eruby class: XmlEruby input: | src: | _out = ''; _out << "\n" _out output: | ## --- name: pattern1 target: [ eruby5 ] options: { :pattern : '<(?:!--)?% %(?:--)?>' } input: | src: | _out = ''; _out << "\n" _out output: | ##