diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-28 13:16:14 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-28 13:16:14 +0200 |
commit | 1ca51df0001a750fafb0da70e4560c1ec439236c (patch) | |
tree | 677de24572e4d698149694145f14dfd5438fea56 /railties/test/generators | |
parent | 4f3e44fa03ab8fc47f75fa710b28c72b9b2328b9 (diff) | |
download | rails-1ca51df0001a750fafb0da70e4560c1ec439236c.tar.gz rails-1ca51df0001a750fafb0da70e4560c1ec439236c.tar.bz2 rails-1ca51df0001a750fafb0da70e4560c1ec439236c.zip |
Added migration and session_migration generator.
Diffstat (limited to 'railties/test/generators')
-rw-r--r-- | railties/test/generators/generators_test_helper.rb | 9 | ||||
-rw-r--r-- | railties/test/generators/migration_generator_test.rb | 58 | ||||
-rw-r--r-- | railties/test/generators/session_migration_generator_test.rb | 24 |
3 files changed, 91 insertions, 0 deletions
diff --git a/railties/test/generators/generators_test_helper.rb b/railties/test/generators/generators_test_helper.rb index 662646fbff..7c58c7b64e 100644 --- a/railties/test/generators/generators_test_helper.rb +++ b/railties/test/generators/generators_test_helper.rb @@ -73,4 +73,13 @@ class GeneratorsTestCase < Test::Unit::TestCase migration = Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first File.basename(migration) if migration end + + def assert_class_method_for(content, method, &block) + assert_instance_method_for content, "self.#{method}", &block + end + + def assert_instance_method_for(content, method) + assert_match /def #{method}(.*?)end/m, content + yield content.match(/def #{method}(.*?)end/m)[1] + end end diff --git a/railties/test/generators/migration_generator_test.rb b/railties/test/generators/migration_generator_test.rb new file mode 100644 index 0000000000..90bc91344f --- /dev/null +++ b/railties/test/generators/migration_generator_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/migration/migration_generator' +require 'generators/rails/migration/migration_generator' + +class MigrationGeneratorTest < GeneratorsTestCase + + def test_migration + @migration = "change_title_body_from_posts" + run_generator + assert_migration "db/migrate/#{@migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration/ + end + + def test_migration_with_class_name + @migration = "ChangeTitleBodyFromPosts" + run_generator + assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{@migration} < ActiveRecord::Migration/ + end + + def test_add_migration_with_attributes + @migration = "add_title_body_to_posts" + run_generator [@migration, "title:string", "body:text"] + content = assert_migration "db/migrate/#{@migration}.rb" + + assert_class_method_for content, :up do |up| + assert_match /add_column :posts, :title, :string/, up + assert_match /add_column :posts, :body, :text/, up + end + + assert_class_method_for content, :down do |down| + assert_match /remove_column :posts, :title/, down + assert_match /remove_column :posts, :body/, down + end + end + + def test_remove_migration_with_attributes + @migration = "remove_title_body_from_posts" + run_generator [@migration, "title:string", "body:text"] + content = assert_migration "db/migrate/#{@migration}.rb" + + assert_class_method_for content, :up do |up| + assert_match /remove_column :posts, :title/, up + assert_match /remove_column :posts, :body/, up + end + + assert_class_method_for content, :down do |down| + assert_match /add_column :posts, :title, :string/, down + assert_match /add_column :posts, :body, :text/, down + end + end + + protected + + def run_generator(args=[@migration]) + silence(:stdout) { Rails::Generators::MigrationGenerator.start args, :root => destination_root } + end + +end diff --git a/railties/test/generators/session_migration_generator_test.rb b/railties/test/generators/session_migration_generator_test.rb new file mode 100644 index 0000000000..05b51b1566 --- /dev/null +++ b/railties/test/generators/session_migration_generator_test.rb @@ -0,0 +1,24 @@ +require 'abstract_unit' +require 'generators/generators_test_helper' +require 'generators/active_record/session_migration/session_migration_generator' +require 'generators/rails/session_migration/session_migration_generator' + +class SessionMigrationGeneratorTest < GeneratorsTestCase + + def test_session_migration_with_default_name + run_generator + assert_migration "db/migrate/add_sessions_table.rb", /class AddSessionsTable < ActiveRecord::Migration/ + end + + def test_session_migration_with_given_name + run_generator ["create_session_table"] + assert_migration "db/migrate/create_session_table.rb", /class CreateSessionTable < ActiveRecord::Migration/ + end + + protected + + def run_generator(args=[]) + silence(:stdout) { Rails::Generators::SessionMigrationGenerator.start args, :root => destination_root } + end + +end |