$:.unshift File.dirname(__FILE__) require 'common_templatelet_test' $:.unshift File.join(File.dirname(__FILE__), '..', '..', '..', 'lib') require 'rexml/document' require 'rexml/xpath' require 'buildmaster/site_spec' require 'buildmaster/source_content' module BuildMaster class LinkTest < CommonTemplateletTest def test_should_generate_link_with_relative_path target = create_element('div') template_content = < text CONTENT template_document = REXML::Document.new(template_content) template = REXML::XPath.first(template_document, '/root/link') source = create_element('source') link = Link.new(SiteSpec.new) link.process(target, template, SourceContent.new(Pathname.new('doc/doc.html'), source)) actual = REXML::XPath.first(target, 'a') assert_equal('../content/path.html', actual.attributes['href']) assert_equal('text', actual.text) end def test_should_copy_all_attributes target = create_element('div') template = create_element('link') template.attributes['href'] = 'content/path.html' template.attributes['attribute1'] = 'value1' template.attributes['attribute2'] = 'value2' source = create_element('source') link = Link.new(SiteSpec.new) link.process(target, template, SourceContent.new(Pathname.new('doc/doc.html'), source)) actual = REXML::XPath.first(target, 'a') assert_equal('value1', actual.attributes['attribute1']) assert_equal('value2', actual.attributes['attribute2']) end def test_should_handle_absolute_path target = create_element('div') template = create_element('link') template.attributes['href'] = '/content/path.html' source = create_element('source') link = Link.new(SiteSpec.new) link.process(target, template, SourceContent.new(Pathname.new('doc/iste/doc.html'), source)) actual = REXML::XPath.first(target, 'a') assert_equal('../../content/path.html', actual.attributes['href']) end def test_should_generate_div_with_current_class_attribute_if_link_is_on_current_page target = create_element('div') template = create_element('link') template.attributes['href'] = '/content/path.html' source = create_element('source') link = Link.new(SiteSpec.new) link.process(target, template, SourceContent.new(Pathname.new('content/path.html'), source)) actual = REXML::XPath.first(target, 'div') assert_equal(nil, actual.attributes['href']) assert_equal('current', actual.attributes['class']) end end end