#!/usr/bin/env ruby require 'test/unit' currentPath = File.dirname(__FILE__) require File.join( currentPath, '../../lib/facets/core/string/starts_with' ) require File.join( currentPath, '../../lib/masterview/core_ext/pathname' ) require File.join( currentPath, '../../lib/masterview/keyword_expander' ) class TestKeywordExpander < Test::Unit::TestCase # bring test subjects into the local name space KeywordExpander = MasterView::KeywordExpander def test_ke ke = KeywordExpander.new 'FOO' => 'bar', 'CAT' => 'dog' ke.set('HELLO', 'world') ke.set('FOO_MAN', 'foo_man') ke.set('FOO_MAN_CHU', 'fmc') ke.set('FOO_HELLO', 'foo_hello') ke.set('ONE_HELLO', 'one_hello') assert_equal 'bar', ke['FOO'] assert_equal 'dog', ke['CAT'] assert_equal 'nosub', ke.resolveAttrAndDelete( {:a => 'nosub'}, :a) assert_equal 'world', ke.resolveAttrAndDelete( {:a => 'HELLO'}, :a) assert_equal 'world', ke.resolveAttrAndDelete( {'a' => 'HELLO'}, 'a') assert_equal 'bar this', ke.resolveAttrAndDelete( {:a => 'FOO this'}, :a) assert_equal 'bar/this.rhtml', ke.resolveAttrAndDelete( {:a => 'FOO/this.rhtml'}, :a) assert_equal 'foo/bar.rhtml', ke.resolveAttrAndDelete( {:a => 'foo/FOO.rhtml'}, :a) assert_equal 'bar', ke.resolveAttrAndDelete( {:a => 'FOO'}, :a) assert_equal 'dog', ke.resolveAttrAndDelete( {:a => 'CAT'}, :a) assert_equal 'bar/dog.rhtml', ke.resolveAttrAndDelete( {:a => 'FOO/CAT.rhtml'}, :a) assert_equal 'bar/dog/foo_man.rhtml', ke.resolveAttrAndDelete( {:a => 'FOO/CAT/FOO_MAN.rhtml'}, :a) assert_equal 'one_hello/dog/foo_man/world.rhtml', ke.resolveAttrAndDelete( {:a => 'ONE_HELLO/CAT/FOO_MAN/HELLO.rhtml'}, :a) assert_equal 'barbarbar', ke.resolveAttrAndDelete( {:a => 'FOOFOOFOO'}, :a) assert_equal 'bar_bar_bar', ke.resolveAttrAndDelete( {:a => 'FOO_FOO_FOO'}, :a) assert_equal nil, ke.resolveAttrAndDelete( {:a => 'HELLO'}, :not_found) end def test_ke_expand_keywords ke = KeywordExpander.new 'FOO' => 'bar', 'CAT' => 'dog' assert_equal nil, ke.expand_keywords(nil) assert_equal 'bar', ke.expand_keywords('FOO') assert_equal 'barbarbar', ke.expand_keywords('FOOFOOFOO') assert_equal 'bardogbar', ke.expand_keywords('FOOCATFOO') assert_equal 'one/bardogbar/dog.html', ke.expand_keywords('one/FOOCATFOO/CAT.html') end def test_template_path ke = KeywordExpander.new ke.set_template_pathname('foo/bar/controller/action.html', '.rhtml') assert_equal 'foo/bar/controller/action', ke['{template_path}'] assert_equal 'foo/bar/controller', ke['{template_dir_path}'] assert_equal 'action', ke['{template_basename}'] assert_equal '.html', ke['{extension}'] # mv:generate="{template_path}" expands to include default output extension assert_equal 'foo/bar/controller/action.rhtml', ke.expand_keywords('{template_path}') # mv:generate="{template_path}.xxx" expands to respect explicitly-specified output extension assert_equal 'foo/bar/controller/action.xxx', ke.expand_keywords('{template_path}.xxx') end def test_template_path_baseonly ke = KeywordExpander.new ke.set_template_pathname('foo_bar', '.rhtml') assert_equal 'foo_bar', ke['{template_path}'] assert_equal '', ke['{template_dir_path}'] assert_equal 'foo_bar', ke['{template_basename}'] assert_equal '', ke['{extension}'] # mv:generate="{template_path}" vs. mv:generate="{template_path}.xxx" assert_equal 'foo_bar.rhtml', ke.expand_keywords('{template_path}') assert_equal 'foo_bar.xxx', ke.expand_keywords('{template_path}.xxx') end def test_template_path_cont_action ke = KeywordExpander.new ke.set_template_pathname('controller/action', '.rhtml') assert_equal 'controller/action', ke['{template_path}'] assert_equal 'controller', ke['{template_dir_path}'] assert_equal 'action', ke['{template_basename}'] assert_equal '', ke['{extension}'] # mv:generate="{template_path}" vs. mv:generate="{template_path}.xxx" assert_equal 'controller/action.rhtml', ke.expand_keywords('{template_path}') assert_equal 'controller/action.xxx', ke.expand_keywords('{template_path}.xxx') end def test_template_path_action_only ke = KeywordExpander.new ke.set_template_pathname('action', '.rhtml') assert_equal 'action', ke['{template_path}'] assert_equal '', ke['{template_dir_path}'] assert_equal 'action', ke['{template_basename}'] assert_equal '', ke['{extension}'] # mv:generate="{template_path}" vs. mv:generate="{template_path}.xxx" assert_equal 'action.rhtml', ke.expand_keywords('{template_path}') assert_equal 'action.xxx', ke.expand_keywords('{template_path}.xxx') end end