Skip to content
This repository
Browse code

New assertion: assert_present [#4299 state:committed]

Signed-off-by: Xavier Noria <fxn@hashref.com>
  • Loading branch information...
commit 589deb39c79cac40eec40d4ee3d86fac16c1f31f 1 parent 7212c29
Juanjo Bazán authored fxn committed
7  activesupport/lib/active_support/testing/assertions.rb
@@ -69,6 +69,13 @@ def assert_no_difference(expression, message = nil, &block)
69 69
       def assert_blank(object)
70 70
         assert object.blank?, "#{object.inspect} is not blank"
71 71
       end
  72
+      
  73
+      # Test if an expression is not blank. Passes if object.present? is true.
  74
+      #
  75
+      #   assert_present {:data => 'x' } # => true
  76
+      def assert_present(object)
  77
+        assert object.present?, "#{object.inspect} is blank"
  78
+      end
72 79
     end
73 80
   end
74 81
 end
22  activesupport/test/test_test.rb
@@ -96,7 +96,7 @@ def empty?() false; end
96 96
 
97 97
 class AssertBlankTest < ActiveSupport::TestCase
98 98
   BLANK = [ EmptyTrue.new, nil, false, '', '   ', "  \n\t  \r ", [], {} ]
99  
-  NOT_BLANK   = [ EmptyFalse.new, Object.new, true, 0, 1, 'j', [nil], { nil => 0 } ]
  99
+  NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]
100 100
   
101 101
   def test_assert_blank_true
102 102
     BLANK.each { |v| assert_blank v }
@@ -114,6 +114,26 @@ def test_assert_blank_false
114 114
   end
115 115
 end
116 116
 
  117
+class AssertPresentTest < ActiveSupport::TestCase
  118
+  BLANK = [ EmptyTrue.new, nil, false, '', '   ', "  \n\t  \r ", [], {} ]
  119
+  NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]
  120
+  
  121
+  def test_assert_blank_true
  122
+    NOT_BLANK.each { |v| assert_present v }
  123
+  end
  124
+  
  125
+  def test_assert_blank_false
  126
+    BLANK.each { |v|
  127
+      begin 
  128
+        assert_present v
  129
+        fail 'should not get to here'
  130
+      rescue Exception => e
  131
+        assert_match(/is blank/, e.message) 
  132
+      end  
  133
+    }
  134
+  end
  135
+end
  136
+
117 137
 # These should always pass
118 138
 if ActiveSupport::Testing.const_defined?(:Default)
119 139
   class NotTestingThingsTest < Test::Unit::TestCase

2 notes on commit 589deb3

Kieran Pilkington

This, along with the other assert_blank needs active_support/core_ext/object/blank require please. People need to remember these else it leads to patchy support for pulling in only what you need,

Xavier Noria
Owner
fxn commented on 589deb3

Added now, thank you Kieran!

Please sign in to comment.
Something went wrong with that request. Please try again.