require 'runit/testcase' require 'amrita/template.rb' class TestTemplate < RUNIT::TestCase include Amrita include HtmlCompiler def test_template_text_direct1 html = '
sample_text
' t = TemplateText.new(html) result = "" t.expand(result, { :a=>"aaa" }) assert_equal('aaa
', result) end def test_template_text_direct2 html = 'sample_text
' t = TemplateText.new(html) t.use_compiler = true result = "" #t.set_hint(HashData[:a=>ScalarData]) t.expand(result, { :a=>"aaa" }) #puts t.src assert_equal('aaa
', result) end def test_template_text_compile2 html = 'sample_text
' t = TemplateText.new(html) t.use_compiler = true result = "" data = { :a=>"aaa" } t.set_hint_by_sample_data(data) t.expand(result, data) #puts t.src assert_equal('aaa
', result) end def test_template_text_autocompile2 html = 'sample_text
END t = TemplateFile.new(tempfile) assert_equal(true, t.need_update?) result = "" t.expand(result, { :a=>"aaa" }) sleep 1.1 # for test of need_update? assert_equal("aaa
\n", result) result = "" t.expand(result, { :a=>"111" }) assert_equal(false, t.need_update?) assert_equal("111
\n", result) File.open(tempfile, "w") { |f| f.print <<-END }sample_text
xxx END result = "" assert_equal(true, t.need_update?) t.expand(result, { :a=>"aaa" }) assert_equal("aaa
xxx\n", result) assert_equal(false, t.need_update?) ensure #File.open(tempfile) { |f| puts f.read } File::unlink(tempfile) end def test_template_file2 tempfile = "/tmp/amritatest#{$$}" File.open(tempfile, "w") { |f| f.print <<-END }sample_text
END t = TemplateFile.new(tempfile) t.xml = true result = "" t.expand(result, { :a=>"aaa" }) assert_equal("aaa
", result) ensure File::unlink(tempfile) end def test_keep_id html = 'sample_text
' t = TemplateText.new(html) t.keep_id = true result = "" t.expand(result, { :a=>"aaa" }) assert_equal('aaa
', result) result = "" t.set_hint(HashData[:a=>ScalarData]) t.expand(result, { :a=>"aaa" }) #puts t.src assert_equal('aaa
', result) end def test_escaped_id html = 'sample_text
' t = TemplateText.new(html) t.escaped_id = :__id result = "" t.expand(result, { :a=>"aaa" }) assert_equal('aaa
', result) result = "" t.set_hint(HashData[:a=>ScalarData]) t.expand(result, { :a=>"aaa" }) #puts t.src assert_equal('aaa
', result) end def test_amrita_id html = 'sample_text
' t = TemplateText.new(html) t.amrita_id = :amrita_id result = "" t.expand(result, { :a=>"aaa" }) assert_equal('aaa
', result) result = "" t.set_hint(HashData[:a=>ScalarData]) t.expand(result, { :a=>"aaa" }) #puts t.src assert_equal('aaa
', result) end def test_amrita_id2 html = 'sample_text
yyy' t = TemplateText.new(html) t.amrita_id = :amrita_id result = "" t.expand(result, { :a=>"aaa" }) assert_equal('aaa
yyy', result) result = "" t.set_hint(HashData[:a=>ScalarData]) t.expand(result, { :a=>"aaa" }) #puts t.src assert_equal('aaa
yyy', result) end def test_sanitize html = 'sample_text
' t = TemplateText.new(html) result = "" t.expand(result, { :a=>"<xxx>&<yyy>
', result) result = "" t.expand(result, { :a=>noescape { "sample_text
' t = TemplateText.new(html) result = "" t.expand(result, { :a=>"<xxx>&<yyy>
', result) t = TemplateText.new(html) result = "" t.prettyprint = true t.expand(result, { :a=>"<xxx>&<yyy>
\n", result) t = TemplateText.new(html) result = "" t.pre_format = true t.expand(result, { :a=>"<xxx>&<yyy>
', result) end def test_template_cache1 require "ftools" tempfile = "/tmp/amritatest#{$$}" cachedir = "/tmp/amritacache#{$$}" File::makedirs(cachedir) TemplateFileWithCache::set_cache_dir(cachedir) File.open(tempfile, "w") { |f| f.print <<-END }sample_text
END t = TemplateFileWithCache.new(tempfile) assert_equal(SourceCache, t.cache_manager.type) t.use_compiler = true assert_equal(true, t.need_update?) result = "" t.expand(result, { :a=>"aaa" }) sleep 1.1 # for test of need_update? assert_equal("aaa
\n", result) t = TemplateFileWithCache.new(tempfile) t.use_compiler = true result = "" t.expand(result, { :a=>"aaa" }) sleep 1.1 # for test of need_update? assert_equal("aaa
\n", result) File.open(tempfile, "w") { |f| f.print <<-END }sample_text
xxx END result = "" assert_equal(true, t.need_update?) t.expand(result, { :a=>"aaa" }) assert_equal("aaa
xxx\n", result) assert_equal(false, t.need_update?) ensure #File.open(tempfile) { |f| puts f.read } File::unlink(tempfile) system "rm -r #{cachedir}" end def test_prettyprint html = 'sample_text
' t = TemplateText.new(html) t.prettyprint = true result = "" t.expand(result, { :a=>"aaa" }) assert_equal("\n\naaa
\n\n", result) def t.setup_taginfo ret = HtmlTagInfo.new ret[:body].pptype = 3 ret[:p].pptype = 3 ret end result = "" t.expand(result, { :a=>"aaa" }) assert_equal("\naaa
\n", result) end def test_sanitizedstring html = 'sample_text
' t = TemplateText.new(html) result = "" t.expand(result, { :a=>"<xxx>
', result) result = "" t.expand(result, { :a=>SanitizedString["sample_text
' t = TemplateText.new(html) t.expand_attr = true result = "" t.expand(result, { :aaa=>"sample_text
', result) result = "" t.expand(result, { :aaa=>SanitizedString['""">sample_text
', result) end def test_url_sanitizing html = 'sample_text' xss = 'xxx">yyy xss}) assert_equal('sample_text', result) result = "" t.expand(result, { :a=> SanitizedString[xss]}) assert_equal('yyysample_text', result) end def test_url_sanitizing2 html = 'body text
bbb