mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Speedup String#blank? and remove some overspecified tests.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8137 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
*SVN*
|
||||
|
||||
* Speedup String#blank? [Jeremy Kemper, Koz]
|
||||
|
||||
* Add documentation for Hash#diff. Closes #9306 [tarmo]
|
||||
|
||||
* Add new superclass_delegating_accessors. Similar to class inheritable attributes but with subtly different semantics. [Koz, tarmo]
|
||||
|
||||
@@ -7,13 +7,7 @@ class Object
|
||||
# to
|
||||
# if !address.blank?
|
||||
def blank?
|
||||
if respond_to?(:empty?) && respond_to?(:strip)
|
||||
empty? or strip.empty?
|
||||
elsif respond_to?(:empty?)
|
||||
empty?
|
||||
else
|
||||
!self
|
||||
end
|
||||
respond_to?(:empty?) ? empty? : !self
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,7 +39,7 @@ end
|
||||
|
||||
class String #:nodoc:
|
||||
def blank?
|
||||
empty? || strip.empty?
|
||||
self !~ /\S/
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -8,47 +8,12 @@ class EmptyFalse
|
||||
def empty?() false; end
|
||||
end
|
||||
|
||||
class EmptyStripNotEmpty
|
||||
def empty?() true; end
|
||||
def strip() 'foo'; end
|
||||
end
|
||||
|
||||
class EmptyStripEmpty
|
||||
def empty?() true; end
|
||||
def strip() ''; end
|
||||
end
|
||||
|
||||
class NotEmptyStripNotEmpty
|
||||
def empty?() false; end
|
||||
def strip() 'foo'; end
|
||||
end
|
||||
|
||||
class NotEmptyStripEmpty
|
||||
def empty?() false; end
|
||||
def strip() ''; end
|
||||
end
|
||||
|
||||
class BlankTest < Test::Unit::TestCase
|
||||
BLANK = [ EmptyTrue.new, EmptyStripNotEmpty.new, EmptyStripEmpty.new,
|
||||
NotEmptyStripEmpty.new, nil, false, '', ' ', " \n\t \r ",
|
||||
[], {} ]
|
||||
NOT = [ EmptyFalse.new, NotEmptyStripNotEmpty.new, Object.new, true,
|
||||
0, 1, 'a', [nil], { nil => 0 } ]
|
||||
|
||||
class EmptyObject
|
||||
def empty?
|
||||
true
|
||||
end
|
||||
alias :strip :empty?
|
||||
end
|
||||
class NoStripObject < EmptyObject; undef :strip; end
|
||||
class NoEmptyStripObject < NoStripObject; undef :empty?; end
|
||||
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ]
|
||||
NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
|
||||
|
||||
def test_blank
|
||||
BLANK.each { |v| assert v.blank? }
|
||||
NOT.each { |v| assert !v.blank? }
|
||||
assert EmptyObject.new.blank?
|
||||
assert NoStripObject.new.blank?
|
||||
assert !NoEmptyStripObject.new.blank?
|
||||
BLANK.each { |v| assert v.blank?, "#{v.inspect} should be blank" }
|
||||
NOT.each { |v| assert !v.blank?, "#{v.inspect} should not be blank" }
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user