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 <