public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Cleanup generator tests by extracting repeated code into generator_test_helper. 
Add test for mailer generator.
Signed-off-by: Joshua Peek <josh@joshpeek.com>
Mislav Marohnić (author)
Fri Apr 18 15:10:58 -0700 2008
josh (committer)
Fri Apr 18 15:10:58 -0700 2008
commit  dfdb9f738e9842752c340634622624544efe18c1
tree    48bf88138e05da9b270f947f3b0d1c8b5eda9024
parent  986aec5dbbdfb578945e706cbe6a54c4f06640e5
...
11
12
13
14
15
16
17
18
19
 
 
20
21
22
...
11
12
13
 
 
 
 
 
 
14
15
16
17
18
0
@@ -11,12 +11,8 @@ class MailerGenerator < Rails::Generator::NamedBase
0
       m.directory File.join('test/fixtures', file_path)
0
 
0
       # Mailer class and unit test.
0
-      m.template "mailer.rb",    File.join('app/models',
0
-                                           class_path,
0
-                                           "#{file_name}.rb")
0
-      m.template "unit_test.rb", File.join('test/unit',
0
-                                           class_path,
0
-                                           "#{file_name}_test.rb")
0
+      m.template "mailer.rb",    File.join('app/models', class_path, "#{file_name}.rb")
0
+      m.template "unit_test.rb", File.join('test/unit', class_path, "#{file_name}_test.rb")
0
 
0
       # View template and fixture for each action.
0
       actions.each do |action|
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
...
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
...
45
46
47
48
49
 
 
50
51
52
...
63
64
65
66
 
67
68
69
...
72
73
74
75
76
 
 
77
78
79
...
89
90
91
92
 
 
93
94
95
96
97
98
99
100
 
 
 
101
102
 
103
104
105
...
108
109
110
111
112
 
 
113
114
 
115
116
117
...
130
131
132
133
 
 
134
135
136
...
147
148
149
150
 
151
152
 
153
154
155
156
157
158
159
160
161
162
163
164
165
 
 
 
 
 
 
166
 
167
168
169
...
175
176
177
178
 
 
179
180
181
182
183
184
185
 
186
187
188
 
 
189
190
191
192
193
194
 
 
195
196
...
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
...
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
117
118
...
120
121
122
 
 
123
124
125
126
127
...
138
139
140
 
141
142
143
144
...
147
148
149
 
 
150
151
152
153
154
...
164
165
166
 
167
168
169
170
171
172
173
 
 
 
174
175
176
177
 
178
179
180
181
...
184
185
186
 
 
187
188
189
 
190
191
192
193
...
206
207
208
 
209
210
211
212
213
...
224
225
226
 
227
228
 
229
230
231
232
233
234
 
 
 
 
 
 
 
 
235
236
237
238
239
240
241
242
243
244
245
...
251
252
253
 
254
255
256
257
258
259
260
261
 
262
263
 
 
264
265
266
267
268
269
 
 
270
271
272
273
0
@@ -1,3 +1,51 @@
0
+require 'test/unit'
0
+require 'fileutils'
0
+
0
+# Mock out what we need from AR::Base
0
+module ActiveRecord
0
+  class Base
0
+    class << self
0
+      attr_accessor :pluralize_table_names
0
+    end
0
+    self.pluralize_table_names = true
0
+  end
0
+
0
+  module ConnectionAdapters
0
+    class Column
0
+      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
0
+      
0
+      def initialize(name, default, sql_type = nil)
0
+        @name = name
0
+        @default = default
0
+        @type = @sql_type = sql_type
0
+      end
0
+
0
+      def human_name
0
+        @name.humanize
0
+      end
0
+    end
0
+  end
0
+end
0
+
0
+# Mock up necessities from ActionView
0
+module ActionView
0
+  module Helpers
0
+    module ActionRecordHelper; end
0
+    class InstanceTag; end
0
+  end
0
+end
0
+
0
+# Set RAILS_ROOT appropriately fixture generation
0
+tmp_dir = "#{File.dirname(__FILE__)}/../fixtures/tmp"
0
+
0
+if defined? RAILS_ROOT
0
+  RAILS_ROOT.replace tmp_dir
0
+else
0
+  RAILS_ROOT = tmp_dir
0
+end
0
+FileUtils.mkdir_p RAILS_ROOT
0
+
0
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
0
 require 'initializer'
