Permalink
Browse files

Test that #[] and #[]= keep working when #read_attribute and #write_a…

…ttribute are overridden
  • Loading branch information...
1 parent f22c36b commit cda5094abd605d8612b4b031cf870a2004938125 @tomstuart tomstuart committed Jan 11, 2012
Showing with 33 additions and 0 deletions.
  1. +33 −0 activerecord/test/cases/attribute_methods_test.rb
View
33 activerecord/test/cases/attribute_methods_test.rb
@@ -294,6 +294,39 @@ def test_read_write_boolean_attribute
# puts ""
end
+ def test_overridden_write_attribute
+ topic = Topic.new
+ def topic.write_attribute(attr_name, value)
+ super(attr_name, value.downcase)
+ end
+
+ topic.send(:write_attribute, :title, "Yet another topic")
+ assert_equal "yet another topic", topic.title
+
+ topic[:title] = "Yet another topic: part 2"
+ assert_equal "yet another topic: part 2", topic.title
+
+ topic.send(:write_attribute, "title", "Yet another topic: part 3")
+ assert_equal "yet another topic: part 3", topic.title
+
+ topic["title"] = "Yet another topic: part 4"
+ assert_equal "yet another topic: part 4", topic.title
+ end
+
+ def test_overridden_read_attribute
+ topic = Topic.new
+ topic.title = "Stop changing the topic"
+ def topic.read_attribute(attr_name)
+ super(attr_name).upcase
+ end
+
+ assert_equal "STOP CHANGING THE TOPIC", topic.send(:read_attribute, "title")
+ assert_equal "STOP CHANGING THE TOPIC", topic["title"]
+
+ assert_equal "STOP CHANGING THE TOPIC", topic.send(:read_attribute, :title)
+ assert_equal "STOP CHANGING THE TOPIC", topic[:title]
+ end
+
def test_read_overridden_attribute
topic = Topic.new(:title => 'a')
def topic.title() 'b' end

0 comments on commit cda5094

Please sign in to comment.