require 'runit/testcase' require 'amrita/parts' class TestParts < RUNIT::TestCase include Amrita class EMText attr_reader :text def initialize(text) @text = text end end def test_parts1 parts_template = TemplateText.new <<-END aaaaa END parts_template.install_parts_to(TestParts) t = TemplateText.new '

' result = "" t.use_compiler = false t.expand(result, { :aaa=>EMText.new("xxx") }) assert_equal("

xxx

", result) t = TemplateText.new '

' result = "" #t.debug_compiler = true t.use_compiler = true t.expand(result, { :aaa=>EMText.new("xxx") }) #puts t.src assert_equal("

xxx

", result) end class Lang attr_reader :lang, :author def initialize(lang, author) @lang, @author = lang, author end end def test_parts2 parts_template = TemplateText.new <<-END
END parts_template.install_parts_to(TestParts) data = { :list => [ Lang.new("Ruby", "matz"), Lang.new("Perl", "Larry Wall") ] } t = TemplateText.new '
' result = "" t.use_compiler = false t.expand(result, data) assert_equal('
Ruby
matz
Perl
Larry Wall
', result) t = TemplateText.new '
' result = "" t.use_compiler = true t.expand(result, data) assert_equal('
Ruby
matz
Perl
Larry Wall
', result) end class Lang2 attr_reader :lang, :author def initialize(lang, author) @lang, @author = lang, author end end def test_parts3 template = TemplateText.new <<-END
END data = { :list => [ Lang2.new("Ruby", "matz"), Lang2.new("Perl", "Larry Wall") ] } template.install_parts_to(TestParts) result = "" template.use_compiler = true template.expand(result, data) assert_equal('
Ruby
matz
Perl
Larry Wall
', result) result = "" template.use_compiler = false template.expand(result, data) assert_equal('
Ruby
matz
Perl
Larry Wall
', result) end class EMText2 attr_reader :text def initialize(text) @text = text end end def test_parts_withcache cachedir = "/tmp/amrit_partstest#{$$}" Dir::mkdir(cachedir) TemplateFileWithCache::set_cache_dir(cachedir) path = File::join(cachedir, "pt1.html") File::open(path, "w") do |f| f.write <<-END aaaaa END end parts_template = TemplateFileWithCache.new(path) parts_template.install_parts_to(TestParts) t = TemplateText.new '

' result = "" t.use_compiler = false t.expand(result, { :aaa=>EMText2.new("xxx") }) assert_equal("

xxx

", result) t = TemplateText.new '

' result = "" t.debug_compiler = true t.use_compiler = true t.expand(result, { :aaa=>EMText.new("xxx") }) assert_equal("

xxx

", result) ensure #system "ls -l #{cachedir}" #system "cat #{cachedir}/*" system "rm -r #{cachedir}" end module Elements end class PartsData include Amrita::ExpandByMember def header extend Elements.const_get('Header') self end def title 'test title' end end def test_dynamic_extend parts_template = TemplateText.new <

END parts_template.install_parts_to(Elements) document_template = TemplateText.new < END data = PartsData.new result = "" document_template.expand(result, data) assert_equal("\n

test title

\n\n", result) document_template = TemplateText.new < END document_template.use_compiler = true # XXX result = "" document_template.debug_compiler = true #document_template.set_hint_by_sample_data({ :test=> data }) document_template.expand(result, data) # puts document_template.src assert_equal("\n

test title

\n\n", result) end end #--- main program ---- if __FILE__ == $0 require 'runit/cui/testrunner' if ARGV.size == 0 RUNIT::CUI::TestRunner.run(TestParts.suite) else ARGV.each do |method| RUNIT::CUI::TestRunner.run(TestParts.new(method)) end end end