1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Cleanup generator tests by extracting repeated code into generator_test_helper. Add test for mailer generator.

Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
Mislav Marohnić 2008-04-18 17:10:58 -05:00 committed by Joshua Peek
parent 986aec5dbb
commit dfdb9f738e
6 changed files with 252 additions and 389 deletions

View file

@ -11,12 +11,8 @@ class MailerGenerator < Rails::Generator::NamedBase
m.directory File.join('test/fixtures', file_path) m.directory File.join('test/fixtures', file_path)
# Mailer class and unit test. # Mailer class and unit test.
m.template "mailer.rb", File.join('app/models', m.template "mailer.rb", File.join('app/models', class_path, "#{file_name}.rb")
class_path, m.template "unit_test.rb", File.join('test/unit', class_path, "#{file_name}_test.rb")
"#{file_name}.rb")
m.template "unit_test.rb", File.join('test/unit',
class_path,
"#{file_name}_test.rb")
# View template and fixture for each action. # View template and fixture for each action.
actions.each do |action| actions.each do |action|

View file

@ -1,3 +1,51 @@
require 'test/unit'
require 'fileutils'
# 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
# Mock up necessities from ActionView
module ActionView
module Helpers
module ActionRecordHelper; end
class InstanceTag; end
end
end
# Set RAILS_ROOT appropriately fixture generation
tmp_dir = "#{File.dirname(__FILE__)}/../fixtures/tmp"
if defined? RAILS_ROOT
RAILS_ROOT.replace tmp_dir
else
RAILS_ROOT = tmp_dir
end
FileUtils.mkdir_p RAILS_ROOT
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
require 'initializer' require 'initializer'
# Mocks out the configuration # Mocks out the configuration
@ -9,8 +57,35 @@ end
require 'rails_generator' require 'rails_generator'
class GeneratorTestCase < Test::Unit::TestCase
include FileUtils
def setup
ActiveRecord::Base.pluralize_table_names = true
mkdir_p "#{RAILS_ROOT}/app/views/layouts"
mkdir_p "#{RAILS_ROOT}/config"
mkdir_p "#{RAILS_ROOT}/db"
mkdir_p "#{RAILS_ROOT}/test/fixtures"
mkdir_p "#{RAILS_ROOT}/public/stylesheets"
File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f|
f << "ActionController::Routing::Routes.draw do |map|\n\nend"
end
end
def teardown
rm_rf "#{RAILS_ROOT}/app"
rm_rf "#{RAILS_ROOT}/test"
rm_rf "#{RAILS_ROOT}/config"
rm_rf "#{RAILS_ROOT}/db"
rm_rf "#{RAILS_ROOT}/public"
end
def test_truth
# don't complain, test/unit
end
module GeneratorTestHelper
# Instantiates the Generator # Instantiates the Generator
def build_generator(name, params) def build_generator(name, params)
Rails::Generator::Base.instance(name, params) Rails::Generator::Base.instance(name, params)
@ -89,7 +164,8 @@ module GeneratorTestHelper
# asserts that the given file exists # asserts that the given file exists
def assert_file_exists(path) def assert_file_exists(path)
assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist" assert File.exist?("#{RAILS_ROOT}/#{path}"),
"The file '#{RAILS_ROOT}/#{path}' should exist"
end end
# asserts that the given class source file was generated. # asserts that the given class source file was generated.
@ -99,7 +175,7 @@ module GeneratorTestHelper
path =~ /\/?(\d+_)?(\w+)$/ path =~ /\/?(\d+_)?(\w+)$/
class_name = $2.camelize class_name = $2.camelize
assert_generated_file("#{path}.rb") do |body| assert_generated_file("#{path}.rb") do |body|
assert body=~/class #{class_name}#{parent.nil? ? '':" < #{parent}"}/,"the file '#{path}.rb' should be a class" assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class"
yield body if block_given? yield body if block_given?
end end
end end
@ -111,7 +187,7 @@ module GeneratorTestHelper
path =~ /\/?(\w+)$/ path =~ /\/?(\w+)$/
module_name = $1.camelize module_name = $1.camelize
assert_generated_file("#{path}.rb") do |body| assert_generated_file("#{path}.rb") do |body|
assert body=~/module #{module_name}/,"the file '#{path}.rb' should be a module" assert_match /module #{module_name}/, body, "the file '#{path}.rb' should be a module"
yield body if block_given? yield body if block_given?
end end
end end
@ -130,7 +206,8 @@ module GeneratorTestHelper
# the parsed yaml tree is passed to a block. # the parsed yaml tree is passed to a block.
def assert_generated_yaml(path) def assert_generated_yaml(path)
assert_generated_file("#{path}.yml") do |body| assert_generated_file("#{path}.yml") do |body|
assert yaml=YAML.load(body) yaml = YAML.load(body)
assert yaml, 'YAML data missing'
yield yaml if block_given? yield yaml if block_given?
end end
end end
@ -149,18 +226,17 @@ module GeneratorTestHelper
# The body of each view is passed to a block # The body of each view is passed to a block
def assert_generated_views_for(name, *actions) def assert_generated_views_for(name, *actions)
actions.each do |action| actions.each do |action|
assert_generated_file("app/views/#{name.to_s.underscore}/#{action.to_s}") do |body| assert_generated_file("app/views/#{name.to_s.underscore}/#{action}") do |body|
yield body if block_given? yield body if block_given?
end end
end end
end end
def assert_generated_migration(name, parent = "ActiveRecord::Migration") def assert_generated_migration(name, parent = "ActiveRecord::Migration")
file = file = Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first
Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first file = file.match(/db\/migrate\/[0-9]+_\w+/).to_s
file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s
assert_generated_class file, parent do |body| assert_generated_class file, parent do |body|
assert body=~/timestamps/, "should have timestamps defined" assert_match /timestamps/, body, "should have timestamps defined"
yield body if block_given? yield body if block_given?
end end
end end
@ -175,7 +251,8 @@ module GeneratorTestHelper
# asserts that the given resource was added to the routes. # asserts that the given resource was added to the routes.
def assert_added_route_for(name) def assert_added_route_for(name)
assert_generated_file("config/routes.rb") do |body| assert_generated_file("config/routes.rb") do |body|
assert body=~/map.resources :#{name.to_s.underscore}/,"should add route for :#{name.to_s.underscore}" assert_match /map.resources :#{name.to_s.underscore}/, body,
"should add route for :#{name.to_s.underscore}"
end end
end end
@ -184,13 +261,13 @@ module GeneratorTestHelper
# The body of each individual method is passed to a block. # The body of each individual method is passed to a block.
def assert_has_method(body, *methods) def assert_has_method(body, *methods)
methods.each do |name| methods.each do |name|
assert body=~/^ def #{name.to_s}\n((\n| .*\n)*) end/,"should have method #{name.to_s}" assert body =~ /^ def #{name}(\(.+\))?\n((\n| .*\n)*) end/, "should have method #{name}"
yield( name, $1 ) if block_given? yield(name, $2) if block_given?
end end
end end
# asserts that the given column is defined in the migration # asserts that the given column is defined in the migration
def assert_generated_column(body, name, type) def assert_generated_column(body, name, type)
assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" assert_match /t\.#{type.to_s} :#{name.to_s}/, body, "should have column #{name.to_s} defined"
end end
end end

