Skip to content

Commit

Permalink
Merge pull request #8769 from senny/deprecate_assert_blank
Browse files Browse the repository at this point in the history
deprecate `assert_blank` and `assert_present`.
  • Loading branch information
dhh committed Jan 5, 2013
2 parents 3d08614 + 947e1d5 commit cbabd1b
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 43 deletions.
2 changes: 1 addition & 1 deletion actionpack/test/controller/action_pack_assertions_test.rb
Expand Up @@ -259,7 +259,7 @@ def test_empty_flash
def test_flash_exist
process :flash_me
assert flash.any?
assert_present flash['hello']
assert flash['hello'].present?
end

def test_flash_does_not_exist
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/output_escaping_test.rb
Expand Up @@ -3,7 +3,7 @@
class OutputEscapingTest < ActiveSupport::TestCase

test "escape_html shouldn't die when passed nil" do
assert_blank ERB::Util.h(nil)
assert ERB::Util.h(nil).blank?
end

test "escapeHTML should escape strings" do
Expand Down
22 changes: 11 additions & 11 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -1221,27 +1221,27 @@ def test_overwritting_rendering_relative_file_with_extension

def test_head_created
post :head_created
assert_blank @response.body
assert @response.body.blank?
assert_response :created
end

def test_head_created_with_application_json_content_type
post :head_created_with_application_json_content_type
assert_blank @response.body
assert @response.body.blank?
assert_equal "application/json", @response.header["Content-Type"]
assert_response :created
end

def test_head_ok_with_image_png_content_type
post :head_ok_with_image_png_content_type
assert_blank @response.body
assert @response.body.blank?
assert_equal "image/png", @response.header["Content-Type"]
assert_response :ok
end

def test_head_with_location_header
get :head_with_location_header
assert_blank @response.body
assert @response.body.blank?
assert_equal "/foo", @response.headers["Location"]
assert_response :ok
end
Expand All @@ -1254,22 +1254,22 @@ def test_head_with_location_object
end

get :head_with_location_object
assert_blank @response.body
assert @response.body.blank?
assert_equal "http://www.nextangle.com/customers/1", @response.headers["Location"]
assert_response :ok
end
end

def test_head_with_custom_header
get :head_with_custom_header
assert_blank @response.body
assert @response.body.blank?
assert_equal "something", @response.headers["X-Custom-Header"]
assert_response :ok
end

def test_head_with_www_authenticate_header
get :head_with_www_authenticate_header
assert_blank @response.body
assert @response.body.blank?
assert_equal "something", @response.headers["WWW-Authenticate"]
assert_response :ok
end
Expand Down Expand Up @@ -1603,7 +1603,7 @@ def test_request_not_modified
@request.if_modified_since = @last_modified
get :conditional_hello
assert_equal 304, @response.status.to_i
assert_blank @response.body
assert @response.body.blank?
assert_equal @last_modified, @response.headers['Last-Modified']
end

Expand All @@ -1618,7 +1618,7 @@ def test_request_modified
@request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
get :conditional_hello
assert_equal 200, @response.status.to_i
assert_present @response.body
assert @response.body.present?
assert_equal @last_modified, @response.headers['Last-Modified']
end

Expand All @@ -1632,7 +1632,7 @@ def test_request_not_modified_with_record
@request.if_modified_since = @last_modified
get :conditional_hello_with_record
assert_equal 304, @response.status.to_i
assert_blank @response.body
assert @response.body.blank?
assert_equal @last_modified, @response.headers['Last-Modified']
end

Expand All @@ -1647,7 +1647,7 @@ def test_request_modified_with_record
@request.if_modified_since = 'Thu, 16 Jul 2008 00:00:00 GMT'
get :conditional_hello_with_record
assert_equal 200, @response.status.to_i
assert_present @response.body
assert @response.body.present?
assert_equal @last_modified, @response.headers['Last-Modified']
end

Expand Down
Expand Up @@ -320,7 +320,7 @@ def test_should_allow_all_methods_without_token

test 'should not emit a csrf-token meta tag' do
get :meta
assert_blank @response.body
assert @response.body.blank?
end
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/test_case_test.rb
Expand Up @@ -692,7 +692,7 @@ def test_params_reset_after_post_request
assert_equal "bar", @request.params[:foo]
@request.recycle!
post :no_op
assert_blank @request.params[:foo]
assert @request.params[:foo].blank?
end

def test_symbolized_path_params_reset_after_request
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/template/atom_feed_helper_test.rb
Expand Up @@ -238,7 +238,7 @@ def test_providing_builder_to_atom_feed
get :index, :id=>"provide_builder"
# because we pass in the non-default builder, the content generated by the
# helper should go 'nowhere'. Leaving the response body blank.
assert_blank @response.body
assert @response.body.blank?
end
end

Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -443,8 +443,8 @@ def test_add_to_self_referential_has_many_through

def test_has_many_through_uses_conditions_specified_on_the_has_many_association
author = Author.first
assert_present author.comments
assert_blank author.nonexistant_comments
assert author.comments.present?
assert author.nonexistant_comments.blank?
end

def test_has_many_through_uses_correct_attributes
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/deprecated_dynamic_methods_test.rb
Expand Up @@ -568,9 +568,9 @@ def test_dynamic_scope
end

def test_dynamic_scope_should_create_methods_after_hitting_method_missing
assert_blank @test_klass.methods.grep(/scoped_by_type/)
assert @test_klass.methods.grep(/scoped_by_type/).blank?
@test_klass.scoped_by_type(nil)
assert_present @test_klass.methods.grep(/scoped_by_type/)
assert @test_klass.methods.grep(/scoped_by_type/).present?
end

