rubigen 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +3 -0
- data/License.txt +17 -0
- data/Manifest.txt +119 -0
- data/README.txt +204 -0
- data/Rakefile +142 -0
- data/bin/ruby_app +12 -0
- data/examples/rails_generators/applications/app/USAGE +16 -0
- data/examples/rails_generators/applications/app/app_generator.rb +177 -0
- data/examples/rails_generators/components/controller/USAGE +29 -0
- data/examples/rails_generators/components/controller/controller_generator.rb +37 -0
- data/examples/rails_generators/components/controller/templates/controller.rb +10 -0
- data/examples/rails_generators/components/controller/templates/functional_test.rb +18 -0
- data/examples/rails_generators/components/controller/templates/helper.rb +2 -0
- data/examples/rails_generators/components/controller/templates/view.html.erb +2 -0
- data/examples/rails_generators/components/integration_test/USAGE +8 -0
- data/examples/rails_generators/components/integration_test/integration_test_generator.rb +16 -0
- data/examples/rails_generators/components/integration_test/templates/integration_test.rb +10 -0
- data/examples/rails_generators/components/mailer/USAGE +16 -0
- data/examples/rails_generators/components/mailer/mailer_generator.rb +34 -0
- data/examples/rails_generators/components/mailer/templates/fixture.erb +3 -0
- data/examples/rails_generators/components/mailer/templates/fixture.rhtml +0 -0
- data/examples/rails_generators/components/mailer/templates/mailer.rb +13 -0
- data/examples/rails_generators/components/mailer/templates/unit_test.rb +37 -0
- data/examples/rails_generators/components/mailer/templates/view.erb +3 -0
- data/examples/rails_generators/components/mailer/templates/view.rhtml +0 -0
- data/examples/rails_generators/components/migration/USAGE +24 -0
- data/examples/rails_generators/components/migration/migration_generator.rb +20 -0
- data/examples/rails_generators/components/migration/templates/migration.rb +7 -0
- data/examples/rails_generators/components/model/USAGE +27 -0
- data/examples/rails_generators/components/model/model_generator.rb +38 -0
- data/examples/rails_generators/components/model/templates/fixtures.yml +15 -0
- data/examples/rails_generators/components/model/templates/migration.rb +14 -0
- data/examples/rails_generators/components/model/templates/model.rb +2 -0
- data/examples/rails_generators/components/model/templates/unit_test.rb +10 -0
- data/examples/rails_generators/components/observer/USAGE +13 -0
- data/examples/rails_generators/components/observer/observer_generator.rb +16 -0
- data/examples/rails_generators/components/observer/templates/observer.rb +2 -0
- data/examples/rails_generators/components/observer/templates/unit_test.rb +10 -0
- data/examples/rails_generators/components/plugin/USAGE +25 -0
- data/examples/rails_generators/components/plugin/plugin_generator.rb +39 -0
- data/examples/rails_generators/components/plugin/templates/MIT-LICENSE +20 -0
- data/examples/rails_generators/components/plugin/templates/README +13 -0
- data/examples/rails_generators/components/plugin/templates/Rakefile +22 -0
- data/examples/rails_generators/components/plugin/templates/USAGE +8 -0
- data/examples/rails_generators/components/plugin/templates/generator.rb +8 -0
- data/examples/rails_generators/components/plugin/templates/init.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/install.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/plugin.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/tasks.rake +4 -0
- data/examples/rails_generators/components/plugin/templates/uninstall.rb +1 -0
- data/examples/rails_generators/components/plugin/templates/unit_test.rb +8 -0
- data/examples/rails_generators/components/resource/USAGE +23 -0
- data/examples/rails_generators/components/resource/resource_generator.rb +72 -0
- data/examples/rails_generators/components/resource/templates/USAGE +18 -0
- data/examples/rails_generators/components/resource/templates/controller.rb +2 -0
- data/examples/rails_generators/components/resource/templates/fixtures.yml +0 -0
- data/examples/rails_generators/components/resource/templates/functional_test.rb +20 -0
- data/examples/rails_generators/components/resource/templates/helper.rb +2 -0
- data/examples/rails_generators/components/scaffold/USAGE +25 -0
- data/examples/rails_generators/components/scaffold/scaffold_generator.rb +90 -0
- data/examples/rails_generators/components/scaffold/templates/controller.rb +85 -0
- data/examples/rails_generators/components/scaffold/templates/functional_test.rb +57 -0
- data/examples/rails_generators/components/scaffold/templates/helper.rb +2 -0
- data/examples/rails_generators/components/scaffold/templates/layout.html.erb +17 -0
- data/examples/rails_generators/components/scaffold/templates/style.css +74 -0
- data/examples/rails_generators/components/scaffold/templates/view_edit.html.erb +19 -0
- data/examples/rails_generators/components/scaffold/templates/view_index.html.erb +24 -0
- data/examples/rails_generators/components/scaffold/templates/view_new.html.erb +18 -0
- data/examples/rails_generators/components/scaffold/templates/view_show.html.erb +10 -0
- data/examples/rails_generators/components/session_migration/USAGE +10 -0
- data/examples/rails_generators/components/session_migration/session_migration_generator.rb +18 -0
- data/examples/rails_generators/components/session_migration/templates/migration.rb +16 -0
- data/examples/rails_generators/components/web_service/USAGE +24 -0
- data/examples/rails_generators/components/web_service/templates/api_definition.rb +5 -0
- data/examples/rails_generators/components/web_service/templates/controller.rb +8 -0
- data/examples/rails_generators/components/web_service/templates/functional_test.rb +19 -0
- data/examples/rails_generators/components/web_service/web_service_generator.rb +29 -0
- data/lib/rubigen/base.rb +168 -0
- data/lib/rubigen/commands.rb +632 -0
- data/lib/rubigen/generated_attribute.rb +40 -0
- data/lib/rubigen/generators/applications/ruby_app/USAGE +10 -0
- data/lib/rubigen/generators/applications/ruby_app/ruby_app_generator.rb +78 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/README.txt +1 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/configs/empty.log +0 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/fresh_rakefile +10 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/module.rb +5 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/script/generate +13 -0
- data/lib/rubigen/generators/applications/ruby_app/templates/test_helper.rb +2 -0
- data/lib/rubigen/generators/components/test_unit/USAGE +14 -0
- data/lib/rubigen/generators/components/test_unit/templates/test +14 -0
- data/lib/rubigen/generators/components/test_unit/test_unit_generator.rb +26 -0
- data/lib/rubigen/helpers/generator_test_helper.rb +133 -0
- data/lib/rubigen/lookup.rb +296 -0
- data/lib/rubigen/manifest.rb +51 -0
- data/lib/rubigen/options.rb +136 -0
- data/lib/rubigen/scripts/destroy.rb +27 -0
- data/lib/rubigen/scripts/generate.rb +7 -0
- data/lib/rubigen/scripts/update.rb +12 -0
- data/lib/rubigen/scripts.rb +69 -0
- data/lib/rubigen/simple_logger.rb +44 -0
- data/lib/rubigen/spec.rb +42 -0
- data/lib/rubigen/version.rb +9 -0
- data/lib/rubigen.rb +44 -0
- data/script/destroy +9 -0
- data/script/generate +9 -0
- data/script/txt2html +65 -0
- data/setup.rb +1585 -0
- data/test/examples_from_rails/generator_test_helper.rb +195 -0
- data/test/examples_from_rails/test_rails_resource_generator.rb +106 -0
- data/test/examples_from_rails/test_rails_scaffold_generator.rb +185 -0
- data/test/test_generate_builtin_application.rb +24 -0
- data/test/test_generate_builtin_test_unit.rb +22 -0
- data/test/test_helper.rb +33 -0
- data/test/test_lookup.rb +103 -0
- data/website/index.html +352 -0
- data/website/index.txt +252 -0
- data/website/javascripts/rounded_corners_lite.inc.js +285 -0
- data/website/stylesheets/screen.css +138 -0
- data/website/template.rhtml +44 -0
- metadata +183 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
module GeneratorTestHelper
|
|
2
|
+
# Instatiates the Generator
|
|
3
|
+
def build_generator(name,params)
|
|
4
|
+
RubiGen::Base.instance(name,params)
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
# Runs the create command (like the command line does)
|
|
8
|
+
def run_generator(name,params)
|
|
9
|
+
silence_generator do
|
|
10
|
+
build_generator(name,params).command(:create).invoke!
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Silences the logger temporarily and returns the output as a String
|
|
15
|
+
def silence_generator
|
|
16
|
+
logger_original=RubiGen::Base.logger
|
|
17
|
+
myout=StringIO.new
|
|
18
|
+
RubiGen::Base.logger=RubiGen::SimpleLogger.new(myout)
|
|
19
|
+
yield if block_given?
|
|
20
|
+
RubiGen::Base.logger=logger_original
|
|
21
|
+
myout.string
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# asserts that the given controller was generated.
|
|
25
|
+
# It takes a name or symbol without the <tt>_controller</tt> part and an optional super class.
|
|
26
|
+
# The contents of the class source file is passed to a block.
|
|
27
|
+
def assert_generated_controller_for(name,parent="ApplicationController")
|
|
28
|
+
assert_generated_class "app/controllers/#{name.to_s.underscore}_controller",parent do |body|
|
|
29
|
+
yield body if block_given?
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# asserts that the given model was generated.
|
|
34
|
+
# It takes a name or symbol and an optional super class.
|
|
35
|
+
# the contents of the class source file is passed to a block.
|
|
36
|
+
def assert_generated_model_for(name,parent="ActiveRecord::Base")
|
|
37
|
+
assert_generated_class "app/models/#{name.to_s.underscore}",parent do |body|
|
|
38
|
+
yield body if block_given?
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# asserts that the given helper was generated.
|
|
43
|
+
# It takes a name or symbol without the <tt>_helper</tt> part
|
|
44
|
+
# the contents of the module source file is passed to a block.
|
|
45
|
+
def assert_generated_helper_for(name)
|
|
46
|
+
assert_generated_module "app/helpers/#{name.to_s.underscore}_helper" do |body|
|
|
47
|
+
yield body if block_given?
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# asserts that the given functional test was generated.
|
|
52
|
+
# It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class.
|
|
53
|
+
# the contents of the class source file is passed to a block.
|
|
54
|
+
def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase")
|
|
55
|
+
assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body|
|
|
56
|
+
yield body if block_given?
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# asserts that the given unit test was generated.
|
|
61
|
+
# It takes a name or symbol without the <tt>_test</tt> part and an optional super class.
|
|
62
|
+
# the contents of the class source file is passed to a block.
|
|
63
|
+
def assert_generated_unit_test_for(name,parent="Test::Unit::TestCase")
|
|
64
|
+
assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body|
|
|
65
|
+
yield body if block_given?
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# asserts that the given file was generated.
|
|
70
|
+
# the contents of the file is passed to a block.
|
|
71
|
+
def assert_generated_file(path)
|
|
72
|
+
assert_file_exists(path)
|
|
73
|
+
File.open("#{RAILS_ROOT}/#{path}") do |f|
|
|
74
|
+
yield f.read if block_given?
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# asserts that the given file exists
|
|
79
|
+
def assert_file_exists(path)
|
|
80
|
+
assert File.exists?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# asserts that the given class source file was generated.
|
|
84
|
+
# It takes a path without the <tt>.rb</tt> part and an optional super class.
|
|
85
|
+
# the contents of the class source file is passed to a block.
|
|
86
|
+
def assert_generated_class(path,parent=nil)
|
|
87
|
+
path=~/\/?(\d+_)?(\w+)$/
|
|
88
|
+
class_name=$2.camelize
|
|
89
|
+
assert_generated_file("#{path}.rb") do |body|
|
|
90
|
+
assert body=~/class #{class_name}#{parent.nil? ? '':" < #{parent}"}/,"the file '#{path}.rb' should be a class"
|
|
91
|
+
yield body if block_given?
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# asserts that the given module source file was generated.
|
|
96
|
+
# It takes a path without the <tt>.rb</tt> part.
|
|
97
|
+
# the contents of the class source file is passed to a block.
|
|
98
|
+
def assert_generated_module(path)
|
|
99
|
+
path=~/\/?(\w+)$/
|
|
100
|
+
module_name=$1.camelize
|
|
101
|
+
assert_generated_file("#{path}.rb") do |body|
|
|
102
|
+
assert body=~/module #{module_name}/,"the file '#{path}.rb' should be a module"
|
|
103
|
+
yield body if block_given?
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# asserts that the given css stylesheet file was generated.
|
|
108
|
+
# It takes a path without the <tt>.css</tt> part.
|
|
109
|
+
# the contents of the stylesheet source file is passed to a block.
|
|
110
|
+
def assert_generated_stylesheet(path)
|
|
111
|
+
assert_generated_file("public/stylesheets/#{path}.css") do |body|
|
|
112
|
+
yield body if block_given?
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# asserts that the given yaml file was generated.
|
|
117
|
+
# It takes a path without the <tt>.yml</tt> part.
|
|
118
|
+
# the parsed yaml tree is passed to a block.
|
|
119
|
+
def assert_generated_yaml(path)
|
|
120
|
+
assert_generated_file("#{path}.yml") do |body|
|
|
121
|
+
assert yaml=YAML.load(body)
|
|
122
|
+
yield yaml if block_given?
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# asserts that the given fixtures yaml file was generated.
|
|
127
|
+
# It takes a fixture name without the <tt>.yml</tt> part.
|
|
128
|
+
# the parsed yaml tree is passed to a block.
|
|
129
|
+
def assert_generated_fixtures_for(name)
|
|
130
|
+
assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml|
|
|
131
|
+
assert_generated_timestamps(yaml)
|
|
132
|
+
yield yaml if block_given?
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# asserts that the given views were generated.
|
|
137
|
+
# It takes a controller name and a list of views (including extensions).
|
|
138
|
+
# The body of each view is passed to a block
|
|
139
|
+
def assert_generated_views_for(name,*actions)
|
|
140
|
+
actions.each do |action|
|
|
141
|
+
assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body|
|
|
142
|
+
yield body if block_given?
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# asserts that the given migration file was generated.
|
|
148
|
+
# It takes the name of the migration as a parameter.
|
|
149
|
+
# The migration body is passed to a block.
|
|
150
|
+
def assert_generated_migration(name,parent="ActiveRecord::Migration")
|
|
151
|
+
assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|
|
|
152
|
+
assert body=~/timestamps/, "should have timestamps defined"
|
|
153
|
+
yield body if block_given?
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Asserts that the given migration file was not generated.
|
|
158
|
+
# It takes the name of the migration as a parameter.
|
|
159
|
+
def assert_skipped_migration(name)
|
|
160
|
+
migration_file = "#{RAILS_ROOT}/db/migrate/001_#{name.to_s.underscore}.rb"
|
|
161
|
+
assert !File.exists?(migration_file), "should not create migration #{migration_file}"
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# asserts that the given resource was added to the routes.
|
|
165
|
+
def assert_added_route_for(name)
|
|
166
|
+
assert_generated_file("config/routes.rb") do |body|
|
|
167
|
+
assert body=~/map.resources :#{name.to_s.underscore}/,"should add route for :#{name.to_s.underscore}"
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# asserts that the given methods are defined in the body.
|
|
172
|
+
# This does assume standard rails code conventions with regards to the source code.
|
|
173
|
+
# The body of each individual method is passed to a block.
|
|
174
|
+
def assert_has_method(body,*methods)
|
|
175
|
+
methods.each do |name|
|
|
176
|
+
assert body=~/^ def #{name.to_s}\n((\n| .*\n)*) end/,"should have method #{name.to_s}"
|
|
177
|
+
yield( name, $1 ) if block_given?
|
|
178
|
+
end
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
# asserts that the given column is defined in the migration
|
|
182
|
+
def assert_generated_column(body,name,type)
|
|
183
|
+
assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined"
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
private
|
|
187
|
+
# asserts that the default timestamps are created in the fixture
|
|
188
|
+
def assert_generated_timestamps(yaml)
|
|
189
|
+
yaml.values.each do |v|
|
|
190
|
+
["created_at", "updated_at"].each do |field|
|
|
191
|
+
assert v.keys.include?(field), "should have #{field} field by default"
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
|
|
3
|
+
# Optionally load RubyGems
|
|
4
|
+
begin
|
|
5
|
+
require 'rubygems'
|
|
6
|
+
rescue LoadError
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Mock out what we need from AR::Base
|
|
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
|
+
|
|
18
|
+
module ConnectionAdapters
|
|
19
|
+
class Column
|
|
20
|
+
attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
|
|
21
|
+
def initialize(name, default, sql_type=nil)
|
|
22
|
+
@namename
|
|
23
|
+
@default=default
|
|
24
|
+
@type=@sql_type=sql_type
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def human_name
|
|
28
|
+
@name.humanize
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Mock up necessities from ActionView
|
|
35
|
+
module ActionView
|
|
36
|
+
module Helpers
|
|
37
|
+
module ActionRecordHelper; end
|
|
38
|
+
class InstanceTag; end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Set RAILS_ROOT appropriately fixture generation
|
|
43
|
+
tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
|
|
44
|
+
if defined?(RAILS_ROOT)
|
|
45
|
+
RAILS_ROOT.replace(tmp_dir)
|
|
46
|
+
else
|
|
47
|
+
RAILS_ROOT=tmp_dir
|
|
48
|
+
end
|
|
49
|
+
Dir.mkdir(RAILS_ROOT) unless File.exists?(RAILS_ROOT)
|
|
50
|
+
|
|
51
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
|
|
52
|
+
require 'rails_generator'
|
|
53
|
+
require "#{File.dirname(__FILE__)}/generator_test_helper"
|
|
54
|
+
|
|
55
|
+
class RailsResourceGeneratorTest < Test::Unit::TestCase
|
|
56
|
+
include GeneratorTestHelper
|
|
57
|
+
|
|
58
|
+
def setup
|
|
59
|
+
ActiveRecord::Base.pluralize_table_names = true
|
|
60
|
+
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
|
|
61
|
+
Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exists?("#{RAILS_ROOT}/app/views")
|
|
62
|
+
Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exists?("#{RAILS_ROOT}/app/views/layouts")
|
|
63
|
+
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
|
|
64
|
+
Dir.mkdir("#{RAILS_ROOT}/db") unless File.exists?("#{RAILS_ROOT}/db")
|
|
65
|
+
Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
|
|
66
|
+
Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exists?("#{RAILS_ROOT}/test/fixtures")
|
|
67
|
+
Dir.mkdir("#{RAILS_ROOT}/public") unless File.exists?("#{RAILS_ROOT}/public")
|
|
68
|
+
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exists?("#{RAILS_ROOT}/public/stylesheets")
|
|
69
|
+
File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
|
|
70
|
+
f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def teardown
|
|
75
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/app"
|
|
76
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/test"
|
|
77
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/config"
|
|
78
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/db"
|
|
79
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/public"
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_resource_generates_resources
|
|
83
|
+
run_generator('scaffold', %w(Product))
|
|
84
|
+
|
|
85
|
+
assert_generated_controller_for :products
|
|
86
|
+
assert_generated_model_for :product
|
|
87
|
+
assert_generated_fixtures_for :products
|
|
88
|
+
assert_generated_functional_test_for :products
|
|
89
|
+
assert_generated_helper_for :products
|
|
90
|
+
assert_generated_migration :create_products
|
|
91
|
+
assert_added_route_for :products
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def test_resource_skip_migration_skips_migration
|
|
95
|
+
run_generator('resource', %w(Product --skip-migration))
|
|
96
|
+
|
|
97
|
+
assert_generated_controller_for :products
|
|
98
|
+
assert_generated_model_for :product
|
|
99
|
+
assert_generated_fixtures_for :products
|
|
100
|
+
assert_generated_functional_test_for :products
|
|
101
|
+
assert_generated_helper_for :products
|
|
102
|
+
assert_skipped_migration :create_products
|
|
103
|
+
assert_added_route_for :products
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
end
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
|
|
3
|
+
# Optionally load RubyGems.
|
|
4
|
+
begin
|
|
5
|
+
require 'rubygems'
|
|
6
|
+
rescue LoadError
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
# Mock out what we need from AR::Base.
|
|
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
|
+
|
|
18
|
+
module ConnectionAdapters
|
|
19
|
+
class Column
|
|
20
|
+
attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
|
|
21
|
+
|
|
22
|
+
def initialize(name, default, sql_type = nil)
|
|
23
|
+
@name=name
|
|
24
|
+
@default=default
|
|
25
|
+
@type=@sql_type=sql_type
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def human_name
|
|
29
|
+
@name.humanize
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# And what we need from ActionView
|
|
36
|
+
module ActionView
|
|
37
|
+
module Helpers
|
|
38
|
+
module ActiveRecordHelper; end
|
|
39
|
+
class InstanceTag; end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Must set before requiring generator libs.
|
|
45
|
+
tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
|
|
46
|
+
if defined?(RAILS_ROOT)
|
|
47
|
+
RAILS_ROOT.replace(tmp_dir)
|
|
48
|
+
else
|
|
49
|
+
RAILS_ROOT=tmp_dir
|
|
50
|
+
end
|
|
51
|
+
Dir.mkdir(RAILS_ROOT) unless File.exists?(RAILS_ROOT)
|
|
52
|
+
|
|
53
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
|
|
54
|
+
require 'rails_generator'
|
|
55
|
+
require "#{File.dirname(__FILE__)}/generator_test_helper"
|
|
56
|
+
|
|
57
|
+
class RailsScaffoldGeneratorTest < Test::Unit::TestCase
|
|
58
|
+
|
|
59
|
+
include GeneratorTestHelper
|
|
60
|
+
|
|
61
|
+
def setup
|
|
62
|
+
ActiveRecord::Base.pluralize_table_names = true
|
|
63
|
+
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
|
|
64
|
+
Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exists?("#{RAILS_ROOT}/app/views")
|
|
65
|
+
Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exists?("#{RAILS_ROOT}/app/views/layouts")
|
|
66
|
+
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
|
|
67
|
+
Dir.mkdir("#{RAILS_ROOT}/db") unless File.exists?("#{RAILS_ROOT}/db")
|
|
68
|
+
Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
|
|
69
|
+
Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exists?("#{RAILS_ROOT}/test/fixtures")
|
|
70
|
+
Dir.mkdir("#{RAILS_ROOT}/public") unless File.exists?("#{RAILS_ROOT}/public")
|
|
71
|
+
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exists?("#{RAILS_ROOT}/public/stylesheets")
|
|
72
|
+
File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
|
|
73
|
+
f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def teardown
|
|
78
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/app"
|
|
79
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/test"
|
|
80
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/config"
|
|
81
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/db"
|
|
82
|
+
FileUtils.rm_rf "#{RAILS_ROOT}/public"
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def test_scaffolded_names
|
|
86
|
+
g = RubiGen::Base.instance('scaffold', %w(ProductLine))
|
|
87
|
+
assert_equal "ProductLines", g.controller_name
|
|
88
|
+
assert_equal "ProductLines", g.controller_class_name
|
|
89
|
+
assert_equal "ProductLine", g.controller_singular_name
|
|
90
|
+
assert_equal "product_lines", g.controller_plural_name
|
|
91
|
+
assert_equal "product_lines", g.controller_file_name
|
|
92
|
+
assert_equal "product_lines", g.controller_table_name
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def test_scaffold_generates_resources
|
|
96
|
+
|
|
97
|
+
run_generator('scaffold', %w(Product))
|
|
98
|
+
|
|
99
|
+
assert_generated_controller_for :products do |f|
|
|
100
|
+
|
|
101
|
+
assert_has_method f, :index do |name, m|
|
|
102
|
+
assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
assert_has_method f, :show, :edit, :update, :destroy do |name, m|
|
|
106
|
+
assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
assert_has_method f, :new do |name, m|
|
|
110
|
+
assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
assert_has_method f, :create do |name, m|
|
|
114
|
+
assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
|
|
115
|
+
assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
assert_generated_model_for :product
|
|
121
|
+
assert_generated_functional_test_for :products
|
|
122
|
+
assert_generated_unit_test_for :product
|
|
123
|
+
assert_generated_fixtures_for :products
|
|
124
|
+
assert_generated_helper_for :products
|
|
125
|
+
assert_generated_stylesheet :scaffold
|
|
126
|
+
assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
|
|
127
|
+
assert_generated_migration :create_products
|
|
128
|
+
assert_added_route_for :products
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def test_scaffold_skip_migration_skips_migration
|
|
132
|
+
run_generator('scaffold', %w(Product --skip-migration))
|
|
133
|
+
|
|
134
|
+
assert_generated_model_for :product
|
|
135
|
+
assert_generated_functional_test_for :products
|
|
136
|
+
assert_generated_unit_test_for :product
|
|
137
|
+
assert_generated_fixtures_for :products
|
|
138
|
+
assert_generated_helper_for :products
|
|
139
|
+
assert_generated_stylesheet :scaffold
|
|
140
|
+
assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
|
|
141
|
+
assert_skipped_migration :create_products
|
|
142
|
+
assert_added_route_for :products
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def test_scaffold_generates_resources_with_attributes
|
|
146
|
+
run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
|
|
147
|
+
|
|
148
|
+
assert_generated_controller_for :products do |f|
|
|
149
|
+
|
|
150
|
+
assert_has_method f, :index do |name, m|
|
|
151
|
+
assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
assert_has_method f, :show, :edit, :update, :destroy do |name, m|
|
|
155
|
+
assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
assert_has_method f, :new do |name, m|
|
|
159
|
+
assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
assert_has_method f, :create do |name, m|
|
|
163
|
+
assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product"
|
|
164
|
+
assert_match /format.xml \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
assert_generated_model_for :product
|
|
170
|
+
assert_generated_functional_test_for :products
|
|
171
|
+
assert_generated_unit_test_for :product
|
|
172
|
+
assert_generated_fixtures_for :products
|
|
173
|
+
assert_generated_helper_for :products
|
|
174
|
+
assert_generated_stylesheet :scaffold
|
|
175
|
+
assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb"
|
|
176
|
+
assert_generated_migration :create_products do |t|
|
|
177
|
+
assert_generated_column t, :name, :string
|
|
178
|
+
assert_generated_column t, :supplier_id, :integer
|
|
179
|
+
assert_generated_column t, :created_at, :timestamp
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
assert_added_route_for :products
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
|
2
|
+
|
|
3
|
+
class TestGenerateBuiltinApplication < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
bare_setup
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def teardown
|
|
10
|
+
bare_teardown
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_ruby_app
|
|
14
|
+
app_name = "myapp"
|
|
15
|
+
run_generator('ruby_app', ["#{APP_ROOT}/#{app_name}"], :app)
|
|
16
|
+
assert_generated_file("#{app_name}/Rakefile")
|
|
17
|
+
assert_generated_file("#{app_name}/README.txt")
|
|
18
|
+
assert_generated_file("#{app_name}/lib/#{app_name}.rb")
|
|
19
|
+
assert_generated_file("#{app_name}/test/test_helper.rb")
|
|
20
|
+
assert_generated_file("#{app_name}/script/generate")
|
|
21
|
+
|
|
22
|
+
assert_generated_module("#{app_name}/lib/#{app_name}")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
|
2
|
+
|
|
3
|
+
class TestGenerateBuiltinTestUnit < Test::Unit::TestCase
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
rubygems_setup
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def teardown
|
|
10
|
+
rubygems_teardown
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def test_with_no_options
|
|
14
|
+
run_generator('test_unit', %w[AccountReceiver], :component)
|
|
15
|
+
assert_generated_file("test/test_account_receiver.rb")
|
|
16
|
+
assert_generated_class("test/test_account_receiver", "Test::Unit::TestCase") do
|
|
17
|
+
assert_has_method("setup")
|
|
18
|
+
assert_has_method("teardown")
|
|
19
|
+
assert_has_method("test_truth")
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
|
|
3
|
+
# Must set before requiring generator libs.
|
|
4
|
+
TMP_ROOT = File.dirname(__FILE__) + "/tmp" unless defined?(TMP_ROOT)
|
|
5
|
+
app_root = File.join(TMP_ROOT, "myproject")
|
|
6
|
+
if defined?(APP_ROOT)
|
|
7
|
+
APP_ROOT.replace(app_root)
|
|
8
|
+
else
|
|
9
|
+
APP_ROOT = app_root
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
require File.dirname(__FILE__) + '/../lib/rubigen'
|
|
13
|
+
require 'rubigen/helpers/generator_test_helper'
|
|
14
|
+
Test::Unit::TestCase.send(:include, RubiGen::GeneratorTestHelper)
|
|
15
|
+
include RubiGen
|
|
16
|
+
|
|
17
|
+
begin
|
|
18
|
+
gem 'mocha'
|
|
19
|
+
rescue LoadError
|
|
20
|
+
require 'rubygems'
|
|
21
|
+
begin
|
|
22
|
+
gem 'mocha'
|
|
23
|
+
rescue LoadError
|
|
24
|
+
puts <<-EOS
|
|
25
|
+
#{$!}
|
|
26
|
+
|
|
27
|
+
This RubyGem is required to run the tests.
|
|
28
|
+
|
|
29
|
+
Install: gem install mocha
|
|
30
|
+
EOS
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
require 'mocha'
|
data/test/test_lookup.rb
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
require File.dirname(__FILE__) + "/test_helper"
|
|
2
|
+
|
|
3
|
+
class TestLookup < Test::Unit::TestCase
|
|
4
|
+
include RubiGen
|
|
5
|
+
|
|
6
|
+
def test_lookup_component
|
|
7
|
+
assert_nothing_raised(GeneratorError, "Could not find test_unit generator") { Base.lookup('test_unit') }
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def test_lookup_unknown_component
|
|
11
|
+
assert_raise(GeneratorError, "Should not find generator") { Base.lookup('dummy') }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# There are 5 sources of generators
|
|
15
|
+
# * HOME/.rubigen/generators
|
|
16
|
+
# * APP_ROOT/generators
|
|
17
|
+
# * APP_ROOT/vendor/generators
|
|
18
|
+
# * APP_ROOT/vendor/plugins/.../
|
|
19
|
+
# * RubyGems internal /generators folder
|
|
20
|
+
#
|
|
21
|
+
# Note, this differs from Rails generator:
|
|
22
|
+
# * RubyGems whose name is suffixed with _generator are not loaded (e.g. ajax_scaffold_generator)
|
|
23
|
+
def test_sources
|
|
24
|
+
sources = Base.sources
|
|
25
|
+
assert(sources.find do |source|
|
|
26
|
+
source.path =~ /\.rubigen\/generators$/ if source.respond_to? :path
|
|
27
|
+
end, "One source should be HOME/.rubigen/generators")
|
|
28
|
+
|
|
29
|
+
assert(sources.find do |source|
|
|
30
|
+
source.path =~ /#{::APP_ROOT}\/generators$/ if source.respond_to? :path
|
|
31
|
+
end, "One source should be APP_ROOT/generators")
|
|
32
|
+
|
|
33
|
+
assert(sources.find do |source|
|
|
34
|
+
source.path =~ /#{::APP_ROOT}\/vendor\/generators$/ if source.respond_to? :path
|
|
35
|
+
end, "One source should be APP_ROOT/vendor/generators")
|
|
36
|
+
|
|
37
|
+
assert(sources.find do |source|
|
|
38
|
+
source.path =~ /#{::APP_ROOT}\/vendor\/plugins\/.*\/generators$/ if source.respond_to? :path
|
|
39
|
+
end, "One source should be APP_ROOT/vendor/plugins/.../generators")
|
|
40
|
+
|
|
41
|
+
assert(sources.find do |source|
|
|
42
|
+
source.is_a?(GemPathSource)
|
|
43
|
+
end, "One source should be RubyGems containing generators")
|
|
44
|
+
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def test_unscoped_gem_path
|
|
48
|
+
source = GemPathSource.new
|
|
49
|
+
assert_equal("", source.send(:filter_str))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def test_scoped_gem_path
|
|
53
|
+
source = GemPathSource.new("rubygems")
|
|
54
|
+
assert_equal("{rubygems_}", source.send(:filter_str))
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def test_alternate_scoped_gem_path
|
|
58
|
+
source = GemPathSource.new(:rubygems, :ruby)
|
|
59
|
+
assert_equal("{rubygems_,ruby_}", source.send(:filter_str))
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def test_scoped_gem_path_using_array
|
|
63
|
+
source = GemPathSource.new([:rubygems, :ruby])
|
|
64
|
+
assert_equal("{rubygems_,ruby_}", source.send(:filter_str))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def test_use_component_sources_without_scope
|
|
68
|
+
Base.use_component_sources!
|
|
69
|
+
gem_path_source = Base.sources.find { |source| source.is_a?(GemPathSource) }
|
|
70
|
+
assert_not_nil(gem_path_source, "Where is the GemPathSource?")
|
|
71
|
+
assert_equal("", gem_path_source.send(:filter_str))
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_use_component_sources_with_scope
|
|
75
|
+
Base.use_component_sources! :rubygems, :ruby
|
|
76
|
+
gem_path_source = Base.sources.find { |source| source.is_a?(GemPathSource) }
|
|
77
|
+
assert_not_nil(gem_path_source, "Where is the GemPathSource?")
|
|
78
|
+
assert_equal("{rubygems_,ruby_}", gem_path_source.send(:filter_str))
|
|
79
|
+
user_path_source = Base.sources.find { |source| source.is_a?(PathFilteredSource) }
|
|
80
|
+
assert_not_nil(user_path_source, "Where is the PathFilteredSource?")
|
|
81
|
+
assert_match(/\.rubigen\/\{rubygems_,ruby_\}generators/, user_path_source.path)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def test_use_application_sources
|
|
85
|
+
Base.use_application_sources!
|
|
86
|
+
expected_path = File.expand_path(File.join(File.dirname(__FILE__), %w[.. lib rubigen generators applications]))
|
|
87
|
+
builtin_source = Base.sources.find { |s| s.path == expected_path if s.respond_to?(:path) }
|
|
88
|
+
assert_not_nil(builtin_source, "Cannot find builtin generators")
|
|
89
|
+
assert_nothing_raised(GeneratorError) do
|
|
90
|
+
generator = Base.lookup('ruby_app')
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def test_use_application_sources_with_scope
|
|
95
|
+
Base.use_application_sources! :rubygems, :newgem
|
|
96
|
+
gem_path_source = Base.sources.find { |source| source.is_a?(GemPathSource) }
|
|
97
|
+
assert_not_nil(gem_path_source, "Where is the GemPathSource?")
|
|
98
|
+
assert_equal("{app_,rubygems_,newgem_}", gem_path_source.send(:filter_str))
|
|
99
|
+
user_path_source = Base.sources.find { |source| source.is_a?(PathFilteredSource) }
|
|
100
|
+
assert_not_nil(user_path_source, "Where is the PathFilteredSource?")
|
|
101
|
+
assert_match(/\.rubigen\/\{app_,rubygems_,newgem_\}generators/, user_path_source.path)
|
|
102
|
+
end
|
|
103
|
+
end
|