From 3c9beb3dab73013af83b90983f283b76625052b8 Mon Sep 17 00:00:00 2001 From: Eugene Bolshakov Date: Mon, 17 Nov 2008 21:55:56 -0600 Subject: Add helper test generators [#1199 state:resolved] Signed-off-by: Joshua Peek --- railties/test/generators/generator_test_helper.rb | 31 +++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) (limited to 'railties/test/generators/generator_test_helper.rb') 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 _helper_test 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 _test 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 .rb 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 -- cgit v1.2.3