From db045dbbf60b53dbe013ef25554fd013baf88134 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 24 Nov 2004 01:04:44 +0000 Subject: Initial git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/template/active_record_helper_test.rb | 76 ++++++++++ actionpack/test/template/date_helper_test.rb | 104 +++++++++++++ actionpack/test/template/form_helper_test.rb | 124 ++++++++++++++++ .../test/template/form_options_helper_test.rb | 165 +++++++++++++++++++++ actionpack/test/template/tag_helper_test.rb | 18 +++ actionpack/test/template/text_helper_test.rb | 62 ++++++++ actionpack/test/template/url_helper_test.rb | 49 ++++++ 7 files changed, 598 insertions(+) create mode 100644 actionpack/test/template/active_record_helper_test.rb create mode 100755 actionpack/test/template/date_helper_test.rb create mode 100644 actionpack/test/template/form_helper_test.rb create mode 100644 actionpack/test/template/form_options_helper_test.rb create mode 100644 actionpack/test/template/tag_helper_test.rb create mode 100644 actionpack/test/template/text_helper_test.rb create mode 100644 actionpack/test/template/url_helper_test.rb (limited to 'actionpack/test/template') diff --git a/actionpack/test/template/active_record_helper_test.rb b/actionpack/test/template/active_record_helper_test.rb new file mode 100644 index 0000000000..4b32f4dd48 --- /dev/null +++ b/actionpack/test/template/active_record_helper_test.rb @@ -0,0 +1,76 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' +# require File.dirname(__FILE__) + '/../../lib/action_view/helpers/active_record_helper' + +class ActiveRecordHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormHelper + include ActionView::Helpers::ActiveRecordHelper + + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + Column = Struct.new("Column", :type, :name, :human_name) + + def setup + @post = Post.new + def @post.errors() Class.new{ def on(field) field == "author_name" || field == "body" end }.new end + def @post.new_record?() true end + + def @post.column_for_attribute(attr_name) + Post.content_columns.select { |column| column.name == attr_name }.first + end + + def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end + + @post.title = "Hello World" + @post.author_name = "" + @post.body = "Back to the hill and over it again!" + @post.secret = 1 + @post.written_on = Date.new(2004, 6, 15) + end + + def test_generic_input_tag + assert_equal( + '', input("post", "title") + ) + end + + def test_text_area_with_errors + assert_equal( + "
", + text_area("post", "body") + ) + end + + def test_text_field_with_errors + assert_equal( + '
', + text_field("post", "author_name") + ) + end + + def test_form_with_string + assert_equal( + "


\n


", + form("post") + ) + end + + def test_form_with_date + def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end + + assert_equal( + "


\n\n\n

", + form("post") + ) + end + + def test_form_with_datetime + def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end + @post.written_on = Time.gm(2004, 6, 15, 16, 30) + + assert_equal( + "


\n\n\n — \n : \n