0
 
0
 # Mocks out the configuration
0
@@ -9,35 +57,62 @@ end
0
 
0
 require 'rails_generator'
0
 
0
+class GeneratorTestCase < Test::Unit::TestCase
0
+  include FileUtils
0
+  
0
+  def setup
0
+    ActiveRecord::Base.pluralize_table_names = true
0
+    
0
+    mkdir_p "#{RAILS_ROOT}/app/views/layouts"
0
+    mkdir_p "#{RAILS_ROOT}/config"
0
+    mkdir_p "#{RAILS_ROOT}/db"
0
+    mkdir_p "#{RAILS_ROOT}/test/fixtures"
0
+    mkdir_p "#{RAILS_ROOT}/public/stylesheets"
0
+    
0
+    File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
0
+      f << "ActionController::Routing::Routes.draw do |map|\n\nend"
0
+    end
0
+  end
0
 
0
-module GeneratorTestHelper
0
+  def teardown
0
+    rm_rf "#{RAILS_ROOT}/app"
0
+    rm_rf "#{RAILS_ROOT}/test"
0
+    rm_rf "#{RAILS_ROOT}/config"
0
+    rm_rf "#{RAILS_ROOT}/db"
0
+    rm_rf "#{RAILS_ROOT}/public"
0
+  end
0
+
0
+  def test_truth
0
+    # don't complain, test/unit
0
+  end
0
+  
0
   # Instantiates the Generator
0
-  def build_generator(name,params)
0
-    Rails::Generator::Base.instance(name,params)
0
+  def build_generator(name, params)
0
+    Rails::Generator::Base.instance(name, params)
0
   end
0
 
0
   # Runs the create command (like the command line does)
0
-  def run_generator(name,params)
0
+  def run_generator(name, params)
0
     silence_generator do
0
-      build_generator(name,params).command(:create).invoke!
0
+      build_generator(name, params).command(:create).invoke!
0
     end
0
   end
0
 
0
   # Silences the logger temporarily and returns the output as a String
0
   def silence_generator
0
-    logger_original=Rails::Generator::Base.logger
0
-    myout=StringIO.new
0
-    Rails::Generator::Base.logger=Rails::Generator::SimpleLogger.new(myout)
0
+    logger_original = Rails::Generator::Base.logger
0
+    myout = StringIO.new
0
+    Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(myout)
0
     yield if block_given?
0
-    Rails::Generator::Base.logger=logger_original
0
+    Rails::Generator::Base.logger = logger_original
0
     myout.string
0
   end
0
 
0
   # asserts that the given controller was generated.
0
   # It takes a name or symbol without the <tt>_controller</tt> part and an optional super class.
0
   # The contents of the class source file is passed to a block.
0
-  def assert_generated_controller_for(name,parent="ApplicationController")
0
-    assert_generated_class "app/controllers/#{name.to_s.underscore}_controller",parent do |body|
0
+  def assert_generated_controller_for(name, parent = "ApplicationController")
0
+    assert_generated_class "app/controllers/#{name.to_s.underscore}_controller", parent do |body|
0
       yield body if block_given?
0
     end
0
   end
0
@@ -45,8 +120,8 @@ module GeneratorTestHelper
0
   # asserts that the given model was generated.
0
   # It takes a name or symbol and an optional super class.
0
   # the contents of the class source file is passed to a block.
0
-  def assert_generated_model_for(name,parent="ActiveRecord::Base")
0
-    assert_generated_class "app/models/#{name.to_s.underscore}",parent do |body|
0
+  def assert_generated_model_for(name, parent = "ActiveRecord::Base")
0
+    assert_generated_class "app/models/#{name.to_s.underscore}", parent do |body|
0
       yield body if block_given?
