generator-spec 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +144 -0
  5. data/Rakefile +48 -0
  6. data/VERSION +1 -0
  7. data/generator-spec.gemspec +106 -0
  8. data/lib/generators/demo/demo_generator.rb +91 -0
  9. data/lib/rspec_for_generators.rb +11 -0
  10. data/lib/rspec_for_generators/fixtures/routes.rb +2 -0
  11. data/lib/rspec_for_generators/generator_spec_helper.rb +95 -0
  12. data/lib/rspec_for_generators/main.rb +18 -0
  13. data/lib/rspec_for_generators/matchers/content/have_block.rb +47 -0
  14. data/lib/rspec_for_generators/matchers/content/have_call.rb +42 -0
  15. data/lib/rspec_for_generators/matchers/content/have_class.rb +32 -0
  16. data/lib/rspec_for_generators/matchers/content/have_method.rb +41 -0
  17. data/lib/rspec_for_generators/matchers/content/have_module.rb +32 -0
  18. data/lib/rspec_for_generators/matchers/content/include_module.rb +32 -0
  19. data/lib/rspec_for_generators/matchers/content/inherit_from.rb +31 -0
  20. data/lib/rspec_for_generators/matchers/file/generate_directory.rb +71 -0
  21. data/lib/rspec_for_generators/matchers/file/generate_file.rb +71 -0
  22. data/lib/rspec_for_generators/matchers/file/generate_migration.rb +50 -0
  23. data/lib/rspec_for_generators/matchers/helpers/content.rb +18 -0
  24. data/lib/rspec_for_generators/matchers/helpers/file.rb +26 -0
  25. data/lib/rspec_for_generators/matchers/helpers/migration.rb +102 -0
  26. data/lib/rspec_for_generators/matchers/migration/have_column.rb +13 -0
  27. data/lib/rspec_for_generators/matchers/migration/have_index.rb +9 -0
  28. data/lib/rspec_for_generators/matchers/migration/have_migration.rb +5 -0
  29. data/lib/rspec_for_generators/matchers/migration/have_table.rb +18 -0
  30. data/lib/rspec_for_generators/matchers/migration/have_tbl_column.rb +25 -0
  31. data/lib/rspec_for_generators/matchers/migration/have_up_down.rb +13 -0
  32. data/lib/rspec_for_generators/rails_helpers/all.rb +6 -0
  33. data/lib/rspec_for_generators/rails_helpers/rails_app.rb +53 -0
  34. data/lib/rspec_for_generators/rails_helpers/rails_controller.rb +36 -0
  35. data/lib/rspec_for_generators/rails_helpers/rails_helper.rb +36 -0
  36. data/lib/rspec_for_generators/rails_helpers/rails_mailer.rb +29 -0
  37. data/lib/rspec_for_generators/rails_helpers/rails_model.rb +27 -0
  38. data/lib/rspec_for_generators/rails_helpers/rails_orm.rb +77 -0
  39. data/lib/rspec_for_generators/rails_helpers/rails_view.rb +29 -0
  40. data/lib/rspec_for_generators/rails_spec_helper.rb +35 -0
  41. data/lib/rspec_for_generators/rspec_test_case.rb +26 -0
  42. data/rspec_for_generators.gemspec +96 -0
  43. data/spec/rspec_for_generators/rspec_for_generators_spec.rb +41 -0
  44. data/spec/spec.opts +1 -0
  45. data/spec/spec_helper.rb +6 -0
  46. data/wiki/Custom Rails 3 Generators.textile +110 -0
  47. data/wiki/rails_view_helpers.textile +167 -0
  48. metadata +205 -0
