| 1 |
require 'test/unit' |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
begin |
|---|
| 5 |
require 'rubygems' |
|---|
| 6 |
rescue LoadError |
|---|
| 7 |
end |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
module ActiveRecord |
|---|
| 11 |
class Base |
|---|
| 12 |
class << self |
|---|
| 13 |
attr_accessor :pluralize_table_names |
|---|
| 14 |
end |
|---|
| 15 |
self.pluralize_table_names = true |
|---|
| 16 |
end |
|---|
| 17 |
end |
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
module ActionView |
|---|
| 21 |
module Helpers |
|---|
| 22 |
module ActiveRecordHelper; end |
|---|
| 23 |
class InstanceTag; end |
|---|
| 24 |
end |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
if defined?(RAILS_ROOT) |
|---|
| 30 |
RAILS_ROOT.replace "#{File.dirname(__FILE__)}/fixtures" |
|---|
| 31 |
else |
|---|
| 32 |
RAILS_ROOT = "#{File.dirname(__FILE__)}/fixtures" |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" |
|---|
| 36 |
require 'rails_generator' |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
class RailsGeneratorTest < Test::Unit::TestCase |
|---|
| 40 |
BUILTINS = %w(controller integration_test mailer migration model observer plugin resource scaffold session_migration) |
|---|
| 41 |
CAPITALIZED_BUILTINS = BUILTINS.map { |b| b.capitalize } |
|---|
| 42 |
|
|---|
| 43 |
def setup |
|---|
| 44 |
ActiveRecord::Base.pluralize_table_names = true |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def test_sources |
|---|
| 48 |
expected = [:lib, :vendor, |
|---|
| 49 |
:plugins, :plugins, |
|---|
| 50 |
:user, |
|---|
| 51 |
:RubyGems, :RubyGems, |
|---|
| 52 |
:builtin] |
|---|
| 53 |
expected.delete(:RubyGems) unless Object.const_defined?(:Gem) |
|---|
| 54 |
assert_equal expected, Rails::Generator::Base.sources.map { |s| s.label } |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
def test_lookup_builtins |
|---|
| 58 |
(BUILTINS + CAPITALIZED_BUILTINS).each do |name| |
|---|
| 59 |
assert_nothing_raised do |
|---|
| 60 |
spec = Rails::Generator::Base.lookup(name) |
|---|
| 61 |
assert_not_nil spec |
|---|
| 62 |
assert_kind_of Rails::Generator::Spec, spec |
|---|
| 63 |
|
|---|
| 64 |
klass = spec.klass |
|---|
| 65 |
assert klass < Rails::Generator::Base |
|---|
| 66 |
assert_equal spec, klass.spec |
|---|
| 67 |
end |
|---|
| 68 |
end |
|---|
| 69 |
end |
|---|
| 70 |
|
|---|
| 71 |
def test_autolookup |
|---|
| 72 |
assert_nothing_raised { ControllerGenerator } |
|---|
| 73 |
assert_nothing_raised { ModelGenerator } |
|---|
| 74 |
end |
|---|
| 75 |
|
|---|
| 76 |
def test_lookup_missing_generator |
|---|
| 77 |
assert_raise(MissingSourceFile) { |
|---|
| 78 |
Rails::Generator::Base.lookup('missing_generator').klass |
|---|
| 79 |
} |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
def test_lookup_missing_class |
|---|
| 83 |
spec = nil |
|---|
| 84 |
assert_nothing_raised { spec = Rails::Generator::Base.lookup('missing_class') } |
|---|
| 85 |
assert_not_nil spec |
|---|
| 86 |
assert_kind_of Rails::Generator::Spec, spec |
|---|
| 87 |
assert_raise(NameError) { spec.klass } |
|---|
| 88 |
end |
|---|
| 89 |
|
|---|
| 90 |
def test_generator_usage |
|---|
| 91 |
(BUILTINS - ["session_migration"]).each do |name| |
|---|
| 92 |
assert_raise(Rails::Generator::UsageError, "Generator '#{name}' should raise an error without arguments") { |
|---|
| 93 |
Rails::Generator::Base.instance(name) |
|---|
| 94 |
} |
|---|
| 95 |
end |
|---|
| 96 |
end |
|---|
| 97 |
|
|---|
| 98 |
def test_generator_spec |
|---|
| 99 |
spec = Rails::Generator::Base.lookup('working') |
|---|
| 100 |
assert_equal 'working', spec.name |
|---|
| 101 |
assert_equal "#{RAILS_ROOT}/lib/generators/working", spec.path |
|---|
| 102 |
assert_equal :lib, spec.source |
|---|
| 103 |
assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) } |
|---|
| 104 |
end |
|---|
| 105 |
|
|---|
| 106 |
def test_named_generator_attributes |
|---|
| 107 |
g = Rails::Generator::Base.instance('working', %w(admin/foo bar baz)) |
|---|
| 108 |
assert_equal 'admin/foo', g.name |
|---|
| 109 |
assert_equal %w(admin), g.class_path |
|---|
| 110 |
assert_equal 'Admin', g.class_nesting |
|---|
| 111 |
assert_equal 'Admin::Foo', g.class_name |
|---|
| 112 |
assert_equal 'foo', g.singular_name |
|---|
| 113 |
assert_equal 'foos', g.plural_name |
|---|
| 114 |
assert_equal g.singular_name, g.file_name |
|---|
| 115 |
assert_equal "admin_#{g.plural_name}", g.table_name |
|---|
| 116 |
assert_equal %w(bar baz), g.args |
|---|
| 117 |
end |
|---|
| 118 |
|
|---|
| 119 |
def test_named_generator_attributes_without_pluralized |
|---|
| 120 |
ActiveRecord::Base.pluralize_table_names = false |
|---|
| 121 |
g = Rails::Generator::Base.instance('working', %w(admin/foo bar baz)) |
|---|
| 122 |
assert_equal "admin_#{g.singular_name}", g.table_name |
|---|
| 123 |
end |
|---|
| 124 |
|
|---|
| 125 |
def test_session_migration_generator_with_pluralization |
|---|
| 126 |
g = Rails::Generator::Base.instance('session_migration') |
|---|
| 127 |
assert_equal 'session'.pluralize, g.send(:default_session_table_name) |
|---|
| 128 |
ActiveRecord::Base.pluralize_table_names = false |
|---|
| 129 |
assert_equal 'session', g.send(:default_session_table_name) |
|---|
| 130 |
end |
|---|
| 131 |
|
|---|
| 132 |
def test_scaffold_controller_name |
|---|
| 133 |
|
|---|
| 134 |
g = Rails::Generator::Base.instance('scaffold', %w(Product)) |
|---|
| 135 |
assert_equal "Products", g.controller_name |
|---|
| 136 |
end |
|---|
| 137 |
end |
|---|