Skip to content

Commit

Permalink
Add test for define_attribute_method using as name an invalid identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Mar 17, 2011
1 parent 0eae625 commit cb44e99
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions activemodel/test/cases/attribute_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ def attribute(name)
end
end

class ModelWithWeirdNamesAttributes
include ActiveModel::AttributeMethods

attribute_method_suffix ''

def attributes
{ :'a?b' => 'value of a?b' }
end

private
def attribute(name)
attributes[name.to_sym]
end
end

class AttributeMethodsTest < ActiveModel::TestCase
test 'unrelated classes should not share attribute method matchers' do
assert_not_equal ModelWithAttributes.send(:attribute_method_matchers),
Expand All @@ -49,6 +64,14 @@ class AttributeMethodsTest < ActiveModel::TestCase
assert_equal "value of foo", ModelWithAttributes.new.foo
end

test '#define_attribute_method generates attribute method with invalid identifier characters' do
ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b')
ModelWithWeirdNamesAttributes.define_attribute_method(:'a?b')

assert_respond_to ModelWithWeirdNamesAttributes.new, :'a?b'
assert_equal "value of a?b", ModelWithWeirdNamesAttributes.new.send('a?b')
end

test '#define_attribute_methods generates attribute methods' do
ModelWithAttributes.define_attribute_methods([:foo])

Expand All @@ -58,15 +81,15 @@ class AttributeMethodsTest < ActiveModel::TestCase

test '#define_attribute_methods generates attribute methods with spaces in their names' do
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])

assert_respond_to ModelWithAttributesWithSpaces.new, :'foo bar'
assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.send(:'foo bar')
end

test '#alias_attribute works with attributes with spaces in their names' do
ModelWithAttributesWithSpaces.define_attribute_methods([:'foo bar'])
ModelWithAttributesWithSpaces.alias_attribute(:'foo_bar', :'foo bar')

assert_equal "value of foo bar", ModelWithAttributesWithSpaces.new.foo_bar
end

Expand Down

0 comments on commit cb44e99

Please sign in to comment.