Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

New assertion: assert_present

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
commit 4b08679ba9627884d531cf59a9bb2fd1d2c86d62 1 parent ccb1bee
Juanjo Bazán xuanxu authored fxn committed
2  activesupport/CHANGELOG
View
@@ -1,5 +1,7 @@
*2.3.6 (pending)*
+* New assertions assert_blank and assert_present. #4299 [Juanjo Bazan]
+
* Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. [Jeremy Kemper]
* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez]
7 activesupport/lib/active_support/testing/assertions.rb
View
@@ -67,6 +67,13 @@ def assert_no_difference(expression, message = nil, &block)
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
22 activesupport/test/test_test.rb
View
@@ -95,7 +95,7 @@ def empty?() false; 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 }
@@ -113,6 +113,26 @@ def test_assert_blank_false
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
Please sign in to comment.
Something went wrong with that request. Please try again.