0
     end
0
   end
0
@@ -63,7 +138,7 @@ module GeneratorTestHelper
0
   # asserts that the given functional test was generated.
0
   # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class.
0
   # the contents of the class source file is passed to a block.
0
-  def assert_generated_functional_test_for(name,parent="ActionController::TestCase")
0
+  def assert_generated_functional_test_for(name, parent = "ActionController::TestCase")
0
     assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body|
0
       yield body if block_given?
0
     end
0
@@ -72,8 +147,8 @@ module GeneratorTestHelper
0
   # asserts that the given unit test was generated.
0
   # It takes a name or symbol without the <tt>_test</tt> part and an optional super class.
0
   # the contents of the class source file is passed to a block.
0
-  def assert_generated_unit_test_for(name,parent="ActiveSupport::TestCase")
0
-    assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body|
0
+  def assert_generated_unit_test_for(name, parent = "ActiveSupport::TestCase")
0
+    assert_generated_class "test/unit/#{name.to_s.underscore}_test", parent do |body|
0
       yield body if block_given?
0
     end
0
   end
0
@@ -89,17 +164,18 @@ module GeneratorTestHelper
0
 
0
   # asserts that the given file exists
0
   def assert_file_exists(path)
0
-    assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist"
0
+    assert File.exist?("#{RAILS_ROOT}/#{path}"),
0
+      "The file '#{RAILS_ROOT}/#{path}' should exist"
0
   end
0
 
0
   # asserts that the given class source file was generated.
0
   # It takes a path without the <tt>.rb</tt> part and an optional super class.
0
   # the contents of the class source file is passed to a block.
0
-  def assert_generated_class(path,parent=nil)
0
-    path=~/\/?(\d+_)?(\w+)$/
0
-    class_name=$2.camelize
0
+  def assert_generated_class(path, parent=nil)
0
+    path =~ /\/?(\d+_)?(\w+)$/
0
+    class_name = $2.camelize
0
     assert_generated_file("#{path}.rb") do |body|
0
-      assert body=~/class #{class_name}#{parent.nil? ? '':" < #{parent}"}/,"the file '#{path}.rb' should be a class"
0
+      assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class"
0
       yield body if block_given?
0
     end
0
   end
0
@@ -108,10 +184,10 @@ module GeneratorTestHelper
0
   # It takes a path without the <tt>.rb</tt> part.
0
   # the contents of the class source file is passed to a block.
0
   def assert_generated_module(path)
0
-    path=~/\/?(\w+)$/
0
-    module_name=$1.camelize
0
+    path =~ /\/?(\w+)$/
0
+    module_name = $1.camelize
0
     assert_generated_file("#{path}.rb") do |body|
0
-      assert body=~/module #{module_name}/,"the file '#{path}.rb' should be a module"
0
+      assert_match /module #{module_name}/, body, "the file '#{path}.rb' should be a module"
0
       yield body if block_given?
0
     end
0
   end
0
@@ -130,7 +206,8 @@ module GeneratorTestHelper
0
   # the parsed yaml tree is passed to a block.
0
   def assert_generated_yaml(path)
0
     assert_generated_file("#{path}.yml") do |body|
0
-      assert yaml=YAML.load(body)
0
+      yaml = YAML.load(body)
0
+      assert yaml, 'YAML data missing'
0
       yield yaml if block_given?
0
     end
0
   end
0
@@ -147,23 +224,22 @@ module GeneratorTestHelper
0
   # asserts that the given views were generated.
0
   # It takes a controller name and a list of views (including extensions).
0
   # The body of each view is passed to a block
0
-  def assert_generated_views_for(name,*actions)
0
+  def assert_generated_views_for(name, *actions)
0
     actions.each do |action|
0
-      assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body|
0
+      assert_generated_file("app/views/#{name.to_s.underscore}/#{action}") do |body|
0
         yield body if block_given?
0
       end
0
     end
0
   end
