require 'test/unit'
require 'htree/template'
require 'stringio'
class TestTemplate < Test::Unit::TestCase
Decl = ''
def assert_xhtml(expected, template, message=nil)
prefix = '' +
""
suffix = ""
result = HTree.expand_template(''){"#{template}"}
assert_match(/\A#{Regexp.quote prefix}/, result)
assert_match(/#{Regexp.quote suffix}\z/, result)
result = result[prefix.length..(-suffix.length-1)]
assert_equal(expected, result, message)
end
def test_text
assert_xhtml("1", 'd')
assert_xhtml('1', 'd')
assert_xhtml("1", 'd')
assert_xhtml("abc", %q{ac})
end
def test_tree
assert_xhtml("x", 'd')
assert_xhtml("x", 'd')
end
def test_attr
assert_xhtml("d", 'd')
assert_xhtml("d", 'd')
assert_xhtml("d", 'd')
end
def test_if
assert_xhtml("d", 'd')
assert_xhtml('', 'd')
assert_xhtml("dd", 'ddd')
assert_xhtml('d', 'd')
end
def test_iter
assert_xhtml("123",
'')
assert_xhtml("123",
'')
end
def test_iter_content
assert_xhtml("123",
'')
assert_xhtml("123",
'')
end
def test_iter_local_template
assert_xhtml("123",
'')
end
def test_call
assert_xhtml("1",
'')
end
def test_template
assert_xhtml('d',
'd')
end
def test_file
assert_equal(<<'End'.chop,
aaa
End
HTree.expand_template("#{File.dirname __FILE__}/template.html", "aaa", ''))
end
def test_whitespace
assert_xhtml("", ' ')
assert_xhtml(" ", ' ')
assert_xhtml("
", '
')
assert_xhtml(" ", %q{ })
assert_xhtml(" ", %q{})
end
def test_ignorable
assert_xhtml("a
", 'a
')
assert_xhtml("a", 'a')
end
def test_template_in_attr
assert_xhtml("", '')
end
def test_empty_block_argument
assert_xhtml("vv", 'v')
end
def test_empty_element
assert_xhtml("", '') # 2004-06-10: reported by Takuo KITAME
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
end
def test_empty_element_start_end_tag
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
assert_xhtml("", '')
end
def test_toplevel_local_variable
eval("htree_test_toplevel_local_variable = :non_modified_value", TOPLEVEL_BINDING)
HTree.expand_template("#{File.dirname __FILE__}/assign.html", "aaa", '')
assert_equal(:non_modified_value, eval("htree_test_toplevel_local_variable", TOPLEVEL_BINDING))
eval("htree_test_toplevel_local_variable = 1", TOPLEVEL_BINDING)
end
def test_extend_compiled_template
m = HTree.compile_template('self is
')
o = "zzz"
o.extend m
assert_equal('self is "zzz"',
HTree.expand_template(''){''})
end
def test_attr_nbsp
@t = HTree::Text.parse_pcdata(' ')
assert_xhtml("d", 'd')
end
def test_text_nbsp
@t = HTree::Text.parse_pcdata(' ')
assert_xhtml(" ", 'd')
end
def test_content_text
assert_xhtml("ab", '"a"+"b"')
assert_xhtml("2", '1+1')
end
end
class MemFile
def initialize(str)
@str = str
end
def read
@str
end
end
class TestTemplateScopeObj
Const = 'good_const'
@@cvar = 'good_cvar'
def initialize
@ivar = 'good_ivar'
end
end
class TestTemplateScope < Test::Unit::TestCase
Const = 'bad_const'
@@cvar = 'bad_cvar'
def setup
@ivar = 'bad_ivar'
eval("test_local_variable = 'bad_lvar'", TOPLEVEL_BINDING)
end
XMLDeclStr = ''
def test_expand_template
obj = TestTemplateScopeObj.new
assert_equal("#{XMLDeclStr}[TestTemplateScopeObj]",
HTree.expand_template(MemFile.new(''), obj, ''))
assert_equal("#{XMLDeclStr}good_ivar",
HTree.expand_template(MemFile.new(''), obj, ''))
assert_equal("#{XMLDeclStr}good_cvar",
HTree.expand_template(MemFile.new(''), obj, ''))
assert_equal("#{XMLDeclStr}good_const",
HTree.expand_template(MemFile.new(''), obj, ''))
test_local_variable = 'bad_lvar'
assert_equal("#{XMLDeclStr}good_lvar",
HTree.expand_template(MemFile.new(''), obj, ''))
end
def test_compile_template
obj = TestTemplateScopeObj.new
mod = HTree.compile_template(MemFile.new(<<-'End'))
End
mod.module_eval <<-'End'
Const = 'mod_const'
@@cvar = 'mod_cvar'
@ivar = 'mod_ivar'
End
assert_equal("[#{mod.inspect}]", mod.test_nesting.extract_text.to_s)
assert_equal("mod_const", mod.test_const.extract_text.to_s)
assert_equal("mod_cvar", mod.test_cvar.extract_text.to_s)
assert_equal("mod_ivar", mod.test_ivar.extract_text.to_s)
obj = Object.new
obj.instance_variable_set :@ivar, 'obj_ivar'
obj.extend mod
assert_equal("[#{mod.inspect}]", obj.__send__(:test_nesting).extract_text.to_s)
assert_equal("mod_const", obj.__send__(:test_const).extract_text.to_s)
assert_equal("mod_cvar", obj.__send__(:test_cvar).extract_text.to_s)
assert_equal("obj_ivar", obj.__send__(:test_ivar).extract_text.to_s)
end
end
class TestCDATA < Test::Unit::TestCase
def test_html_script
v = "x