diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-17 18:51:38 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-17 20:09:27 -0600 |
commit | c7802dccba69517083000261477f7e1d410e1cca (patch) | |
tree | 2548317ff70e5e9009da1a1303c5ac7ae425d76c /activerecord/test/cases/type | |
parent | bdc39899ebb2a17e91cfde1803da3d1170531f04 (diff) | |
download | rails-c7802dccba69517083000261477f7e1d410e1cca.tar.gz rails-c7802dccba69517083000261477f7e1d410e1cca.tar.bz2 rails-c7802dccba69517083000261477f7e1d410e1cca.zip |
Detect in-place modifications on Strings
Diffstat (limited to 'activerecord/test/cases/type')
-rw-r--r-- | activerecord/test/cases/type/string_test.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activerecord/test/cases/type/string_test.rb b/activerecord/test/cases/type/string_test.rb new file mode 100644 index 0000000000..420177ed49 --- /dev/null +++ b/activerecord/test/cases/type/string_test.rb @@ -0,0 +1,36 @@ +require 'cases/helper' + +module ActiveRecord + class StringTypeTest < ActiveRecord::TestCase + test "type casting" do + type = Type::String.new + assert_equal "1", type.type_cast_from_user(true) + assert_equal "0", type.type_cast_from_user(false) + assert_equal "123", type.type_cast_from_user(123) + end + + test "values are duped coming out" do + s = "foo" + type = Type::String.new + assert_not_same s, type.type_cast_from_user(s) + assert_not_same s, type.type_cast_from_database(s) + end + + test "string mutations are detected" do + klass = Class.new(Base) + klass.table_name = 'authors' + + author = klass.create!(name: 'Sean') + assert_not author.changed? + + author.name << ' Griffin' + assert author.name_changed? + + author.save! + author.reload + + assert_equal 'Sean Griffin', author.name + assert_not author.changed? + end + end +end |