0
 
0
-  def assert_generated_migration(name,parent="ActiveRecord::Migration")
0
-      file =
0
-   Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first
0
-      file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s
0
-      assert_generated_class file,parent do |body|
0
-        assert body=~/timestamps/, "should have timestamps defined"
0
-        yield body if block_given?
0
-      end
0
+  def assert_generated_migration(name, parent = "ActiveRecord::Migration")
0
+    file = Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first
0
+    file = file.match(/db\/migrate\/[0-9]+_\w+/).to_s
0
+    assert_generated_class file, parent do |body|
0
+      assert_match /timestamps/, body, "should have timestamps defined"
0
+      yield body if block_given?
0
     end
0
+  end
0
 
0
   # Asserts that the given migration file was not generated.
0
   # It takes the name of the migration as a parameter.
0
@@ -175,22 +251,23 @@ module GeneratorTestHelper
0
   # asserts that the given resource was added to the routes.
0
   def assert_added_route_for(name)
0
     assert_generated_file("config/routes.rb") do |body|
0
-      assert body=~/map.resources :#{name.to_s.underscore}/,"should add route for :#{name.to_s.underscore}"
0
+      assert_match /map.resources :#{name.to_s.underscore}/, body,
0
+        "should add route for :#{name.to_s.underscore}"
0
     end
0
   end
0
 
0
   # asserts that the given methods are defined in the body.
0
   # This does assume standard rails code conventions with regards to the source code.
0
   # The body of each individual method is passed to a block.
0
-  def assert_has_method(body,*methods)
0
+  def assert_has_method(body, *methods)
0
     methods.each do |name|
0
-      assert body=~/^  def #{name.to_s}\n((\n|   .*\n)*)  end/,"should have method #{name.to_s}"
0
-      yield( name, $1 ) if block_given?
0
+      assert body =~ /^  def #{name}(\(.+\))?\n((\n|   .*\n)*)  end/, "should have method #{name}"
0
+      yield(name, $2) if block_given?
0
     end
0
   end
0
 
0
   # asserts that the given column is defined in the migration
0
-  def assert_generated_column(body,name,type)
0
-      assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined"
0
+  def assert_generated_column(body, name, type)
0
+    assert_match /t\.#{type.to_s} :#{name.to_s}/, body, "should have column #{name.to_s} defined"
0
   end
0
 end
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
0
@@ -1,84 +1,6 @@
0
-require 'test/unit'
0
-
0
-# Optionally load RubyGems
0
-begin
0
-  require 'rubygems'
0
-rescue LoadError
0
-end
0
-
0
-# Mock out what we need from AR::Base
0
-module ActiveRecord
0
-  class Base
0
-    class << self
0
-      attr_accessor :pluralize_table_names
0
-    end
0
-    self.pluralize_table_names = true
0
-  end
0
-
0
-  module ConnectionAdapters
0
-    class Column
0
-      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
0
-      def initialize(name, default, sql_type=nil)
0
-        @namename
0
-        @default=default
0
-        @type=@sql_type=sql_type
0
-      end
0
-
0
-      def human_name
0
-        @name.humanize
0
-      end
0
-    end
0
-  end
0
-end
0
-
0
-# Mock up necessities from ActionView
0
-module ActionView
0
-  module Helpers
0
-    module ActionRecordHelper; end
0
-    class InstanceTag; end
0
-  end
0
-end
0
-
0
-# Set RAILS_ROOT appropriately fixture generation
0
-tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
0
-if defined?(RAILS_ROOT)
0
-  RAILS_ROOT.replace(tmp_dir)
0
-else
0
-  RAILS_ROOT=tmp_dir
0
-end
0
-Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
0
-
0
-
0
-$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
0
-
0
 require 'generators/generator_test_helper'
0
 
