Skip to content

Commit 5ff6de0

Browse files
Jeff Kreeftmeijerjosevalim
authored andcommitted
Added assert_attribute_type to clean up GeneratedAttributeTest [rails#2377 state:resolved]
Signed-off-by: José Valim <[email protected]>
1 parent 99b38f3 commit 5ff6de0

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

railties/lib/rails/generators/test_case.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ def assert_instance_method(method, content)
189189
end
190190
alias :assert_method :assert_instance_method
191191

192+
# Asserts the given field name gets translated to an attribute type
193+
# properly.
194+
#
195+
# assert_attribute_type 'date', :date_select
196+
#
197+
def assert_attribute_type(name, attribute_type)
198+
assert_equal(
199+
Rails::Generators::GeneratedAttribute.new('test', name).field_type,
200+
attribute_type
201+
)
202+
end
203+
192204
# Runs the generator configured for this class. The first argument is an array like
193205
# command line arguments:
194206
#
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require 'generators/generators_test_helper'
2+
require 'rails/generators/generated_attribute'
3+
4+
class GeneratedAttributeTest < Rails::Generators::TestCase
5+
include GeneratorsTestHelper
6+
7+
def test_field_type_returns_text_field
8+
%w(integer float decimal string).each do |name|
9+
assert_attribute_type name, :text_field
10+
end
11+
end
12+
13+
def test_field_type_returns_datetime_select
14+
%w(datetime timestamp).each do |name|
15+
assert_attribute_type name, :datetime_select
16+
end
17+
end
18+
19+
def test_field_type_returns_time_select
20+
assert_attribute_type 'time', :time_select
21+
end
22+
23+
def test_field_type_returns_date_select
24+
assert_attribute_type 'date', :date_select
25+
end
26+
27+
def test_field_type_returns_text_area
28+
assert_attribute_type 'text', :text_area
29+
end
30+
31+
def test_field_type_returns_check_box
32+
assert_attribute_type 'boolean', :check_box
33+
end
34+
35+
def test_field_type_with_unknown_type_returns_text_field
36+
%w(foo bar baz).each do |name|
37+
assert_attribute_type name, :text_field
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)