From daee6fd92ac16878f6806c3382a9e74592aa9656 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 7 Feb 2005 13:14:05 +0000 Subject: Added new generator framework that informs about its doings on generation and enables updating and destruction of generated artifacts. See the new script/destroy and script/update for more details #487 [bitsweat] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@518 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- railties/test/rails_generator_test.rb | 126 ++++++++++++++-------------------- 1 file changed, 53 insertions(+), 73 deletions(-) (limited to 'railties/test/rails_generator_test.rb') diff --git a/railties/test/rails_generator_test.rb b/railties/test/rails_generator_test.rb index 3a699b04b2..8470bed943 100644 --- a/railties/test/rails_generator_test.rb +++ b/railties/test/rails_generator_test.rb @@ -1,101 +1,81 @@ $LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" RAILS_ROOT = File.dirname(__FILE__) -require File.dirname(__FILE__) + '/../../activerecord/lib/active_record/support/inflector' -require 'rails_generator' require 'test/unit' -# Railties test directory has RAILS_ROOT/generators instead of the expected -# RAILS_ROOT/script/generators, so override it manually. -old_verbose, $VERBOSE = $VERBOSE, nil -Rails::Generator.const_set(:CONTRIB_ROOT, "#{RAILS_ROOT}/generators") -$VERBOSE = old_verbose +require 'rails_generator' +require 'rails_generator/simple_logger' +Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new +begin + require 'rubygems' + require_gem 'actionpack' +rescue LoadError +end class RailsGeneratorTest < Test::Unit::TestCase BUILTINS = %w(controller mailer model scaffold) + CAPITALIZED_BUILTINS = BUILTINS.map { |b| b.capitalize } - def test_instance_builtins - BUILTINS.each do |name| - object = nil - assert_nothing_raised { object = Rails::Generator.instance(name, ['foo']) } - assert_not_nil object - assert_match /#{name.capitalize}Generator/, object.class.name - assert_respond_to object, :generate - end - end + 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 - def test_instance_without_rails_root - old_verbose, $VERBOSE = $VERBOSE, nil - old_rails_root = Object.const_get(:RAILS_ROOT) - begin - Object.const_set(:RAILS_ROOT, nil) - assert_raise(Rails::Generator::GeneratorError) { - Rails::Generator.instance('model', ['name']) - } - ensure - Object.const_set(:RAILS_ROOT, old_rails_root) - $VERBOSE = old_verbose + klass = spec.klass + assert klass < Rails::Generator::Base + assert_equal spec, klass.spec + end end end - def test_instance_not_found - assert_raise(Rails::Generator::GeneratorError) { - Rails::Generator.instance('foobar') - } - end - - def test_instance_missing_templates - assert_raise(Rails::Generator::GeneratorError) { - Rails::Generator.instance('missing_templates') - } - end - - def test_instance_missing_generator - assert_raise(Rails::Generator::GeneratorError) { - Rails::Generator.instance('missing_generator') - } - end - - def test_instance_missing_class - assert_raise(Rails::Generator::GeneratorError) { - Rails::Generator.instance('missing_class') - } + def test_autolookup + assert_nothing_raised { ControllerGenerator } + assert_nothing_raised { ModelGenerator } end - def test_builtin_generators - assert_nothing_raised { - assert_equal [], Rails::Generator.builtin_generators - BUILTINS + def test_lookup_missing_generator + assert_raise(LoadError) { + Rails::Generator::Base.lookup('missing_generator').klass } end - def test_generator_name - assert_equal 'model', Rails::Generator.instance('model', ['name']).class.generator_name + 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 } end def test_generator_usage - assert_raise(Rails::Generator::UsageError) { - assert_equal 'model', Rails::Generator.instance('model') - } + BUILTINS.each do |name| + assert_raise(Rails::Generator::UsageError) { + Rails::Generator::Base.instance(name) + } + end end - def test_generator_vars - model = Rails::Generator.instance('model', ['model']) - assert_equal "#{Rails::Generator::BUILTIN_ROOT}/model/templates", model.template_root - assert_equal RAILS_ROOT, model.destination_root - assert_equal 'Model', model.class_name - assert_equal 'model', model.singular_name - assert_equal 'models', model.plural_name - assert_equal model.singular_name, model.file_name - assert_equal model.plural_name, model.table_name - assert_equal [], model.args + def test_generator_spec + spec = Rails::Generator::Base.lookup('working') + assert_equal 'working', spec.name + assert_equal "#{RAILS_ROOT}/script/generators/working", spec.path + assert_equal :app, spec.source + assert_nothing_raised { assert_match /WorkingGenerator$/, spec.klass.name } end - def test_generator_generator - assert_nothing_raised { - model = Rails::Generator.instance('model', ['name']) - mailer = model.send(:generator, 'mailer') - assert_equal 'mailer', mailer.class.generator_name - } + 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 + assert_equal 'Foo', g.class_name + assert_equal 'foo', g.singular_name + assert_equal 'foos', g.plural_name + assert_equal g.singular_name, g.file_name + assert_equal g.plural_name, g.table_name + assert_equal %w(bar baz), g.args end end -- cgit v1.2.3