0
-class RailsModelGeneratorTest < Test::Unit::TestCase
0
-  include GeneratorTestHelper
0
-
0
-  def setup
0
-    ActiveRecord::Base.pluralize_table_names = true
0
-    Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
0
-    Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
0
-    Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
0
-    Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
0
-    Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
0
-    Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
0
-    Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
0
-    Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
0
-    Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets")
0
-    File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
0
-      f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
0
-    end
0
-  end
0
-
0
-    def teardown
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/app"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/test"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/config"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/db"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/public"
0
-  end
0
+class RailsModelGeneratorTest < GeneratorTestCase
0
 
0
   def test_model_generates_resources
0
     run_generator('model', %w(Product name:string))
...
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
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
6
0
@@ -1,82 +1,6 @@
0
-require 'test/unit'
0
-
0
-# Optionally load RubyGems
0
-begin
0
-  require 'rubygems'
0
-rescue LoadError
0
-end
0
-
0
-# Mock out what we need from AR::Base
0
-module ActiveRecord
0
-  class Base
0
-    class << self
0
-      attr_accessor :pluralize_table_names
0
-    end
0
-    self.pluralize_table_names = true
0
-  end
0
-
0
-  module ConnectionAdapters
0
-    class Column
0
-      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
0
-      def initialize(name, default, sql_type=nil)
0
-        @namename
0
-        @default=default
0
-        @type=@sql_type=sql_type
0
-      end
0
-
0
-      def human_name
0
-        @name.humanize
0
-      end
0
-    end
0
-  end
0
-end
0
-
0
-# Mock up necessities from ActionView
0
-module ActionView
0
-  module Helpers
0
-    module ActionRecordHelper; end
0
-    class InstanceTag; end
0
-  end
0
-end
0
-
0
-# Set RAILS_ROOT appropriately fixture generation
0
-tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
0
-if defined?(RAILS_ROOT)
0
-  RAILS_ROOT.replace(tmp_dir)
0
-else
0
-  RAILS_ROOT=tmp_dir
0
-end
0
-Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
0
-
0
-$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
0
 require 'generators/generator_test_helper'
0
 
0
-class RailsResourceGeneratorTest < Test::Unit::TestCase
0
-  include GeneratorTestHelper
0
-
0
-  def setup
0
-    ActiveRecord::Base.pluralize_table_names = true
0
-    Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
0
-    Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
0
-    Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
0
-    Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
0
-    Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
0
-    Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
0
-    Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
0
-    Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
0
-    Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets")
0
-    File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
0
-      f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
0
-    end
0
-  end
0
-
0
-  def teardown
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/app"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/test"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/config"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/db"
0
-    FileUtils.rm_rf "#{RAILS_ROOT}/public"
0
-  end
0
+class RailsResourceGeneratorTest < GeneratorTestCase
0
 
0
   def test_resource_generates_resources
0
     run_generator('resource', %w(Product name:string))
...
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
117
 
 
 
118
 
 
119
120
121
122
123
124
125
126
127
 
 
 
 
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
 
 
 
 
 
 
146
147
148
 
 
 
149
150
 
 
 
 
 
 
 
 
 
 
 
 
 
151
152
153
154
 
 
155
156
157
158
 
159
160
161
162
 
 
 
163
164
165
166
167
 
 
 
168
 
 
169
170
171
172
173
174
175
176
177
178
179
180
181
182
 
 
 
183
184
185
186
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
189
190
 
 
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
0
@@ -1,189 +1,107 @@
0
-require 'abstract_unit'
0
-
0
-# Optionally load RubyGems.
0
-begin
0
-  require 'rubygems'
0
-rescue LoadError
0
-end
0
-
0
-# Mock out what we need from AR::Base.
0
-module ActiveRecord
0
-  class Base
0
-    class << self
0
-      attr_accessor :pluralize_table_names
0
-    end
0
-    self.pluralize_table_names = true
0
-  end
0
-
0
-  module ConnectionAdapters
0
-    class Column
0
-      attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
0
-
0
-      def initialize(name, default, sql_type = nil)
0
-        @name=name
0
-        @default=default
0
-        @type=@sql_type=sql_type
0
-      end
0
-
0
-      def human_name
0
-        @name.humanize
0
-      end
0
-    end
0
-  end
0
-end
0
+require 'generators/generator_test_helper'
0
 
