aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/rails_generator_test.rb
blob: 3a699b04b24a940cd4135f56edaec81265a2cda0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
$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


class RailsGeneratorTest < Test::Unit::TestCase
  BUILTINS = %w(controller mailer model scaffold)

  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_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
    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')
    }
  end

  def test_builtin_generators
    assert_nothing_raised {
      assert_equal [], Rails::Generator.builtin_generators - BUILTINS
    }
  end

  def test_generator_name
    assert_equal 'model', Rails::Generator.instance('model', ['name']).class.generator_name
  end

  def test_generator_usage
    assert_raise(Rails::Generator::UsageError) {
      assert_equal 'model', Rails::Generator.instance('model')
    }
  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
  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
    }
  end
end