#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../lib/masterview' ) class TestTemplateSpec < Test::Unit::TestCase CurrentPath = File.dirname(__FILE__) MasterViewHTMLFile = File.join(CurrentPath, '../examples/product.html') MasterViewHTMLImportFile = File.join(CurrentPath, '../examples/test.import') def setup end def test_scan content_hash = {} template = <<-END
Hello
END spec = MasterView::TemplateSpec.scan_template(template, 'hello', content_hash) assert_equal MasterView::TemplateSpec::Status::OK, spec.status assert_equal '', spec.message assert_equal %w{ bar/baz.rhtml cat/_foo.rhtml layout/foo.rhtml }, spec.gen_parts end def test_badxhtml content_hash = {} template = <<-END
Hello
END spec = MasterView::TemplateSpec.scan_template(template, 'hello', content_hash) assert_equal MasterView::TemplateSpec::Status::InvalidXHTML, spec.status assert_not_equal '', spec.message assert_equal [], spec.gen_parts end def test_conflicts content_hash = {} template = <<-END
Hello
END spec1 = MasterView::TemplateSpec.scan_template(template, 'hello', content_hash) template2 = <<-END
Hello
END spec = MasterView::TemplateSpec.scan_template(template2, 'world', content_hash) assert_equal MasterView::TemplateSpec::Status::Conflicts, spec.status assert_not_equal '', spec.message assert_equal %w{ cat/_foo.rhtml layout/foo.rhtml }, spec.gen_parts end def test_imports_not_outdated content_hash = {} template1 = <<-END
Hello
END spec1 = MasterView::TemplateSpec.scan_template(template1, 'hello', content_hash) template2 = <<-END
Hello
END spec2 = MasterView::TemplateSpec.scan_template(template2, 'world', content_hash) invalid_parts = MasterView::TemplateSpec.template_out_of_sync?(template1, content_hash) spec1.update_status_from_invalid_parts(invalid_parts) assert_equal [], invalid_parts assert_equal MasterView::TemplateSpec::Status::OK, spec1.status assert_equal '', spec1.message assert_equal [], spec1.gen_parts invalid_parts = MasterView::TemplateSpec.template_out_of_sync?(template2, content_hash) spec2.update_status_from_invalid_parts(invalid_parts) assert_equal [], invalid_parts assert_equal MasterView::TemplateSpec::Status::OK, spec2.status assert_equal '', spec2.message assert_equal %w{ bar/baz.rhtml cat/_foo.rhtml layout/foo.rhtml }, spec2.gen_parts end def test_imports_outdated content_hash = {} template1 = <<-END This should make it outdated
Hello this will also make this outdated
END spec1 = MasterView::TemplateSpec.scan_template(template1, 'hello', content_hash) template2 = <<-END
Hello
END spec2 = MasterView::TemplateSpec.scan_template(template2, 'world', content_hash) invalid_parts = MasterView::TemplateSpec.template_out_of_sync?(template1, content_hash) spec1.update_status_from_invalid_parts(invalid_parts) assert_equal %w{ cat/_foo.rhtml layout/foo.rhtml }, invalid_parts assert_equal MasterView::TemplateSpec::Status::ImportsOutdated, spec1.status assert_equal 'Outdated parts: cat/_foo.rhtml, layout/foo.rhtml', spec1.message assert_equal [], spec1.gen_parts invalid_parts = MasterView::TemplateSpec.template_out_of_sync?(template2, content_hash) spec2.update_status_from_invalid_parts(invalid_parts) assert_equal [], invalid_parts assert_equal MasterView::TemplateSpec::Status::OK, spec2.status assert_equal '', spec2.message assert_equal %w{ bar/baz.rhtml cat/_foo.rhtml layout/foo.rhtml }, spec2.gen_parts end def test_rebuild_template content_hash = {} template1 = <<-END This should make it outdated
Hello this will also make this outdated
END spec1 = MasterView::TemplateSpec.scan_template(template1, 'hello', content_hash) template2 = <<-END
Hello
END spec2 = MasterView::TemplateSpec.scan_template(template2, 'world', content_hash) invalid_parts = MasterView::TemplateSpec.template_out_of_sync?(template1, content_hash) spec1.update_status_from_invalid_parts(invalid_parts) assert_equal %w{ cat/_foo.rhtml layout/foo.rhtml }, invalid_parts assert_equal MasterView::TemplateSpec::Status::ImportsOutdated, spec1.status assert_equal 'Outdated parts: cat/_foo.rhtml, layout/foo.rhtml', spec1.message assert_equal [], spec1.gen_parts expected_rebuilt_template = <<-END
Hello
END rebuilt_template = spec1.rebuild_template(content_hash) assert_equal expected_rebuilt_template.strip, rebuilt_template end # test the creation of an empty shell using simply the source and action inferring the rest def test_create_empty_shell_for_action source = <<-END
Hello
END source.strip! empty_erb = <<-END
controller_file_name=<%= @controller_file_name %>
END empty_erb.strip! expected_result = <<-END
controller_file_name=foo
END src_path = 'foo_bar.html' content_hash = {} template_spec_to_copy_from = MasterView::TemplateSpec.scan_template(source, src_path, content_hash) # setup content_hash since not getting from dir options = { :template_source => source, :content_hash => content_hash } result = MasterView::TemplateSpec.create_empty_shell_for_action(src_path, 'cat', empty_erb, options) assert_equal expected_result.strip, result end # test the creation of an empty shell using simply the source and action inferring the rest, src_path = app/views/masterview/foo_bar.html def test_create_empty_shell_for_action_full_path source = <<-END
Hello
END source.strip! empty_erb = <<-END
controller_file_name=<%= @controller_file_name %>
END empty_erb.strip! expected_result = <<-END
controller_file_name=foo
END src_path = 'app/views/masterview/foo_bar.html' content_hash = {} template_spec_to_copy_from = MasterView::TemplateSpec.scan_template(source, src_path, content_hash) # setup content_hash since not getting from dir options = { :template_source => source, :content_hash => content_hash } result = MasterView::TemplateSpec.create_empty_shell_for_action(src_path, 'cat', empty_erb, options) assert_equal expected_result.strip, result end # test the creation of an empty shell using simply the source and action inferring the rest, src_path = foo_bar def test_create_empty_shell_for_action_no_ext source = <<-END
Hello
END source.strip! empty_erb = <<-END
controller_file_name=<%= @controller_file_name %>
END empty_erb.strip! expected_result = <<-END
controller_file_name=foo
END src_path = 'foo_bar' content_hash = {} template_spec_to_copy_from = MasterView::TemplateSpec.scan_template(source, src_path, content_hash) # setup content_hash since not getting from dir options = { :template_source => source, :content_hash => content_hash } result = MasterView::TemplateSpec.create_empty_shell_for_action(src_path, 'cat', empty_erb, options) assert_equal expected_result.strip, result end end