aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators/named_base_test.rb
blob: f23701e99e3fa337e5315c6a456d2279f10af567 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'

# 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

class NamedBaseTest < Rails::Generators::TestCase
  include GeneratorsTestHelper
  tests Rails::Generators::ScaffoldControllerGenerator

  def test_named_generator_with_underscore
    g = generator ['line_item']
    assert_name g, 'line_item',  :name
    assert_name g, %w(),         :class_path
    assert_name g, 'LineItem',   :class_name
    assert_name g, 'line_item',  :file_path
    assert_name g, 'line_item',  :file_name
    assert_name g, 'Line item',  :human_name
    assert_name g, 'line_item',  :singular_name
    assert_name g, 'line_items', :plural_name
    assert_name g, 'line_item',  :i18n_scope
    assert_name g, 'line_items', :table_name
  end

  def test_named_generator_attributes
    g = generator ['admin/foo']
    assert_name g, 'admin/foo',  :name
    assert_name g, %w(admin),    :class_path
    assert_name g, 'Admin::Foo', :class_name
    assert_name g, 'admin/foo',  :file_path
    assert_name g, 'foo',        :file_name
    assert_name g, 'Foo',        :human_name
    assert_name g, 'foo',        :singular_name
    assert_name g, 'foos',       :plural_name
    assert_name g, 'admin.foo',  :i18n_scope
    assert_name g, 'admin_foos', :table_name
  end

  def test_named_generator_attributes_as_ruby
    g = generator ['Admin::Foo']
    assert_name g, 'Admin::Foo', :name
    assert_name g, %w(admin),    :class_path
    assert_name g, 'Admin::Foo', :class_name
    assert_name g, 'admin/foo',  :file_path
    assert_name g, 'foo',        :file_name
    assert_name g, 'foo',        :singular_name
    assert_name g, 'Foo',        :human_name
    assert_name g, 'foos',       :plural_name
    assert_name g, 'admin.foo',  :i18n_scope
    assert_name g, 'admin_foos', :table_name
  end

  def test_named_generator_attributes_without_pluralized
    ActiveRecord::Base.pluralize_table_names = false
    g = generator ['admin/foo']
    assert_name g, 'admin_foo', :table_name
  ensure
    ActiveRecord::Base.pluralize_table_names = true
  end

  def test_scaffold_plural_names
    g = generator ['admin/foo']
    assert_name g, 'admin/foos',  :controller_name
    assert_name g, %w(admin),     :controller_class_path
    assert_name g, 'Admin::Foos', :controller_class_name
    assert_name g, 'admin/foos',  :controller_file_path
    assert_name g, 'foos',        :controller_file_name
    assert_name g, 'admin.foos',  :controller_i18n_scope
  end

  def test_scaffold_plural_names_as_ruby
    g = generator ['Admin::Foo']
    assert_name g, 'Admin::Foos', :controller_name
    assert_name g, %w(admin),     :controller_class_path
    assert_name g, 'Admin::Foos', :controller_class_name
    assert_name g, 'admin/foos',  :controller_file_path
    assert_name g, 'foos',        :controller_file_name
    assert_name g, 'admin.foos',  :controller_i18n_scope
  end

  def test_application_name
    g = generator ['Admin::Foo']
    Rails.stubs(:application).returns(Object.new)
    assert_name g, "object", :application_name
    Rails.stubs(:application).returns(nil)
    assert_name g, "application", :application_name
  end

  def test_index_helper
    g = generator ['Post']
    assert_name g, 'posts', :index_helper
  end

  def test_index_helper_to_pluralize_once
    g = generator ['Stadium']
    assert_name g, 'stadia', :index_helper
  end

  def test_index_helper_with_uncountable
    g = generator ['Sheep']
    assert_name g, 'sheep_index', :index_helper
  end

  protected

    def assert_name(generator, value, method)
      assert_equal value, generator.send(method)
    end
end