Skip to content
This repository
Browse code

code gardening: we have assert_(nil|blank|present), more concise, wit…

…h better default failure messages - let's use them
  • Loading branch information...
commit fb6b80562041e8d2378cad1b51f8c234fe76fd5e 1 parent d14e298
Xavier Noria authored August 17, 2010
2  actionpack/test/controller/output_escaping_test.rb
@@ -3,7 +3,7 @@
3 3
 class OutputEscapingTest < ActiveSupport::TestCase
4 4
 
5 5
   test "escape_html shouldn't die when passed nil" do
6  
-    assert ERB::Util.h(nil).blank?
  6
+    assert_blank ERB::Util.h(nil)
7 7
   end
8 8
 
9 9
   test "escapeHTML should escape strings" do
12  actionpack/test/controller/render_test.rb
@@ -1113,7 +1113,7 @@ def test_overwritting_rendering_relative_file_with_extension
1113 1113
 
1114 1114
   def test_head_with_location_header
1115 1115
     get :head_with_location_header
1116  
-    assert @response.body.blank?
  1116
+    assert_blank @response.body
1117 1117
     assert_equal "/foo", @response.headers["Location"]
1118 1118
     assert_response :ok
1119 1119
   end
@@ -1126,7 +1126,7 @@ def test_head_with_location_object
1126 1126
       end
1127 1127
 
1128 1128
       get :head_with_location_object
1129  
-      assert @response.body.blank?
  1129
+      assert_blank @response.body
1130 1130
       assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
1131 1131
       assert_response :ok
1132 1132
     end
@@ -1134,7 +1134,7 @@ def test_head_with_location_object
1134 1134
 
1135 1135
   def test_head_with_custom_header
1136 1136
     get :head_with_custom_header
1137  
-    assert @response.body.blank?
  1137
+    assert_blank @response.body
1138 1138
     assert_equal "something", @response.headers["X-Custom-Header"]
1139 1139
     assert_response :ok
1140 1140
   end
@@ -1414,7 +1414,7 @@ def test_render_should_not_set_etag_when_last_modified_has_been_specified
1414 1414
     assert_equal 200, @response.status.to_i
1415 1415
     assert_not_nil @response.last_modified
1416 1416
     assert_nil @response.etag
1417  
-    assert @response.body.present?
  1417
+    assert_present @response.body
1418 1418
   end
1419 1419
 
1420 1420
   def test_render_with_etag
@@ -1499,7 +1499,7 @@ def test_request_not_modified
1499 1499
     @request.if_modified_since = @last_modified
1500 1500
     get :conditional_hello
1501 1501
     assert_equal 304, @response.status.to_i
1502  
-    assert @response.body.blank?, @response.body
  1502
+    assert_blank @response.body
1503 1503
     assert_equal @last_modified, @response.headers['Last-Modified']
1504 1504
   end
1505 1505
 
@@ -1514,7 +1514,7 @@ def test_request_modified
1514 1514
     @request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
1515 1515
     get :conditional_hello
1516 1516
     assert_equal 200, @response.status.to_i
1517  
-    assert !@response.body.blank?
  1517
+    assert_present @response.body
1518 1518
     assert_equal @last_modified, @response.headers['Last-Modified']
1519 1519
   end
1520 1520
 
2  actionpack/test/controller/request_forgery_protection_test.rb
@@ -251,7 +251,7 @@ def test_should_allow_all_methods_without_token
251 251
 
252 252
   test 'should not emit a csrf-token meta tag' do
253 253
     get :meta
254  
-    assert @response.body.blank?
  254
+    assert_blank @response.body
255 255
   end
256 256
 end
257 257
 
2  actionpack/test/controller/test_test.rb
@@ -546,7 +546,7 @@ def test_params_reset_after_post_request
546 546
     assert_equal "bar", @request.params[:foo]
547 547
     @request.recycle!
548 548
     post :no_op
549  
-    assert @request.params[:foo].blank?
  549
+    assert_blank @request.params[:foo]
550 550
   end
551 551
 
552 552
   def test_should_have_knowledge_of_client_side_cookie_state_even_if_they_are_not_set
4  actionpack/test/controller/webservice_test.rb
@@ -29,7 +29,7 @@ def setup
29 29
   def test_check_parameters
30 30
     with_test_route_set do
31 31
       get "/"
32  
-      assert @controller.response.body.blank?
  32
+      assert_blank @controller.response.body
33 33
     end
34 34
   end
35 35
 
@@ -161,7 +161,7 @@ def test_register_and_use_xml_simple
161 161
   def test_use_xml_ximple_with_empty_request
162 162
     with_test_route_set do
