aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext/string_ext_test.rb
diff options
context:
space:
mode:
authorErnie Miller <ernie@erniemiller.org>2014-04-18 14:55:30 -0400
committerErnie Miller <ernie@erniemiller.org>2014-04-18 14:55:30 -0400
commit9e67954dcfdc92f1087a03c0b1c61251d993e270 (patch)
tree3e9e799de5bbbf39c4ea506040e7912e590cdac3 /activesupport/test/core_ext/string_ext_test.rb
parent99352088ca9629147150627b2702684f9bf1e608 (diff)
downloadrails-9e67954dcfdc92f1087a03c0b1c61251d993e270.tar.gz
rails-9e67954dcfdc92f1087a03c0b1c61251d993e270.tar.bz2
rails-9e67954dcfdc92f1087a03c0b1c61251d993e270.zip
Fix inconsistent behavior from String#first/#last
While calling String#first or String#last with zero or a Fixnum < the string's length returns a new string, a Fixnum >= the string's length returns the string itself. This inconsistency can lead to inadvertent mutation of a string.
Diffstat (limited to 'activesupport/test/core_ext/string_ext_test.rb')
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index ea12f1ced5..f8d4d0f0dc 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -296,6 +296,12 @@ class StringAccessTest < ActiveSupport::TestCase
assert_equal 'x', 'x'.first(4)
end
+ test "#first with Fixnum >= string length still returns a new string" do
+ string = "hello"
+ different_string = string.first(5)
+ assert_not_same different_string, string
+ end
+
test "#last returns the last character" do
assert_equal "o", "hello".last
assert_equal 'x', 'x'.last
@@ -308,6 +314,12 @@ class StringAccessTest < ActiveSupport::TestCase
assert_equal 'x', 'x'.last(4)
end
+ test "#last with Fixnum >= string length still returns a new string" do
+ string = "hello"
+ different_string = string.last(5)
+ assert_not_same different_string, string
+ end
+
test "access returns a real string" do
hash = {}
hash["h"] = true