", + form("post") + ) + end +end \ No newline at end of file diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb new file mode 100755 index 0000000000..a8ad37918d --- /dev/null +++ b/actionpack/test/template/date_helper_test.rb @@ -0,0 +1,104 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/date_helper' + +class DateHelperTest < Test::Unit::TestCase + include ActionView::Helpers::DateHelper + + def test_distance_in_words + from = Time.mktime(2004, 3, 6, 21, 41, 18) + + assert_equal "less than a minute", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 41, 25)) + assert_equal "5 minutes", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 21, 46, 25)) + assert_equal "about 1 hour", distance_of_time_in_words(from, Time.mktime(2004, 3, 6, 22, 47, 25)) + assert_equal "about 3 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 0, 41)) + assert_equal "about 4 hours", distance_of_time_in_words(from, Time.mktime(2004, 3, 7, 1, 20)) + assert_equal "2 days", distance_of_time_in_words(from, Time.mktime(2004, 3, 9, 15, 40)) + end + + def test_select_day + expected = "\n" + + assert_equal expected, select_day(Time.mktime(2003, 8, 16)) + assert_equal expected, select_day(16) + end + + def test_select_day_with_blank + expected = "\n" + + assert_equal expected, select_day(Time.mktime(2003, 8, 16), :include_blank => true) + assert_equal expected, select_day(16, :include_blank => true) + end + + def test_select_month + expected = "\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16)) + assert_equal expected, select_month(8) + end + + def test_select_month_with_numbers + expected = "\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16), :use_month_numbers => true) + assert_equal expected, select_month(8, :use_month_numbers => true) + end + + def test_select_month_with_numbers_and_names + expected = "\n" + + assert_equal expected, select_month(Time.mktime(2003, 8, 16), :add_month_numbers => true) + assert_equal expected, select_month(8, :add_month_numbers => true) + end + + def test_select_year + expected = "\n" + + assert_equal expected, select_year(Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005) + assert_equal expected, select_year(2003, :start_year => 2003, :end_year => 2005) + end + + def test_select_year_with_type_discarding + expected = "\n" + + assert_equal expected, select_year( + Time.mktime(2003, 8, 16), :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) + assert_equal expected, select_year( + 2003, :prefix => "date_year", :discard_type => true, :start_year => 2003, :end_year => 2005) + end + + + def test_select_date + expected = "\n" + + expected << "\n" + + expected << "\n" + + assert_equal expected, select_date( + Time.mktime(2003, 8, 16), :start_year => 2003, :end_year => 2005, :prefix => "date[first]" + ) + end +end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb new file mode 100644 index 0000000000..8f3d5ebb94 --- /dev/null +++ b/actionpack/test/template/form_helper_test.rb @@ -0,0 +1,124 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_helper' + +class FormHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormHelper + + old_verbose, $VERBOSE = $VERBOSE, nil + Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on) + $VERBOSE = old_verbose + + def setup + @post = Post.new + def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end + + @post.title = "Hello World" + @post.author_name = "" + @post.body = "Back to the hill and over it again!" + @post.secret = 1 + @post.written_on = Date.new(2004, 6, 15) + end + + def test_text_field + assert_equal( + '', text_field("post", "title") + ) + assert_equal( + '', password_field("post", "title") + ) + assert_equal( + '', password_field("person", "name") + ) + end + + def test_text_field_with_escapes + @post.title = "Hello World" + assert_equal( + '', text_field("post", "title") + ) + end + + def test_text_field_with_options + assert_equal( + '', + text_field("post", "title", "size" => "35") + ) + end + + def test_text_field_assuming_size + assert_equal( + '', + text_field("post", "title", "maxlength" => 35) + ) + end + + def test_check_box + assert_equal( + '', + check_box("post", "secret") + ) + + @post.secret = 0 + assert_equal( + '', + check_box("post", "secret") + ) + + @post.secret = true + assert_equal( + '', + check_box("post", "secret") + ) + end + + def test_text_area + assert_equal( + '', + text_area("post", "body") + ) + end + + def test_text_area_with_escapes + @post.body = "Back to the hill and over it again!" + assert_equal( + '', + text_area("post", "body") + ) + end + + def test_date_selects + assert_equal( + '', + text_area("post", "body") + ) + end + + + def test_explicit_name + assert_equal( + '', text_field("post", "title", "name" => "dont guess") + ) + assert_equal( + '', + text_area("post", "body", "name" => "really!") + ) + assert_equal( + '', + check_box("post", "secret", "name" => "i mean it") + ) + end + + def test_explicit_id + assert_equal( + '', text_field("post", "title", "id" => "dont guess") + ) + assert_equal( + '', + text_area("post", "body", "id" => "really!") + ) + assert_equal( + '', + check_box("post", "secret", "id" => "i mean it") + ) + end +end diff --git a/actionpack/test/template/form_options_helper_test.rb b/actionpack/test/template/form_options_helper_test.rb new file mode 100644 index 0000000000..fa0a37aa36 --- /dev/null +++ b/actionpack/test/template/form_options_helper_test.rb @@ -0,0 +1,165 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/form_options_helper' + +class FormOptionsHelperTest < Test::Unit::TestCase + include ActionView::Helpers::FormOptionsHelper + + old_verbose, $VERBOSE = $VERBOSE, nil + Post = Struct.new('Post', :title, :author_name, :body, :secret, :written_on, :category, :origin) + Continent = Struct.new('Continent', :continent_name, :countries) + Country = Struct.new('Country', :country_id, :country_name) + $VERBOSE = old_verbose + + def test_collection_options + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title") + ) + end + + + def test_collection_options_with_preselected_value + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", "Babe") + ) + end + + def test_collection_options_with_preselected_value_array + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + assert_equal( + "\n\n", + options_from_collection_for_select(@posts, "author_name", "title", [ "Babe", "Cabe" ]) + ) + end + + def test_array_options_for_select + assert_equal( + "\n\n", + options_for_select([ "", "USA", "Sweden" ]) + ) + end + + def test_array_options_for_select_with_selection + assert_equal( + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], "") + ) + end + + def test_array_options_for_select_with_selection_array + assert_equal( + "\n\n", + options_for_select([ "Denmark", "", "Sweden" ], [ "", "Sweden" ]) + ) + end + + def test_hash_options_for_select + assert_equal( + "\n", + options_for_select({ "$" => "Dollar", "" => "" }) + ) + end + + def test_hash_options_for_select_with_selection + assert_equal( + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, "Dollar") + ) + end + + def test_hash_options_for_select_with_selection + assert_equal( + "\n", + options_for_select({ "$" => "Dollar", "" => "" }, [ "Dollar", "" ]) + ) + end + + def test_html_option_groups_from_collection + @continents = [ + Continent.new("", [Country.new("", ""), Country.new("so", "Somalia")] ), + Continent.new("Europe", [Country.new("dk", "Denmark"), Country.new("ie", "Ireland")] ) + ] + + assert_equal( + "\n\n", + option_groups_from_collection_for_select(@continents, "countries", "continent_name", "country_id", "country_name", "dk") + ) + end + + def test_select + @post = Post.new + @post.category = "" + assert_equal( + "", + select("post", "category", %w( abe hest)) + ) + end + + def test_select_with_blank + @post = Post.new + @post.category = "" + assert_equal( + "", + select("post", "category", %w( abe hest), :include_blank => true) + ) + end + + def test_collection_select + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + assert_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name") + ) + end + + def test_collection_select_with_blank_and_style + @posts = [ + Post.new(" went home", "", "To a little house", "shh!"), + Post.new("Babe went home", "Babe", "To a little house", "shh!"), + Post.new("Cabe went home", "Cabe", "To a little house", "shh!") + ] + + @post = Post.new + @post.author_name = "Babe" + + assert_equal( + "", + collection_select("post", "author_name", @posts, "author_name", "author_name", { :include_blank => true }, "style" => "width: 200px") + ) + end + + def test_country_select + @post = Post.new + @post.origin = "Denmark" + assert_equal( + "", + country_select("post", "origin") + ) + end +end diff --git a/actionpack/test/template/tag_helper_test.rb b/actionpack/test/template/tag_helper_test.rb new file mode 100644 index 0000000000..c3289af50c --- /dev/null +++ b/actionpack/test/template/tag_helper_test.rb @@ -0,0 +1,18 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' + +class TagHelperTest < Test::Unit::TestCase + include ActionView::Helpers::TagHelper + include ActionView::Helpers::UrlHelper + + def test_tag + assert_equal "

