diff options
Diffstat (limited to 'railties/test/generators')
5 files changed, 148 insertions, 37 deletions
diff --git a/railties/test/generators/controller_generator_test.rb b/railties/test/generators/controller_generator_test.rb index 6ef0e1cb3d..e3fe2594b9 100644 --- a/railties/test/generators/controller_generator_test.rb +++ b/railties/test/generators/controller_generator_test.rb @@ -67,8 +67,11 @@ class ControllerGeneratorTest < GeneratorsTestCase def test_actions_are_turned_into_methods run_generator - assert_file "app/controllers/account_controller.rb", /def foo/ - assert_file "app/controllers/account_controller.rb", /def bar/ + + assert_file "app/controllers/account_controller.rb" do |controller| + assert_instance_method controller, :foo + assert_instance_method controller, :bar + end end protected diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 7c58c7b64e..591da45c72 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -38,7 +38,9 @@ class GeneratorsTestCase < Test::Unit::TestCase absolute = File.join(destination_root, relative) assert File.exists?(absolute), "Expected file #{relative.inspect} to exist, but does not" - read = File.read(absolute) unless File.directory?(absolute) + read = File.read(absolute) if block_given? || !contents.empty? + yield read if block_given? + contents.each do |content| case content when String @@ -47,7 +49,6 @@ class GeneratorsTestCase < Test::Unit::TestCase assert_match content, read end end - read end def assert_no_file(relative) @@ -55,10 +56,10 @@ class GeneratorsTestCase < Test::Unit::TestCase assert !File.exists?(absolute), "Expected file #{relative.inspect} to not exist, but does" end - def assert_migration(relative, *contents) + def assert_migration(relative, *contents, &block) file_name = migration_file_name(relative) assert file_name, "Expected migration #{relative} to exist, but was not found" - assert_file File.join(File.dirname(relative), file_name), *contents + assert_file File.join(File.dirname(relative), file_name), *contents, &block end def assert_no_migration(relative) @@ -66,20 +67,22 @@ class GeneratorsTestCase < Test::Unit::TestCase assert_nil file_name, "Expected migration #{relative} to not exist, but found #{file_name}" end - def migration_file_name(relative) - absolute = File.join(destination_root, relative) - dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '') - - migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first - File.basename(migration) if migration - end - - def assert_class_method_for(content, method, &block) - assert_instance_method_for content, "self.#{method}", &block + def assert_class_method(content, method, &block) + assert_instance_method content, "self.#{method}", &block end - def assert_instance_method_for(content, method) + def assert_instance_method(content, method) assert_match /def #{method}(.*?)end/m, content - yield content.match(/def #{method}(.*?)end/m)[1] + yield content.match(/def #{method}(.*?)end/m)[1] if block_given? end + + protected + + def migration_file_name(relative) + absolute = File.join(destination_root, relative) + dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '') + + migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first + File.basename(migration) if migration + end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb index 90bc91344f..547ca6a9e3 100644 --- a/railties/test/generators/migration_generator_test.rb +++ b/railties/test/generators/migration_generator_test.rb @@ -20,32 +20,34 @@ class MigrationGeneratorTest < GeneratorsTestCase def test_add_migration_with_attributes @migration = "add_title_body_to_posts" run_generator [@migration, "title:string", "body:text"] - content = assert_migration "db/migrate/#{@migration}.rb" - assert_class_method_for content, :up do |up| - assert_match /add_column :posts, :title, :string/, up - assert_match /add_column :posts, :body, :text/, up - end + assert_migration "db/migrate/#{@migration}.rb" do |content| + assert_class_method content, :up do |up| + assert_match /add_column :posts, :title, :string/, up + assert_match /add_column :posts, :body, :text/, up + end - assert_class_method_for content, :down do |down| - assert_match /remove_column :posts, :title/, down - assert_match /remove_column :posts, :body/, down + assert_class_method content, :down do |down| + assert_match /remove_column :posts, :title/, down + assert_match /remove_column :posts, :body/, down + end end end def test_remove_migration_with_attributes @migration = "remove_title_body_from_posts" run_generator [@migration, "title:string", "body:text"] - content = assert_migration "db/migrate/#{@migration}.rb" - assert_class_method_for content, :up do |up| - assert_match /remove_column :posts, :title/, up - assert_match /remove_column :posts, :body/, up - end + assert_migration "db/migrate/#{@migration}.rb" do |content| + assert_class_method content, :up do |up| + assert_match /remove_column :posts, :title/, up + assert_match /remove_column :posts, :body/, up + end - assert_class_method_for content, :down do |down| - assert_match /add_column :posts, :title, :string/, down - assert_match /add_column :posts, :body, :text/, down + assert_class_method content, :down do |down| + assert_match /add_column :posts, :title, :string/, down + assert_match /add_column :posts, :body, :text/, down + end end end diff --git a/railties/test/generators/model_generator_test.rb b/railties/test/generators/model_generator_test.rb index 212f0f3691..14cafe7b0d 100644 --- a/railties/test/generators/model_generator_test.rb +++ b/railties/test/generators/model_generator_test.rb @@ -20,6 +20,7 @@ class ModelGeneratorTest < GeneratorsTestCase def test_model_with_parent_option run_generator ["account", "--parent", "Admin::Account"] assert_file "app/models/account.rb", /class Account < Admin::Account/ + assert_no_migration "db/migrate/create_accounts.rb" end def test_model_with_underscored_parent_option @@ -39,7 +40,18 @@ class ModelGeneratorTest < GeneratorsTestCase def test_migration_with_attributes run_generator ["product", "name:string", "supplier_id:integer"] - assert_migration "db/migrate/create_products.rb", /t\.string :name/, /t\.integer :supplier_id/ + + assert_migration "db/migrate/create_products.rb" do |m| + assert_class_method m, :up do |up| + assert_match /create_table :products/, up + assert_match /t\.string :name/, up + assert_match /t\.integer :supplier_id/, up + end + + assert_class_method m, :down do |down| + assert_match /drop_table :products/, down + end + end end def test_model_with_references_attribute_generates_belongs_to_associations @@ -59,8 +71,12 @@ class ModelGeneratorTest < GeneratorsTestCase def test_migration_timestamps_are_skipped run_generator ["account", "--no-timestamps"] - content = assert_migration "db/migrate/create_accounts.rb" - assert_no_match /t.timestamps/, content + + assert_migration "db/migrate/create_accounts.rb" do |m| + assert_class_method m, :up do |up| + assert_no_match /t.timestamps/, up + end + end end def test_invokes_default_test_framework diff --git a/railties/test/generators/resource_generator_test.rb b/railties/test/generators/resource_generator_test.rb new file mode 100644 index 0000000000..aed45f190d --- /dev/null +++ b/railties/test/generators/resource_generator_test.rb @@ -0,0 +1,87 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/rails/resource/resource_generator' + +# Model +require 'generators/active_record/model/model_generator' +require 'generators/rails/model/model_generator' +require 'generators/test_unit/model/model_generator' + +# Controller +require 'generators/erb/controller/controller_generator' +require 'generators/rails/controller/controller_generator' +require 'generators/rails/helper/helper_generator' +require 'generators/test_unit/controller/controller_generator' +require 'generators/test_unit/helper/helper_generator' + +class ResourceGeneratorTest < GeneratorsTestCase + + def setup + super + routes = Rails::Generators::ResourceGenerator.source_root + routes = File.join(routes, "..", "..", "app", "templates", "config", "routes.rb") + destination = File.join(destination_root, "config") + + FileUtils.mkdir_p(destination) + FileUtils.cp File.expand_path(routes), destination + end + + def test_help_with_inherited_options + content = run_generator ["--help"] + assert_match /ActiveRecord options:/, content + assert_match /TestUnit options:/, content + end + + def test_files_from_inherited_invocation + run_generator + + %w( + app/models/account.rb + test/unit/account_test.rb + test/fixtures/accounts.yml + ).each { |path| assert_file path } + + assert_migration "db/migrate/create_accounts.rb" + end + + def test_inherited_invocations_with_attributes + run_generator ["account", "name:string"] + assert_migration "db/migrate/create_accounts.rb", /t.string :name/ + end + + def test_resource_controller_with_pluralized_class_name + run_generator + assert_file "app/controllers/accounts_controller.rb", /class AccountsController < ApplicationController/ + assert_file "test/functional/accounts_controller_test.rb", /class AccountsControllerTest < ActionController::TestCase/ + + assert_file "app/helpers/accounts_helper.rb", /module AccountsHelper/ + assert_file "test/unit/helpers/accounts_helper_test.rb", /class AccountsHelperTest < ActionView::TestCase/ + end + + def test_resource_controller_with_actions + run_generator ["account", "--actions", "index", "new"] + + assert_file "app/controllers/accounts_controller.rb" do |controller| + assert_instance_method controller, :index + assert_instance_method controller, :new + end + + assert_file "app/views/accounts/index.html.erb" + assert_file "app/views/accounts/new.html.erb" + end + + def test_resource_routes_are_added + run_generator + + assert_file "config/routes.rb" do |route| + assert_match /map\.resources :accounts$/, route + end + end + + protected + + def run_generator(args=["account"]) + silence(:stdout) { Rails::Generators::ResourceGenerator.start args, :root => destination_root } + end + +end |