From 8c70844d6169f68bff1d185730c67063688732be Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Sat, 16 Apr 2011 10:46:52 +0200 Subject: [PATCH] test suite extraction --- Gemfile | 7 + Gemfile.lock | 96 ++++++ Rakefile | 10 + lib/rjs.rb | 6 +- test/abstract_unit.rb | 275 ++++++++++++++++++ test/assert_select_test.rb | 12 +- test/controller/caching_test.rb | 7 +- test/controller/render_js_test.rb | 2 - test/fixtures/functional_caching/_partial.erb | 3 + .../respond_to/layouts/standard.html.erb | 1 + test/fixtures/test/_one.html.erb | 1 + test/fixtures/test/_partial.html.erb | 1 + test/fixtures/test/_partial.js.erb | 1 + test/fixtures/test/_two.html.erb | 1 + test/lib/controller/fake_models.rb | 29 ++ test/render_other_test.rb | 1 - test/template/render_test.rb | 2 - 17 files changed, 445 insertions(+), 10 deletions(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile create mode 100644 test/abstract_unit.rb create mode 100644 test/fixtures/functional_caching/_partial.erb create mode 100644 test/fixtures/respond_to/layouts/standard.html.erb create mode 100644 test/fixtures/test/_one.html.erb create mode 100644 test/fixtures/test/_partial.html.erb create mode 100644 test/fixtures/test/_partial.js.erb create mode 100644 test/fixtures/test/_two.html.erb create mode 100644 test/lib/controller/fake_models.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e8ad2b6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source :rubygems + +gem 'rails', :git => 'git://github.com/rails/rails.git' +gem "rack", :git => "git://github.com/rack/rack.git" +gem "rack-test", :git => "git://github.com/brynary/rack-test.git" +gem 'mocha' + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..2b3c306 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,96 @@ +GIT + remote: git://github.com/brynary/rack-test.git + revision: 38fa75d41eb11b259fd06f87e9b15f2b4f8f8b76 + specs: + rack-test (0.5.7) + rack (>= 1.0) + +GIT + remote: git://github.com/rack/rack.git + revision: cd73f8f0db954659286abe006ad803603d261b55 + specs: + rack (1.2.1) + +GIT + remote: git://github.com/rails/rails.git + revision: c630750fa59e248fb5af96f850392333e341ccd7 + specs: + actionmailer (3.1.0.beta) + actionpack (= 3.1.0.beta) + mail (~> 2.2.15) + actionpack (3.1.0.beta) + activemodel (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + builder (~> 3.0.0) + erubis (~> 2.7.0) + i18n (~> 0.5.0) + rack (~> 1.2.1) + rack-cache (~> 1.0.0) + rack-mount (~> 0.7.1) + rack-test (~> 0.5.7) + tzinfo (~> 0.3.23) + activemodel (3.1.0.beta) + activesupport (= 3.1.0.beta) + bcrypt-ruby (~> 2.1.4) + builder (~> 3.0.0) + i18n (~> 0.5.0) + activerecord (3.1.0.beta) + activemodel (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + arel (~> 2.0.2) + tzinfo (~> 0.3.23) + activeresource (3.1.0.beta) + activemodel (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + activesupport (3.1.0.beta) + rails (3.1.0.beta) + actionmailer (= 3.1.0.beta) + actionpack (= 3.1.0.beta) + activerecord (= 3.1.0.beta) + activeresource (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + bundler (~> 1.0) + railties (= 3.1.0.beta) + railties (3.1.0.beta) + actionpack (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + rack-ssl (~> 1.3.2) + rake (>= 0.8.7) + thor (~> 0.14.4) + +GEM + remote: http://rubygems.org/ + specs: + arel (2.0.9) + bcrypt-ruby (2.1.4) + builder (3.0.0) + erubis (2.7.0) + i18n (0.5.0) + mail (2.2.15) + activesupport (>= 2.3.6) + i18n (>= 0.4.0) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.16) + mocha (0.9.12) + polyglot (0.3.1) + rack-cache (1.0.1) + rack (>= 0.4) + rack-mount (0.7.1) + rack (>= 1.0.0) + rack-ssl (1.3.2) + rack + rake (0.8.7) + thor (0.14.6) + treetop (1.4.9) + polyglot (>= 0.3.1) + tzinfo (0.3.26) + +PLATFORMS + ruby + +DEPENDENCIES + mocha + rack! + rack-test! + rails! diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..777dcbd --- /dev/null +++ b/Rakefile @@ -0,0 +1,10 @@ +require 'rake/testtask' + +task :default => :test + +Rake::TestTask.new do |t| + t.libs << 'test' + t.test_files = Dir.glob('test/*_test.rb') + Dir.glob('test/{controller,template}/**/*_test.rb') + t.warning = true + t.verbose = true +end diff --git a/lib/rjs.rb b/lib/rjs.rb index 654c416..32f6dec 100644 --- a/lib/rjs.rb +++ b/lib/rjs.rb @@ -20,7 +20,11 @@ include ActionView::Helpers::ScriptaculousHelper end + ActionView::TestCase.class_eval do + include ActionView::Helpers::PrototypeHelper + include ActionView::Helpers::ScriptaculousHelper + end + ActionView::Template.register_template_handler :rjs, ActionView::Template::Handlers::RJS.new end - diff --git a/test/abstract_unit.rb b/test/abstract_unit.rb new file mode 100644 index 0000000..1eb51e6 --- /dev/null +++ b/test/abstract_unit.rb @@ -0,0 +1,275 @@ +lib = File.expand_path("#{File.dirname(__FILE__)}/../lib") +$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) + +$:.unshift(File.dirname(__FILE__) + '/lib') + +if defined? Gem + Gem.source_index + gem 'bundler' +else + require 'rubygems' +end +require 'bundler' +Bundler.setup + +require 'test/unit' +require 'active_support' +require 'action_controller' +require 'action_view' +require 'action_view/testing/resolvers' + +require 'rjs' + +FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures') +FIXTURES = Pathname.new(FIXTURE_LOAD_PATH) + +class Rack::TestCase < ActionDispatch::IntegrationTest + def self.testing(klass = nil) + if klass + @testing = "/#{klass.name.underscore}".sub!(/_controller$/, '') + else + @testing + end + end + + def get(thing, *args) + if thing.is_a?(Symbol) + super("#{self.class.testing}/#{thing}", *args) + else + super + end + end + + def assert_body(body) + assert_equal body, Array.wrap(response.body).join + end + + def assert_status(code) + assert_equal code, response.status + end + + def assert_response(body, status = 200, headers = {}) + assert_body body + assert_status status + headers.each do |header, value| + assert_header header, value + end + end + + def assert_content_type(type) + assert_equal type, response.headers["Content-Type"] + end + + def assert_header(name, value) + assert_equal value, response.headers[name] + end +end + +module SetupOnce + extend ActiveSupport::Concern + + included do + cattr_accessor :setup_once_block + self.setup_once_block = nil + + setup :run_setup_once + end + + module ClassMethods + def setup_once(&block) + self.setup_once_block = block + end + end + + private + def run_setup_once + if self.setup_once_block + self.setup_once_block.call + self.setup_once_block = nil + end + end +end + +SharedTestRoutes = ActionDispatch::Routing::RouteSet.new + +module ActiveSupport + class TestCase + include SetupOnce + # Hold off drawing routes until all the possible controller classes + # have been loaded. + setup_once do + SharedTestRoutes.draw do + match ':controller(/:action)' + end + + ActionDispatch::IntegrationTest.app.routes.draw do + match ':controller(/:action)' + end + end + end +end + +class RoutedRackApp + attr_reader :routes + + def initialize(routes, &blk) + @routes = routes + @stack = ActionDispatch::MiddlewareStack.new(&blk).build(@routes) + end + + def call(env) + @stack.call(env) + end +end + +class BasicController + attr_accessor :request + + def config + @config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config).tap do |config| + # VIEW TODO: View tests should not require a controller + public_dir = File.expand_path("../fixtures/public", __FILE__) + config.assets_dir = public_dir + config.javascripts_dir = "#{public_dir}/javascripts" + config.stylesheets_dir = "#{public_dir}/stylesheets" + config + end + end +end + +class ActionDispatch::IntegrationTest < ActiveSupport::TestCase + setup do + @routes = SharedTestRoutes + end + + def self.build_app(routes = nil) + RoutedRackApp.new(routes || ActionDispatch::Routing::RouteSet.new) do |middleware| + middleware.use "ActionDispatch::ShowExceptions" + middleware.use "ActionDispatch::Callbacks" + middleware.use "ActionDispatch::ParamsParser" + middleware.use "ActionDispatch::Cookies" + middleware.use "ActionDispatch::Flash" + middleware.use "ActionDispatch::Head" + yield(middleware) if block_given? + end + end + + self.app = build_app + + # Stub Rails dispatcher so it does not get controller references and + # simply return the controller#action as Rack::Body. + class StubDispatcher < ::ActionDispatch::Routing::RouteSet::Dispatcher + protected + def controller_reference(controller_param) + controller_param + end + + def dispatch(controller, action, env) + [200, {'Content-Type' => 'text/html'}, ["#{controller}##{action}"]] + end + end + + def self.stub_controllers + old_dispatcher = ActionDispatch::Routing::RouteSet::Dispatcher + ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher } + ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, StubDispatcher } + yield ActionDispatch::Routing::RouteSet.new + ensure + ActionDispatch::Routing::RouteSet.module_eval { remove_const :Dispatcher } + ActionDispatch::Routing::RouteSet.module_eval { const_set :Dispatcher, old_dispatcher } + end + + def with_routing(&block) + temporary_routes = ActionDispatch::Routing::RouteSet.new + old_app, self.class.app = self.class.app, self.class.build_app(temporary_routes) + old_routes = SharedTestRoutes + silence_warnings { Object.const_set(:SharedTestRoutes, temporary_routes) } + + yield temporary_routes + ensure + self.class.app = old_app + silence_warnings { Object.const_set(:SharedTestRoutes, old_routes) } + end + + def with_autoload_path(path) + path = File.join(File.dirname(__FILE__), "fixtures", path) + if ActiveSupport::Dependencies.autoload_paths.include?(path) + yield + else + begin + ActiveSupport::Dependencies.autoload_paths << path + yield + ensure + ActiveSupport::Dependencies.autoload_paths.reject! {|p| p == path} + ActiveSupport::Dependencies.clear + end + end + end +end + +# Temporary base class +class Rack::TestCase < ActionDispatch::IntegrationTest + def self.testing(klass = nil) + if klass + @testing = "/#{klass.name.underscore}".sub!(/_controller$/, '') + else + @testing + end + end + + def get(thing, *args) + if thing.is_a?(Symbol) + super("#{self.class.testing}/#{thing}", *args) + else + super + end + end + + def assert_body(body) + assert_equal body, Array.wrap(response.body).join + end + + def assert_status(code) + assert_equal code, response.status + end + + def assert_response(body, status = 200, headers = {}) + assert_body body + assert_status status + headers.each do |header, value| + assert_header header, value + end + end + + def assert_content_type(type) + assert_equal type, response.headers["Content-Type"] + end + + def assert_header(name, value) + assert_equal value, response.headers[name] + end +end + +module ActionController + class Base + include ActionController::Testing + # This stub emulates the Railtie including the URL helpers from a Rails application + include SharedTestRoutes.url_helpers + + self.view_paths = FIXTURE_LOAD_PATH + + def self.test_routes(&block) + routes = ActionDispatch::Routing::RouteSet.new + routes.draw(&block) + include routes.url_helpers + end + end + + class TestCase + include ActionDispatch::TestProcess + + setup do + @routes = SharedTestRoutes + end + end +end diff --git a/test/assert_select_test.rb b/test/assert_select_test.rb index 1ee59a8..cabd277 100644 --- a/test/assert_select_test.rb +++ b/test/assert_select_test.rb @@ -1,12 +1,18 @@ -# encoding: utf-8 - require 'abstract_unit' class AssertSelectTest < ActionController::TestCase Assertion = ActiveSupport::TestCase::Assertion class AssertSelectController < ActionController::Base - def rjs() + def response_with=(content) + @content = content + end + + def response_with(&block) + @update = block + end + + def rjs render :update do |page| @update.call page end diff --git a/test/controller/caching_test.rb b/test/controller/caching_test.rb index 7b1ffda..152b19c 100644 --- a/test/controller/caching_test.rb +++ b/test/controller/caching_test.rb @@ -1,12 +1,17 @@ require 'fileutils' require 'abstract_unit' -require 'active_record_unit' CACHE_DIR = 'test_cache' # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) ActionController::Base.page_cache_directory = FILE_STORE_PATH +class CachingController < ActionController::Base + abstract! + + self.cache_store = :file_store, FILE_STORE_PATH +end + class FunctionalCachingController < CachingController def js_fragment_cached_with_partial respond_to do |format| diff --git a/test/controller/render_js_test.rb b/test/controller/render_js_test.rb index c61d973..5b9c550 100644 --- a/test/controller/render_js_test.rb +++ b/test/controller/render_js_test.rb @@ -1,6 +1,4 @@ require 'abstract_unit' -require 'controller/fake_models' -require 'pathname' class RenderJSTest < ActionController::TestCase class TestController < ActionController::Base diff --git a/test/fixtures/functional_caching/_partial.erb b/test/fixtures/functional_caching/_partial.erb new file mode 100644 index 0000000..ec0da7c --- /dev/null +++ b/test/fixtures/functional_caching/_partial.erb @@ -0,0 +1,3 @@ +<% cache do %> +Old fragment caching in a partial +<% end %> diff --git a/test/fixtures/respond_to/layouts/standard.html.erb b/test/fixtures/respond_to/layouts/standard.html.erb new file mode 100644 index 0000000..c6c1a58 --- /dev/null +++ b/test/fixtures/respond_to/layouts/standard.html.erb @@ -0,0 +1 @@ +
<%= yield %>
\ No newline at end of file diff --git a/test/fixtures/test/_one.html.erb b/test/fixtures/test/_one.html.erb new file mode 100644 index 0000000..f796291 --- /dev/null +++ b/test/fixtures/test/_one.html.erb @@ -0,0 +1 @@ +<%= render :partial => "two" %> world diff --git a/test/fixtures/test/_partial.html.erb b/test/fixtures/test/_partial.html.erb new file mode 100644 index 0000000..e39f6c9 --- /dev/null +++ b/test/fixtures/test/_partial.html.erb @@ -0,0 +1 @@ +partial html \ No newline at end of file diff --git a/test/fixtures/test/_partial.js.erb b/test/fixtures/test/_partial.js.erb new file mode 100644 index 0000000..b350cdd --- /dev/null +++ b/test/fixtures/test/_partial.js.erb @@ -0,0 +1 @@ +partial js \ No newline at end of file diff --git a/test/fixtures/test/_two.html.erb b/test/fixtures/test/_two.html.erb new file mode 100644 index 0000000..5ab2f8a --- /dev/null +++ b/test/fixtures/test/_two.html.erb @@ -0,0 +1 @@ +Hello \ No newline at end of file diff --git a/test/lib/controller/fake_models.rb b/test/lib/controller/fake_models.rb new file mode 100644 index 0000000..14ad191 --- /dev/null +++ b/test/lib/controller/fake_models.rb @@ -0,0 +1,29 @@ +require "active_model" + +class Customer < Struct.new(:name, :id) + extend ActiveModel::Naming + include ActiveModel::Conversion + + undef_method :to_json + + def to_xml(options={}) + if options[:builder] + options[:builder].name name + else + "#{name}" + end + end + + def to_js(options={}) + "name: #{name.inspect}" + end + alias :to_text :to_js + + def errors + [] + end + + def persisted? + id.present? + end +end diff --git a/test/render_other_test.rb b/test/render_other_test.rb index eda777e..debc9cc 100644 --- a/test/render_other_test.rb +++ b/test/render_other_test.rb @@ -1,5 +1,4 @@ require 'abstract_unit' -require 'controller/fake_models' require 'pathname' ActionController.add_renderer :simon do |says, options| diff --git a/test/template/render_test.rb b/test/template/render_test.rb index 1ff2a52..bd4833b 100644 --- a/test/template/render_test.rb +++ b/test/template/render_test.rb @@ -1,6 +1,4 @@ -# encoding: utf-8 require 'abstract_unit' -require 'controller/fake_models' class TestController < ActionController::Base end