@@ -0,0 +1,71 @@
1
+ # Asserts a given file exists. You need to supply an absolute path or a path relative
2
+ # to the configured destination:
3
+ #
4
+ # generator.should have_generated_file "app/controller/products_controller.rb"
5
+ #
6
+
7
+ module RSpec::FileMatchers
8
+ class GenerateFile
9
+
10
+ def initialize(relative_path, type = nil)
11
+ @relative_path = relative_rails_file relative_path, type
12
+ end
13
+
14
+ def matches?(generator, &block)
15
+ @expected = File.expand_path(@relative_path, generator.class.destination_root)
16
+ ex = File.exists?(@expected)
17
+ if block && ex
18
+ read = File.read(@expected)
19
+ ruby_content = read.extend(RSpec::RubyContent::Helpers)
20
+ yield ruby_content
21
+ else
22
+ ex
23
+ end
24
+ end
25
+
26
+ def failure_message
27
+ "Expected file #{@relative_path} to have been generated, but it was not"
28
+ end
29
+
30
+ def negative_failure_message
31
+ "Did not expected file #{@relative_path} to have been generated, but it was"
32
+ end
33
+
34
+ protected
35
+
36
+ def relative_rails_file path, type = nil
37
+ return File.join(::Rails.root, base_dir(type), folder(type), "#{path.to_s}.rb") if type
38
+ File.join(::Rails.root, path)
39
+ end
40
+
41
+ def folder type
42
+ case type
43
+ when :observer
44
+ 'models'
45
+ else
46
+ type.to_s.pluralize
47
+ end
48
+ end
49
+
50
+ def base_dir type
51
+ case type
52
+ when :model, :controller, :view, :helper, :observer
53
+ 'app'
54
+ when :migration
55
+ 'db'
56
+ when :javascript, :stylesheet
57
+ 'public'
58
+ when :initializer, :locale
59
+ 'config'
60
+ else
61
+ ''
62
+ end
63
+ end
64
+ end
65
+
66
+ def generate_file(relative, type = nil)
67
+ GenerateFile.new(relative, type)
68
+ end
69
+ alias_method :generate_directory, :generate_file
70
+ end
71
+
@@ -0,0 +1,50 @@
1
+ # This method manipulates the given path and tries to find any migration which
2
+ # matches the migration name. For example, the call above is converted to:
3
+ #
4
+ # generator.should generate_migration "db/migrate/003_create_products.rb"
5
+ #
6
+ # Consequently it accepts the same arguments as the matcher have_file.
7
+
8
+ module RSpec
9
+ module FileMatchers
10
+ class GenerateMigration
11
+
12
+ def initialize(relative)
13
+ @relative = relative
14
+ end
15
+
16
+ def matches?(generator)
17
+ ex = migration_file_name(relative, generator)
18
+ if block && ex
19
+ read = File.read(@expected)
20
+ ruby_content = read.extend(RSpec::RubyContent::Helpers)
21
+ yield ruby_content
22
+ else
23
+ ex
24
+ end
25
+ end
26
+
27
+ def failure_message
28
+ "Expected migration #{relative} to have been generated, but it was not"
29
+ end
30
+
31
+ def negative_failure_message
32
+ "Did not expect migration #{relative} to have been generated, but it was"
33
+ end
34
+
35
+ protected
36
+
37
+ def migration_file_name(relative, generator) #:nodoc:
38
+ absolute = File.expand_path(relative, generator.class.destination_root)
39
+ dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
40
+ Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
41
+ end
42
+
43
+ end
44
+
45
+ def generate_migration(relative)
46
+ GenerateMigration.new(relative)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ module RSpec::RubyContent
2
+ module Helpers
3
+ def check_matchings matchings
4
+ matchings.each do |matching|
5
+ self.should match /#{Regexp.escape(matching)}/
6
+ end
7
+ end
8
+ alias_method :matchings, :check_matchings
9
+
10
+
11
+ def check_methods methods, type=nil
12
+ methods.each do |method_name|
13
+ self.should have_method(method_name, type)
14
+ end
15
+ end
16
+ alias_method :methods, :check_methods
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ module RSpec::Generator
2
+ module Helpers
3
+ def check_file file_name, type=nil, &block
4
+ self.should generate_file(file_name, type, &block)
5
+ end
6
+ alias_method :file, :check_file
7
+
8
+ def check_view(file_name, matchings)
9
+ self.should generate_file(file_name, :view) do |content|
10
+ check_matchings matchings
11
+ end
12
+ end
13
+ alias_method :view, :check_view
14
+
15
+ def check_model(file_name, clazz, options = {})
16
+ self.should generate_file("#{file_name.underscore}.rb", :model) do |file_content|
17
+ file_content.should have_class clazz do |content|
18
+ check_matchings options[:matchings]
19
+ check_methods(options[:methods])
20
+ check_class_methods(options[:class_methods])
21
+ end
22
+ end
23
+ end
24
+ alias_method :model, :check_model
25
+ end
26
+ end
@@ -0,0 +1,102 @@
1
+ module RSpec::Generator
2
+ module Helpers
3
+ def check_migration(file_name, &block)
4
+ self.should generate_file(file_name, :migration) do |content|
5
+ yield content if block
6
+ end
7
+ end
8
+ alias_method :migration, :check_migration
9
+ end
10
+ end
11
+
12
+ module RSpec::RubyContent
13
+ module Helpers
14
+ def check_up(&block)
15
+ self.should have_method('up', :class) do |content|
16
+ yield content if block
17
+ end
18
+ end
19
+ alias_method :up, :check_up
20
+
21
+ def check_down(&block)
22
+ self.should have_method('down', :class) do |content|
23
+ yield content if block
24
+ end
25
+ end
26
+ alias_method :down, :check_down
27
+
28
+ def check_create_table(name, &block)
29
+ self.should match /create_table\s+:#{name}\s+(.*)end/m
30
+ if block_given?
31
+ ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
32
+ yield ruby_content
33
+ end
34
+ end
35
+ alias_method :creates_table, :check_create_table
36
+
37
+ def check_change_table(name, &block)
38
+ self.should match /change_table\s+:#{name}\s+(.*)end/m
39
+ if block_given?
40
+ ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
41
+ yield ruby_content
42
+ end
43
+ end
44
+ alias_method :changes_table, :check_change_table
45
+
46
+ def check_drop_table name
47
+ self.should match /drop_table\s+:#{name}/
48
+ end
49
+ alias_method :drops_table, :check_drop_table
50
+
51
+ def check_rename_table name
52
+ self.should match /rename_table\s+:#{name}/
53
+ end
54
+ alias_method :renames_table, :check_rename_table
55
+
56
+ def check_remove_column name
57
+ self.should match /t.remove\s+:#{name}/
58
+ end
59
+ alias_method :removes_column, :check_remove_column
60
+
61
+ def check_add_column name, type='string'
62
+ self.should match /t.#{type}\s+:#{name}/
63
+ end
64
+ alias_method :adds_column, :check_add_column
65
+
66
+ def check_change_column name, type='string'
67
+ self.should match /t.change\s+:#{name}\s*,\s*:#{type}/
68
+ end
69
+ alias_method :changes_column, :check_change_column
70
+
71
+ def check_add_tbl_column tbl_name, name, type='string'
72
+ self.should match /add_column\s+:#{tbl_name}\s*,\s*:#{name},#{type}/
73
+ end
74
+ alias_method :adds_tbl_column, :check_add_tbl_column
75
+
76
+ def check_change_tbl_column tbl_name, name, type='string'
77
+ self.should match /change_column\s+:#{tbl_name}\s*,\s*:#{name}\s*,\s*#{type}/
78
+ end
79
+ alias_method :changes_tbl_column, :check_change_tbl_column
80
+
81
+ def check_rename_tbl_column tbl_name, new_name
82
+ self.should match /rename_column\s+:#{tbl_name}\s*,\s*:#{new_name}/
83
+ end
84
+ alias_method :renames_tbl_column, :check_rename_tbl_column
85
+
86
+ def check_remove_tbl_column tbl_name, name
87
+ self.should match /remove_column\s+:#{tbl_name}\s*,\s*:#{name}/
88
+ end
89
+ alias_method :removes_tbl_column, :check_remove_tbl_column
90
+
91
+ def check_add_tbl_index tbl_name, name
92
+ self.should match /add_index\s+:#{tbl_name}\s*,\s*:#{name}/
93
+ end
94
+ alias_method :adds_tbl_index, :check_add_tbl_index
95
+
96
+ def check_remove_tbl_index tbl_name, name
97
+ self.should match /remove_index\s+:#{tbl_name}\s*,\s*:#{name}/
98
+ end
99
+ alias_method :removes_tbl_index, :check_remove_tbl_index
100
+
101
+ end
102
+ end
@@ -0,0 +1,13 @@
1
+ module RSpec::RubyContentMatchers
2
+ def have_remove_column(name)
3
+ HaveCall.new('t.remove', ":#{name}")
4
+ end
5
+
6
+ def have_add_column(name, type='string')
7
+ HaveCall.new("t.#{type}", ":#{name}")
8
+ end
9
+
10
+ def have_change_column(name, type='string')
11
+ HaveCall.new("t.change", ":#{name}\s*,\s*:#{type}")
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module RSpec::RubyContentMatchers
2
+ def have_add_tbl_index(tbl_name, index_name)
3
+ HaveCall.new('add_index', ":#{tbl_name}\s*,\s*:#{index_name}")
4
+ end
5
+
6
+ def have_remove_index(name)
7
+ HaveCall.new('remove_index', ":#{tbl_name}\s*,\s*:#{index_name}")
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec::FileMatchers
2
+ def have_migration(name)
3
+ GenerateFile.new(name, :migration)
4
+ end
5
+ end
@@ -0,0 +1,18 @@
1
+ module RSpec::RubyContentMatchers
2
+ def have_create_table(name)
3
+ HaveBlock.new('create_table', :args => ":#{name}")
4
+ end
5
+
6
+ def have_change_table(name)
7
+ HaveBlock.new('change_table', :args => ":#{name}")
8
+ end
9
+
10
+ def have_drop_table(name)
11
+ HaveCall.new('drop_table', ":#{name}")
12
+ end
13
+
14
+ def have_rename_table(name)
15
+ HaveCall.new('rename_table', ":#{name}")
16
+ end
17
+ end
18
+
@@ -0,0 +1,25 @@
1
+ module RSpec::RubyContentMatchers
2
+ def have_remove_tbl_column(tbl_name, col_name)
3
+ HaveCall.new('remove_column', ":#{tbl_name}\s*,\s*:#{col_name}")
4
+ end
5
+
6
+ def have_rename_tbl_column(tbl_name, new_name)
7
+ HaveCall.new('rename_column', ":#{tbl_name}\s*,\s*:#{new_name}")
8
+ end
9
+
10
+ def have_add_tbl_column(tbl_name, col_name, type = 'string')
11
+ HaveCall.new('add_column', ":#{tbl_name}\s*,\s*:#{col_name}\s*,\s*:#{type}")
12
+ end
13
+
14
+ def have_change_tbl_column tbl_name, col_name, type='string'
15
+ HaveCall.new('change_column', ":#{tbl_name}\s*,\s*:#{col_name}\s*,\s*:#{type}")
16
+ end
17
+
18
+ def have_add_column(name, type='string')
19
+ HaveCall.new("t.#{type}", ":#{name}")
20
+ end
21
+
22
+ def have_change_column(name, type='string')
23
+ HaveCall.new("t.change", ":#{name}\s*,\s*:#{type}")
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module RSpec::RubyContentMatchers
2
+ def have_up(method, type = nil)
3
+ HaveMethod.new up, :class
4
+ end
5
+
6
+ def have_down(method, type = nil)
7
+ HaveMethod.new up, :class
8
+ end
9
+ end
10
+
11
+
12
+
13
+
@@ -0,0 +1,6 @@
1
+ require 'rspec_for_generators/rails_helpers/rails_app'
2
+ require 'rspec_for_generators/rails_helpers/rails_orm'
3
+ require 'rspec_for_generators/rails_helpers/rails_controller'
4
+ require 'rspec_for_generators/rails_helpers/rails_helper'
5
+ require 'rspec_for_generators/rails_helpers/rails_view'
6
+ require 'rspec_for_generators/rails_helpers/rails_mailer'
@@ -0,0 +1,53 @@
1
+ module RSpec::Rails
2
+ module App
3
+ def self.debug!
4
+ self.extend(Debug)
5
+ end
6
+
7
+ module Debug
8
+ def rails_files
9
+ FileList["#{::Rails.root}/**/*.rb"]
10
+ end
11
+
12
+ def rails_app_files
13
+ FileList["#{::Rails.root}/app/**/*.rb"]
14
+ end
15
+
16
+ def rails_model_files
17
+ FileList["#{::Rails.root}/app/models/**/*.rb"]
18
+ end
19
+
20
+ def rails_controller_files
21
+ FileList["#{::Rails.root}/app/controllers/**/*.rb"]
22
+ end
23
+
24
+ def rails_helper_files
25
+ FileList["#{::Rails.root}/app/helpers/**/*.rb"]
26
+ end
27
+
28
+ def rails_view_files
29
+ FileList["#{::Rails.root}/app/views/**/*.yml"]
30
+ end
31
+
32
+ def rails_initializer_files
33
+ FileList["#{::Rails.root}/config/initializers/**/*.rb"]
34
+ end
35
+
36
+ def rails_migration_files
37
+ FileList["#{::Rails.root}/db/migration/**/*.rb"]
38
+ end
39
+
40
+ def rails_locale_files
41
+ FileList["#{::Rails.root}/config/locales/**/*.yml"]
42
+ end
43
+
44
+ def rails_javascript_files
45
+ FileList["#{::Rails.root}/public/javascripts/**/*.yml"]
46
+ end
47
+
48
+ def rails_stylesheet_files
49
+ FileList["#{::Rails.root}/public/stylesheets/**/*.yml"]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,36 @@
1
+ module RSpec
2
+ module Rails
3
+ module Controller
4
+ def create_controller name, content=nil
5
+ file = controller_file_name(name)
6
+ unless File.exist?(file)
7
+ FileUtils.mkdir_p File.dirname(file)
8
+ content ||= yield if block_given?
9
+ File.open(file, 'w') do |f|
10
+ f.puts controller_content(content)
11
+ end
12
+ end
13
+ end
14
+
15
+ def controller_content name, content=nil
16
+ %Q{class #{name.to_s.camelize}Controller < ActionController::Base
17
+ #{content}
18
+ end}
19
+ end
20
+
21
+ def remove_controller name
22
+ file = controller_file_name(name)
23
+ FileUtils.rm_f(file) if File.exist?(file)
24
+ end
25
+
26
+ def remove_controllers *names
27
+ names.each{|name| remove_controller name }
28
+ end
29
+
30
+ def controller_file_name name
31
+ File.join(::Rails.root, "app/controllers/#{name}.rb")
32
+ end
33
+
34
+ end
35
+ end
36
+ end