0
-# And what we need from ActionView
0
-module ActionView
0
-  module Helpers
0
-    module ActiveRecordHelper; end
0
-    class InstanceTag; end
0
+class RailsScaffoldGeneratorTest < GeneratorTestCase
0
+  
0
+  def test_scaffolded_names
0
+    g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))
0
+    assert_equal "ProductLines", g.controller_name
0
+    assert_equal "ProductLines", g.controller_class_name
0
+    assert_equal "ProductLine", g.controller_singular_name
0
+    assert_equal "product_lines", g.controller_plural_name
0
+    assert_equal "product_lines", g.controller_file_name
0
+    assert_equal "product_lines", g.controller_table_name
0
   end
0
-end
0
 
0
+  def test_scaffold_generates_resources
0
 
0
-# Must set before requiring generator libs.
0
-tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
0
-if defined?(RAILS_ROOT)
0
-  RAILS_ROOT.replace(tmp_dir)
0
-else
0
-  RAILS_ROOT=tmp_dir
0
-end
0
-Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
0
-
0
-$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
0
+    run_generator('scaffold', %w(Product name:string))
0
 
0
-require 'generators/generator_test_helper'
0
+    assert_generated_controller_for :products do |f|
0
 
0
-uses_mocha "Scaffold Generator Tests" do
0
-  class RailsScaffoldGeneratorTest < Test::Unit::TestCase
0
-
0
-    include GeneratorTestHelper
0
-
0
-    def setup
0
-      ActiveRecord::Base.pluralize_table_names = true
0
-      Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
0
-      Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
0
-      Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
0
-      Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
0
-      Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
0
-      Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
0
-      Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
0
-      Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
0
-      Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets")
0
-      File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
0
-        f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
0
+      assert_has_method f, :index do |name, m|
0
+        assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
0
       end
0
-    end
0
-
0
-    def teardown
0
-      FileUtils.rm_rf "#{RAILS_ROOT}/app"
0
-      FileUtils.rm_rf "#{RAILS_ROOT}/test"
0
-      FileUtils.rm_rf "#{RAILS_ROOT}/config"
0
-      FileUtils.rm_rf "#{RAILS_ROOT}/db"
0
-      FileUtils.rm_rf "#{RAILS_ROOT}/public"
0
-    end
0
-
0
-    def test_scaffolded_names
0
-      g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))
0
-      assert_equal "ProductLines", g.controller_name
0
-      assert_equal "ProductLines", g.controller_class_name
0
-      assert_equal "ProductLine", g.controller_singular_name
0
-      assert_equal "product_lines", g.controller_plural_name
0
-      assert_equal "product_lines", g.controller_file_name
0
-      assert_equal "product_lines", g.controller_table_name
0
-    end
0
-
0
-    def test_scaffold_generates_resources
0
-
0
-      run_generator('scaffold', %w(Product name:string))
0
 
0
-      assert_generated_controller_for :products do |f|
0
-
0
-        assert_has_method f, :index do |name, m|
0
-          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
0
-        end
0
-
0
-        assert_has_method f, :show, :edit, :update, :destroy do |name, m|
0
-          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
0
-        end
0
-
0
-        assert_has_method f, :new do |name, m|
0
-          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
0
-        end
0
-
0
-        assert_has_method f, :create do |name, m|
0
-          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
0
-          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
0
-        end
0
+      assert_has_method f, :show, :edit, :update, :destroy do |name, m|
0
+        assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
0
+      end
0
 
0
+      assert_has_method f, :new do |name, m|
0
+        assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
0
       end
0
 
0
-      assert_generated_model_for :product
0
-      assert_generated_functional_test_for :products
0
-      assert_generated_unit_test_for :product
0
-      assert_generated_fixtures_for :products
0
-      assert_generated_helper_for :products
0
-      assert_generated_stylesheet :scaffold
0
-      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
0
+      assert_has_method f, :create do |name, m|
0
+        assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
0
+        assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
0
+      end
0
 