163 163
       assert_nothing_raised { post "/", "", {'CONTENT_TYPE' => 'application/xml'} }
164  
-      assert @controller.response.body.blank?
  164
+      assert_blank @controller.response.body
165 165
     end
166 166
   end
167 167
 
2  actionpack/test/template/atom_feed_helper_test.rb
@@ -226,7 +226,7 @@ def test_providing_builder_to_atom_feed
226 226
       get :index, :id=>"provide_builder"
227 227
       # because we pass in the non-default builder, the content generated by the
228 228
       # helper should go 'nowhere'.  Leaving the response body blank.
229  
-      assert @response.body.blank?
  229
+      assert_blank @response.body
230 230
     end
231 231
   end
232 232
 
6  activemodel/test/cases/mass_assignment_security_test.rb
@@ -25,13 +25,13 @@ def test_attributes_protected_by_default
25 25
   end
26 26
 
27 27
   def test_mass_assignment_protection_inheritance
28  
-    assert LoosePerson.accessible_attributes.blank?
  28
+    assert_blank LoosePerson.accessible_attributes
29 29
     assert_equal Set.new([ 'credit_rating', 'administrator']), LoosePerson.protected_attributes
30 30
 
31  
-    assert LooseDescendant.accessible_attributes.blank?
  31
+    assert_blank LooseDescendant.accessible_attributes
32 32
     assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number']), LooseDescendant.protected_attributes
33 33
 
34  
-    assert LooseDescendantSecond.accessible_attributes.blank?
  34
+    assert_blank LooseDescendantSecond.accessible_attributes
35 35
     assert_equal Set.new([ 'credit_rating', 'administrator', 'phone_number', 'name']), LooseDescendantSecond.protected_attributes,
36 36
       'Running attr_protected twice in one class should merge the protections'
37 37
 
2  activerecord/test/cases/aggregations_test.rb
@@ -99,7 +99,7 @@ def test_allow_nil_address_loaded_when_only_some_attributes_are_nil
99 99
     customers(:zaphod).save
100 100
     customers(:zaphod).reload
101 101
     assert_kind_of Address, customers(:zaphod).address
102  
-    assert customers(:zaphod).address.street.nil?
  102
+    assert_nil customers(:zaphod).address.street
103 103
   end
104 104
 
105 105
   def test_nil_assignment_results_in_nil
2  activerecord/test/cases/associations/belongs_to_associations_test.rb
@@ -22,7 +22,7 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
22 22
   def test_belongs_to
23 23
     Client.find(3).firm.name
24 24
     assert_equal companies(:first_firm).name, Client.find(3).firm.name
25  
-    assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
  25
+    assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
26 26
   end
27 27
 
28 28
   def test_belongs_to_with_primary_key
4  activerecord/test/cases/associations/has_many_associations_test.rb
@@ -633,7 +633,7 @@ def test_clearing_an_association_collection
633 633
 
634 634
     # Should not be destroyed since the association is not dependent.
635 635
     assert_nothing_raised do
636  
-      assert Client.find(client_id).firm.nil?
  636
+      assert_nil Client.find(client_id).firm
637 637
     end
638 638
   end
639 639
 
@@ -658,7 +658,7 @@ def test_clearing_a_dependent_association_collection
658 658
     assert_equal [client_id], Client.destroyed_client_ids[firm.id]
659 659
 
660 660
     # Should be destroyed since the association is dependent.
661  
-    assert Client.find_by_id(client_id).nil?
  661
+    assert_nil Client.find_by_id(client_id)
662 662
   end
663 663
 
664 664
   def test_clearing_an_exclusively_dependent_association_collection
2  activerecord/test/cases/associations/has_one_associations_test.rb
@@ -253,7 +253,7 @@ def test_dependence_with_missing_association
253 253
   def test_dependence_with_missing_association_and_nullify
254 254
     Account.destroy_all
255 255
     firm = DependentFirm.find(:first)
256  
-    assert firm.account.nil?
  256
+    assert_nil firm.account
257 257
     firm.destroy
258 258
   end
259 259
 
4  activerecord/test/cases/associations/join_model_test.rb
@@ -427,8 +427,8 @@ def test_add_to_self_referential_has_many_through
427 427
 
428 428
   def test_has_many_through_uses_conditions_specified_on_the_has_many_association
429 429
     author = Author.find(:first)
430  
-    assert !author.comments.blank?
431  
-    assert author.nonexistant_comments.blank?
  430
+    assert_present author.comments
  431
+    assert_blank author.nonexistant_comments
432 432
   end
433 433
 
434 434
   def test_has_many_through_uses_correct_attributes
