require 'test/unit'
require 'template'
class TC_template < Test::Unit::TestCase
def test_expand_line
t = Template.new('%stuff%')
out = ''
t.write_html_on(out, {'stuff' => 'stuff'})
assert_equal("stuff", out.strip)
end
def test_class_method_call
t = Template.new("%Array#length%")
out = ''
t.write_html_on(out, [1, 2, 3])
assert_equal("3", out.strip!)
end
def test_var_method_call
t = Template.new("%my_array.length%")
out = ''
t.write_html_on(out, {'my_array' => [1, 2]})
assert_equal("2", out.strip)
end
def test_if
t = Template.new(%{
%stuff%
IF:some_stuff
%some_stuff%
ENDIF:some_stuff
})
out = ''
t.write_html_on(out, {'some_stuff' => 'some_stuff', 'stuff' => 'stuff'})
assert_equal("stuff\nsome_stuff", out.strip)
end
def test_if_empty
t = Template.new(%{
%stuff%
IF:some_stuff
%some_stuff%
ENDIF:some_stuff
})
out = ''
t.write_html_on(out, {'stuff' => 'stuff'})
assert_equal("stuff", out.strip)
end
def test_foreach
t = Template.new(%{
FOREACH:array_of_stuff
%some_stuff%
ENDEACH:array_of_stuff
})
out = ''
t.write_html_on(out, {
'array_of_stuff' => [
{'some_stuff' => 'some_stuff'},
{'some_stuff' => 'more_stuff'}
]})
assert_equal("some_stuff\nmore_stuff", out.strip)
end
def test_with
t = Template.new(%{
%stuff%
WITH:inner_stuff
%some_inner_stuff%
ENDWITH:inner_stuff
})
out = ''
t.write_html_on(out, {
'stuff' => 'stuff',
'inner_stuff' => {'some_inner_stuff' => 'inner_stuff'},
})
assert_equal("stuff\ninner_stuff", out.strip)
end
def test_foreach_self
t = Template.new(%{
FOREACH:self
%some_stuff%
ENDEACH:self
})
out = ''
t.write_html_on(out, [
{'some_stuff' => 'some_stuff'},
{'some_stuff' => 'more_stuff'}
])
assert_equal("some_stuff\nmore_stuff", out.strip)
end
def test_labelled_include_plain_template_page
t_header = Template.new(%{This is the header})
t_footer = Template.new(%{This is the footer})
t = Template.new(%{
There should be a header
!INCLUDE:header!
There should be a footer
!INCLUDE:footer!
}, {"header" => t_header, "footer" => t_footer})
out = ''
t.write_html_on(out)
assert_equal(%{There should be a header
This is the header
There should be a footer
This is the footer}, out.strip)
end
def test_labelled_include_with_substitution_stuffs
t_header = Template.new(%{This is the %header_label%})
t_footer = Template.new(%{This is the %footer_label%})
t = Template.new(%{H:!INCLUDE:header!F:!INCLUDE:footer!},
{"header" => t_header, "footer" => t_footer})
out = ''
t.write_html_on(out, {"header_label" => "header",
"footer_label" => "footer"})
assert_equal("H:This is the headerF:This is the footer", out.strip)
end
def test_unlabelled_include_plain_template_page
t_header = Template.new(%{This is the %header_label%})
t_footer = Template.new(%{This is the %footer_label%})
t = Template.new(%{H:!INCLUDE!F:!INCLUDE!},
t_header, t_footer)
out = ''
t.write_html_on(out, {"header_label" => "header",
"footer_label" => "footer"})
assert_equal("H:This is the headerF:This is the footer", out.strip)
end
def test_unlabelled_include_string
t_header = "This is the %header_label%"
t_footer = "This is the %footer_label%"
t = Template.new(%{H:!INCLUDE!F:!INCLUDE!},
t_header, t_footer)
out = ''
t.write_html_on(out, {"header_label" => "header",
"footer_label" => "footer"})
assert_equal("H:This is the headerF:This is the footer", out.strip)
end
def test_ddlb_value
t = Template.new(%{
%ddlb:name:options%
})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => %w! a b c !
})
assert_equal(%!!, out.strip)
end
def test_ddlb_selected
t = Template.new(%{%sddlb:name:options:selected%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => %w! a b c !,
'selected' => 'a',
})
assert_equal(%!!, out.strip)
end
def test_ddlb_multi_selected
t = Template.new(%{%sddlb:name:options:selected%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => %w! a b c !,
'selected' => %w! a b !,
})
assert_equal(%!!, out.strip)
end
def test_ddlb_value_text
t = Template.new(%{%ddlb:name:options%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => { '1' => 'one', '2' => 'two' },
})
assert_equal(%!!, out.strip)
end
def test_ddlb_value_keys
foo = Struct.new('Foo', :first, :second, :third)
t = Template.new(%{%ddlb:name:options:Struct::Foo#first%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => [
foo.new('a', 'b', 'c'),
foo.new('1', '2', '3'),
foo.new('point', 'line', 'plane'),
]
})
assert_equal(%!!, out.strip)
end
def test_ddlb_value_text_keys
foo = Struct.new('Foo', :first, :second, :third)
t = Template.new(%{%ddlb:name:options:Struct::Foo#first:Struct::Foo#second%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'options' => [
foo.new('a', 'b', 'c'),
foo.new('1', '2', '3'),
foo.new('point', 'line', 'plane'),
]
})
assert_equal(%!!, out.strip)
end
def test_ddlb_value_text_keys_selected
foo = Struct.new('Foo', :first, :second, :third)
t = Template.new(%{%sddlb:name:options:selected:Struct::Foo#first:Struct::Foo#second%})
out = ''
t.write_html_on(out, {
'name' => 'name',
'selected' => '1',
'options' => [
foo.new('a', 'b', 'c'),
foo.new('1', '2', '3'),
foo.new('point', 'line', 'plane'),
]
})
assert_equal(%!!, out.strip)
end
end