require "rubygems" require 'test/unit' require 'ostruct' require 'active_support' require 'action_pack' require 'action_controller' require 'action_view' require File.dirname(__FILE__) + '/../lib/text_area_with_status' # A quick and dirty mock object class MyMock def id() 17 end def description() 'hello' end end class TextAreaWithStatusTest < Test::Unit::TestCase include ActionView::Helpers::FormHelper include ActionView::Helpers::FormTagHelper include ActionView::Helpers::AssetTagHelper include ActionView::Helpers::TagHelper include TextAreaWithStatus require File.dirname(__FILE__) + '/../init' # Expected results ResPlainArea1 = "" ResPlainArea2 = "" ResAreaWithStatus1 = "
\nYou have 5 characters left." ResAreaWithStatus2 = "
\nLeft chars: 15" ResAreaWithStatus3 = "
\nRails-Fu Magic No. #94" ResAreaWithStatus4 = "
\nYou have 10 characters left." ResAreaWithStatus5 = "
\nLeft chars: 20" ResAreaWithStatus6 = "
\nYou have 6 characters left." ResAreaWithStatus7 = "
\nLeft chars: 17" ResAreaWithStatus8 = "
\nRails-Fu Magic No. #94" def setup @controller = OpenStruct.new @controller.request = OpenStruct.new @thing = MyMock.new end def test_defaults_include_plugins_js assert javascript_include_tag(:defaults) =~ /limit_chars.js/, "javascript_include_tag(:defaults) should include limit_chars.js" end def test_text_area_with_status # Let's make sure the _without versions works ok in absence of our custom options assert_equal ResPlainArea1, text_area(:thing, :description) # Now let's test it with our options included assert_equal ResAreaWithStatus1, text_area(:thing, :description, :max_chars => 10) assert_equal ResAreaWithStatus2, text_area(:thing, :description, :max_chars => 20, :max_chars_msg => 'Left chars: %d') assert_equal ResAreaWithStatus3, text_area(:thing, :description, :max_chars => 99, :id => 'rails_fu_17', :max_chars_msg => 'Rails-Fu Magic No. #%d') end def test_text_area_with_status_and_form_builder f = ActionView::Helpers::FormBuilder.new(:thing, @thing, ActionView::Base.new, nil, nil) # Let's make sure the _without versions works ok in absence of our custom options assert_equal ResPlainArea1, f.text_area(:description) # Now let's test it with our options included assert_equal ResAreaWithStatus1, f.text_area(:description, :max_chars => 10) assert_equal ResAreaWithStatus2, f.text_area(:description, :max_chars => 20, :max_chars_msg => 'Left chars: %d') assert_equal ResAreaWithStatus3, f.text_area(:description, :max_chars => 99, :id => 'rails_fu_17', :max_chars_msg => 'Rails-Fu Magic No. #%d') end def test_text_area_tag_with_status # Let's make sure the _without versions works ok in absence of our custom options assert_equal ResPlainArea2, text_area_tag('description') # Now let's test it with our options included assert_equal ResAreaWithStatus4, text_area_tag('description', nil, :max_chars => 10) assert_equal ResAreaWithStatus5, text_area_tag('description', nil, :max_chars => 20, :max_chars_msg => 'Left chars: %d') assert_equal ResAreaWithStatus6, text_area_tag('description', "abcd", :max_chars => 10) assert_equal ResAreaWithStatus7, text_area_tag('description', "abc", :max_chars => 20, :max_chars_msg => 'Left chars: %d') assert_equal ResAreaWithStatus8, text_area_tag('description', "abcde", :max_chars => 99, :id => 'rails_fu_17', :max_chars_msg => 'Rails-Fu Magic No. #%d') end end