2010-09-23 13:13:07 -04:00
|
|
|
require 'generators/generators_test_helper'
|
|
|
|
require 'rails/generators/rails/controller/controller_generator'
|
|
|
|
require 'rails/generators/rails/model/model_generator'
|
2010-09-25 07:26:08 -04:00
|
|
|
require 'rails/generators/rails/observer/observer_generator'
|
2010-09-25 07:35:29 -04:00
|
|
|
require 'rails/generators/mailer/mailer_generator'
|
2010-11-03 15:12:08 -04:00
|
|
|
require 'rails/generators/rails/scaffold/scaffold_generator'
|
2010-09-23 13:13:07 -04:00
|
|
|
|
|
|
|
class NamespacedGeneratorTestCase < Rails::Generators::TestCase
|
|
|
|
def setup
|
2010-10-09 14:59:20 -04:00
|
|
|
TestApp::Application.isolate_namespace(TestApp)
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
if TestApp.respond_to?(:_railtie)
|
|
|
|
TestApp.singleton_class.send(:undef_method, :_railtie)
|
|
|
|
TestApp.singleton_class.send(:undef_method, :table_name_prefix)
|
2010-10-09 14:59:20 -04:00
|
|
|
TestApp::Application.isolated = false
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NamespacedControllerGeneratorTest < NamespacedGeneratorTestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
arguments %w(Account foo bar)
|
|
|
|
tests Rails::Generators::ControllerGenerator
|
|
|
|
|
|
|
|
setup :copy_routes
|
|
|
|
|
|
|
|
def test_namespaced_controller_skeleton_is_created
|
|
|
|
run_generator
|
|
|
|
assert_file "app/controllers/test_app/account_controller.rb", /module TestApp/, / class AccountController < ApplicationController/
|
2010-09-25 07:19:27 -04:00
|
|
|
assert_file "test/functional/test_app/account_controller_test.rb", /module TestApp/, / class AccountControllerTest/
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_skipping_namespace
|
|
|
|
run_generator ["Account", "--skip-namespace"]
|
|
|
|
assert_file "app/controllers/account_controller.rb", /class AccountController < ApplicationController/
|
|
|
|
assert_file "app/helpers/account_helper.rb", /module AccountHelper/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_namespaced_controller_with_additional_namespace
|
|
|
|
run_generator ["admin/account"]
|
|
|
|
assert_file "app/controllers/test_app/admin/account_controller.rb", /module TestApp/, / class Admin::AccountController < ApplicationController/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_helpr_is_also_namespaced
|
|
|
|
run_generator
|
|
|
|
assert_file "app/helpers/test_app/account_helper.rb", /module TestApp/, / module AccountHelper/
|
2010-09-25 07:19:27 -04:00
|
|
|
assert_file "test/unit/helpers/test_app/account_helper_test.rb", /module TestApp/, / class AccountHelperTest/
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
|
|
|
assert_file "test/functional/test_app/account_controller_test.rb"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_template_engine
|
|
|
|
run_generator
|
|
|
|
assert_file "app/views/test_app/account/foo.html.erb", %r(app/views/test_app/account/foo\.html\.erb)
|
|
|
|
assert_file "app/views/test_app/account/bar.html.erb", %r(app/views/test_app/account/bar\.html\.erb)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_routes_should_not_be_namespaced
|
|
|
|
run_generator
|
|
|
|
assert_file "config/routes.rb", /get "account\/foo"/, /get "account\/bar"/
|
|
|
|
end
|
|
|
|
#
|
|
|
|
def test_invokes_default_template_engine_even_with_no_action
|
|
|
|
run_generator ["account"]
|
|
|
|
assert_file "app/views/test_app/account"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NamespacedModelGeneratorTest < NamespacedGeneratorTestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
arguments %w(Account name:string age:integer)
|
|
|
|
tests Rails::Generators::ModelGenerator
|
|
|
|
|
2010-09-24 15:54:16 -04:00
|
|
|
def test_module_file_is_not_created
|
|
|
|
run_generator
|
|
|
|
assert_no_file "app/models/test_app.rb"
|
|
|
|
end
|
|
|
|
|
2010-09-23 13:13:07 -04:00
|
|
|
def test_adds_namespace_to_model
|
|
|
|
run_generator
|
|
|
|
assert_file "app/models/test_app/account.rb", /module TestApp/, / class Account < ActiveRecord::Base/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_model_with_namespace
|
|
|
|
run_generator ["admin/account"]
|
|
|
|
assert_file "app/models/test_app/admin.rb", /module TestApp/, /module Admin/
|
|
|
|
assert_file "app/models/test_app/admin.rb", /def self\.table_name_prefix/
|
|
|
|
assert_file "app/models/test_app/admin.rb", /'admin_'/
|
|
|
|
assert_file "app/models/test_app/admin/account.rb", /module TestApp/, /class Admin::Account < ActiveRecord::Base/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration
|
|
|
|
run_generator
|
|
|
|
assert_migration "db/migrate/create_test_app_accounts.rb", /create_table :test_app_accounts/, /class CreateTestAppAccounts < ActiveRecord::Migration/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_namespace
|
|
|
|
run_generator ["Gallery::Image"]
|
|
|
|
assert_migration "db/migrate/create_test_app_gallery_images", /class CreateTestAppGalleryImages < ActiveRecord::Migration/
|
|
|
|
assert_no_migration "db/migrate/create_test_app_images"
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_nested_namespace
|
|
|
|
run_generator ["Admin::Gallery::Image"]
|
|
|
|
assert_no_migration "db/migrate/create_images"
|
|
|
|
assert_no_migration "db/migrate/create_gallery_images"
|
|
|
|
assert_migration "db/migrate/create_test_app_admin_gallery_images", /class CreateTestAppAdminGalleryImages < ActiveRecord::Migration/
|
|
|
|
assert_migration "db/migrate/create_test_app_admin_gallery_images", /create_table :test_app_admin_gallery_images/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_migration_with_nested_namespace_without_pluralization
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
run_generator ["Admin::Gallery::Image"]
|
|
|
|
assert_no_migration "db/migrate/create_images"
|
|
|
|
assert_no_migration "db/migrate/create_gallery_images"
|
|
|
|
assert_no_migration "db/migrate/create_test_app_admin_gallery_images"
|
|
|
|
assert_migration "db/migrate/create_test_app_admin_gallery_image", /class CreateTestAppAdminGalleryImage < ActiveRecord::Migration/
|
|
|
|
assert_migration "db/migrate/create_test_app_admin_gallery_image", /create_table :test_app_admin_gallery_image/
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
2010-09-25 07:19:27 -04:00
|
|
|
assert_file "test/unit/test_app/account_test.rb", /module TestApp/, /class AccountTest < ActiveSupport::TestCase/
|
2010-09-23 13:13:07 -04:00
|
|
|
assert_file "test/fixtures/test_app/accounts.yml", /name: MyString/, /age: 1/
|
|
|
|
end
|
|
|
|
end
|
2010-09-25 07:26:08 -04:00
|
|
|
|
|
|
|
class NamespacedObserverGeneratorTest < NamespacedGeneratorTestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
arguments %w(account)
|
|
|
|
tests Rails::Generators::ObserverGenerator
|
|
|
|
|
|
|
|
def test_invokes_default_orm
|
|
|
|
run_generator
|
|
|
|
assert_file "app/models/test_app/account_observer.rb", /module TestApp/, / class AccountObserver < ActiveRecord::Observer/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_orm_with_class_path
|
|
|
|
run_generator ["admin/account"]
|
|
|
|
assert_file "app/models/test_app/admin/account_observer.rb", /module TestApp/, / class Admin::AccountObserver < ActiveRecord::Observer/
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
|
|
|
assert_file "test/unit/test_app/account_observer_test.rb", /module TestApp/, / class AccountObserverTest < ActiveSupport::TestCase/
|
|
|
|
end
|
|
|
|
end
|
2010-09-25 07:35:29 -04:00
|
|
|
|
|
|
|
class NamespacedMailerGeneratorTest < NamespacedGeneratorTestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
arguments %w(notifier foo bar)
|
|
|
|
tests Rails::Generators::MailerGenerator
|
|
|
|
|
|
|
|
def test_mailer_skeleton_is_created
|
|
|
|
run_generator
|
|
|
|
assert_file "app/mailers/test_app/notifier.rb" do |mailer|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/module TestApp/, mailer)
|
|
|
|
assert_match(/class Notifier < ActionMailer::Base/, mailer)
|
2011-05-14 04:35:54 -04:00
|
|
|
if RUBY_VERSION < "1.9"
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/default :from => "from@example.com"/, mailer)
|
2011-05-14 04:35:54 -04:00
|
|
|
else
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/default from: "from@example.com"/, mailer)
|
2011-05-14 04:35:54 -04:00
|
|
|
end
|
2010-09-25 07:35:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mailer_with_i18n_helper
|
|
|
|
run_generator
|
|
|
|
assert_file "app/mailers/test_app/notifier.rb" do |mailer|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/en\.notifier\.foo\.subject/, mailer)
|
|
|
|
assert_match(/en\.notifier\.bar\.subject/, mailer)
|
2010-09-25 07:35:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_test_framework
|
|
|
|
run_generator
|
|
|
|
assert_file "test/functional/test_app/notifier_test.rb" do |test|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(/module TestApp/, test)
|
|
|
|
assert_match(/class NotifierTest < ActionMailer::TestCase/, test)
|
|
|
|
assert_match(/test "foo"/, test)
|
|
|
|
assert_match(/test "bar"/, test)
|
2010-09-25 07:35:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_template_engine
|
|
|
|
run_generator
|
|
|
|
assert_file "app/views/test_app/notifier/foo.text.erb" do |view|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(%r(app/views/test_app/notifier/foo\.text\.erb), view)
|
|
|
|
assert_match(/<%= @greeting %>/, view)
|
2010-09-25 07:35:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "app/views/test_app/notifier/bar.text.erb" do |view|
|
2011-05-18 07:37:57 -04:00
|
|
|
assert_match(%r(app/views/test_app/notifier/bar\.text\.erb), view)
|
|
|
|
assert_match(/<%= @greeting %>/, view)
|
2010-09-25 07:35:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_invokes_default_template_engine_even_with_no_action
|
|
|
|
run_generator ["notifier"]
|
|
|
|
assert_file "app/views/test_app/notifier"
|
|
|
|
end
|
|
|
|
end
|
2010-11-03 15:12:08 -04:00
|
|
|
|
|
|
|
class NamespacedScaffoldGeneratorTest < NamespacedGeneratorTestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
arguments %w(product_line title:string price:integer)
|
|
|
|
tests Rails::Generators::ScaffoldGenerator
|
|
|
|
|
|
|
|
setup :copy_routes
|
|
|
|
|
|
|
|
def test_scaffold_on_invoke
|
|
|
|
run_generator
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/test_app/product_line.rb", /module TestApp\n class ProductLine < ActiveRecord::Base/
|
|
|
|
assert_file "test/unit/test_app/product_line_test.rb", /module TestApp\n class ProductLineTest < ActiveSupport::TestCase/
|
|
|
|
assert_file "test/fixtures/test_app/product_lines.yml"
|
|
|
|
assert_migration "db/migrate/create_test_app_product_lines.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_match(/resources :product_lines$/, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_file "app/controllers/test_app/product_lines_controller.rb" do |content|
|
|
|
|
assert_match(/module TestApp\n class ProductLinesController < ApplicationController/, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "test/functional/test_app/product_lines_controller_test.rb",
|
|
|
|
/module TestApp\n class ProductLinesControllerTest < ActionController::TestCase/
|
|
|
|
|
|
|
|
# Views
|
|
|
|
%w(
|
|
|
|
index
|
|
|
|
edit
|
|
|
|
new
|
|
|
|
show
|
|
|
|
_form
|
|
|
|
).each { |view| assert_file "app/views/test_app/product_lines/#{view}.html.erb" }
|
|
|
|
assert_no_file "app/views/layouts/test_app/product_lines.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_file "app/helpers/test_app/product_lines_helper.rb"
|
|
|
|
assert_file "test/unit/helpers/test_app/product_lines_helper_test.rb"
|
|
|
|
|
|
|
|
# Stylesheets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2010-11-03 15:12:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_on_revoke
|
|
|
|
run_generator
|
|
|
|
run_generator ["product_line"], :behavior => :revoke
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_no_file "app/models/test_app/product_line.rb"
|
|
|
|
assert_no_file "test/unit/test_app/product_line_test.rb"
|
|
|
|
assert_no_file "test/fixtures/test_app/product_lines.yml"
|
|
|
|
assert_no_migration "db/migrate/create_test_app_product_lines.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_no_match(/resources :product_lines$/, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_no_file "app/controllers/test_app/product_lines_controller.rb"
|
|
|
|
assert_no_file "test/functional/test_app/product_lines_controller_test.rb"
|
|
|
|
|
|
|
|
# Views
|
|
|
|
assert_no_file "app/views/test_app/product_lines"
|
|
|
|
assert_no_file "app/views/test_app/layouts/product_lines.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_no_file "app/helpers/test_app/product_lines_helper.rb"
|
|
|
|
assert_no_file "test/unit/helpers/test_app/product_lines_helper_test.rb"
|
|
|
|
|
|
|
|
# Stylesheets (should not be removed)
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2010-11-03 15:12:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_with_namespace_on_invoke
|
|
|
|
run_generator [ "admin/role", "name:string", "description:string" ]
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/test_app/admin.rb", /module TestApp\n module Admin/
|
|
|
|
assert_file "app/models/test_app/admin/role.rb", /module TestApp\n class Admin::Role < ActiveRecord::Base/
|
|
|
|
assert_file "test/unit/test_app/admin/role_test.rb", /module TestApp\n class Admin::RoleTest < ActiveSupport::TestCase/
|
|
|
|
assert_file "test/fixtures/test_app/admin/roles.yml"
|
|
|
|
assert_migration "db/migrate/create_test_app_admin_roles.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_match(/namespace :admin do resources :roles end$/, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_file "app/controllers/test_app/admin/roles_controller.rb" do |content|
|
|
|
|
assert_match(/module TestApp\n class Admin::RolesController < ApplicationController/, content)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_file "test/functional/test_app/admin/roles_controller_test.rb",
|
|
|
|
/module TestApp\n class Admin::RolesControllerTest < ActionController::TestCase/
|
|
|
|
|
|
|
|
# Views
|
|
|
|
%w(
|
|
|
|
index
|
|
|
|
edit
|
|
|
|
new
|
|
|
|
show
|
|
|
|
_form
|
|
|
|
).each { |view| assert_file "app/views/test_app/admin/roles/#{view}.html.erb" }
|
|
|
|
assert_no_file "app/views/layouts/admin/roles.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_file "app/helpers/test_app/admin/roles_helper.rb"
|
|
|
|
assert_file "test/unit/helpers/test_app/admin/roles_helper_test.rb"
|
|
|
|
|
|
|
|
# Stylesheets
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2010-11-03 15:12:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_scaffold_with_namespace_on_revoke
|
|
|
|
run_generator [ "admin/role", "name:string", "description:string" ]
|
|
|
|
run_generator [ "admin/role" ], :behavior => :revoke
|
|
|
|
|
|
|
|
# Model
|
|
|
|
assert_file "app/models/test_app/admin.rb" # ( should not be remove )
|
|
|
|
assert_no_file "app/models/test_app/admin/role.rb"
|
|
|
|
assert_no_file "test/unit/test_app/admin/role_test.rb"
|
|
|
|
assert_no_file "test/fixtures/test_app/admin/roles.yml"
|
|
|
|
assert_no_migration "db/migrate/create_test_app_admin_roles.rb"
|
|
|
|
|
|
|
|
# Route
|
|
|
|
assert_file "config/routes.rb" do |route|
|
|
|
|
assert_no_match(/namespace :admin do resources :roles end$/, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Controller
|
|
|
|
assert_no_file "app/controllers/test_app/admin/roles_controller.rb"
|
|
|
|
assert_no_file "test/functional/test_app/admin/roles_controller_test.rb"
|
|
|
|
|
|
|
|
# Views
|
|
|
|
assert_no_file "app/views/test_app/admin/roles"
|
|
|
|
assert_no_file "app/views/layouts/test_app/admin/roles.html.erb"
|
|
|
|
|
|
|
|
# Helpers
|
|
|
|
assert_no_file "app/helpers/test_app/admin/roles_helper.rb"
|
|
|
|
assert_no_file "test/unit/helpers/test_app/admin/roles_helper_test.rb"
|
|
|
|
|
|
|
|
# Stylesheets (should not be removed)
|
2011-05-24 02:39:04 -04:00
|
|
|
assert_file "app/assets/stylesheets/scaffold.css"
|
2010-11-03 15:12:08 -04:00
|
|
|
end
|
|
|
|
end
|