Add discovery tests.

This commit is contained in:
José Valim 2011-04-27 21:26:02 +02:00
parent 1520353fc2
commit 3f81a1dfdf
3 changed files with 81 additions and 2 deletions

View File

@ -349,8 +349,7 @@ module SimpleForm
def mapping_override(klass) #:nodoc:
name = klass.name
if name =~ /^SimpleForm::Inputs/
attempt = "#{name.split("::").last}Input"
attempt_mapping attempt, Object
attempt_mapping name.split("::").last, Object
end
end

21
test/discovery_inputs.rb Normal file
View File

@ -0,0 +1,21 @@
class StringInput < SimpleForm::Inputs::StringInput
def input
"<section>#{super}</section>".html_safe
end
end
class NumericInput < SimpleForm::Inputs::NumericInput
def input
"<section>#{super}</section>".html_safe
end
end
class CustomizedInput < SimpleForm::Inputs::StringInput
def input
"<section>#{super}</section>".html_safe
end
def input_method
:text_field
end
end

View File

@ -681,4 +681,63 @@ class FormBuilderTest < ActionView::TestCase
assert_nil user.class.mappings[:custom_type]
end
end
# DISCOVERY
# Setup new inputs and remove them after the test.
def discovery(value=false)
swap SimpleForm, :cache_discovery => value do
begin
load "discovery_inputs.rb"
yield
ensure
SimpleForm::FormBuilder.discovery_cache.clear
Object.send :remove_const, :StringInput
Object.send :remove_const, :NumericInput
Object.send :remove_const, :CustomizedInput
end
end
end
test 'builder should not discover new inputs if cached' do
with_form_for @user, :name
assert_select 'form input#user_name.string'
discovery(true) do
with_form_for @user, :name
assert_no_select 'form section input#user_name.string'
end
end
test 'builder should discover new inputs' do
discovery do
with_form_for @user, :name, :as => :customized
assert_select 'form section input#user_name.string'
end
end
test 'builder should not discover new inputs if discovery is off' do
with_form_for @user, :name
assert_select 'form input#user_name.string'
swap SimpleForm, :inputs_discovery => false do
discovery do
with_form_for @user, :name
assert_no_select 'form section input#user_name.string'
end
end
end
test 'builder should discover new inputs from mappings if not cached' do
discovery do
with_form_for @user, :name
assert_select 'form section input#user_name.string'
end
end
test 'builder should discover new inputs from internal fallbacks if not cached' do
discovery do
with_form_for @user, :age
assert_select 'form section input#user_age.numeric.integer'
end
end
end