def test_dynamic_scope_with_less_number_of_arguments
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/relation_scoping_test.rb
Expand Up @@ -227,7 +227,7 @@ def test_nested_scoped_create
def test_nested_exclusive_scope_for_create
comment = Comment.create_with(:body => "Hey guys, nested scopes are broken. Please fix!").scoping do
Comment.unscoped.create_with(:post_id => 1).scoping do
assert_blank Comment.new.body
assert Comment.new.body.blank?
Comment.create :body => "Hey guys"
end
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -509,7 +509,7 @@ def test_find_ids

def test_find_in_empty_array
authors = Author.all.where(:id => [])
assert_blank authors.to_a
assert authors.to_a.blank?
end

def test_where_with_ar_object
Expand Down Expand Up @@ -723,7 +723,7 @@ def test_relation_merging_with_eager_load

def test_relation_merging_with_locks
devs = Developer.lock.where("salary >= 80000").order("id DESC").merge(Developer.limit(2))
assert_present devs.locked
assert devs.locked.present?
end

def test_relation_merging_with_preload
Expand Down
5 changes: 5 additions & 0 deletions activesupport/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##

* Deprecate `assert_present` and `assert_blank` in favor of
`assert object.blank?` and `assert object.present?`

*Yves Senn*

* Change `String#to_date` to use `Date.parse`. This gives more consistent error
messages and allows the use of partial dates.

Expand Down
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/testing/assertions.rb
Expand Up @@ -103,6 +103,7 @@ def assert_no_difference(expression, message = nil, &block)
#
# assert_blank [], 'this should be blank'
def assert_blank(object, message=nil)
ActiveSupport::Deprecation.warn('"assert_blank" is deprecated. Please use "assert object.blank?" instead')
message ||= "#{object.inspect} is not blank"
assert object.blank?, message
end
Expand All @@ -117,6 +118,7 @@ def assert_blank(object, message=nil)
#
# assert_present({ data: 'x' }, 'this should not be blank')
def assert_present(object, message=nil)
ActiveSupport::Deprecation.warn('"assert_present" is deprecated. Please use "assert object.present?" instead')
message ||= "#{object.inspect} is blank"
assert object.present?, message
end
Expand Down
6 changes: 3 additions & 3 deletions activesupport/test/caching_test.rb
Expand Up @@ -675,7 +675,7 @@ def test_delete_matched_when_cache_directory_does_not_exist
def test_log_exception_when_cache_read_fails
File.expects(:exist?).raises(StandardError, "failed")
@cache.send(:read_entry, "winston", {})
assert_present @buffer.string
assert @buffer.string.present?
end
end

Expand Down Expand Up @@ -897,12 +897,12 @@ def setup

def test_logging
@cache.fetch('foo') { 'bar' }
assert_present @buffer.string
assert @buffer.string.present?
end

def test_mute_logging
@cache.mute { @cache.fetch('foo') { 'bar' } }
assert_blank @buffer.string
assert @buffer.string.blank?
end
end

Expand Down
7 changes: 4 additions & 3 deletions activesupport/test/deprecation_test.rb
Expand Up @@ -119,9 +119,10 @@ def test_default_silence_behavior
ActiveSupport::Deprecation.behavior = :silence
behavior = ActiveSupport::Deprecation.behavior.first

assert_blank capture(:stderr) {
stderr_output = capture(:stderr) {
assert_nil behavior.call('Some error!', ['call stack!'])
}
assert stderr_output.blank?
end

def test_deprecated_instance_variable_proxy
Expand Down Expand Up @@ -254,10 +255,10 @@ def test_deprecated_constant_with_deprecator_given
klass::OLD.to_s
end
end

def test_deprecated_instance_variable_with_instance_deprecator
deprecator = deprecator_with_messages

klass = Class.new() do
def initialize(deprecator)
@request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
Expand Down
36 changes: 22 additions & 14 deletions activesupport/test/test_test.rb
Expand Up @@ -92,17 +92,21 @@ class AssertBlankTest < ActiveSupport::TestCase
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]

def test_assert_blank_true
BLANK.each { |v| assert_blank v }
BLANK.each { |value|
assert_deprecated { assert_blank value }
}
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
assert_deprecated {
begin
assert_blank v
fail 'should not get to here'
rescue Exception => e
assert_match(/is not blank/, e.message)
end
}
}
end
end
Expand All @@ -112,17 +116,21 @@ class AssertPresentTest < ActiveSupport::TestCase
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]

def test_assert_present_true
NOT_BLANK.each { |v| assert_present v }
NOT_BLANK.each { |v|
assert_deprecated { assert_present v }
}
end

def test_assert_present_false
BLANK.each { |v|
begin
assert_present v
fail 'should not get to here'
rescue Exception => e
assert_match(/is blank/, e.message)
end
assert_deprecated {
begin
assert_present v
fail 'should not get to here'
rescue Exception => e
assert_match(/is blank/, e.message)
end
}
}
end
end
Expand Down
1 change: 1 addition & 0 deletions guides/source/4_0_release_notes.md
Expand Up @@ -142,6 +142,7 @@ Please refer to the [Changelog](https://github.com/rails/rails/blob/master/activ

* BufferedLogger is deprecated. Use ActiveSupport::Logger, or the logger from Ruby stdlib.

* Deprecate `assert_present` and `assert_blank` in favor of `assert object.blank?` and `assert object.present?`

Action Pack
-----------
Expand Down

0 comments on commit cbabd1b

Please sign in to comment.