diff --git a/railties/lib/rails/generators/test_case.rb b/railties/lib/rails/generators/test_case.rb index 6b97c69d8d9c1ded0a281492cdabfe9047e0131a..24b57aabf92c5c3bb626b92951447ecba2dc4d07 100644 --- a/railties/lib/rails/generators/test_case.rb +++ b/railties/lib/rails/generators/test_case.rb @@ -189,6 +189,18 @@ def assert_instance_method(method, content) end alias :assert_method :assert_instance_method + # Asserts the given field name gets translated to an attribute type + # properly. + # + # assert_attribute_type 'date', :date_select + # + def assert_attribute_type(name, attribute_type) + assert_equal( + Rails::Generators::GeneratedAttribute.new('test', name).field_type, + attribute_type + ) + end + # Runs the generator configured for this class. The first argument is an array like # command line arguments: # diff --git a/railties/test/generators/generated_attribute_test.rb b/railties/test/generators/generated_attribute_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..ccd0c6590537656a7ca95f6e00e1852fae1fd7f9 --- /dev/null +++ b/railties/test/generators/generated_attribute_test.rb @@ -0,0 +1,40 @@ +require 'generators/generators_test_helper' +require 'rails/generators/generated_attribute' + +class GeneratedAttributeTest < Rails::Generators::TestCase + include GeneratorsTestHelper + + def test_field_type_returns_text_field + %w(integer float decimal string).each do |name| + assert_attribute_type name, :text_field + end + end + + def test_field_type_returns_datetime_select + %w(datetime timestamp).each do |name| + assert_attribute_type name, :datetime_select + end + end + + def test_field_type_returns_time_select + assert_attribute_type 'time', :time_select + end + + def test_field_type_returns_date_select + assert_attribute_type 'date', :date_select + end + + def test_field_type_returns_text_area + assert_attribute_type 'text', :text_area + end + + def test_field_type_returns_check_box + assert_attribute_type 'boolean', :check_box + end + + def test_field_type_with_unknown_type_returns_text_field + %w(foo bar baz).each do |name| + assert_attribute_type name, :text_field + end + end +end