View file

@ -0,0 +1,26 @@
require 'generators/generator_test_helper'
class RailsMailerGeneratorTest < GeneratorTestCase
def test_generates_mailer
run_generator('mailer', %w(Notifier reset_password))
assert_generated_model_for :notifier, 'ActionMailer::Base' do |model|
assert_has_method model, :reset_password do |name, body|
assert_equal [
"@subject = 'Notifier#reset_password'",
"@body = {}",
"@recipients = ''",
"@from = ''",
"@sent_on = sent_at",
"@headers = {}"
],
body.split("\n").map{|line| line.sub(' '*4, '') }
end
end
assert_generated_views_for :notifier, 'reset_password.erb'
assert_generated_unit_test_for :notifier, 'ActionMailer::TestCase'
assert_generated_file "test/fixtures/notifier/reset_password"
end
end

View file

@ -1,84 +1,6 @@
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)
@namename
@default=default
@type=@sql_type=sql_type
end
def human_name
@name.humanize
end
end
end
end
# Mock up necessities from ActionView
module ActionView
module Helpers
module ActionRecordHelper; end
class InstanceTag; end
end
end
# Set RAILS_ROOT appropriately fixture generation
tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
if defined?(RAILS_ROOT)
RAILS_ROOT.replace(tmp_dir)
else
RAILS_ROOT=tmp_dir
end
Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
require 'generators/generator_test_helper' require 'generators/generator_test_helper'
class RailsModelGeneratorTest < Test::Unit::TestCase class RailsModelGeneratorTest < GeneratorTestCase
include GeneratorTestHelper
def setup
ActiveRecord::Base.pluralize_table_names = true
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{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_model_generates_resources def test_model_generates_resources
run_generator('model', %w(Product name:string)) run_generator('model', %w(Product name:string))

View file

@ -1,82 +1,6 @@
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)
@namename
@default=default
@type=@sql_type=sql_type
end
def human_name
@name.humanize
end
end
end
end
# Mock up necessities from ActionView
module ActionView
module Helpers
module ActionRecordHelper; end
class InstanceTag; end
end
end
# Set RAILS_ROOT appropriately fixture generation
tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
if defined?(RAILS_ROOT)
RAILS_ROOT.replace(tmp_dir)
else
RAILS_ROOT=tmp_dir
end
Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
require 'generators/generator_test_helper' require 'generators/generator_test_helper'
class RailsResourceGeneratorTest < Test::Unit::TestCase class RailsResourceGeneratorTest < GeneratorTestCase
include GeneratorTestHelper
def setup
ActiveRecord::Base.pluralize_table_names = true
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{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_resource_generates_resources def test_resource_generates_resources
run_generator('resource', %w(Product name:string)) run_generator('resource', %w(Product name:string))

View file

@ -1,87 +1,6 @@
require 'abstract_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.
tmp_dir="#{File.dirname(__FILE__)}/../fixtures/tmp"
if defined?(RAILS_ROOT)
RAILS_ROOT.replace(tmp_dir)
else
RAILS_ROOT=tmp_dir
end
Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT)
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
require 'generators/generator_test_helper' require 'generators/generator_test_helper'
uses_mocha "Scaffold Generator Tests" do class RailsScaffoldGeneratorTest < GeneratorTestCase
class RailsScaffoldGeneratorTest < Test::Unit::TestCase
include GeneratorTestHelper
def setup
ActiveRecord::Base.pluralize_table_names = true
Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app")
Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views")
Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts")
Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config")
Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db")
Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test")
Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures")
Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public")
Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{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 def test_scaffolded_names
g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) g = Rails::Generator::Base.instance('scaffold', %w(ProductLine))
@ -186,4 +105,3 @@ uses_mocha "Scaffold Generator Tests" do
end end
end end
end