0
-      assert_generated_migration :create_products
0
-      assert_added_route_for :products
0
     end
0
 
0
-    def test_scaffold_skip_migration_skips_migration
0
-      run_generator('scaffold', %w(Product name:string --skip-migration))
0
-
0
-      assert_generated_model_for :product
0
-      assert_generated_functional_test_for :products
0
-      assert_generated_unit_test_for :product
0
-      assert_generated_fixtures_for :products
0
-      assert_generated_helper_for :products
0
-      assert_generated_stylesheet :scaffold
0
-      assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
0
-      assert_skipped_migration :create_products
0
-      assert_added_route_for :products
0
-    end
0
+    assert_generated_model_for :product
0
+    assert_generated_functional_test_for :products
0
+    assert_generated_unit_test_for :product
0
+    assert_generated_fixtures_for :products
0
+    assert_generated_helper_for :products
0
+    assert_generated_stylesheet :scaffold
0
+    assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
0
 
0
-    def test_scaffold_generates_resources_with_attributes
0
-      run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
0
+    assert_generated_migration :create_products
0
+    assert_added_route_for :products
0
+  end
0
 
0
-      assert_generated_controller_for :products do |f|
0
+  def test_scaffold_skip_migration_skips_migration
0
+    run_generator('scaffold', %w(Product name:string --skip-migration))
0
+
0
+    assert_generated_model_for :product
0
+    assert_generated_functional_test_for :products
0
+    assert_generated_unit_test_for :product
0
+    assert_generated_fixtures_for :products
0
+    assert_generated_helper_for :products
0
+    assert_generated_stylesheet :scaffold
0
+    assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
0
+    assert_skipped_migration :create_products
0
+    assert_added_route_for :products
0
+  end
0
 
0
-        assert_has_method f, :index do |name, m|
0
-          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
0
-        end
0
+  def test_scaffold_generates_resources_with_attributes
0
+    run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
0
 
0
-        assert_has_method f, :show, :edit, :update, :destroy do |name, m|
0
-          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
0
-        end
0
+    assert_generated_controller_for :products do |f|
0
 
0
-        assert_has_method f, :new do |name, m|
0
-          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
0
-        end
0
+      assert_has_method f, :index do |name, m|
0
+        assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
0
+      end
0
 
0
-        assert_has_method f, :create do |name, m|
0
-          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
0
-          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
0
-        end
0
+      assert_has_method f, :show, :edit, :update, :destroy do |name, m|
0
+        assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
0
+      end
0
 
0
+      assert_has_method f, :new do |name, m|
0
+        assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
0
       end
0
 
0
-      assert_generated_model_for :product
0
-      assert_generated_functional_test_for :products
0
-      assert_generated_unit_test_for :product
0
-      assert_generated_fixtures_for :products
0
-      assert_generated_helper_for :products
0
-      assert_generated_stylesheet :scaffold
0
-      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
0
-
0
-      assert_generated_migration :create_products do |t|
0
-        assert_generated_column t, :name, :string
0
-        assert_generated_column t, :supplier_id, :integer
0
-        assert_generated_column t, :created_at, :timestamp
0
+      assert_has_method f, :create do |name, m|
0
+        assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
0
+        assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
0
       end
0
 
0
-      assert_added_route_for :products
0
     end
0
 
0
+    assert_generated_model_for :product
0
+    assert_generated_functional_test_for :products
0
+    assert_generated_unit_test_for :product
0
+    assert_generated_fixtures_for :products
0
+    assert_generated_helper_for :products
0
+    assert_generated_stylesheet :scaffold
0
+    assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
0
+
0
+    assert_generated_migration :create_products do |t|
0
+      assert_generated_column t, :name, :string
0
+      assert_generated_column t, :supplier_id, :integer
0
+      assert_generated_column t, :created_at, :timestamp
0
+    end
0
+
0
+    assert_added_route_for :products
0
   end
0
-end
0
\ No newline at end of file
0
+
0
+end

Comments