From 4494a752c3c568c65fa899c54486153a1a5fa187 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 23 Jun 2009 19:10:42 +0200 Subject: Change current sstructure. --- railties/test/generators/actions_test.rb | 184 +++++++++++++ railties/test/generators/app_test.rb | 132 +++++++++ railties/test/generators/generator_test_helper.rb | 305 ++------------------- .../generators/rails_controller_generator_test.rb | 44 --- .../test/generators/rails_helper_generator_test.rb | 36 --- .../test/generators/rails_mailer_generator_test.rb | 29 -- .../test/generators/rails_model_generator_test.rb | 48 ---- .../rails_model_subclass_generator_test.rb | 15 - .../generators/rails_resource_generator_test.rb | 29 -- .../generators/rails_scaffold_generator_test.rb | 150 ---------- 10 files changed, 345 insertions(+), 627 deletions(-) create mode 100644 railties/test/generators/actions_test.rb create mode 100644 railties/test/generators/app_test.rb delete mode 100644 railties/test/generators/rails_controller_generator_test.rb delete mode 100644 railties/test/generators/rails_helper_generator_test.rb delete mode 100644 railties/test/generators/rails_mailer_generator_test.rb delete mode 100644 railties/test/generators/rails_model_generator_test.rb delete mode 100644 railties/test/generators/rails_model_subclass_generator_test.rb delete mode 100644 railties/test/generators/rails_resource_generator_test.rb delete mode 100644 railties/test/generators/rails_scaffold_generator_test.rb (limited to 'railties/test/generators') diff --git a/railties/test/generators/actions_test.rb b/railties/test/generators/actions_test.rb new file mode 100644 index 0000000000..6030504fa3 --- /dev/null +++ b/railties/test/generators/actions_test.rb @@ -0,0 +1,184 @@ +require 'abstract_unit' +require 'generators/generator_test_helper' + +class ActionsTest < GeneratorTestCase + def setup + super + @git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git' + @svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk' + end + + def test_apply_loads_and_evaluates_a_template + template = <<-TEMPLATE + @foo = "FOO" + TEMPLATE + template.instance_eval "def read; self; end" # Make the string respond to read + + generator.expects(:open).with("http://gist.github.com/103208.txt").returns(template) + action :apply, "http://gist.github.com/103208.txt" + assert_equal generator.instance_variable_get("@foo"), "FOO" + end + + def test_file_should_write_data_to_file_path + action :file, 'lib/test_file.rb', 'heres test data' + assert_file 'lib/test_file.rb', 'heres test data' + end + + def test_file_should_write_block_contents_to_file_path + action(:file, 'lib/test_file.rb'){ 'heres block data' } + assert_file 'lib/test_file.rb', 'heres block data' + end + + def test_plugin_with_git_option_should_run_plugin_install + generator.expects(:run).once.with("ruby script/plugin install #{@git_plugin_uri}", false) + action :plugin, 'restful-authentication', :git => @git_plugin_uri + end + + def test_plugin_with_svn_option_should_run_plugin_install + generator.expects(:run).once.with("ruby script/plugin install #{@svn_plugin_uri}", false) + action :plugin, 'restful-authentication', :svn => @svn_plugin_uri + end + + def test_plugin_with_git_option_and_submodule_should_use_git_scm + generator.expects(:run).with("git submodule add #{@git_plugin_uri} vendor/plugins/rest_auth", false) + action :plugin, 'rest_auth', :git => @git_plugin_uri, :submodule => true + end + + def test_plugin_with_no_options_should_skip_method + generator.expects(:run).never + action :plugin, 'rest_auth', {} + end + + def test_gem_should_put_gem_dependency_in_enviroment + run_generator + action :gem, 'will-paginate' + assert_file 'config/environment.rb', /config\.gem 'will\-paginate'/ + end + + def test_gem_with_options_should_include_options_in_gem_dependency_in_environment + run_generator + action :gem, 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com' + + regexp = /#{Regexp.escape("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'")}/ + assert_file 'config/environment.rb', regexp + end + + def test_gem_with_env_string_should_put_gem_dependency_in_specified_environment + run_generator + action :gem, 'rspec', :env => 'test' + assert_file 'config/environments/test.rb', /config\.gem 'rspec'/ + end + + def test_gem_with_env_array_should_put_gem_dependency_in_specified_environments + run_generator + action :gem, 'quietbacktrace', :env => %w[ development test ] + assert_file 'config/environments/development.rb', /config\.gem 'quietbacktrace'/ + assert_file 'config/environments/test.rb', /config\.gem 'quietbacktrace'/ + end + + def test_gem_with_lib_option_set_to_false_should_put_gem_dependency_in_enviroment_correctly + run_generator + action :gem, 'mislav-will-paginate', :lib => false + assert_file 'config/environment.rb', /config\.gem 'mislav\-will\-paginate'\, :lib => false/ + end + + def test_environment_should_include_data_in_environment_initializer_block + run_generator + load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]' + action :environment, load_paths + assert_file 'config/environment.rb', /#{Regexp.escape(load_paths)}/ + end + + def test_environment_with_block_should_include_block_contents_in_environment_initializer_block + run_generator + + action :environment do + '# This wont be added' + '# This will be added' + end + + assert_file 'config/environment.rb', /# This will be added/ + end + + def test_git_with_symbol_should_run_command_using_git_scm + generator.expects(:run).once.with('git init') + action :git, :init + end + + def test_git_with_hash_should_run_each_command_using_git_scm + generator.expects(:run).times(2) + action :git, :rm => 'README', :add => '.' + end + + def test_vendor_should_write_data_to_file_in_vendor + action :vendor, 'vendor_file.rb', '# vendor data' + assert_file 'vendor/vendor_file.rb', '# vendor data' + end + + def test_lib_should_write_data_to_file_in_lib + action :lib, 'my_library.rb', 'class MyLibrary' + assert_file 'lib/my_library.rb', 'class MyLibrary' + end + + def test_rakefile_should_write_date_to_file_in_lib_tasks + action :rakefile, 'myapp.rake', 'task :run => [:environment]' + assert_file 'lib/tasks/myapp.rake', 'task :run => [:environment]' + end + + def test_initializer_should_write_date_to_file_in_config_initializers + action :initializer, 'constants.rb', 'MY_CONSTANT = 42' + assert_file 'config/initializers/constants.rb', 'MY_CONSTANT = 42' + end + + def test_generate_should_run_script_generate_with_argument_and_options + generator.expects(:run).once.with('ruby script/generate model MyModel', false) + action :generate, 'model', 'MyModel' + end + + def test_rake_should_run_rake_command_with_development_env + generator.expects(:run).once.with('rake log:clear RAILS_ENV=development', false) + action :rake, 'log:clear' + end + + def test_rake_with_env_option_should_run_rake_command_in_env + generator.expects(:run).once.with('rake log:clear RAILS_ENV=production', false) + action :rake, 'log:clear', :env => 'production' + end + + def test_rake_with_sudo_option_should_run_rake_command_with_sudo + generator.expects(:run).once.with('sudo rake log:clear RAILS_ENV=development', false) + action :rake, 'log:clear', :sudo => true + end + + def test_capify_should_run_the_capify_command + generator.expects(:run).once.with('capify .', false) + action :capify! + end + + def test_freeze_should_freeze_rails_edge + generator.expects(:run).once.with('rake rails:freeze:edge', false) + action :freeze! + end + + def test_route_should_add_data_to_the_routes_block_in_config_routes + run_generator + route_command = "map.route '/login', :controller => 'sessions', :action => 'new'" + action :route, route_command + assert_file 'config/routes.rb', /#{Regexp.escape(route_command)}/ + end + + protected + + def run_generator + silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root] } + end + + def generator(config={}) + @generator ||= Rails::Generators::Base.new([], {}, { :root => destination_root }.merge!(config)) + end + + def action(*args, &block) + silence(:stdout){ generator.send(*args, &block) } + end + +end diff --git a/railties/test/generators/app_test.rb b/railties/test/generators/app_test.rb new file mode 100644 index 0000000000..a67f7e4926 --- /dev/null +++ b/railties/test/generators/app_test.rb @@ -0,0 +1,132 @@ +require 'abstract_unit' +require 'generators/generator_test_helper' + +class AppTest < GeneratorTestCase + + def test_application_skeleton_is_created + run_generator + + %w( + app/controllers + app/helpers + app/models + app/views/layouts + config/environments + config/initializers + config/locales + db + doc + lib + lib/tasks + log + public/images + public/javascripts + public/stylesheets + script/performance + test/fixtures + test/functional + test/integration + test/performance + test/unit + vendor + vendor/plugins + tmp/sessions + tmp/sockets + tmp/cache + tmp/pids + ).each{ |path| assert_file path } + end + + def test_invalid_database_option_raises_an_error + content = capture(:stderr){ run_generator(["-d", "unknown"]) } + assert_match /Invalid value for \-\-database option/, content + end + + def test_dispatchers_are_not_added_by_default + run_generator + assert_no_file "config.ru" + assert_no_file "public/dispatch.cgi" + assert_no_file "public/dispatch.fcgi" + end + + def test_dispatchers_are_added_if_required + run_generator ["--with-dispatchers"] + assert_file "config.ru" + assert_file "public/dispatch.cgi" + assert_file "public/dispatch.fcgi" + end + + def test_config_database_is_added_by_default + run_generator + assert_file "config/database.yml", /sqlite3/ + end + + def test_config_database_is_not_added_if_skip_activerecord_is_given + run_generator ["--skip-activerecord"] + assert_no_file "config/database.yml" + end + + def test_activerecord_is_removed_from_frameworks_if_skip_activerecord_is_given + run_generator ["--skip-activerecord"] + assert_file "config/environment.rb", /config\.frameworks \-= \[ :active_record \]/ + end + + def test_prototype_and_test_unit_are_added_by_default + run_generator + assert_file "public/javascripts/prototype.js" + assert_file "test" + end + + def test_prototype_and_test_unit_are_skipped_if_required + run_generator ["--skip-prototype", "--skip-testunit"] + assert_no_file "public/javascripts/prototype.js" + assert_no_file "test" + end + + def test_shebang_is_added_to_files + run_generator ["--ruby", "foo/bar/baz"] + + %w( + about + console + dbconsole + destroy + generate + plugin + runner + server + ).each { |path| assert_file "script/#{path}", /#!foo\/bar\/baz/ } + end + + def test_rails_is_frozen + generator(:freeze => true, :database => "sqlite3").expects(:run).with("rake rails:freeze:edge", false) + silence(:stdout){ generator.invoke(:all) } + assert_file 'config/environment.rb', /# RAILS_GEM_VERSION/ + end + + def test_template_raises_an_error_with_invalid_path + content = capture(:stderr){ run_generator(["-m", "non/existant/path"]) } + assert_match /The template \[.*\] could not be loaded/, content + assert_match /non\/existant\/path/, content + end + + def test_template_is_executed_when_supplied + path = "http://gist.github.com/103208.txt" + template = %{ say "It works!" } + template.instance_eval "def read; self; end" # Make the string respond to read + + generator(:template => path, :database => "sqlite3").expects(:open).with(path).returns(template) + assert_match /It works!/, silence(:stdout){ generator.invoke(:all) } + end + + protected + + def run_generator(args=[]) + silence(:stdout) { Rails::Generators::AppGenerator.start [destination_root].concat(args) } + end + + def generator(options={}) + @generator ||= Rails::Generators::AppGenerator.new([destination_root], options, :root => destination_root) + end + +end diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb index 01bf1c90bd..2b988bde7a 100644 --- a/railties/test/generators/generator_test_helper.rb +++ b/railties/test/generators/generator_test_helper.rb @@ -1,303 +1,56 @@ require 'test/unit' require 'fileutils' -# Mock out what we need from AR::Base -module ActiveRecord - class Base - class << self - attr_accessor :pluralize_table_names, :timestamped_migrations - end - self.pluralize_table_names = true - self.timestamped_migrations = true - end - - module ConnectionAdapters - class Column - attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale - - def initialize(name, default, sql_type = nil) - @name = name - @default = default - @type = @sql_type = sql_type - end - - def human_name - @name.humanize - end - end - end -end - -# Mock up necessities from ActionView -module ActionView - module Helpers - module ActionRecordHelper; end - class InstanceTag; end - end -end - -# Set RAILS_ROOT appropriately fixture generation -tmp_dir = "#{File.dirname(__FILE__)}/../fixtures/tmp" - -if defined? RAILS_ROOT - RAILS_ROOT.replace tmp_dir -else - RAILS_ROOT = tmp_dir -end -FileUtils.mkdir_p RAILS_ROOT - $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" -require 'initializer' -# Mocks out the configuration -module Rails - def self.configuration - Rails::Configuration.new - end -end - -require 'rails_generator' +# For this while, let's load all generators by hand +require 'generators' +require 'generators/rails/app/app_generator' class GeneratorTestCase < Test::Unit::TestCase include FileUtils - def setup - ActiveRecord::Base.pluralize_table_names = true - - mkdir_p "#{RAILS_ROOT}/app/views/layouts" - mkdir_p "#{RAILS_ROOT}/config" - mkdir_p "#{RAILS_ROOT}/db" - mkdir_p "#{RAILS_ROOT}/test/fixtures" - mkdir_p "#{RAILS_ROOT}/public/stylesheets" - - File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| - f << "ActionController::Routing::Routes.draw do |map|\n\nend" - end + def destination_root + @destination_root ||= File.expand_path("#{File.dirname(__FILE__)}/../fixtures/tmp") end - def teardown - rm_rf "#{RAILS_ROOT}/app" - rm_rf "#{RAILS_ROOT}/test" - rm_rf "#{RAILS_ROOT}/config" - rm_rf "#{RAILS_ROOT}/db" - rm_rf "#{RAILS_ROOT}/public" + def setup + mkdir_p(destination_root) + rm_rf(destination_root) end def test_truth # don't complain, test/unit end - # Instantiates the Generator. - def build_generator(name, params) - Rails::Generator::Base.instance(name, params) - end - - # Runs the +create+ command (like the command line does). - def run_generator(name, params) - silence_generator do - build_generator(name, params).command(:create).invoke! - end - end - - # Silences the logger temporarily and returns the output as a String. - def silence_generator - logger_original = Rails::Generator::Base.logger - myout = StringIO.new - Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(myout) - yield if block_given? - Rails::Generator::Base.logger = logger_original - myout.string - end - - # Asserts that the given controller was generated. - # It takes a name or symbol without the _controller part and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_controller_for(name, parent = "ApplicationController") - assert_generated_class "app/controllers/#{name.to_s.underscore}_controller", parent do |body| - yield body if block_given? - end - end - - # Asserts that the given model was generated. - # It takes a name or symbol and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_model_for(name, parent = "ActiveRecord::Base") - assert_generated_class "app/models/#{name.to_s.underscore}", parent do |body| - yield body if block_given? - end - end - - # Asserts that the given helper was generated. - # It takes a name or symbol without the _helper part. - # The contents of the module source file is passed to a block. - def assert_generated_helper_for(name) - assert_generated_module "app/helpers/#{name.to_s.underscore}_helper" do |body| - yield body if block_given? - end - end - - # Asserts that the given functional test was generated. - # It takes a name or symbol without the _controller_test part and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_functional_test_for(name, parent = "ActionController::TestCase") - assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body| - yield body if block_given? - end - end - - # Asserts that the given helper test test was generated. - # It takes a name or symbol without the _helper_test part and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_helper_test_for(name, parent = "ActionView::TestCase") - path = "test/unit/helpers/#{name.to_s.underscore}_helper_test" - # Have to pass the path without the "test/" part so that class_name_from_path will return a correct result - class_name = class_name_from_path(path.gsub(/^test\//, '')) - - assert_generated_class path,parent,class_name do |body| - yield body if block_given? - end - end - - # Asserts that the given unit test was generated. - # It takes a name or symbol without the _test part and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_unit_test_for(name, parent = "ActiveSupport::TestCase") - assert_generated_class "test/unit/#{name.to_s.underscore}_test", parent do |body| - yield body if block_given? - end - end - - # Asserts that the given file was generated. - # The contents of the file is passed to a block. - def assert_generated_file(path) - assert_file_exists(path) - File.open("#{RAILS_ROOT}/#{path}") do |f| - yield f.read if block_given? - end - end - - # asserts that the given file exists - def assert_file_exists(path) - assert File.exist?("#{RAILS_ROOT}/#{path}"), - "The file '#{RAILS_ROOT}/#{path}' should exist" - end - - # Asserts that the given class source file was generated. - # It takes a path without the .rb part and an optional super class. - # The contents of the class source file is passed to a block. - def assert_generated_class(path, parent = nil, class_name = class_name_from_path(path)) - assert_generated_file("#{path}.rb") do |body| - assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class" - yield body if block_given? - end - end - - def class_name_from_path(path) - # FIXME: Sucky way to detect namespaced classes - if path.split('/').size > 3 - path =~ /\/?(\d+_)?(\w+)\/(\w+)$/ - "#{$2.camelize}::#{$3.camelize}" - else - path =~ /\/?(\d+_)?(\w+)$/ - $2.camelize - end - end - - # Asserts that the given module source file was generated. - # It takes a path without the .rb part. - # The contents of the class source file is passed to a block. - def assert_generated_module(path) - # FIXME: Sucky way to detect namespaced modules - if path.split('/').size > 3 - path =~ /\/?(\w+)\/(\w+)$/ - module_name = "#{$1.camelize}::#{$2.camelize}" - else - path =~ /\/?(\w+)$/ - module_name = $1.camelize + def capture(stream) + begin + stream = stream.to_s + eval "$#{stream} = StringIO.new" + yield + result = eval("$#{stream}").string + ensure + eval("$#{stream} = #{stream.upcase}") end - assert_generated_file("#{path}.rb") do |body| - assert_match /module #{module_name}/, body, "the file '#{path}.rb' should be a module" - yield body if block_given? - end - end - - # Asserts that the given CSS stylesheet file was generated. - # It takes a path without the .css part. - # The contents of the stylesheet source file is passed to a block. - def assert_generated_stylesheet(path) - assert_generated_file("public/stylesheets/#{path}.css") do |body| - yield body if block_given? - end - end - - # Asserts that the given YAML file was generated. - # It takes a path without the .yml part. - # The parsed YAML tree is passed to a block. - def assert_generated_yaml(path) - assert_generated_file("#{path}.yml") do |body| - yaml = YAML.load(body) - assert yaml, 'YAML data missing' - yield yaml if block_given? - end - end - - # Asserts that the given fixtures YAML file was generated. - # It takes a fixture name without the .yml part. - # The parsed YAML tree is passed to a block. - def assert_generated_fixtures_for(name) - assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| - yield yaml if block_given? - end + result end + alias :silence :capture - # Asserts that the given views were generated. - # It takes a controller name and a list of views (including extensions). - # The body of each view is passed to a block. - def assert_generated_views_for(name, *actions) - actions.each do |action| - assert_generated_file("app/views/#{name.to_s.underscore}/#{action}") do |body| - yield body if block_given? - end - end - end - - def assert_generated_migration(name, parent = "ActiveRecord::Migration") - file = Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first - file = file.match(/db\/migrate\/[0-9]+_\w+/).to_s - assert_generated_class file, parent do |body| - assert_match /timestamps/, body, "should have timestamps defined" - yield body if block_given? - end - end - - # Asserts that the given migration file was not generated. - # It takes the name of the migration as a parameter. - def assert_skipped_migration(name) - migration_file = "#{RAILS_ROOT}/db/migrate/001_#{name.to_s.underscore}.rb" - assert !File.exist?(migration_file), "should not create migration #{migration_file}" - end - - # Asserts that the given resource was added to the routes. - def assert_added_route_for(name) - assert_generated_file("config/routes.rb") do |body| - assert_match /map.resources :#{name.to_s.underscore}/, body, - "should add route for :#{name.to_s.underscore}" - end - end + def assert_file(relative, content=nil) + absolute = File.join(destination_root, relative) + assert File.exists?(absolute) - # Asserts that the given methods are defined in the body. - # This does assume standard rails code conventions with regards to the source code. - # The body of each individual method is passed to a block. - def assert_has_method(body, *methods) - methods.each do |name| - assert body =~ /^ def #{name}(\(.+\))?\n((\n| .*\n)*) end/, "should have method #{name}" - yield(name, $2) if block_given? + case content + when String + assert_equal content, File.read(absolute) + when Regexp + assert_match content, File.read(absolute) end end - # Asserts that the given column is defined in the migration. - def assert_generated_column(body, name, type) - assert_match /t\.#{type.to_s} :#{name.to_s}/, body, "should have column #{name.to_s} defined" + def assert_no_file(relative, content=nil) + absolute = File.join(destination_root, relative) + assert !File.exists?(absolute) end end diff --git a/railties/test/generators/rails_controller_generator_test.rb b/railties/test/generators/rails_controller_generator_test.rb deleted file mode 100644 index 43fbe972e2..0000000000 --- a/railties/test/generators/rails_controller_generator_test.rb +++ /dev/null @@ -1,44 +0,0 @@ -require 'generators/generator_test_helper' - -module Admin -end - -class RailsControllerGeneratorTest < GeneratorTestCase - - def test_controller_generates_controller - run_generator('controller', %w(products)) - - assert_generated_controller_for :products - assert_generated_functional_test_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - end - - def test_controller_generates_namespaced_controller - run_generator('controller', %w(admin::products)) - - assert_generated_controller_for "admin::products" - assert_generated_functional_test_for "admin::products" - assert_generated_helper_for "admin::products" - assert_generated_helper_test_for "admin::products" - end - - def test_controller_generates_namespaced_and_not_namespaced_controllers - run_generator('controller', %w(products)) - - # We have to require the generated helper to show the problem because - # the test helpers just check for generated files and contents but - # do not actually load them. But they have to be loaded (as in a real environment) - # to make the second generator run fail - require "#{RAILS_ROOT}/app/helpers/products_helper" - - assert_nothing_raised do - begin - run_generator('controller', %w(admin::products)) - ensure - # cleanup - Object.send(:remove_const, :ProductsHelper) - end - end - end -end diff --git a/railties/test/generators/rails_helper_generator_test.rb b/railties/test/generators/rails_helper_generator_test.rb deleted file mode 100644 index 8d05f555e6..0000000000 --- a/railties/test/generators/rails_helper_generator_test.rb +++ /dev/null @@ -1,36 +0,0 @@ -require File.dirname(__FILE__) + '/generator_test_helper' - -class RailsHelperGeneratorTest < GeneratorTestCase - def test_helper_generates_helper - run_generator('helper', %w(products)) - - assert_generated_helper_for :products - assert_generated_helper_test_for :products - end - - def test_helper_generates_namespaced_helper - run_generator('helper', %w(admin::products)) - - assert_generated_helper_for "admin::products" - assert_generated_helper_test_for "admin::products" - end - - def test_helper_generates_namespaced_and_not_namespaced_helpers - run_generator('helper', %w(products)) - - # We have to require the generated helper to show the problem because - # the test helpers just check for generated files and contents but - # do not actually load them. But they have to be loaded (as in a real environment) - # to make the second generator run fail - require "#{RAILS_ROOT}/app/helpers/products_helper" - - assert_nothing_raised do - begin - run_generator('helper', %w(admin::products)) - ensure - # cleanup - Object.send(:remove_const, :ProductsHelper) - end - end - end -end diff --git a/railties/test/generators/rails_mailer_generator_test.rb b/railties/test/generators/rails_mailer_generator_test.rb deleted file mode 100644 index de61e6736d..0000000000 --- a/railties/test/generators/rails_mailer_generator_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'generators/generator_test_helper' - -class RailsMailerGeneratorTest < GeneratorTestCase - - def test_generates_mailer - run_generator('mailer', %w(Notifier reset_password)) - - assert_generated_model_for :notifier, 'ActionMailer::Base' do |model| - assert_has_method model, :reset_password do |name, body| - assert_equal [ - "subject 'Notifier#reset_password'", - "recipients ''", - "from ''", - "sent_on sent_at", - "", - "body :greeting => 'Hi,'" - ], - body.split("\n").map{|line| line.sub(' '*4, '') } - end - - assert_no_match /(self.default_url_options =|default_url_options\[.*\] =)/, model, - 'individual mailer models should not set default_url_options because the options are shared by all mailers' - end - - assert_generated_views_for :notifier, 'reset_password.erb' - assert_generated_unit_test_for :notifier, 'ActionMailer::TestCase' - assert_generated_file "test/fixtures/notifier/reset_password" - end -end diff --git a/railties/test/generators/rails_model_generator_test.rb b/railties/test/generators/rails_model_generator_test.rb deleted file mode 100644 index aea2abafba..0000000000 --- a/railties/test/generators/rails_model_generator_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require 'generators/generator_test_helper' - -class RailsModelGeneratorTest < GeneratorTestCase - - def test_model_generates_resources - run_generator('model', %w(Product name:string)) - - assert_generated_model_for :product - assert_generated_fixtures_for :products - assert_generated_migration :create_products - end - - def test_model_skip_migration_skips_migration - run_generator('model', %w(Product name:string --skip-migration)) - - assert_generated_model_for :product - assert_generated_fixtures_for :products - assert_skipped_migration :create_products - end - - def test_model_with_attributes_generates_resources_with_attributes - run_generator('model', %w(Product name:string supplier_id:integer created_at:timestamp)) - - assert_generated_model_for :product - assert_generated_fixtures_for :products - assert_generated_migration :create_products do |t| - assert_generated_column t, :name, :string - assert_generated_column t, :supplier_id, :integer - assert_generated_column t, :created_at, :timestamp - end - end - - def test_model_with_reference_attributes_generates_belongs_to_associations - run_generator('model', %w(Product name:string supplier:references)) - - assert_generated_model_for :product do |body| - assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" - end - end - - def test_model_with_belongs_to_attributes_generates_belongs_to_associations - run_generator('model', %w(Product name:string supplier:belongs_to)) - - assert_generated_model_for :product do |body| - assert body =~ /^\s+belongs_to :supplier/, "#{body.inspect} should contain 'belongs_to :supplier'" - end - end -end diff --git a/railties/test/generators/rails_model_subclass_generator_test.rb b/railties/test/generators/rails_model_subclass_generator_test.rb deleted file mode 100644 index 30066b5a3c..0000000000 --- a/railties/test/generators/rails_model_subclass_generator_test.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'generators/generator_test_helper' - -class RailsModelSubclassGeneratorTest < GeneratorTestCase - - def test_model_subclass_generates_resources - run_generator('model_subclass', %w(Car Product)) - - assert_generated_model_for :car, "Product" - assert_generated_unit_test_for :car - end - - def test_model_subclass_must_have_a_parent_class_name - assert_raise(Rails::Generator::UsageError) { run_generator('model_subclass', %w(Car)) } - end -end \ No newline at end of file diff --git a/railties/test/generators/rails_resource_generator_test.rb b/railties/test/generators/rails_resource_generator_test.rb deleted file mode 100644 index 1f5bd0ef1e..0000000000 --- a/railties/test/generators/rails_resource_generator_test.rb +++ /dev/null @@ -1,29 +0,0 @@ -require 'generators/generator_test_helper' - -class RailsResourceGeneratorTest < GeneratorTestCase - def test_resource_generates_resources - run_generator('resource', %w(Product name:string)) - - assert_generated_controller_for :products - assert_generated_model_for :product - assert_generated_fixtures_for :products - assert_generated_functional_test_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_migration :create_products - assert_added_route_for :products - end - - def test_resource_skip_migration_skips_migration - run_generator('resource', %w(Product name:string --skip-migration)) - - assert_generated_controller_for :products - assert_generated_model_for :product - assert_generated_fixtures_for :products - assert_generated_functional_test_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_skipped_migration :create_products - assert_added_route_for :products - end -end diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb deleted file mode 100644 index 70829a77fd..0000000000 --- a/railties/test/generators/rails_scaffold_generator_test.rb +++ /dev/null @@ -1,150 +0,0 @@ -require 'generators/generator_test_helper' -require 'abstract_unit' - -class RailsScaffoldGeneratorTest < GeneratorTestCase - def test_scaffolded_names - g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) - assert_equal "ProductLines", g.controller_name - assert_equal "ProductLines", g.controller_class_name - assert_equal "ProductLine", g.controller_singular_name - assert_equal "product_lines", g.controller_plural_name - assert_equal "product_lines", g.controller_file_name - assert_equal "product_lines", g.controller_table_name - end - - def test_scaffold_generates_resources - - run_generator('scaffold', %w(Product name:string)) - - assert_generated_controller_for :products do |f| - - assert_has_method f, :index do |name, m| - assert_match /@products = Product\.all/, m, "#{name} should query products table" - end - - assert_has_method f, :show, :edit, :update, :destroy do |name, m| - assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" - end - - assert_has_method f, :new do |name, m| - assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" - end - - assert_has_method f, :create do |name, m| - assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" - assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" - end - - end - - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" - - assert_generated_migration :create_products - assert_added_route_for :products - end - - def test_scaffold_skip_migration_skips_migration - run_generator('scaffold', %w(Product name:string --skip-migration)) - - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" - assert_skipped_migration :create_products - assert_added_route_for :products - end - - def test_scaffold_generates_resources_with_attributes - run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) - - assert_generated_controller_for :products do |f| - - assert_has_method f, :index do |name, m| - assert_match /@products = Product\.all/, m, "#{name} should query products table" - end - - assert_has_method f, :show, :edit, :update, :destroy do |name, m| - assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" - end - - assert_has_method f, :new do |name, m| - assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" - end - - assert_has_method f, :create do |name, m| - assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" - assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" - end - - end - - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" - - assert_generated_migration :create_products do |t| - assert_generated_column t, :name, :string - assert_generated_column t, :supplier_id, :integer - assert_generated_column t, :created_at, :timestamp - end - - assert_added_route_for :products - end - - def test_scaffolded_plural_names - Rails::Generator::Base.logger.expects(:warning) - g = Rails::Generator::Base.instance('scaffold', %w(ProductLines)) - assert_equal "ProductLines", g.controller_name - assert_equal "ProductLines", g.controller_class_name - assert_equal "ProductLine", g.controller_singular_name - assert_equal "product_lines", g.controller_plural_name - assert_equal "product_lines", g.controller_file_name - assert_equal "product_lines", g.controller_table_name - end - - def test_scaffold_plural_model_name_without_force_plural_generates_singular_model - run_generator('scaffold', %w(Products name:string)) - - assert_generated_model_for :product - assert_generated_functional_test_for :products - assert_generated_unit_test_for :product - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" - assert_skipped_migration :create_products - assert_added_route_for :products - end - - def test_scaffold_plural_model_name_with_force_plural_forces_plural_model - run_generator('scaffold', %w(Products name:string --force-plural)) - - assert_generated_model_for :products - assert_generated_functional_test_for :products - assert_generated_unit_test_for :products - assert_generated_fixtures_for :products - assert_generated_helper_for :products - assert_generated_helper_test_for :products - assert_generated_stylesheet :scaffold - assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" - assert_skipped_migration :create_products - assert_added_route_for :products - end -end -- cgit v1.2.3