Module: RubiGen::GeneratorTestHelper
- Defined in:
- lib/rubigen/helpers/generator_test_helper.rb
Instance Method Summary collapse
- #app_root_files ⇒ Object
-
#assert_directory_exists(path) ⇒ Object
asserts that the given directory exists.
-
#assert_file_exists(path) ⇒ Object
asserts that the given file exists.
-
#assert_generated_class(path, parent = nil) ⇒ Object
asserts that the given class source file was generated.
-
#assert_generated_file(path) ⇒ Object
asserts that the given file was generated.
-
#assert_generated_module(path) ⇒ Object
asserts that the given module source file was generated.
-
#assert_generated_test_for(name, parent = "Test::Unit::TestCase") ⇒ Object
asserts that the given unit test was generated.
-
#assert_has_method(body, *methods) ⇒ Object
asserts that the given methods are defined in the body.
- #bare_setup ⇒ Object
- #bare_teardown ⇒ Object
-
#build_generator(name, params, sources, options) ⇒ Object
Instatiates the Generator.
- #rubygem_folders ⇒ Object
- #rubygems_setup ⇒ Object
- #rubygems_teardown ⇒ Object
-
#run_generator(name, params, sources, options = {}) ⇒ Object
Runs the create command (like the command line does).
-
#silence_generator ⇒ Object
Silences the logger temporarily and returns the output as a String.
Instance Method Details
#app_root_files ⇒ Object
104 105 106 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 104 def app_root_files Dir[APP_ROOT + '/**/*'] end |
#assert_directory_exists(path) ⇒ Object
asserts that the given directory exists
57 58 59 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 57 def assert_directory_exists(path) assert File.directory?("#{APP_ROOT}/#{path}"),"The directory '#{path}' should exist" end |
#assert_file_exists(path) ⇒ Object
asserts that the given file exists
52 53 54 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 52 def assert_file_exists(path) assert File.exists?("#{APP_ROOT}/#{path}"),"The file '#{path}' should exist" end |
#assert_generated_class(path, parent = nil) ⇒ Object
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.
64 65 66 67 68 69 70 71 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 64 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 |
#assert_generated_file(path) ⇒ Object
asserts that the given file was generated. the contents of the file is passed to a block.
44 45 46 47 48 49 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 44 def assert_generated_file(path) assert_file_exists(path) File.open("#{APP_ROOT}/#{path}") do |f| yield f.read if block_given? end end |
#assert_generated_module(path) ⇒ Object
asserts that the given module source file was generated. It takes a path without the .rb
part. the contents of the class source file is passed to a block.
76 77 78 79 80 81 82 83 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 76 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 |
#assert_generated_test_for(name, parent = "Test::Unit::TestCase") ⇒ Object
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.
88 89 90 91 92 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 88 def assert_generated_test_for(name, parent="Test::Unit::TestCase") assert_generated_class "test/test_#{name.to_s.underscore}", parent do |body| yield body if block_given? end end |
#assert_has_method(body, *methods) ⇒ Object
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.
97 98 99 100 101 102 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 97 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 |
#bare_setup ⇒ Object
123 124 125 126 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 123 def FileUtils.mkdir_p(APP_ROOT) @stdout = StringIO.new end |
#bare_teardown ⇒ Object
128 129 130 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 128 def FileUtils.rm_rf TMP_ROOT || APP_ROOT end |
#build_generator(name, params, sources, options) ⇒ Object
Instatiates the Generator
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 15 def build_generator(name, params, sources, ) @stdout ||= StringIO.new .merge!(:collision => :force) # so no questions are prompted .merge!(:stdout => @stdout) # so stdout is piped to a StringIO if sources.is_a?(Symbol) if sources == :app RubiGen::Base.use_application_sources! else RubiGen::Base.use_component_sources! end else RubiGen::Base.reset_sources RubiGen::Base.prepend_sources(*sources) unless sources.blank? end RubiGen::Base.instance(name, params, ) end |
#rubygem_folders ⇒ Object
108 109 110 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 108 def rubygem_folders %w[bin examples lib test] end |
#rubygems_setup ⇒ Object
112 113 114 115 116 117 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 112 def rubygems_setup rubygem_folders.each do |folder| Dir.mkdir("#{APP_ROOT}/#{folder}") unless File.exists?("#{APP_ROOT}/#{folder}") end end |
#rubygems_teardown ⇒ Object
119 120 121 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 119 def rubygems_teardown end |
#run_generator(name, params, sources, options = {}) ⇒ Object
Runs the create command (like the command line does)
6 7 8 9 10 11 12 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 6 def run_generator(name, params, sources, = {}) generator = build_generator(name, params, sources, ) silence_generator do generator.command(:create).invoke! end generator end |
#silence_generator ⇒ Object
Silences the logger temporarily and returns the output as a String
33 34 35 36 37 38 39 40 |
# File 'lib/rubigen/helpers/generator_test_helper.rb', line 33 def silence_generator logger_original = RubiGen::Base.logger myout = StringIO.new RubiGen::Base.logger = RubiGen::SimpleLogger.new(myout) yield if block_given? RubiGen::Base.logger = logger_original myout.string end |