test/buildmaster/tc_xtemplate.rb in BuildMaster-0.7.0 vs test/buildmaster/tc_xtemplate.rb in BuildMaster-0.8.0

- old
+ new

@@ -1,21 +1,22 @@ $:.unshift File.join(File.dirname(__FILE__), "..", "..", "lib") require 'rexml/xpath' require 'test/unit' require 'buildmaster' +require 'buildmaster/source_content' module BuildMaster class XTemplateTest < Test::Unit::TestCase protected def setup super end public def test_should_initialize_with_io - template = BuildMaster::XTemplate.new(File.open(File.join(File.dirname(__FILE__), "template.xhtml"))) + template = BuildMaster::XTemplate.new(File.open(File.join(File.dirname(__FILE__), "template.xhtml")), Hash.new) end def test_should_initialize_with_content template_content = <<CONTENT <html xmlns="http://www.w3.org/1999/xhtml" @@ -24,45 +25,20 @@ <title>BuildMaster</title> </head> <body>Body</body> </html> CONTENT - template = XTemplate.new(template_content) - xml_output = template.process('') + template = XTemplate.new(template_content, Hash.new) + xml_output = template.process(SourceContent.new(Pathname.new('content'), '')) assert_equal('Body', REXML::XPath.first(xml_output, '/html/body').text) end - def test_should_process_include_with_elements_attribute + def test_should_hook_up_templatelets template_content = <<INCLUDE_CONTENT <html xmlns="http://www.w3.org/1999/xhtml" xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> <head> - <title>BuildMaster - <template:include elements="/html/head/title/text()"/></title> - </head> - <body> - </body> -</html> -INCLUDE_CONTENT - source_content = <<INCLUDE_SOURCE -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>Page Title</title></head> - <body></body> -</html> -INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_output = REXML::Document.new(template.process(source_content).to_s) - assert_equal('BuildMaster - Page Title', REXML::XPath.first(xml_output, '/html/head/title').text) - end - - def test_should_process_when_with_select_value_attribute - template_content = <<INCLUDE_CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> <title>BuildMaster - <template:when test="index_file?">Index</template:when></title> </head> <body> <template:when test="what?"> <ul> @@ -79,221 +55,25 @@ <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Page Title</title></head> <body></body> </html> INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_document = template.process(source_content) {|expression| return true} + template = XTemplate.new(template_content, load_templatelets) + xml_document = template.process(SourceContent.new(nil, source_content)) xml_output = REXML::Document.new(xml_document.to_s()) assert_equal('BuildMaster - Index', REXML::XPath.first(xml_output, '/html/head/title').text) assert_equal('one', REXML::XPath.first(xml_output, '/html/body/ul/li').text) end - def test_should_not_process_when_if_select_false - template_content = <<INCLUDE_CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster<template:when test="index_file?"> - Index</template:when></title> - </head> - <body> - </body> -</html> -INCLUDE_CONTENT - source_content = <<INCLUDE_SOURCE -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>Page Title</title></head> - <body></body> -</html> -INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_document = template.process(source_content) {|expression| return false} - xml_output = REXML::Document.new(xml_document.to_s()) - assert_equal('BuildMaster', REXML::XPath.first(xml_output, '/html/head/title').text) + def what?(path) + return true end - def test_should_process_attr_with_eval_attribute - template_content = <<INCLUDE_CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster</title> - </head> - <body> - <a><template:attribute name="href" eval="document_link"/></a> - </body> -</html> -INCLUDE_CONTENT - source_content = <<INCLUDE_SOURCE -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>Page Title</title></head> - <body></body> -</html> -INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_document = template.process(source_content) {|expression| return 'http://svn.buildmaster/'} - xml_output = REXML::Document.new(xml_document.to_s()) - assert_equal('http://svn.buildmaster', REXML::XPath.first(xml_output, '/html/body/a/@href').text) + def index_file?(path) + return true end - def test_should_process_text_with_eval_attribute - template_content = <<INCLUDE_CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster</title> - </head> - <body> - <h1><template:text eval="release_info"/></h1> - </body> -</html> -INCLUDE_CONTENT - source_content = <<INCLUDE_SOURCE -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>Page Title</title></head> - <body></body> -</html> -INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_document = template.process(source_content) {|expression| return 'release 0.0.1 (build 235)'} - xml_output = REXML::Document.new(xml_document.to_s()) - assert_equal('release 0.0.1 (build 235)', REXML::XPath.first(xml_output, '/html/body/h1').text) + def load_templatelets + return {'when' => When.new(self, self)} end - - def test_should_process_each_with_children - template_content = <<INCLUDE_CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster</title> - </head> - <body> - <div class="NewsGroup"> - <h1>Recent News</h1> - <template:each source="rss" select="/rss/channel/item" count="3"> - <div class="NewsItem"> - <p class="NewsTitle"><template:include elements="./title/text()"/></p> - <p class="NewsDate"><template:include elements="./pubDate/text()"/></p> - <p class="NewsText"><template:include elements="./xhtml:div/node()"/></p> - </div> - </template:each> - </div> - </body> -</html> -INCLUDE_CONTENT - source_content = <<INCLUDE_SOURCE -<!DOCTYPE html - PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>Page Title</title></head> - <body></body> -</html> -INCLUDE_SOURCE - template = XTemplate.new(template_content) - xml_document = template.process(source_content) do |expression| <<NEWS -<?xml version="1.0" encoding="UTF-8"?> -<rss version="2.0"> -<channel> - <title>BuildMaster News</title> - <link>http://buildmaster.rubyforge.org/</link> - <description>BuildMaster is a set of classes in Ruby to help easy the task of project building, releasing - and website building</description> - <language>en-us</language> - <lastBuildDate>Tue, 09 May 2006 17:12:46 GMT</lastBuildDate> - <image> - <url>http://buildmaster.rubyforge.org/logo.gif</url> - <title>BuildMaster</title> - <link>http://buildmaster.rubyforge.org/</link> - </image> - <item> - <title>BuildMaster website created</title> - <link>http://buildmaster.rubyforge.org/</link> - <description>BuildMaster website has been created by its own script</description> - <category>Documentation</category> - <pubDate>Sat, 27 May 2006 16:00:00 GMT</pubDate> - </item> - <item> - <title>XTemplate script created</title> - <link>http://buildmaster.rubyforge.org</link> - <description>XTemplate script has been created, following the ones that is currently being used for jBehave and jMock</description> - <category>Development</category> - <pubDate>Wed, 05 Apr 2006 16:00:00 GMT</pubDate> - </item> - <item> - <title>BuildMaster code structure created</title> - <link>http://buildmaster.rubyforge.org</link> - <description>BuildMaster project structure has been created with according files so that a gem can be created - and released onto rubyforge</description> - <category>Development</category> - <pubDate>Sat, 08 Apr 2006 16:00:00 GMT</pubDate> - </item> - <item> - <title>BuildMaster moved from RubyBuilder</title> - <description>RubyBuilder project has been deleted and renamed to BuildMaster to easy any confusion</description> - <category>Development</category> - <pubDate>Sat, 01 Apr 2006 16:00:00 GMT</pubDate> - </item> -</channel> -</rss> -NEWS - end - xml_output = REXML::Document.new(xml_document.to_s()) - assert_equal(3, REXML::XPath.match(xml_output, '/html/body/div/div').size) - assert_equal('BuildMaster website created', - REXML::XPath.first(xml_output, '/html/body/div/div[1]/p').text()) - end - - def test_should_process_link_element_to_anchor_with_relative_path_when_not_current_page - template_content = <<CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster</title> - </head> - <body><template:link href="/dir/file" class="value">File</template:link></body> -</html> -CONTENT - template = XTemplate.new(template_content) - xml_output = template.process('') do |expression| - assert_equal('relative_to_root', expression) - Pathname.new('hello.textile') - end - actual_element = REXML::XPath.first(xml_output, '/html/body/a') - assert_equal('dir/file', actual_element.attributes['href']) - assert_equal('value', actual_element.attributes['class']) - assert_equal('File', actual_element.text) - end - - def test_should_process_link_element_to_div_when_on_current_page - template_content = <<CONTENT -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:template="http://buildmaster.rubyforge.org/xtemplate/1.0"> - <head> - <title>BuildMaster</title> - </head> - <body><template:link href="/dir/file.html" class="value">File</template:link></body> -</html> -CONTENT - template = XTemplate.new(template_content) - xml_output = template.process('') do |expression| - assert_equal('relative_to_root', expression) - Pathname.new('dir/file.textile') - end - actual_element = REXML::XPath.first(xml_output, '/html/body/div') - assert_equal(nil, actual_element.attributes['href']) - assert_equal('value', actual_element.attributes['class']) - assert_equal('File', actual_element.text) - end - - end end