2  activerecord/test/cases/method_scoping_test.rb
@@ -449,7 +449,7 @@ def test_nested_exclusive_scope_for_create
449 449
     Comment.send(:with_scope, :create => { :body => "Hey guys, nested scopes are broken. Please fix!" }) do
450 450
       Comment.send(:with_exclusive_scope, :create => { :post_id => 1 }) do
451 451
         assert_equal({:post_id => 1}, Comment.scoped.send(:scope_for_create))
452  
-        assert Comment.new.body.blank?
  452
+        assert_blank Comment.new.body
453 453
         comment = Comment.create :body => "Hey guys"
454 454
       end
455 455
     end
4  activerecord/test/cases/named_scope_test.rb
@@ -480,8 +480,8 @@ def test_dynamic_scope
480 480
   end
481 481
 
482 482
   def test_dynamic_scope_should_create_methods_after_hitting_method_missing
483  
-    assert Developer.methods.grep(/scoped_by_created_at/).blank?
  483
+    assert_blank Developer.methods.grep(/scoped_by_created_at/)
484 484
     Developer.scoped_by_created_at(nil)
485  
-    assert Developer.methods.grep(/scoped_by_created_at/).present?
  485
+    assert_present Developer.methods.grep(/scoped_by_created_at/)
486 486
   end
487 487
 end
2  activerecord/test/cases/primary_keys_test.rb
@@ -11,7 +11,7 @@ class PrimaryKeysTest < ActiveRecord::TestCase
11 11
 
12 12
   def test_to_key_with_default_primary_key
13 13
     topic = Topic.new
14  
-    assert topic.to_key.nil?
  14
+    assert_nil topic.to_key
15 15
     topic = Topic.find(1)
16 16
     assert_equal [1], topic.to_key
17 17
   end
2  activerecord/test/cases/relation_scoping_test.rb
@@ -209,7 +209,7 @@ def test_nested_scoped_create
209 209
   def test_nested_exclusive_scope_for_create
210 210
     comment = Comment.create_with(:body => "Hey guys, nested scopes are broken. Please fix!").scoping do
211 211
       Comment.unscoped.create_with(:post_id => 1).scoping do
212  
-        assert Comment.new.body.blank?
  212
+        assert_blank Comment.new.body
213 213
         Comment.create :body => "Hey guys"
214 214
       end
215 215
     end
4  activerecord/test/cases/relations_test.rb
@@ -411,7 +411,7 @@ def test_find_ids
411 411
 
412 412
   def test_find_in_empty_array
413 413
     authors = Author.scoped.where(:id => [])
414  
-    assert authors.all.blank?
  414
+    assert_blank authors.all
415 415
   end
416 416
 
417 417
   def test_exists
@@ -486,7 +486,7 @@ def test_relation_merging_with_eager_load
486 486
 
487 487
   def test_relation_merging_with_locks
488 488
     devs = Developer.lock.where("salary >= 80000").order("id DESC") & Developer.limit(2)
489  
-    assert devs.locked.present?
  489
+    assert_present devs.locked
490 490
   end
491 491
 
492 492
   def test_relation_merging_with_preload
10  activesupport/lib/active_support/testing/assertions.rb
@@ -67,15 +67,17 @@ def assert_no_difference(expression, message = nil, &block)
67 67
       # Test if an expression is blank. Passes if object.blank? is true.
68 68
       #
69 69
       #   assert_blank [] # => true
70  
-      def assert_blank(object)
71  
-        assert object.blank?, "#{object.inspect} is not blank"
  70
+      def assert_blank(object, message=nil)
  71
+        message ||= "#{object.inspect} is not blank"
  72
+        assert object.blank?, message
72 73
       end
73 74
 
74 75
       # Test if an expression is not blank. Passes if object.present? is true.
75 76
       #
76 77
       #   assert_present {:data => 'x' } # => true
77  
-      def assert_present(object)
78  
-        assert object.present?, "#{object.inspect} is blank"
  78
+      def assert_present(object, message=nil)
  79
+        message ||= "#{object.inspect} is blank"
  80
+        assert object.present?, message
79 81
       end
80 82
     end
81 83
   end
4  activesupport/test/caching_test.rb
@@ -649,12 +649,12 @@ def setup
649 649
 
650 650
   def test_logging
651 651
     @cache.fetch('foo') { 'bar' }
652  
-    assert @buffer.string.present?
  652
+    assert_present @buffer.string
653 653
   end
654 654
 
655 655
   def test_mute_logging
656 656
     @cache.mute { @cache.fetch('foo') { 'bar' } }
657  
-    assert @buffer.string.blank?
  657
+    assert_blank @buffer.string
658 658
   end
659 659
 end
660 660
 

0 notes on commit fb6b805

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