$:.unshift File.join(File.dirname(__FILE__), "..", "..", "lib") require 'rexml/xpath' require 'rexml/document' require 'test/unit' require 'pathname' require 'buildmaster/template_runner' require 'buildmaster/source_content' require 'buildmaster/templatelets' module BuildMaster class TemplateRunnerTest < Test::Unit::TestCase def test_process_element_in_document target_content = <

END template_content = <

END source_content = < Name One Name Two END target_xml = REXML::Document.new(target_content) target_element = REXML::XPath.first(target_xml, '/html/h1') template_element = REXML::XPath.first(REXML::Document.new(template_content), "/html/p") source_xml = REXML::Document.new(source_content) source_element = REXML::XPath.first(source_xml, '/persons/person[@id="1"]') runner = TemplateRunner.new(target_element, template_element, SourceContent.new(Pathname.new('content'), source_element)) runner.templatelets['include'] = Include.new(self) runner.process assert_equal('Name One', REXML::XPath.first(REXML::Document.new(target_xml.to_s), '/html/h1/text()').value.strip) end def test_should_use_templatelet_based_on_name output = REXML::Document.new template_content = <

END template_document = REXML::Document.new(template_content) runner = TemplateRunner.new(output, template_document, SourceContent.new(Pathname.new('content/index.textile'), REXML::Document.new)) runner.templatelets['name'] = NameTemplatelet.new(self) runner.process assert_equal('p', @element_target.name) assert_equal('name', @element_processed.name) assert_equal('value', @element_processed.attributes['attribute']) assert_equal('value2', @element_processed.attributes['attribute2']) end def processed(target, element) @element_target = target @element_processed = element end end class NameTemplatelet def initialize(logger) @logger = logger end def process(target, element, current_path) @logger.processed(target, element) end end end