2004-12-07 06:49:38 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
# Optionally load RubyGems.
|
2005-02-07 08:14:05 -05:00
|
|
|
begin
|
|
|
|
require 'rubygems'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2004-12-07 06:49:38 -05:00
|
|
|
|
2006-08-29 06:33:40 -04:00
|
|
|
# Mock out what we need from AR::Base.
|
|
|
|
module ActiveRecord
|
|
|
|
class Base
|
|
|
|
class << self
|
|
|
|
attr_accessor :pluralize_table_names
|
|
|
|
end
|
|
|
|
self.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# And what we need from ActionView
|
|
|
|
module ActionView
|
|
|
|
module Helpers
|
|
|
|
module ActiveRecordHelper; end
|
|
|
|
class InstanceTag; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
# Must set before requiring generator libs.
|
2006-09-02 20:04:20 -04:00
|
|
|
if defined?(RAILS_ROOT)
|
|
|
|
RAILS_ROOT.replace "#{File.dirname(__FILE__)}/fixtures"
|
|
|
|
else
|
|
|
|
RAILS_ROOT = "#{File.dirname(__FILE__)}/fixtures"
|
|
|
|
end
|
2006-08-29 06:33:40 -04:00
|
|
|
|
|
|
|
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
|
|
|
require 'rails_generator'
|
2005-02-14 20:45:35 -05:00
|
|
|
|
|
|
|
|
2004-12-07 06:49:38 -05:00
|
|
|
class RailsGeneratorTest < Test::Unit::TestCase
|
2007-09-22 13:43:31 -04:00
|
|
|
BUILTINS = %w(controller integration_test mailer migration model observer plugin resource scaffold session_migration web_service)
|
2005-02-07 08:14:05 -05:00
|
|
|
CAPITALIZED_BUILTINS = BUILTINS.map { |b| b.capitalize }
|
2004-12-07 06:49:38 -05:00
|
|
|
|
2006-11-19 23:17:48 -05:00
|
|
|
def setup
|
|
|
|
ActiveRecord::Base.pluralize_table_names = true
|
|
|
|
end
|
|
|
|
|
2005-02-14 20:45:35 -05:00
|
|
|
def test_sources
|
2007-09-22 13:43:31 -04:00
|
|
|
expected = [:lib, :vendor,
|
|
|
|
:plugins, :plugins, # <plugin>/generators and <plugin>/rails_generators
|
|
|
|
:user,
|
|
|
|
:RubyGems, :RubyGems, # gems named <x>_generator, gems containing /rails_generator/ folder
|
|
|
|
:builtin]
|
|
|
|
expected.delete(:RubyGems) unless Object.const_defined?(:Gem)
|
2005-02-14 20:45:35 -05:00
|
|
|
assert_equal expected, Rails::Generator::Base.sources.map { |s| s.label }
|
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_lookup_builtins
|
|
|
|
(BUILTINS + CAPITALIZED_BUILTINS).each do |name|
|
|
|
|
assert_nothing_raised do
|
|
|
|
spec = Rails::Generator::Base.lookup(name)
|
|
|
|
assert_not_nil spec
|
|
|
|
assert_kind_of Rails::Generator::Spec, spec
|
2004-12-07 06:49:38 -05:00
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
klass = spec.klass
|
|
|
|
assert klass < Rails::Generator::Base
|
|
|
|
assert_equal spec, klass.spec
|
|
|
|
end
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_autolookup
|
|
|
|
assert_nothing_raised { ControllerGenerator }
|
|
|
|
assert_nothing_raised { ModelGenerator }
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_lookup_missing_generator
|
2005-07-12 22:12:00 -04:00
|
|
|
assert_raise(MissingSourceFile) {
|
2005-02-07 08:14:05 -05:00
|
|
|
Rails::Generator::Base.lookup('missing_generator').klass
|
2004-12-07 06:49:38 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_lookup_missing_class
|
|
|
|
spec = nil
|
|
|
|
assert_nothing_raised { spec = Rails::Generator::Base.lookup('missing_class') }
|
|
|
|
assert_not_nil spec
|
|
|
|
assert_kind_of Rails::Generator::Spec, spec
|
|
|
|
assert_raise(NameError) { spec.klass }
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_generator_usage
|
2007-09-22 13:43:31 -04:00
|
|
|
(BUILTINS - ["session_migration"]).each do |name|
|
|
|
|
assert_raise(Rails::Generator::UsageError, "Generator '#{name}' should raise an error without arguments") {
|
2005-02-07 08:14:05 -05:00
|
|
|
Rails::Generator::Base.instance(name)
|
|
|
|
}
|
|
|
|
end
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_generator_spec
|
|
|
|
spec = Rails::Generator::Base.lookup('working')
|
|
|
|
assert_equal 'working', spec.name
|
2005-11-07 04:59:19 -05:00
|
|
|
assert_equal "#{RAILS_ROOT}/lib/generators/working", spec.path
|
2006-08-29 06:33:40 -04:00
|
|
|
assert_equal :lib, spec.source
|
2006-09-02 20:04:20 -04:00
|
|
|
assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) }
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
|
|
|
|
2005-02-07 08:14:05 -05:00
|
|
|
def test_named_generator_attributes
|
|
|
|
g = Rails::Generator::Base.instance('working', %w(admin/foo bar baz))
|
|
|
|
assert_equal 'admin/foo', g.name
|
|
|
|
assert_equal %w(admin), g.class_path
|
|
|
|
assert_equal 'Admin', g.class_nesting
|
2005-02-14 20:45:35 -05:00
|
|
|
assert_equal 'Admin::Foo', g.class_name
|
2005-02-07 08:14:05 -05:00
|
|
|
assert_equal 'foo', g.singular_name
|
|
|
|
assert_equal 'foos', g.plural_name
|
2006-11-19 23:17:48 -05:00
|
|
|
assert_equal g.singular_name, g.file_name
|
2006-11-11 02:52:23 -05:00
|
|
|
assert_equal "admin_#{g.plural_name}", g.table_name
|
2005-02-07 08:14:05 -05:00
|
|
|
assert_equal %w(bar baz), g.args
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|
2005-09-27 20:06:39 -04:00
|
|
|
|
|
|
|
def test_named_generator_attributes_without_pluralized
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
g = Rails::Generator::Base.instance('working', %w(admin/foo bar baz))
|
2006-11-19 23:17:48 -05:00
|
|
|
assert_equal "admin_#{g.singular_name}", g.table_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_session_migration_generator_with_pluralization
|
|
|
|
g = Rails::Generator::Base.instance('session_migration')
|
|
|
|
assert_equal 'session'.pluralize, g.send(:default_session_table_name)
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
assert_equal 'session', g.send(:default_session_table_name)
|
2005-09-27 20:06:39 -04:00
|
|
|
end
|
2006-11-19 23:17:48 -05:00
|
|
|
|
2005-10-28 03:50:42 -04:00
|
|
|
def test_scaffold_controller_name
|
|
|
|
# Default behaviour is use the model name
|
|
|
|
g = Rails::Generator::Base.instance('scaffold', %w(Product))
|
2006-11-19 23:17:48 -05:00
|
|
|
assert_equal "Products", g.controller_name
|
|
|
|
end
|
2004-12-07 06:49:38 -05:00
|
|
|
end
|