From 1894edc24eb0816c3607d0fc5d0e14c22ca8bbec Mon Sep 17 00:00:00 2001 From: Juanjo Bazan Date: Tue, 30 Mar 2010 23:18:44 +0200 Subject: [PATCH 1/2] new assertion: assert_blank --- .../lib/active_support/testing/assertions.rb | 7 +++++ activesupport/test/test_test.rb | 28 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index c529b92..30a22fc 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -62,6 +62,13 @@ module ActiveSupport 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 1928da5..3906705 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -86,6 +86,34 @@ class AssertDifferenceTest < ActiveSupport::TestCase 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 -- 1.6.5.2 From 14b67862a83aae37c9ae96bb47b76fd8d4058cf9 Mon Sep 17 00:00:00 2001 From: Juanjo Bazan Date: Tue, 30 Mar 2010 23:39:00 +0200 Subject: [PATCH 2/2] New assertion: assert_present --- .../lib/active_support/testing/assertions.rb | 7 ++++++ activesupport/test/test_test.rb | 22 +++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/testing/assertions.rb b/activesupport/lib/active_support/testing/assertions.rb index 30a22fc..e01aa80 100644 --- a/activesupport/lib/active_support/testing/assertions.rb +++ b/activesupport/lib/active_support/testing/assertions.rb @@ -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 diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 3906705..cc7960b 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -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 -- 1.6.5.2