aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2009-06-28 19:46:34 +0200
committerJosé Valim <jose.valim@gmail.com>2009-06-28 19:46:34 +0200
commit9413dba432b11d86b80449ca7b959b32ecb0844c (patch)
tree13eab65aefcc7cc4550f70a29da32b560b358641
parent9068bc05b8c5a1f2a0ad6959cb897d5831046d0a (diff)
downloadrails-9413dba432b11d86b80449ca7b959b32ecb0844c.tar.gz
rails-9413dba432b11d86b80449ca7b959b32ecb0844c.tar.bz2
rails-9413dba432b11d86b80449ca7b959b32ecb0844c.zip
Added tessts for resources generator.
-rw-r--r--railties/Rakefile3
-rw-r--r--railties/lib/generators/actions.rb2
-rw-r--r--railties/lib/generators/rails/resource/resource_generator.rb4
-rw-r--r--railties/test/generators/controller_generator_test.rb7
-rw-r--r--railties/test/generators/generators_test_helper.rb35
-rw-r--r--railties/test/generators/migration_generator_test.rb34
-rw-r--r--railties/test/generators/model_generator_test.rb22
-rw-r--r--railties/test/generators/resource_generator_test.rb87
8 files changed, 153 insertions, 41 deletions
diff --git a/railties/Rakefile b/railties/Rakefile
index 69c1ca762a..56389f9c33 100644
--- a/railties/Rakefile
+++ b/railties/Rakefile
@@ -26,7 +26,8 @@ task :default => :test
## below passes. It's not ideal, but at least
## we can see the failures
task :test do
- Dir['test/**/*_test.rb'].all? do |file|
+ dir = ENV["TEST_DIR"] || "**"
+ Dir["test/#{dir}/*_test.rb"].all? do |file|
ruby = File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME'))
system(ruby, '-Itest', "-I#{File.dirname(__FILE__)}/../activesupport/lib", file)
end or raise "Failures"
diff --git a/railties/lib/generators/actions.rb b/railties/lib/generators/actions.rb
index f0d630e15a..080d374a2f 100644
--- a/railties/lib/generators/actions.rb
+++ b/railties/lib/generators/actions.rb
@@ -252,7 +252,7 @@ module Rails
sentinel = "ActionController::Routing::Routes.draw do |map|"
in_root do
- inject_into_file 'config/routes.rb', "\n #{routing_code}", { :after => sentinel }, false
+ inject_into_file 'config/routes.rb', "\n #{routing_code}\n", { :after => sentinel }, false
end
end
diff --git a/railties/lib/generators/rails/resource/resource_generator.rb b/railties/lib/generators/rails/resource/resource_generator.rb
index 5fdf8c5b1b..938fd6298c 100644
--- a/railties/lib/generators/rails/resource/resource_generator.rb
+++ b/railties/lib/generators/rails/resource/resource_generator.rb
@@ -16,7 +16,7 @@ module Rails
if klass
args = []
args << class_name.pluralize
- args.concat(options[:actions])
+ args << options[:actions]
say_status :invoke, options[:resource_controller], :blue
klass.new(args, options.dup, _overrides_config).invoke(:all)
@@ -26,7 +26,7 @@ module Rails
end
# TODO Add singleton support
- def add_resource_routes
+ def add_resource_route
route "map.resources :#{file_name.pluralize}"
end
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