aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/generators
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-05-23 22:42:24 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-05-23 22:42:24 +0000
commitda7f9f516a8f34303ae08a9b89502757b41a578a (patch)
tree84468a301d31233b92ff04e93f6202bc7fef1608 /railties/test/generators
parent4bd3e3aa1b92c75fafbb727820aeacae5e9bcb42 (diff)
downloadrails-da7f9f516a8f34303ae08a9b89502757b41a578a.tar.gz
rails-da7f9f516a8f34303ae08a9b89502757b41a578a.tar.bz2
rails-da7f9f516a8f34303ae08a9b89502757b41a578a.zip
Scaffold generator tests. Closes #8443.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6824 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/test/generators')
-rw-r--r--railties/test/generators/generator_test_helper.rb177
-rw-r--r--railties/test/generators/rails_scaffold_generator_test.rb167
2 files changed, 344 insertions, 0 deletions
diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb
new file mode 100644
index 0000000000..0bfb2a0964
--- /dev/null
+++ b/railties/test/generators/generator_test_helper.rb
@@ -0,0 +1,177 @@
+module GeneratorTestHelper
+ # Instatiates the Generator
+ def build_generator(name,params)
+ Rails::Generator::Base.instance(name,params)
+ end
+
+ # Runs the create command (like the command line does)
+ def run_generator(name,params)
+ silence_generator do
+ build_generator(name,params).command(:create).invoke!
+ end
+ end
+
+ # Silences the logger temporarily and returns the output as a String
+ def silence_generator
+ logger_original=Rails::Generator::Base.logger
+ myout=StringIO.new
+ Rails::Generator::Base.logger=Rails::Generator::SimpleLogger.new(myout)
+ yield if block_given?
+ Rails::Generator::Base.logger=logger_original
+ myout.string
+ end
+
+ # asserts that the given controller was generated.
+ # It takes a name or symbol without the <tt>_controller</tt> part and an optional super class.
+ # The contents of the class source file is passed to a block.
+ def assert_generated_controller_for(name,parent="ApplicationController")
+ assert_generated_class "app/controllers/#{name.to_s.underscore}_controller",parent do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given model was generated.
+ # It takes a name or symbol and an optional super class.
+ # the contents of the class source file is passed to a block.
+ def assert_generated_model_for(name,parent="ActiveRecord::Base")
+ assert_generated_class "app/models/#{name.to_s.underscore}",parent do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given helper was generated.
+ # It takes a name or symbol without the <tt>_helper</tt> part
+ # the contents of the module source file is passed to a block.
+ def assert_generated_helper_for(name)
+ assert_generated_module "app/helpers/#{name.to_s.underscore}_helper" do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given functional test was generated.
+ # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class.
+ # the contents of the class source file is passed to a block.
+ def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase")
+ assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given unit test was generated.
+ # It takes a name or symbol without the <tt>_test</tt> part and an optional super class.
+ # the contents of the class source file is passed to a block.
+ def assert_generated_unit_test_for(name,parent="Test::Unit::TestCase")
+ assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given file was generated.
+ # the contents of the file is passed to a block.
+ def assert_generated_file(path)
+ assert_file_exists(path)
+ File.open("#{RAILS_ROOT}/#{path}") do |f|
+ yield f.read if block_given?
+ end
+ end
+
+ # asserts that the given file exists
+ def assert_file_exists(path)
+ assert File.exists?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist"
+ end
+
+ # asserts that the given class source file was generated.
+ # It takes a path without the <tt>.rb</tt> part and an optional super class.
+ # the contents of the class source file is passed to a block.
+ def assert_generated_class(path,parent=nil)
+ path=~/\/?(\d+_)?(\w+)$/
+ class_name=$2.camelize
+ assert_generated_file("#{path}.rb") do |body|
+ assert body=~/class #{class_name}#{parent.nil? ? '':" < #{parent}"}/,"the file '#{path}.rb' should be a class"
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given module source file was generated.
+ # It takes a path without the <tt>.rb</tt> part.
+ # the contents of the class source file is passed to a block.
+ def assert_generated_module(path)
+ path=~/\/?(\w+)$/
+ module_name=$1.camelize
+ assert_generated_file("#{path}.rb") do |body|
+ assert body=~/module #{module_name}/,"the file '#{path}.rb' should be a module"
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given css stylesheet file was generated.
+ # It takes a path without the <tt>.css</tt> part.
+ # the contents of the stylesheet source file is passed to a block.
+ def assert_generated_stylesheet(path)
+ assert_generated_file("public/stylesheets/#{path}.css") do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given yaml file was generated.
+ # It takes a path without the <tt>.yml</tt> part.
+ # the parsed yaml tree is passed to a block.
+ def assert_generated_yaml(path)
+ assert_generated_file("#{path}.yml") do |body|
+ assert yaml=YAML.load(body)
+ yield yaml if block_given?
+ end
+ end
+
+ # asserts that the given fixtures yaml file was generated.
+ # It takes a fixture name without the <tt>.yml</tt> part.
+ # the parsed yaml tree is passed to a block.
+ def assert_generated_fixtures_for(name)
+ assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml|
+ yield yaml if block_given?
+ end
+ end
+
+ # asserts that the given views were generated.
+ # It takes a controller name and a list of views (including extensions).
+ # The body of each view is passed to a block
+ def assert_generated_views_for(name,*actions)
+ actions.each do |action|
+ assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body|
+ yield body if block_given?
+ end
+ end
+ end
+
+ # asserts that the given migration file was generated.
+ # It takes the name of the migration as a parameter.
+ # the migration body is passed to a block.
+ def assert_generated_migration(name,parent="ActiveRecord::Migration")
+ assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body|
+ yield body if block_given?
+ end
+ end
+
+ # asserts that the given resource was added to the routes.
+ def assert_added_route_for(name)
+ assert_generated_file("config/routes.rb") do |body|
+ assert body=~/map.resources :#{name.to_s.underscore}/,"should add route for :#{name.to_s.underscore}"
+ end
+ end
+
+ # asserts that the given methods are defined in the body.
+ # This does assume standard rails code conventions with regards to the source code.
+ # The body of each individual method is passed to a block.
+ def assert_has_method(body,*methods)
+ methods.each do |name|
+ assert body=~/^ def #{name.to_s}\n((\n| .*\n)*) end/,"should have method #{name.to_s}"
+ yield( name, $1 ) if block_given?
+ end
+ end
+
+ # asserts that the given column is defined in the migration
+ def assert_generated_column(body,name,type)
+ assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined"
+ end
+
+end \ No newline at end of file
diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb
new file mode 100644
index 0000000000..cbf116996c
--- /dev/null
+++ b/railties/test/generators/rails_scaffold_generator_test.rb
@@ -0,0 +1,167 @@
+require 'test/unit'
+
+# Optionally load RubyGems.
+begin
+ require 'rubygems'
+rescue LoadError
+end
+
+# 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
+
+ module ConnectionAdapters
+ class Column
+ attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale
+
+ def initialize(name, default, sql_type = nil)
+ @name=name
+ @default=default
+ @type=@sql_type=sql_type
+ end
+
+ def human_name
+ @name.humanize
+ end
+ end
+ end
+end
+
+# And what we need from ActionView
+module ActionView
+ module Helpers
+ module ActiveRecordHelper; end
+ class InstanceTag; end
+ end
+end
+
+
+# Must set before requiring generator libs.
+if defined?(RAILS_ROOT)
+ RAILS_ROOT.replace "#{File.dirname(__FILE__)}/../fixtures/tmp"
+else
+ RAILS_ROOT = "#{File.dirname(__FILE__)}/../fixtures/tmp"
+end
+Dir.mkdir(RAILS_ROOT) unless File.exists?(RAILS_ROOT)
+
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
+require 'rails_generator'
+require "#{File.dirname(__FILE__)}/generator_test_helper"
+
+class RailsScaffoldGeneratorTest < Test::Unit::TestCase
+
+ include GeneratorTestHelper
+
+ def setup
+ ActiveRecord::Base.pluralize_table_names = true
+ Dir.mkdir("#{RAILS_ROOT}/app") unless File.exists?("#{RAILS_ROOT}/app")
+ Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exists?("#{RAILS_ROOT}/app/views")
+ Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exists?("#{RAILS_ROOT}/app/views/layouts")
+ Dir.mkdir("#{RAILS_ROOT}/config") unless File.exists?("#{RAILS_ROOT}/config")
+ Dir.mkdir("#{RAILS_ROOT}/db") unless File.exists?("#{RAILS_ROOT}/db")
+ Dir.mkdir("#{RAILS_ROOT}/test") unless File.exists?("#{RAILS_ROOT}/test")
+ Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exists?("#{RAILS_ROOT}/test/fixtures")
+ Dir.mkdir("#{RAILS_ROOT}/public") unless File.exists?("#{RAILS_ROOT}/public")
+ Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exists?("#{RAILS_ROOT}/public/stylesheets")
+ File.open("#{RAILS_ROOT}/config/routes.rb",'w') do |f|
+ f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n"
+ end
+ end
+
+ def teardown
+ FileUtils.rm_rf "#{RAILS_ROOT}/app"
+ FileUtils.rm_rf "#{RAILS_ROOT}/test"
+ FileUtils.rm_rf "#{RAILS_ROOT}/config"
+ FileUtils.rm_rf "#{RAILS_ROOT}/db"
+ FileUtils.rm_rf "#{RAILS_ROOT}/public"
+ end
+
+ def test_scaffolded_names
+ g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))
+ assert_equal "ProductLines", g.controller_name
+ assert_equal "ProductLines", g.controller_class_name
+ assert_equal "ProductLine", g.controller_singular_name
+ assert_equal "product_lines", g.controller_plural_name
+ assert_equal "product_lines", g.controller_file_name
+ assert_equal "product_lines", g.controller_table_name
+ end
+
+ def test_scaffold_generates_resources
+
+ run_generator('scaffold', %w(Product))
+
+ assert_generated_controller_for :products do |f|
+
+ assert_has_method f,:index do |name,m|
+ assert m=~/@products = Product\.find\(:all\)/,"#{name} should query products table"
+ end
+
+ assert_has_method f,:show,:edit,:update,:destroy do |name,m|
+ assert m=~/@product = Product\.find\(params\[:id\]\)/,"#{name.to_s} should query products table"
+ end
+
+ assert_has_method f,:new do |name,m|
+ assert m=~/@product = Product\.new/,"#{name.to_s} should instantiate a product"
+ end
+
+ assert_has_method f,:create do |name,m|
+ assert m=~/@product = Product\.new\(params\[:product\]\)/,"#{name.to_s} should instantiate a product"
+ end
+
+ end
+
+ assert_generated_model_for :product
+ assert_generated_functional_test_for :products
+ assert_generated_unit_test_for :product
+ assert_generated_fixtures_for :products
+ assert_generated_helper_for :products
+ assert_generated_stylesheet :scaffold
+ assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
+ assert_generated_migration :create_products
+ assert_added_route_for :products
+ end
+
+ def test_scaffold_generates_resources_with_attributes
+ run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp))
+
+ assert_generated_controller_for :products do |f|
+
+ assert_has_method f,:index do |name,m|
+ assert m=~/@products = Product\.find\(:all\)/,"#{name} should query products table"
+ end
+
+ assert_has_method f,:show,:edit,:update,:destroy do |name,m|
+ assert m=~/@product = Product\.find\(params\[:id\]\)/,"#{name.to_s} should query products table"
+ end
+
+ assert_has_method f,:new do |name,m|
+ assert m=~/@product = Product\.new/,"#{name.to_s} should instantiate a product"
+ end
+
+ assert_has_method f,:create do |name,m|
+ assert m=~/@product = Product\.new\(params\[:product\]\)/,"#{name.to_s} should instantiate a product"
+ end
+
+ end
+
+ assert_generated_model_for :product
+ assert_generated_functional_test_for :products
+ assert_generated_unit_test_for :product
+ assert_generated_fixtures_for :products
+ assert_generated_helper_for :products
+ assert_generated_stylesheet :scaffold
+ assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb"
+ assert_generated_migration :create_products do |t|
+ assert_generated_column t, :name, :string
+ assert_generated_column t, :supplier_id, :integer
+ assert_generated_column t, :created_at,:timestamp
+ end
+
+ assert_added_route_for :products
+ end
+end