1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Refactoring tests and splitting files.

This commit is contained in:
Carlos Antonio da Silva 2009-12-08 11:48:31 -02:00
parent 4509ec9202
commit b65d076183
10 changed files with 162 additions and 68 deletions

View file

@ -0,0 +1,5 @@
* Input
* Label
* I18n
* Errors
* Instructions and/or Examples

View file

@ -5,7 +5,8 @@ module SimpleForm
input_type = (options.delete(:as) || default_input_type(attribute)).to_sym
html_options = options.delete(:html) || {}
html_options[:class] = "#{html_options[:class]} #{input_type}".strip
case input_type
input_field = case input_type
when :boolean then check_box(attribute, html_options)
when :radio then
['yes', 'no'].inject('') do |result, value|
@ -20,6 +21,18 @@ module SimpleForm
time_select(attribute, options, html_options)
else text_field(attribute, html_options)
end
label = if options[:label] == false
''
else
unless options[:label]
default = @object.try(:human_attribute_name, attribute.to_s) || attribute.to_s.humanize
options[:label] ||= I18n.t("views.labels.#{@object.class.name.underscore}.#{attribute}", :default => default)
end
label(attribute, options[:label])
end
label << input_field
end
private

View file

@ -1,7 +1,6 @@
require 'test_helper'
class FormHelperTest < ActionView::TestCase
tests SimpleForm::FormHelper
test 'yields an instance of FormBuilder' do
simple_form_for :user do |f|

View file

@ -1,53 +1,12 @@
require 'test_helper'
require 'ostruct'
class User < OpenStruct
def id
1
end
def new_record?
false
end
def column_for_attribute(attribute)
case attribute.to_sym
when :name, :status then :string
when :description then :text
when :age then :integer
when :credit_limit then :decimal
when :active then :boolean
when :born_at then :date
when :delivery_time then :time
when :created_at then :datetime
when :updated_at then :timestamp
end
end
end
class FormHelperTest < ActionView::TestCase
tests SimpleForm::FormBuilder
def user_path(*args)
'/users'
end
def setup
@user = User.new(
:name => 'New in Simple Form!',
:description => 'Hello!',
:created_at => Time.now
)
end
class FormBuilderTest < ActionView::TestCase
test 'input should generate a default text field' do
simple_form_for @user do |f|
concat f.input :name
concat f.input :description
end
assert_select 'form input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]'
assert_select 'form textarea[name=\'user[description]\'][id=user_description]', 'Hello!'
end
test 'input should generate a default class for each input' do
@ -110,7 +69,7 @@ class FormHelperTest < ActionView::TestCase
assert_select "form select.date#user_born_at_1i"
assert_select "form select.date#user_born_at_2i"
assert_select "form select.date#user_born_at_3i"
assert_no_tag "select", :attributes => { :id => "user_born_at_4i" }
assert_no_tag :tag => 'select', :attributes => { :id => "user_born_at_4i" }
end
test 'input should generate a time select by default for time attributes' do
@ -133,11 +92,12 @@ class FormHelperTest < ActionView::TestCase
assert_select 'form input.string#user_born_at'
end
test 'it should allow boolean fields as radio buttons' do
test 'input should allow boolean fields as radio buttons' do
simple_form_for @user do |f|
concat f.input :active, :as => :radio
end
assert_select 'form input[type=radio][value=yes].radio#user_active_yes'
assert_select 'form input[type=radio][value=no].radio#user_active_no'
end
end

47
test/label_test.rb Normal file
View file

@ -0,0 +1,47 @@
require 'test_helper'
class FormBuilderTest < ActionView::TestCase
test 'input should generate a label with the text field' do
simple_form_for @user do |f|
concat f.input :name
end
assert_select 'form label[for=user_name]', 'Name'
end
test 'input should allow not using a label' do
simple_form_for @user do |f|
concat f.input :name, :label => false
end
assert_no_tag :tag => 'label'
end
test 'input should allow using a customized label' do
simple_form_for @user do |f|
concat f.input :name, :label => 'My label!'
end
assert_select 'form label[for=user_name]', 'My label!'
end
test 'input should use human attribute name if it responds to it' do
@super_user = SuperUser.new
simple_form_for @super_user do |f|
concat f.input :name
end
assert_select 'form label[for=super_user_name]', 'Super User Name!'
end
test 'input should use i18n to pick up translation' do
store_translations(:en, :views => { :labels => { :super_user => {
:description => 'Descrição', :age => 'Idade'
} } } ) do
@super_user = SuperUser.new
simple_form_for @super_user do |f|
concat f.input :description
concat f.input :age
end
assert_select 'form label[for=super_user_description]', 'Descrição'
assert_select 'form label[for=super_user_age]', 'Idade'
end
end
end

View file

@ -0,0 +1,10 @@
module I18nHelper
def store_translations(locale, translations, &block)
begin
I18n.backend.store_translations locale, translations
yield
ensure
I18n.reload!
end
end
end

View file

@ -0,0 +1,6 @@
class MockController
def url_for(*args)
"http://example.com"
end
end

View file

@ -0,0 +1,14 @@
class MockResponse
def initialize(test_case)
@test_case = test_case
end
def content_type
'text/html'
end
def body
@test_case.send :output_buffer
end
end

40
test/support/models.rb Normal file
View file

@ -0,0 +1,40 @@
require 'ostruct'
class User < OpenStruct
def id
1
end
def new_record?
false
end
def column_for_attribute(attribute)
case attribute.to_sym
when :name, :status then :string
when :description then :text
when :age then :integer
when :credit_limit then :decimal
when :active then :boolean
when :born_at then :date
when :delivery_time then :time
when :created_at then :datetime
when :updated_at then :timestamp
end
end
def human_attribute_name(attribute)
nil
end
end
class SuperUser < User
def human_attribute_name(attribute)
case attribute
when 'name' then 'Super User Name!'
else super
end
end
end

View file

@ -1,36 +1,23 @@
require 'rubygems'
require 'test/unit'
require 'i18n'
require 'action_controller'
require 'action_view/test_case'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
require File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
class MockController
def url_for(*args)
"http://example.com"
end
end
class MockResponse
def initialize(test_case)
@test_case = test_case
end
def content_type
'text/html'
end
def body
@test_case.send :output_buffer
end
end
I18n.default_locale = :en
class ActionView::TestCase
include I18nHelper
tests SimpleForm::FormHelper
setup :set_controller
setup :set_response
setup :setup_new_user
def set_controller
@controller = MockController.new
@ -40,7 +27,20 @@ class ActionView::TestCase
@response = MockResponse.new(self)
end
def setup_new_user
@user = User.new(
:name => 'New in Simple Form!',
:description => 'Hello!',
:created_at => Time.now
)
end
def protect_against_forgery?
false
end
def user_path(*args)
'/users'
end
alias :super_user_path :user_path
end