mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
New assertion: assert_present [#4299 state:committed]
Signed-off-by: Xavier Noria <fxn@hashref.com>
This commit is contained in:
committed by
Xavier Noria
parent
7212c29802
commit
589deb39c7
@@ -69,6 +69,13 @@ module ActiveSupport
|
||||
def assert_blank(object)
|
||||
assert object.blank?, "#{object.inspect} is not blank"
|
||||
end
|
||||
|
||||
# Test if an expression is not blank. Passes if object.present? is true.
|
||||
#
|
||||
# assert_present {:data => 'x' } # => true
|
||||
def assert_present(object)
|
||||
assert object.present?, "#{object.inspect} is blank"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -96,7 +96,7 @@ end
|
||||
|
||||
class AssertBlankTest < ActiveSupport::TestCase
|
||||
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ]
|
||||
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'j', [nil], { nil => 0 } ]
|
||||
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]
|
||||
|
||||
def test_assert_blank_true
|
||||
BLANK.each { |v| assert_blank v }
|
||||
@@ -114,6 +114,26 @@ class AssertBlankTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
class AssertPresentTest < ActiveSupport::TestCase
|
||||
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ]
|
||||
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]
|
||||
|
||||
def test_assert_blank_true
|
||||
NOT_BLANK.each { |v| assert_present v }
|
||||
end
|
||||
|
||||
def test_assert_blank_false
|
||||
BLANK.each { |v|
|
||||
begin
|
||||
assert_present v
|
||||
fail 'should not get to here'
|
||||
rescue Exception => e
|
||||
assert_match(/is blank/, e.message)
|
||||
end
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# These should always pass
|
||||
if ActiveSupport::Testing.const_defined?(:Default)
|
||||
class NotTestingThingsTest < Test::Unit::TestCase
|
||||
|
||||
Reference in New Issue
Block a user