diff options
Diffstat (limited to 'railties/test/generators')
5 files changed, 83 insertions, 27 deletions
diff --git a/railties/test/generators/generator_test_helper.rb b/railties/test/generators/generator_test_helper.rb index 0901b215e4..01bf1c90bd 100644 --- a/railties/test/generators/generator_test_helper.rb +++ b/railties/test/generators/generator_test_helper.rb @@ -145,6 +145,19 @@ class GeneratorTestCase < Test::Unit::TestCase end end + # Asserts that the given helper test test was generated. + # It takes a name or symbol without the <tt>_helper_test</tt> part and an optional super class. + # The contents of the class source file is passed to a block. + def assert_generated_helper_test_for(name, parent = "ActionView::TestCase") + path = "test/unit/helpers/#{name.to_s.underscore}_helper_test" + # Have to pass the path without the "test/" part so that class_name_from_path will return a correct result + class_name = class_name_from_path(path.gsub(/^test\//, '')) + + assert_generated_class path,parent,class_name 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. @@ -172,19 +185,21 @@ class GeneratorTestCase < Test::Unit::TestCase # 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) + def assert_generated_class(path, parent = nil, class_name = class_name_from_path(path)) + assert_generated_file("#{path}.rb") do |body| + assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class" + yield body if block_given? + end + end + + def class_name_from_path(path) # FIXME: Sucky way to detect namespaced classes if path.split('/').size > 3 path =~ /\/?(\d+_)?(\w+)\/(\w+)$/ - class_name = "#{$2.camelize}::#{$3.camelize}" + "#{$2.camelize}::#{$3.camelize}" else path =~ /\/?(\d+_)?(\w+)$/ - class_name = $2.camelize - end - - assert_generated_file("#{path}.rb") do |body| - assert_match /class #{class_name}#{parent.nil? ? '':" < #{parent}"}/, body, "the file '#{path}.rb' should be a class" - yield body if block_given? + $2.camelize end end diff --git a/railties/test/generators/rails_controller_generator_test.rb b/railties/test/generators/rails_controller_generator_test.rb index f839ea97e6..43fbe972e2 100644 --- a/railties/test/generators/rails_controller_generator_test.rb +++ b/railties/test/generators/rails_controller_generator_test.rb @@ -11,6 +11,7 @@ class RailsControllerGeneratorTest < GeneratorTestCase assert_generated_controller_for :products assert_generated_functional_test_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products end def test_controller_generates_namespaced_controller @@ -19,24 +20,25 @@ class RailsControllerGeneratorTest < GeneratorTestCase assert_generated_controller_for "admin::products" assert_generated_functional_test_for "admin::products" assert_generated_helper_for "admin::products" + assert_generated_helper_test_for "admin::products" end def test_controller_generates_namespaced_and_not_namespaced_controllers - run_generator('controller', %w(products)) - - # We have to require the generated helper to show the problem because - # the test helpers just check for generated files and contents but - # do not actually load them. But they have to be loaded (as in a real environment) - # to make the second generator run fail - require "#{RAILS_ROOT}/app/helpers/products_helper" - - assert_nothing_raised do - begin - run_generator('controller', %w(admin::products)) - ensure - # cleanup - Object.send(:remove_const, :ProductsHelper) - end + run_generator('controller', %w(products)) + + # We have to require the generated helper to show the problem because + # the test helpers just check for generated files and contents but + # do not actually load them. But they have to be loaded (as in a real environment) + # to make the second generator run fail + require "#{RAILS_ROOT}/app/helpers/products_helper" + + assert_nothing_raised do + begin + run_generator('controller', %w(admin::products)) + ensure + # cleanup + Object.send(:remove_const, :ProductsHelper) end + end end end diff --git a/railties/test/generators/rails_helper_generator_test.rb b/railties/test/generators/rails_helper_generator_test.rb new file mode 100644 index 0000000000..8d05f555e6 --- /dev/null +++ b/railties/test/generators/rails_helper_generator_test.rb @@ -0,0 +1,36 @@ +require File.dirname(__FILE__) + '/generator_test_helper' + +class RailsHelperGeneratorTest < GeneratorTestCase + def test_helper_generates_helper + run_generator('helper', %w(products)) + + assert_generated_helper_for :products + assert_generated_helper_test_for :products + end + + def test_helper_generates_namespaced_helper + run_generator('helper', %w(admin::products)) + + assert_generated_helper_for "admin::products" + assert_generated_helper_test_for "admin::products" + end + + def test_helper_generates_namespaced_and_not_namespaced_helpers + run_generator('helper', %w(products)) + + # We have to require the generated helper to show the problem because + # the test helpers just check for generated files and contents but + # do not actually load them. But they have to be loaded (as in a real environment) + # to make the second generator run fail + require "#{RAILS_ROOT}/app/helpers/products_helper" + + assert_nothing_raised do + begin + run_generator('helper', %w(admin::products)) + ensure + # cleanup + Object.send(:remove_const, :ProductsHelper) + end + end + end +end diff --git a/railties/test/generators/rails_resource_generator_test.rb b/railties/test/generators/rails_resource_generator_test.rb index 45e4850ef5..1f5bd0ef1e 100644 --- a/railties/test/generators/rails_resource_generator_test.rb +++ b/railties/test/generators/rails_resource_generator_test.rb @@ -1,7 +1,6 @@ require 'generators/generator_test_helper' class RailsResourceGeneratorTest < GeneratorTestCase - def test_resource_generates_resources run_generator('resource', %w(Product name:string)) @@ -10,6 +9,7 @@ class RailsResourceGeneratorTest < GeneratorTestCase assert_generated_fixtures_for :products assert_generated_functional_test_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_migration :create_products assert_added_route_for :products end @@ -22,8 +22,8 @@ class RailsResourceGeneratorTest < GeneratorTestCase assert_generated_fixtures_for :products assert_generated_functional_test_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_skipped_migration :create_products assert_added_route_for :products end - end diff --git a/railties/test/generators/rails_scaffold_generator_test.rb b/railties/test/generators/rails_scaffold_generator_test.rb index de6b38dafe..926607f55c 100644 --- a/railties/test/generators/rails_scaffold_generator_test.rb +++ b/railties/test/generators/rails_scaffold_generator_test.rb @@ -2,7 +2,6 @@ require 'generators/generator_test_helper' require 'abstract_unit' class RailsScaffoldGeneratorTest < GeneratorTestCase - def test_scaffolded_names g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) assert_equal "ProductLines", g.controller_name @@ -43,6 +42,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase assert_generated_unit_test_for :product assert_generated_fixtures_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_stylesheet :scaffold assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" @@ -58,6 +58,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase assert_generated_unit_test_for :product assert_generated_fixtures_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_stylesheet :scaffold assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" assert_skipped_migration :create_products @@ -93,6 +94,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase assert_generated_unit_test_for :product assert_generated_fixtures_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_stylesheet :scaffold assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" @@ -126,6 +128,7 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase assert_generated_unit_test_for :product assert_generated_fixtures_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_stylesheet :scaffold assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" assert_skipped_migration :create_products @@ -140,10 +143,10 @@ class RailsScaffoldGeneratorTest < GeneratorTestCase assert_generated_unit_test_for :products assert_generated_fixtures_for :products assert_generated_helper_for :products + assert_generated_helper_test_for :products assert_generated_stylesheet :scaffold assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" assert_skipped_migration :create_products assert_added_route_for :products end - end |