From d52b4a664538943b8042cd2bc468983b72fde508 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 3 Sep 2006 20:27:48 +0000 Subject: Moved in deprecated assertions and remove duplicated requires git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4935 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/assertions.rb | 2 + .../assertions/deprecated_assertions.rb | 202 ++++++++++++++++++++ .../action_controller/assertions/dom_assertions.rb | 3 - .../assertions/model_assertions.rb | 3 - .../assertions/response_assertions.rb | 2 - .../assertions/routing_assertions.rb | 3 - .../assertions/selector_assertions.rb | 3 - .../action_controller/assertions/tag_assertions.rb | 2 - .../lib/action_controller/deprecated_assertions.rb | 204 --------------------- actionpack/lib/action_controller/test_process.rb | 7 +- 10 files changed, 205 insertions(+), 226 deletions(-) create mode 100644 actionpack/lib/action_controller/assertions/deprecated_assertions.rb delete mode 100644 actionpack/lib/action_controller/deprecated_assertions.rb (limited to 'actionpack') diff --git a/actionpack/lib/action_controller/assertions.rb b/actionpack/lib/action_controller/assertions.rb index b0297b6c01..4eab8f9ffd 100644 --- a/actionpack/lib/action_controller/assertions.rb +++ b/actionpack/lib/action_controller/assertions.rb @@ -53,6 +53,7 @@ require File.dirname(__FILE__) + '/assertions/tag_assertions' require File.dirname(__FILE__) + '/assertions/dom_assertions' require File.dirname(__FILE__) + '/assertions/routing_assertions' require File.dirname(__FILE__) + '/assertions/model_assertions' +require File.dirname(__FILE__) + '/assertions/deprecated_assertions' module Test #:nodoc: module Unit #:nodoc: @@ -63,6 +64,7 @@ module Test #:nodoc: include ActionController::Assertions::TagAssertions include ActionController::Assertions::DomAssertions include ActionController::Assertions::ModelAssertions + include ActionController::Assertions::DeprecatedAssertions def clean_backtrace(&block) yield diff --git a/actionpack/lib/action_controller/assertions/deprecated_assertions.rb b/actionpack/lib/action_controller/assertions/deprecated_assertions.rb new file mode 100644 index 0000000000..01262bdf08 --- /dev/null +++ b/actionpack/lib/action_controller/assertions/deprecated_assertions.rb @@ -0,0 +1,202 @@ +require 'rexml/document' + +module ActionController #:nodoc: + module Assertions #:nodoc: + module DeprecatedAssertions + def assert_success(message=nil) #:nodoc: + assert_response(:success, message) + end + + def assert_redirect(message=nil) #:nodoc: + assert_response(:redirect, message) + end + + def assert_rendered_file(expected=nil, message=nil) #:nodoc: + assert_template(expected, message) + end + + # ensure that the session has an object with the specified name + def assert_session_has(key=nil, message=nil) #:nodoc: + msg = build_message(message, " is not in the session ", key, @response.session) + assert_block(msg) { @response.has_session_object?(key) } + end + + # ensure that the session has no object with the specified name + def assert_session_has_no(key=nil, message=nil) #:nodoc: + msg = build_message(message, " is in the session ", key, @response.session) + assert_block(msg) { !@response.has_session_object?(key) } + end + + def assert_session_equal(expected = nil, key = nil, message = nil) #:nodoc: + msg = build_message(message, " expected in session['?'] but was ", expected, key, @response.session[key]) + assert_block(msg) { expected == @response.session[key] } + end + + # -- cookie assertions --------------------------------------------------- + + def assert_no_cookie(key = nil, message = nil) #:nodoc: + actual = @response.cookies[key] + msg = build_message(message, " not expected in cookies['?']", actual, key) + assert_block(msg) { actual.nil? or actual.empty? } + end + + def assert_cookie_equal(expected = nil, key = nil, message = nil) #:nodoc: + actual = @response.cookies[key] + actual = actual.first if actual + msg = build_message(message, " expected in cookies['?'] but was ", expected, key, actual) + assert_block(msg) { expected == actual } + end + + # -- flash assertions --------------------------------------------------- + + # ensure that the flash has an object with the specified name + def assert_flash_has(key=nil, message=nil) #:nodoc: + msg = build_message(message, " is not in the flash ", key, @response.flash) + assert_block(msg) { @response.has_flash_object?(key) } + end + + # ensure that the flash has no object with the specified name + def assert_flash_has_no(key=nil, message=nil) #:nodoc: + msg = build_message(message, " is in the flash ", key, @response.flash) + assert_block(msg) { !@response.has_flash_object?(key) } + end + + # ensure the flash exists + def assert_flash_exists(message=nil) #:nodoc: + msg = build_message(message, "the flash does not exist ", @response.session['flash'] ) + assert_block(msg) { @response.has_flash? } + end + + # ensure the flash does not exist + def assert_flash_not_exists(message=nil) #:nodoc: + msg = build_message(message, "the flash exists ", @response.flash) + assert_block(msg) { !@response.has_flash? } + end + + # ensure the flash is empty but existent + def assert_flash_empty(message=nil) #:nodoc: + msg = build_message(message, "the flash is not empty ", @response.flash) + assert_block(msg) { !@response.has_flash_with_contents? } + end + + # ensure the flash is not empty + def assert_flash_not_empty(message=nil) #:nodoc: + msg = build_message(message, "the flash is empty") + assert_block(msg) { @response.has_flash_with_contents? } + end + + def assert_flash_equal(expected = nil, key = nil, message = nil) #:nodoc: + msg = build_message(message, " expected in flash['?'] but was ", expected, key, @response.flash[key]) + assert_block(msg) { expected == @response.flash[key] } + end + + + # ensure our redirection url is an exact match + def assert_redirect_url(url=nil, message=nil) #:nodoc: + assert_redirect(message) + msg = build_message(message, " is not the redirected location ", url, @response.redirect_url) + assert_block(msg) { @response.redirect_url == url } + end + + # ensure our redirection url matches a pattern + def assert_redirect_url_match(pattern=nil, message=nil) #:nodoc: + assert_redirect(message) + msg = build_message(message, " was not found in the location: ", pattern, @response.redirect_url) + assert_block(msg) { @response.redirect_url_match?(pattern) } + end + + + # -- template assertions ------------------------------------------------ + + # ensure that a template object with the given name exists + def assert_template_has(key=nil, message=nil) #:nodoc: + msg = build_message(message, " is not a template object", key ) + assert_block(msg) { @response.has_template_object?(key) } + end + + # ensure that a template object with the given name does not exist + def assert_template_has_no(key=nil,message=nil) #:nodoc: + msg = build_message(message, " is a template object ", key, @response.template_objects[key]) + assert_block(msg) { !@response.has_template_object?(key) } + end + + # ensures that the object assigned to the template on +key+ is equal to +expected+ object. + def assert_template_equal(expected = nil, key = nil, message = nil) #:nodoc: + msg = build_message(message, " expected in assigns['?'] but was ", expected, key, @response.template.assigns[key.to_s]) + assert_block(msg) { expected == @response.template.assigns[key.to_s] } + end + alias_method :assert_assigned_equal, :assert_template_equal + + # Asserts that the template returns the +expected+ string or array based on the XPath +expression+. + # This will only work if the template rendered a valid XML document. + def assert_template_xpath_match(expression=nil, expected=nil, message=nil) #:nodoc: + xml, matches = REXML::Document.new(@response.body), [] + xml.elements.each(expression) { |e| matches << e.text } + if matches.empty? then + msg = build_message(message, " not found in document", expression) + flunk(msg) + return + elsif matches.length < 2 then + matches = matches.first + end + + msg = build_message(message, " found , not ", expression, matches, expected) + assert_block(msg) { matches == expected } + end + + # Assert the template object with the given name is an Active Record descendant and is valid. + def assert_valid_record(key = nil, message = nil) #:nodoc: + record = find_record_in_template(key) + msg = build_message(message, "Active Record is invalid )", record.errors.full_messages) + assert_block(msg) { record.valid? } + end + + # Assert the template object with the given name is an Active Record descendant and is invalid. + def assert_invalid_record(key = nil, message = nil) #:nodoc: + record = find_record_in_template(key) + msg = build_message(message, "Active Record is valid)") + assert_block(msg) { !record.valid? } + end + + # Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid. + def assert_valid_column_on_record(key = nil, columns = "", message = nil) #:nodoc: + record = find_record_in_template(key) + record.send(:validate) + + cols = glue_columns(columns) + cols.delete_if { |col| !record.errors.invalid?(col) } + msg = build_message(message, "Active Record has invalid columns )", cols.join(",") ) + assert_block(msg) { cols.empty? } + end + + # Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid. + def assert_invalid_column_on_record(key = nil, columns = "", message = nil) #:nodoc: + record = find_record_in_template(key) + record.send(:validate) + + cols = glue_columns(columns) + cols.delete_if { |col| record.errors.invalid?(col) } + msg = build_message(message, "Active Record has valid columns )", cols.join(",") ) + assert_block(msg) { cols.empty? } + end + + private + def glue_columns(columns) + cols = [] + cols << columns if columns.class == String + cols += columns if columns.class == Array + cols + end + + def find_record_in_template(key = nil) + assert_template_has(key) + record = @response.template_objects[key] + + assert_not_nil(record) + assert_kind_of ActiveRecord::Base, record + + return record + end + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/assertions/dom_assertions.rb b/actionpack/lib/action_controller/assertions/dom_assertions.rb index a765dff2cc..d1eea59ea0 100644 --- a/actionpack/lib/action_controller/assertions/dom_assertions.rb +++ b/actionpack/lib/action_controller/assertions/dom_assertions.rb @@ -1,6 +1,3 @@ -require 'test/unit' -require 'test/unit/assertions' - module ActionController module Assertions module DomAssertions diff --git a/actionpack/lib/action_controller/assertions/model_assertions.rb b/actionpack/lib/action_controller/assertions/model_assertions.rb index b807a8902f..a9bcd7db9a 100644 --- a/actionpack/lib/action_controller/assertions/model_assertions.rb +++ b/actionpack/lib/action_controller/assertions/model_assertions.rb @@ -1,6 +1,3 @@ -require 'test/unit' -require 'test/unit/assertions' - module ActionController module Assertions module ModelAssertions diff --git a/actionpack/lib/action_controller/assertions/response_assertions.rb b/actionpack/lib/action_controller/assertions/response_assertions.rb index 5d5effc493..a3834e3a3e 100644 --- a/actionpack/lib/action_controller/assertions/response_assertions.rb +++ b/actionpack/lib/action_controller/assertions/response_assertions.rb @@ -1,5 +1,3 @@ -require 'test/unit' -require 'test/unit/assertions' require 'rexml/document' require File.dirname(__FILE__) + "/../vendor/html-scanner/html/document" diff --git a/actionpack/lib/action_controller/assertions/routing_assertions.rb b/actionpack/lib/action_controller/assertions/routing_assertions.rb index a94bb719f2..2ecf1b9e8e 100644 --- a/actionpack/lib/action_controller/assertions/routing_assertions.rb +++ b/actionpack/lib/action_controller/assertions/routing_assertions.rb @@ -1,6 +1,3 @@ -require 'test/unit' -require 'test/unit/assertions' - module ActionController module Assertions module RoutingAssertions diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/assertions/selector_assertions.rb index 7e22d6b655..7984e14c67 100644 --- a/actionpack/lib/action_controller/assertions/selector_assertions.rb +++ b/actionpack/lib/action_controller/assertions/selector_assertions.rb @@ -3,12 +3,9 @@ # Under MIT and/or CC By license. #++ -require 'test/unit' -require 'test/unit/assertions' require 'rexml/document' require File.dirname(__FILE__) + "/../vendor/html-scanner/html/document" - module ActionController module Assertions # Adds the #assert_select method for use in Rails functional diff --git a/actionpack/lib/action_controller/assertions/tag_assertions.rb b/actionpack/lib/action_controller/assertions/tag_assertions.rb index dc80b442b7..f5f7a23e61 100644 --- a/actionpack/lib/action_controller/assertions/tag_assertions.rb +++ b/actionpack/lib/action_controller/assertions/tag_assertions.rb @@ -1,5 +1,3 @@ -require 'test/unit' -require 'test/unit/assertions' require 'rexml/document' require File.dirname(__FILE__) + "/../vendor/html-scanner/html/document" diff --git a/actionpack/lib/action_controller/deprecated_assertions.rb b/actionpack/lib/action_controller/deprecated_assertions.rb deleted file mode 100644 index dd94f4cee8..0000000000 --- a/actionpack/lib/action_controller/deprecated_assertions.rb +++ /dev/null @@ -1,204 +0,0 @@ -require 'test/unit' -require 'test/unit/assertions' -require 'rexml/document' - -module Test #:nodoc: - module Unit #:nodoc: - module Assertions - def assert_success(message=nil) #:nodoc: - assert_response(:success, message) - end - - def assert_redirect(message=nil) #:nodoc: - assert_response(:redirect, message) - end - - def assert_rendered_file(expected=nil, message=nil) #:nodoc: - assert_template(expected, message) - end - - # ensure that the session has an object with the specified name - def assert_session_has(key=nil, message=nil) #:nodoc: - msg = build_message(message, " is not in the session ", key, @response.session) - assert_block(msg) { @response.has_session_object?(key) } - end - - # ensure that the session has no object with the specified name - def assert_session_has_no(key=nil, message=nil) #:nodoc: - msg = build_message(message, " is in the session ", key, @response.session) - assert_block(msg) { !@response.has_session_object?(key) } - end - - def assert_session_equal(expected = nil, key = nil, message = nil) #:nodoc: - msg = build_message(message, " expected in session['?'] but was ", expected, key, @response.session[key]) - assert_block(msg) { expected == @response.session[key] } - end - - # -- cookie assertions --------------------------------------------------- - - def assert_no_cookie(key = nil, message = nil) #:nodoc: - actual = @response.cookies[key] - msg = build_message(message, " not expected in cookies['?']", actual, key) - assert_block(msg) { actual.nil? or actual.empty? } - end - - def assert_cookie_equal(expected = nil, key = nil, message = nil) #:nodoc: - actual = @response.cookies[key] - actual = actual.first if actual - msg = build_message(message, " expected in cookies['?'] but was ", expected, key, actual) - assert_block(msg) { expected == actual } - end - - # -- flash assertions --------------------------------------------------- - - # ensure that the flash has an object with the specified name - def assert_flash_has(key=nil, message=nil) #:nodoc: - msg = build_message(message, " is not in the flash ", key, @response.flash) - assert_block(msg) { @response.has_flash_object?(key) } - end - - # ensure that the flash has no object with the specified name - def assert_flash_has_no(key=nil, message=nil) #:nodoc: - msg = build_message(message, " is in the flash ", key, @response.flash) - assert_block(msg) { !@response.has_flash_object?(key) } - end - - # ensure the flash exists - def assert_flash_exists(message=nil) #:nodoc: - msg = build_message(message, "the flash does not exist ", @response.session['flash'] ) - assert_block(msg) { @response.has_flash? } - end - - # ensure the flash does not exist - def assert_flash_not_exists(message=nil) #:nodoc: - msg = build_message(message, "the flash exists ", @response.flash) - assert_block(msg) { !@response.has_flash? } - end - - # ensure the flash is empty but existent - def assert_flash_empty(message=nil) #:nodoc: - msg = build_message(message, "the flash is not empty ", @response.flash) - assert_block(msg) { !@response.has_flash_with_contents? } - end - - # ensure the flash is not empty - def assert_flash_not_empty(message=nil) #:nodoc: - msg = build_message(message, "the flash is empty") - assert_block(msg) { @response.has_flash_with_contents? } - end - - def assert_flash_equal(expected = nil, key = nil, message = nil) #:nodoc: - msg = build_message(message, " expected in flash['?'] but was ", expected, key, @response.flash[key]) - assert_block(msg) { expected == @response.flash[key] } - end - - - # ensure our redirection url is an exact match - def assert_redirect_url(url=nil, message=nil) #:nodoc: - assert_redirect(message) - msg = build_message(message, " is not the redirected location ", url, @response.redirect_url) - assert_block(msg) { @response.redirect_url == url } - end - - # ensure our redirection url matches a pattern - def assert_redirect_url_match(pattern=nil, message=nil) #:nodoc: - assert_redirect(message) - msg = build_message(message, " was not found in the location: ", pattern, @response.redirect_url) - assert_block(msg) { @response.redirect_url_match?(pattern) } - end - - - # -- template assertions ------------------------------------------------ - - # ensure that a template object with the given name exists - def assert_template_has(key=nil, message=nil) #:nodoc: - msg = build_message(message, " is not a template object", key ) - assert_block(msg) { @response.has_template_object?(key) } - end - - # ensure that a template object with the given name does not exist - def assert_template_has_no(key=nil,message=nil) #:nodoc: - msg = build_message(message, " is a template object ", key, @response.template_objects[key]) - assert_block(msg) { !@response.has_template_object?(key) } - end - - # ensures that the object assigned to the template on +key+ is equal to +expected+ object. - def assert_template_equal(expected = nil, key = nil, message = nil) #:nodoc: - msg = build_message(message, " expected in assigns['?'] but was ", expected, key, @response.template.assigns[key.to_s]) - assert_block(msg) { expected == @response.template.assigns[key.to_s] } - end - alias_method :assert_assigned_equal, :assert_template_equal - - # Asserts that the template returns the +expected+ string or array based on the XPath +expression+. - # This will only work if the template rendered a valid XML document. - def assert_template_xpath_match(expression=nil, expected=nil, message=nil) #:nodoc: - xml, matches = REXML::Document.new(@response.body), [] - xml.elements.each(expression) { |e| matches << e.text } - if matches.empty? then - msg = build_message(message, " not found in document", expression) - flunk(msg) - return - elsif matches.length < 2 then - matches = matches.first - end - - msg = build_message(message, " found , not ", expression, matches, expected) - assert_block(msg) { matches == expected } - end - - # Assert the template object with the given name is an Active Record descendant and is valid. - def assert_valid_record(key = nil, message = nil) #:nodoc: - record = find_record_in_template(key) - msg = build_message(message, "Active Record is invalid )", record.errors.full_messages) - assert_block(msg) { record.valid? } - end - - # Assert the template object with the given name is an Active Record descendant and is invalid. - def assert_invalid_record(key = nil, message = nil) #:nodoc: - record = find_record_in_template(key) - msg = build_message(message, "Active Record is valid)") - assert_block(msg) { !record.valid? } - end - - # Assert the template object with the given name is an Active Record descendant and the specified column(s) are valid. - def assert_valid_column_on_record(key = nil, columns = "", message = nil) #:nodoc: - record = find_record_in_template(key) - record.send(:validate) - - cols = glue_columns(columns) - cols.delete_if { |col| !record.errors.invalid?(col) } - msg = build_message(message, "Active Record has invalid columns )", cols.join(",") ) - assert_block(msg) { cols.empty? } - end - - # Assert the template object with the given name is an Active Record descendant and the specified column(s) are invalid. - def assert_invalid_column_on_record(key = nil, columns = "", message = nil) #:nodoc: - record = find_record_in_template(key) - record.send(:validate) - - cols = glue_columns(columns) - cols.delete_if { |col| record.errors.invalid?(col) } - msg = build_message(message, "Active Record has valid columns )", cols.join(",") ) - assert_block(msg) { cols.empty? } - end - - private - def glue_columns(columns) - cols = [] - cols << columns if columns.class == String - cols += columns if columns.class == Array - cols - end - - def find_record_in_template(key = nil) - assert_template_has(key) - record = @response.template_objects[key] - - assert_not_nil(record) - assert_kind_of ActiveRecord::Base, record - - return record - end - end - end -end \ No newline at end of file diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb index f6c7776b44..bfd2abbb48 100644 --- a/actionpack/lib/action_controller/test_process.rb +++ b/actionpack/lib/action_controller/test_process.rb @@ -1,9 +1,4 @@ -begin - require File.dirname(__FILE__) + '/assertions' - require File.dirname(__FILE__) + '/deprecated_assertions' -rescue Exception => e - print e -end +require File.dirname(__FILE__) + '/assertions' module ActionController #:nodoc: class Base -- cgit v1.2.3