diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2018-09-08 05:56:22 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-08 05:56:22 +0900 |
commit | c3e569550ca0a90561fe990f519719ba52402504 (patch) | |
tree | aead9cc86d228ba66b413ba4b3749dfc75947489 /activemodel | |
parent | dd29fabebf1b702031213361b7757be5892cd59f (diff) | |
parent | ba406d9c220c211439820271aabcf83395c60957 (diff) | |
download | rails-c3e569550ca0a90561fe990f519719ba52402504.tar.gz rails-c3e569550ca0a90561fe990f519719ba52402504.tar.bz2 rails-c3e569550ca0a90561fe990f519719ba52402504.zip |
Merge pull request #33804 from yskkin/num_string
Fix non_numeric_string?
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/type/helpers/numeric.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/type/decimal_test.rb | 5 | ||||
-rw-r--r-- | activemodel/test/cases/type/float_test.rb | 5 | ||||
-rw-r--r-- | activemodel/test/cases/type/integer_test.rb | 6 |
4 files changed, 14 insertions, 4 deletions
diff --git a/activemodel/lib/active_model/type/helpers/numeric.rb b/activemodel/lib/active_model/type/helpers/numeric.rb index 16e14f9e5f..473cdb0c67 100644 --- a/activemodel/lib/active_model/type/helpers/numeric.rb +++ b/activemodel/lib/active_model/type/helpers/numeric.rb @@ -29,7 +29,7 @@ module ActiveModel # 'wibble'.to_i will give zero, we want to make sure # that we aren't marking int zero to string zero as # changed. - value.to_s !~ /\A-?\d+\.?\d*\z/ + !/\A[-+]?\d+/.match?(value.to_s) end end end diff --git a/activemodel/test/cases/type/decimal_test.rb b/activemodel/test/cases/type/decimal_test.rb index c0cf6ce590..be60c4f7fa 100644 --- a/activemodel/test/cases/type/decimal_test.rb +++ b/activemodel/test/cases/type/decimal_test.rb @@ -57,9 +57,12 @@ module ActiveModel def test_changed? type = Decimal.new - assert type.changed?(5.0, 5.0, "5.0wibble") + assert type.changed?(0.0, 0, "wibble") + assert type.changed?(5.0, 0, "wibble") + assert_not type.changed?(5.0, 5.0, "5.0wibble") assert_not type.changed?(5.0, 5.0, "5.0") assert_not type.changed?(-5.0, -5.0, "-5.0") + assert_not type.changed?(5.0, 5.0, "0.5e+1") end def test_scale_is_applied_before_precision_to_prevent_rounding_errors diff --git a/activemodel/test/cases/type/float_test.rb b/activemodel/test/cases/type/float_test.rb index 28318e06f8..230a8dda32 100644 --- a/activemodel/test/cases/type/float_test.rb +++ b/activemodel/test/cases/type/float_test.rb @@ -21,9 +21,12 @@ module ActiveModel def test_changing_float type = Type::Float.new - assert type.changed?(5.0, 5.0, "5wibble") + assert type.changed?(0.0, 0, "wibble") + assert type.changed?(5.0, 0, "wibble") + assert_not type.changed?(5.0, 5.0, "5wibble") assert_not type.changed?(5.0, 5.0, "5") assert_not type.changed?(5.0, 5.0, "5.0") + assert_not type.changed?(500.0, 500.0, "0.5E+4") assert_not type.changed?(nil, nil, nil) end end diff --git a/activemodel/test/cases/type/integer_test.rb b/activemodel/test/cases/type/integer_test.rb index 8c5d18c9b3..df12098974 100644 --- a/activemodel/test/cases/type/integer_test.rb +++ b/activemodel/test/cases/type/integer_test.rb @@ -53,9 +53,13 @@ module ActiveModel test "changed?" do type = Type::Integer.new - assert type.changed?(5, 5, "5wibble") + assert type.changed?(0, 0, "wibble") + assert type.changed?(5, 0, "wibble") + assert_not type.changed?(5, 5, "5wibble") assert_not type.changed?(5, 5, "5") assert_not type.changed?(5, 5, "5.0") + assert_not type.changed?(5, 5, "+5") + assert_not type.changed?(5, 5, "+5.0") assert_not type.changed?(-5, -5, "-5") assert_not type.changed?(-5, -5, "-5.0") assert_not type.changed?(nil, nil, nil) |