", tag("p", "class" => "show") + end + + def test_content_tag + assert_equal "Create", content_tag("a", "Create", "href" => "create") + end + + # FIXME: Test form tag +end \ No newline at end of file diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb new file mode 100644 index 0000000000..347420a72b --- /dev/null +++ b/actionpack/test/template/text_helper_test.rb @@ -0,0 +1,62 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/text_helper' + +class TextHelperTest < Test::Unit::TestCase + include ActionView::Helpers::TextHelper + + def test_truncate + assert_equal "Hello World!", truncate("Hello World!", 12) + assert_equal "Hello Worl...", truncate("Hello World!!", 12) + end + + def test_strip_links + assert_equal "on my mind", strip_links("on my mind") + end + + def test_highlighter + assert_equal( + "This is a beautiful morning", + highlight("This is a beautiful morning", "beautiful") + ) + + assert_equal( + "This is a beautiful morning, but also a beautiful day", + highlight("This is a beautiful morning, but also a beautiful day", "beautiful") + ) + + assert_equal( + "This is a beautiful morning, but also a beautiful day", + highlight("This is a beautiful morning, but also a beautiful day", "beautiful", '\1') + ) + end + + def test_highlighter_with_regexp + assert_equal( + "This is a beautiful! morning", + highlight("This is a beautiful! morning", "beautiful!") + ) + + assert_equal( + "This is a beautiful! morning", + highlight("This is a beautiful! morning", "beautiful! morning") + ) + + assert_equal( + "This is a beautiful? morning", + highlight("This is a beautiful? morning", "beautiful? morning") + ) + end + + def test_excerpt + assert_equal("...is a beautiful morni...", excerpt("This is a beautiful morning", "beautiful", 5)) + assert_equal("This is a...", excerpt("This is a beautiful morning", "this", 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) + assert_equal("...iful morning", excerpt("This is a beautiful morning", "morning", 5)) + assert_nil excerpt("This is a beautiful morning", "day") + end + + def test_pluralization + assert_equal("1 count", pluralize(1, "count")) + assert_equal("2 counts", pluralize(2, "count")) + end +end \ No newline at end of file diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb new file mode 100644 index 0000000000..198b26b113 --- /dev/null +++ b/actionpack/test/template/url_helper_test.rb @@ -0,0 +1,49 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper' + +class UrlHelperTest < Test::Unit::TestCase + include ActionView::Helpers::UrlHelper + include ActionView::Helpers::TagHelper + + def setup + @controller = Class.new do + def url_for(options, *parameters_for_method_reference) + "http://www.world.com" + end + end + @controller = @controller.new + end + + # todo: missing test cases + def test_link_tag_with_straight_url + assert_equal "Hello", link_to("Hello", "http://www.world.com") + end + + def test_link_tag_with_javascript_confirm + assert_equal( + "Hello", + link_to("Hello", "http://www.world.com", :confirm => "Are you sure?") + ) + end + + def test_link_unless_current + @params = { "controller" => "weblog", "action" => "show"} + assert_equal "Showing", link_to_unless_current("Showing", :action => "show", :controller => "weblog") + assert "Listing", link_to_unless_current("Listing", :action => "list", :controller => "weblog") + end + + def test_mail_to + assert_equal "david@loudthinking.com", mail_to("david@loudthinking.com") + assert_equal "David Heinemeier Hansson", mail_to("david@loudthinking.com", "David Heinemeier Hansson") + assert_equal( + "David Heinemeier Hansson", + mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin") + ) + end + + def test_link_with_nil_html_options + assert "Hello", + link_to("Hello", {:action => 'myaction'}, nil) + end +end \ No newline at end of file -- cgit v1.2.3