diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index c529b92240afcc103bb1d6e6f393bdb9d56f74e1..30a22fcc0e8ad799ac6cb87c4e01e17c588806e6 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -62,6 +62,13 @@ def assert_difference(expression, difference = 1, message = nil, &block) def assert_no_difference(expression, message = nil, &block) assert_difference expression, 0, message, &block end + + # Test if an expression is blank. Passes if object.blank? is true. + # + # assert_blank [] # => true + def assert_blank(object) + assert object.blank?, "#{object.inspect} is not blank" + end end end end diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 1928da51caf4e735f8372d0e7560ebc41880526e..390670570d46db9a5241fdd53f57f9d5e38522aa 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -86,6 +86,34 @@ def default_test; end end end +class EmptyTrue + def empty?() true; end +end + +class EmptyFalse + def empty?() false; end +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 } ] + + def test_assert_blank_true + BLANK.each { |v| assert_blank v } + end + + def test_assert_blank_false + NOT_BLANK.each { |v| + begin + assert_blank v + fail 'should not get to here' + rescue Exception => e + assert_match(/is not blank/, e.message) + end + } + end +end + # These should always pass if ActiveSupport::Testing.const_defined?(:Default) class NotTestingThingsTest < Test::Unit::TestCase