Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9176

Show
Ignore:
Timestamp:
04/01/08 00:29:24 (4 months ago)
Author:
bitsweat
Message:

Update generator tests. Closes #11487 [thechrisoshow]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/lib/rails_generator/commands.rb

    r9122 r9176  
    187187        # :shebang sets the #!/usr/bin/ruby line for scripts 
    188188        #   file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby' 
    189         # :collision sets the collision option only for the destination file:  
     189        # :collision sets the collision option only for the destination file: 
    190190        #   file 'settings/server.yml', 'config/server.yml', :collision => :skip 
    191191        # 
     
    201201          # If source and destination are identical then we're done. 
    202202          if destination_exists and identical?(source, destination, &block) 
    203             return logger.identical(relative_destination)  
     203            return logger.identical(relative_destination) 
    204204          end 
    205205 
     
    301301          else 
    302302            logger.create relative_path 
    303                  unless options[:pretend] 
    304                    FileUtils.mkdir_p(path) 
    305                    # git doesn't require adding the paths, adding the files later will 
    306                    # automatically do a path add. 
    307                
    308                    # Subversion doesn't do path adds, so we need to add 
    309                    # each directory individually. 
    310                    # So stack up the directory tree and add the paths to 
    311                    # subversion in order without recursion. 
    312                    if options[:svn] 
    313                            stack=[relative_path] 
    314                            until File.dirname(stack.last) == stack.last # dirname('.') == '.' 
    315                              stack.push File.dirname(stack.last) 
    316                            end 
    317                            stack.reverse_each do |rel_path| 
    318                              svn_path = destination_path(rel_path) 
    319                              system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) 
    320                            end 
    321                    end 
     303            unless options[:pretend] 
     304              FileUtils.mkdir_p(path) 
     305              # git doesn't require adding the paths, adding the files later will 
     306              # automatically do a path add. 
     307 
     308              # Subversion doesn't do path adds, so we need to add 
     309              # each directory individually. 
     310              # So stack up the directory tree and add the paths to 
     311              # subversion in order without recursion. 
     312              if options[:svn] 
     313                stack = [relative_path] 
     314                until File.dirname(stack.last) == stack.last # dirname('.') == '.' 
     315                  stack.push File.dirname(stack.last) 
     316                end 
     317                stack.reverse_each do |rel_path| 
     318                  svn_path = destination_path(rel_path) 
     319                  system("svn add -N #{svn_path}") unless File.directory?(File.join(svn_path, '.svn')) 
     320                end 
     321              end 
    322322            end 
    323323          end 
     
    545545          logger.readme args.join(', ') 
    546546        end 
    547          
     547 
    548548        def migration_template(relative_source, relative_destination, options = {}) 
    549549          migration_directory relative_destination 
  • trunk/railties/test/generators/generator_test_helper.rb

    r8365 r9176  
     1require 'initializer' 
     2 
     3# Mocks out the configuration 
     4module Rails 
     5  def self.configuration 
     6    Rails::Configuration.new 
     7  end 
     8end 
     9 
     10require 'rails_generator' 
     11 
     12 
    113module GeneratorTestHelper 
    214  # Instantiates the Generator 
     
    5264  # It takes a name or symbol without the <tt>_controller_test</tt> part and an optional super class. 
    5365  # the contents of the class source file is passed to a block. 
    54   def assert_generated_functional_test_for(name,parent="Test::Unit::TestCase") 
     66  def assert_generated_functional_test_for(name,parent="ActionController::TestCase") 
    5567    assert_generated_class "test/functional/#{name.to_s.underscore}_controller_test",parent do |body| 
    5668      yield body if block_given? 
     
    6173  # It takes a name or symbol without the <tt>_test</tt> part and an optional super class. 
    6274  # the contents of the class source file is passed to a block. 
    63   def assert_generated_unit_test_for(name,parent="Test::Unit::TestCase") 
     75  def assert_generated_unit_test_for(name,parent="ActiveSupport::TestCase") 
    6476    assert_generated_class "test/unit/#{name.to_s.underscore}_test",parent do |body| 
    6577      yield body if block_given? 
     
    7890  # asserts that the given file exists 
    7991  def assert_file_exists(path) 
    80     assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{path}' should exist" 
     92    assert File.exist?("#{RAILS_ROOT}/#{path}"),"The file '#{RAILS_ROOT}/#{path}' should exist" 
    8193  end 
    8294 
     
    129141  def assert_generated_fixtures_for(name) 
    130142    assert_generated_yaml "test/fixtures/#{name.to_s.underscore}" do |yaml| 
    131       assert_generated_timestamps(yaml) 
    132143      yield yaml if block_given? 
    133144    end 
     
    145156  end 
    146157 
    147   # asserts that the given migration file was generated. 
    148   # It takes the name of the migration as a parameter. 
    149   # The migration body is passed to a block. 
    150158  def assert_generated_migration(name,parent="ActiveRecord::Migration") 
    151     assert_generated_class "db/migrate/001_#{name.to_s.underscore}",parent do |body| 
    152       assert body=~/timestamps/, "should have timestamps defined" 
    153       yield body if block_given? 
     159      file = 
     160   Dir.glob("#{RAILS_ROOT}/db/migrate/*_#{name.to_s.underscore}.rb").first 
     161      file = file.match(/db\/migrate\/[0-9]+_#{name.to_s.underscore}/).to_s 
     162      assert_generated_class file,parent do |body| 
     163        assert body=~/timestamps/, "should have timestamps defined" 
     164        yield body if block_given? 
     165      end 
    154166    end 
    155   end 
    156167 
    157168  # Asserts that the given migration file was not generated. 
     
    183194      assert body=~/t\.#{type.to_s} :#{name.to_s}/, "should have column #{name.to_s} defined" 
    184195  end 
    185  
    186   private 
    187     # asserts that the default timestamps are created in the fixture 
    188     def assert_generated_timestamps(yaml) 
    189       yaml.values.each do |v| 
    190         ["created_at", "updated_at"].each do |field| 
    191           assert v.keys.include?(field), "should have #{field} field by default" 
    192         end 
    193       end 
    194     end 
    195196end 
  • trunk/railties/test/generators/rails_model_generator_test.rb

    r8569 r9176  
    4949Dir.mkdir(RAILS_ROOT) unless File.exist?(RAILS_ROOT) 
    5050 
     51 
    5152$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    52 require 'rails_generator' 
     53 
    5354require 'generators/generator_test_helper' 
    5455 
     
    8182 
    8283  def test_model_generates_resources 
    83     run_generator('model', %w(Product)) 
     84    run_generator('model', %w(Product name:string)) 
    8485 
    8586    assert_generated_model_for :product 
     
    8990 
    9091  def test_model_skip_migration_skips_migration 
    91     run_generator('model', %w(Product --skip-migration)) 
     92    run_generator('model', %w(Product name:string --skip-migration)) 
    9293 
    9394    assert_generated_model_for :product 
  • trunk/railties/test/generators/rails_resource_generator_test.rb

    r8569 r9176  
    5050 
    5151$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    52 require 'rails_generator' 
    5352require 'generators/generator_test_helper' 
    5453 
     
    8180 
    8281  def test_resource_generates_resources 
    83     run_generator('scaffold', %w(Product)) 
     82    run_generator('resource', %w(Product name:string)) 
    8483 
    8584    assert_generated_controller_for :products 
     
    9392 
    9493  def test_resource_skip_migration_skips_migration 
    95     run_generator('resource', %w(Product --skip-migration)) 
     94    run_generator('resource', %w(Product name:string --skip-migration)) 
    9695 
    9796    assert_generated_controller_for :products 
  • trunk/railties/test/generators/rails_scaffold_generator_test.rb

    r8569 r9176  
    1 require 'test/unit' 
     1require 'abstract_unit' 
    22 
    33# Optionally load RubyGems. 
     
    5252 
    5353$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib" 
    54 require 'rails_generator' 
     54 
    5555require 'generators/generator_test_helper' 
    5656 
    57 class RailsScaffoldGeneratorTest < Test::Unit::TestCase 
     57uses_mocha "Scaffold Generator Tests" do 
     58  class RailsScaffoldGeneratorTest < Test::Unit::TestCase 
    5859 
    59   include GeneratorTestHelper 
     60    include GeneratorTestHelper 
    6061 
    61   def setup 
    62     ActiveRecord::Base.pluralize_table_names = true 
    63     Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 
    64     Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 
    65     Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 
    66     Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 
    67     Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 
    68     Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 
    69     Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 
    70     Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 
    71     Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 
    72     File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 
    73       f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 
     62    def setup 
     63      ActiveRecord::Base.pluralize_table_names = true 
     64      Dir.mkdir("#{RAILS_ROOT}/app") unless File.exist?("#{RAILS_ROOT}/app") 
     65      Dir.mkdir("#{RAILS_ROOT}/app/views") unless File.exist?("#{RAILS_ROOT}/app/views") 
     66      Dir.mkdir("#{RAILS_ROOT}/app/views/layouts") unless File.exist?("#{RAILS_ROOT}/app/views/layouts") 
     67      Dir.mkdir("#{RAILS_ROOT}/config") unless File.exist?("#{RAILS_ROOT}/config") 
     68      Dir.mkdir("#{RAILS_ROOT}/db") unless File.exist?("#{RAILS_ROOT}/db") 
     69      Dir.mkdir("#{RAILS_ROOT}/test") unless File.exist?("#{RAILS_ROOT}/test") 
     70      Dir.mkdir("#{RAILS_ROOT}/test/fixtures") unless File.exist?("#{RAILS_ROOT}/test/fixtures") 
     71      Dir.mkdir("#{RAILS_ROOT}/public") unless File.exist?("#{RAILS_ROOT}/public") 
     72      Dir.mkdir("#{RAILS_ROOT}/public/stylesheets") unless File.exist?("#{RAILS_ROOT}/public/stylesheets") 
     73      File.open("#{RAILS_ROOT}/config/routes.rb", 'w') do |f| 
     74        f<<"ActionController::Routing::Routes.draw do |map|\n\nend\n" 
     75      end 
    7476    end 
    75   end 
    7677 
    77   def teardown 
    78     FileUtils.rm_rf "#{RAILS_ROOT}/app" 
    79     FileUtils.rm_rf "#{RAILS_ROOT}/test" 
    80     FileUtils.rm_rf "#{RAILS_ROOT}/config" 
    81     FileUtils.rm_rf "#{RAILS_ROOT}/db" 
    82     FileUtils.rm_rf "#{RAILS_ROOT}/public" 
    83   end 
     78    def teardown 
     79      FileUtils.rm_rf "#{RAILS_ROOT}/app" 
     80      FileUtils.rm_rf "#{RAILS_ROOT}/test" 
     81      FileUtils.rm_rf "#{RAILS_ROOT}/config" 
     82      FileUtils.rm_rf "#{RAILS_ROOT}/db" 
     83      FileUtils.rm_rf "#{RAILS_ROOT}/public" 
     84    end 
    8485 
    85   def test_scaffolded_names 
    86     g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) 
    87     assert_equal "ProductLines", g.controller_name 
    88     assert_equal "ProductLines", g.controller_class_name 
    89     assert_equal "ProductLine", g.controller_singular_name 
    90     assert_equal "product_lines", g.controller_plural_name 
    91     assert_equal "product_lines", g.controller_file_name 
    92     assert_equal "product_lines", g.controller_table_name 
    93   end 
     86    def test_scaffolded_names 
     87      g = Rails::Generator::Base.instance('scaffold', %w(ProductLine)) 
     88      assert_equal "ProductLines", g.controller_name 
     89      assert_equal "ProductLines", g.controller_class_name 
     90      assert_equal "ProductLine", g.controller_singular_name 
     91      assert_equal "product_lines", g.controller_plural_name 
     92      assert_equal "product_lines", g.controller_file_name 
     93      assert_equal "product_lines", g.controller_table_name 
     94    end 
    9495 
    95   def test_scaffold_generates_resources 
     96    def test_scaffold_generates_resources 
    9697 
    97     run_generator('scaffold', %w(Product)) 
     98      run_generator('scaffold', %w(Product name:string)) 
    9899 
    99     assert_generated_controller_for :products do |f| 
     100      assert_generated_controller_for :products do |f| 
    100101 
    101       assert_has_method f, :index do |name, m| 
    102         assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
     102        assert_has_method f, :index do |name, m| 
     103          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
     104        end 
     105 
     106        assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
     107          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
     108        end 
     109 
     110        assert_has_method f, :new do |name, m| 
     111          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
     112        end 
     113 
     114        assert_has_method f, :create do |name, m| 
     115          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
     116          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     117        end 
     118 
    103119      end 
    104120 
    105       assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
    106         assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
     121      assert_generated_model_for :product 
     122      assert_generated_functional_test_for :products 
     123      assert_generated_unit_test_for :product 
     124      assert_generated_fixtures_for :products 
     125      assert_generated_helper_for :products 
     126      assert_generated_stylesheet :scaffold 
     127      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
     128 
     129      assert_generated_migration :create_products 
     130      assert_added_route_for :products 
     131    end 
     132 
     133    def test_scaffold_skip_migration_skips_migration 
     134      run_generator('scaffold', %w(Product name:string --skip-migration)) 
     135 
     136      assert_generated_model_for :product 
     137      assert_generated_functional_test_for :products 
     138      assert_generated_unit_test_for :product 
     139      assert_generated_fixtures_for :products 
     140      assert_generated_helper_for :products 
     141      assert_generated_stylesheet :scaffold 
     142      assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" 
     143      assert_skipped_migration :create_products 
     144      assert_added_route_for :products 
     145    end 
     146 
     147    def test_scaffold_generates_resources_with_attributes 
     148      run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) 
     149 
     150      assert_generated_controller_for :products do |f| 
     151 
     152        assert_has_method f, :index do |name, m| 
     153          assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
     154        end 
     155 
     156        assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
     157          assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
     158        end 
     159 
     160        assert_has_method f, :new do |name, m| 
     161          assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
     162        end 
     163 
     164        assert_has_method f, :create do |name, m| 
     165          assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
     166          assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
     167        end 
     168 
    107169      end 
    108170 
    109       assert_has_method f, :new do |name, m| 
    110         assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
     171      assert_generated_model_for :product 
     172      assert_generated_functional_test_for :products 
     173      assert_generated_unit_test_for :product 
     174      assert_generated_fixtures_for :products 
     175      assert_generated_helper_for :products 
     176      assert_generated_stylesheet :scaffold 
     177      assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
     178 
     179      assert_generated_migration :create_products do |t| 
     180        assert_generated_column t, :name, :string 
     181        assert_generated_column t, :supplier_id, :integer 
     182        assert_generated_column t, :created_at, :timestamp 
    111183      end 
    112184 
    113       assert_has_method f, :create do |name, m| 
    114         assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
    115         assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
    116       end 
    117  
     185      assert_added_route_for :products 
    118186    end 
    119187 
    120     assert_generated_model_for :product 
    121     assert_generated_functional_test_for :products 
    122     assert_generated_unit_test_for :product 
    123     assert_generated_fixtures_for :products 
    124     assert_generated_helper_for :products 
    125     assert_generated_stylesheet :scaffold 
    126     assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
    127     assert_generated_migration :create_products 
    128     assert_added_route_for :products 
    129188  end 
    130  
    131   def test_scaffold_skip_migration_skips_migration 
    132     run_generator('scaffold', %w(Product --skip-migration)) 
    133  
    134     assert_generated_model_for :product 
    135     assert_generated_functional_test_for :products 
    136     assert_generated_unit_test_for :product 
    137     assert_generated_fixtures_for :products 
    138     assert_generated_helper_for :products 
    139     assert_generated_stylesheet :scaffold 
    140     assert_generated_views_for :products, "index.html.erb","new.html.erb","edit.html.erb","show.html.erb" 
    141     assert_skipped_migration :create_products 
    142     assert_added_route_for :products 
    143   end 
    144  
    145   def test_scaffold_generates_resources_with_attributes 
    146     run_generator('scaffold', %w(Product name:string supplier_id:integer created_at:timestamp)) 
    147  
    148     assert_generated_controller_for :products do |f| 
    149  
    150       assert_has_method f, :index do |name, m| 
    151         assert_match /@products = Product\.find\(:all\)/, m, "#{name} should query products table" 
    152       end 
    153  
    154       assert_has_method f, :show, :edit, :update, :destroy do |name, m| 
    155         assert_match /@product = Product\.find\(params\[:id\]\)/, m, "#{name.to_s} should query products table" 
    156       end 
    157  
    158       assert_has_method f, :new do |name, m| 
    159         assert_match /@product = Product\.new/, m, "#{name.to_s} should instantiate a product" 
    160       end 
    161  
    162       assert_has_method f, :create do |name, m| 
    163         assert_match /@product = Product\.new\(params\[:product\]\)/, m, "#{name.to_s} should instantiate a product" 
    164         assert_match /format.xml  \{ render :xml => @product.errors, :status => :unprocessable_entity \}/, m, "#{name.to_s} should set status to :unprocessable_entity code for xml" 
    165       end 
    166  
    167     end 
    168  
    169     assert_generated_model_for :product 
    170     assert_generated_functional_test_for :products 
    171     assert_generated_unit_test_for :product 
    172     assert_generated_fixtures_for :products 
    173     assert_generated_helper_for :products 
    174     assert_generated_stylesheet :scaffold 
    175     assert_generated_views_for :products, "index.html.erb", "new.html.erb", "edit.html.erb", "show.html.erb" 
    176     assert_generated_migration :create_products do |t| 
    177       assert_generated_column t, :name, :string 
    178       assert_generated_column t, :supplier_id, :integer 
    179       assert_generated_column t, :created_at, :timestamp 
    180     end 
    181  
    182     assert_added_route_for :products 
    183   end 
    184  
    185189end 
  • trunk/railties/test/rails_generator_test.rb

    r7593 r9176  
    3434 
    3535$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" 
    36 require 'rails_generator' 
    37  
     36require 'generators/generator_test_helper' 
    3837 
    3938class RailsGeneratorTest < Test::Unit::TestCase 
     
    4746  def test_sources 
    4847    expected = [:lib, :vendor,  
    49                 :plugins, :plugins, # <plugin>/generators and <plugin>/rails_generators 
     48                "plugins (vendor/plugins)".to_sym, # <plugin>/generators and <plugin>/rails_generators 
    5049                :user,  
    5150                :RubyGems, :RubyGems, # gems named <x>_generator, gems containing /rails_generator/ folder 
     
    7574 
    7675  def test_lookup_missing_generator 
    77     assert_raise(MissingSourceFile) { 
     76    assert_raise Rails::Generator::GeneratorError do 
    7877      Rails::Generator::Base.lookup('missing_generator').klass 
    79     } 
     78    end 
    8079  end 
    8180 
     
    9998    spec = Rails::Generator::Base.lookup('working') 
    10099    assert_equal 'working', spec.name 
    101     assert_equal "#{RAILS_ROOT}/lib/generators/working", spec.path 
     100    assert_match(/#{spec.path}$/, "#{RAILS_ROOT}/lib/generators/working") 
    102101    assert_equal :lib, spec.source 
    103102    assert_nothing_raised { assert_match(/WorkingGenerator$/, spec.klass.name) }