rspec_for_generators 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +111 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/generators/demo/demo_generator.rb +91 -0
- data/lib/rspec_for_generators.rb +11 -0
- data/lib/rspec_for_generators/fixtures/routes.rb +2 -0
- data/lib/rspec_for_generators/generator_spec_helper.rb +73 -0
- data/lib/rspec_for_generators/main.rb +7 -0
- data/lib/rspec_for_generators/matchers/content/all.rb +1 -0
- data/lib/rspec_for_generators/matchers/content/have_class.rb +36 -0
- data/lib/rspec_for_generators/matchers/content/have_method.rb +49 -0
- data/lib/rspec_for_generators/matchers/content/have_module.rb +34 -0
- data/lib/rspec_for_generators/matchers/content/include_module.rb +36 -0
- data/lib/rspec_for_generators/matchers/content/inherit_from.rb +33 -0
- data/lib/rspec_for_generators/matchers/file/all.rb +1 -0
- data/lib/rspec_for_generators/matchers/file/generate_file.rb +73 -0
- data/lib/rspec_for_generators/matchers/file/generate_migration.rb +43 -0
- data/lib/rspec_for_generators/matchers/helpers/content.rb +20 -0
- data/lib/rspec_for_generators/matchers/helpers/file.rb +28 -0
- data/lib/rspec_for_generators/rails_helpers/all.rb +6 -0
- data/lib/rspec_for_generators/rails_helpers/rails_app.rb +55 -0
- data/lib/rspec_for_generators/rails_helpers/rails_controller.rb +34 -0
- data/lib/rspec_for_generators/rails_helpers/rails_helper.rb +36 -0
- data/lib/rspec_for_generators/rails_helpers/rails_mailer.rb +29 -0
- data/lib/rspec_for_generators/rails_helpers/rails_model.rb +94 -0
- data/lib/rspec_for_generators/rails_helpers/rails_view.rb +29 -0
- data/lib/rspec_for_generators/rails_spec_helper.rb +17 -0
- data/lib/rspec_for_generators/rspec_test_case.rb +26 -0
- data/rspec_for_generators.gemspec +96 -0
- data/spec/rspec_for_generators/rspec_for_generators_spec.rb +43 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/tmp/rails/config/routes.rb +2 -0
- data/wiki/Custom Rails 3 Generators.textile +110 -0
- metadata +195 -0
@@ -0,0 +1,49 @@
|
|
1
|
+
# This method tries to see if a specific method is contained in the generated file.
|
2
|
+
# It can operate (should) on either a file name or the raw content
|
3
|
+
#
|
4
|
+
# generated_file_name.should have_method "hello" # 'my/path/say_hello.rb'.should have_method "hello"
|
5
|
+
#
|
6
|
+
# say_hello_file_content.should have_method "hello"
|
7
|
+
#
|
8
|
+
module RSpec
|
9
|
+
module RubyContentMatchers
|
10
|
+
class HaveMethod
|
11
|
+
|
12
|
+
attr_reader :method
|
13
|
+
|
14
|
+
def initialize(method, type)
|
15
|
+
@type = type
|
16
|
+
@method = method
|
17
|
+
end
|
18
|
+
|
19
|
+
def matches?(content)
|
20
|
+
@content = content
|
21
|
+
case @type
|
22
|
+
when :class
|
23
|
+
@content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)/m
|
24
|
+
else
|
25
|
+
@content =~ /def\s+#{method}\s*(\(.+\))?(.*?)/m
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def failure_message
|
30
|
+
return "Expected there to be the class method #{method}, but there wasn't" if @type == :class
|
31
|
+
"Expected there to be the method #{method}, but there wasn't"
|
32
|
+
end
|
33
|
+
|
34
|
+
def negative_failure_message
|
35
|
+
return "Did not expect there to be the method #{method}, but there was" if @type == :class
|
36
|
+
"Did not expect there to be the method #{method}, but there was"
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
def have_method(method, type = nil)
|
42
|
+
HaveMethod.new(method, type)
|
43
|
+
end
|
44
|
+
alias_method :have_instance_method, :have_method
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RSpec
|
2
|
+
module RubyContentMatchers
|
3
|
+
class HaveModule
|
4
|
+
|
5
|
+
attr_reader :module_name
|
6
|
+
|
7
|
+
def initialize(content)
|
8
|
+
@content = content
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(module_name)
|
12
|
+
@module_name = module_name
|
13
|
+
@content =~ /module\s+#{@module_name}\s+(.*)end/m
|
14
|
+
if block_given?
|
15
|
+
ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
|
16
|
+
yield ruby_content
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message
|
21
|
+
"Expected there to be the module #{module_name}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def negative_failure_message
|
25
|
+
"Did no expected there to be the module #{module_name}"
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
def have_module(module_name)
|
31
|
+
HaveModule.new(module_name)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RSpec
|
2
|
+
module RubyContentMatchers
|
3
|
+
class IncludeModule
|
4
|
+
|
5
|
+
attr_reader :module_name
|
6
|
+
|
7
|
+
def initialize module_name
|
8
|
+
@module_name = module_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(content)
|
12
|
+
@content = content
|
13
|
+
match_res = (@content =~ /include\s+#{module_name}/)
|
14
|
+
if block_given? && match_res
|
15
|
+
ruby_content = content.extend(RSpec::RubyContent::Helpers)
|
16
|
+
yield ruby_content
|
17
|
+
else
|
18
|
+
match_res
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def failure_message
|
23
|
+
"Expected there to be an inclusion of module #{module_name}"
|
24
|
+
end
|
25
|
+
|
26
|
+
def negative_failure_message
|
27
|
+
"Did not expect there to be an inclusion of module #{module_name}"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def include_module(module_name)
|
33
|
+
IncludeModule.new(module_name)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module RSpec
|
2
|
+
module RubyContentMatchers
|
3
|
+
class InheritFrom
|
4
|
+
|
5
|
+
attr_reader :klass
|
6
|
+
|
7
|
+
def initialize(content)
|
8
|
+
@klass = klass
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(content)
|
12
|
+
@content = content
|
13
|
+
@content =~ /class\s+(.*?)<\s+#{klass}(.*)end/
|
14
|
+
if block_given?
|
15
|
+
ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
|
16
|
+
yield ruby_content
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message
|
21
|
+
"Expected the class to inherit from #{klass}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def negative_failure_message
|
25
|
+
"Did not expect the class to inherit from #{klass}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def inherit_from(klass)
|
30
|
+
InheritFrom.new(klass)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require_all File.dirname(__FILE__)
|
@@ -0,0 +1,73 @@
|
|
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
|
8
|
+
module FileMatchers
|
9
|
+
class GenerateFile
|
10
|
+
|
11
|
+
def initialize(relative_path, type = nil)
|
12
|
+
@relative_path = relative_rails_file relative_path, type
|
13
|
+
end
|
14
|
+
|
15
|
+
def matches?(generator, &block)
|
16
|
+
@expected = File.expand_path(@relative_path, generator.class.destination_root)
|
17
|
+
ex = File.exists?(@expected)
|
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 file #{@relative_path} to have been generated, but it was not"
|
29
|
+
end
|
30
|
+
|
31
|
+
def negative_failure_message
|
32
|
+
"Did not expected file #{@relative_path} to have been generated, but it was"
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def relative_rails_file path, type = nil
|
38
|
+
return File.join(::Rails.root, base_dir(type), folder(type), "#{path.to_s}.rb") if type
|
39
|
+
File.join(::Rails.root, path)
|
40
|
+
end
|
41
|
+
|
42
|
+
def folder type
|
43
|
+
case type
|
44
|
+
when :observer
|
45
|
+
'models'
|
46
|
+
else
|
47
|
+
type.to_s.pluralize
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def base_dir type
|
52
|
+
case type
|
53
|
+
when :model, :controller, :view, :helper, :observer
|
54
|
+
'app'
|
55
|
+
when :migration
|
56
|
+
'db'
|
57
|
+
when :javascript, :stylesheet
|
58
|
+
'public'
|
59
|
+
when :initializer, :locale
|
60
|
+
'config'
|
61
|
+
else
|
62
|
+
''
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def generate_file(relative, type = nil)
|
68
|
+
GenerateFile.new(relative, type)
|
69
|
+
end
|
70
|
+
alias_method :generate_directory, :generate_file
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
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
|
+
migration_file_name(relative, generator)
|
18
|
+
end
|
19
|
+
|
20
|
+
def failure_message
|
21
|
+
"Expected migration #{relative} to have been generated, but it was not"
|
22
|
+
end
|
23
|
+
|
24
|
+
def negative_failure_message
|
25
|
+
"Did not expect migration #{relative} to have been generated, but it was"
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def migration_file_name(relative, generator) #:nodoc:
|
31
|
+
absolute = File.expand_path(relative, generator.class.destination_root)
|
32
|
+
dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
|
33
|
+
Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def generate_migration(relative)
|
39
|
+
GenerateMigration.new(relative)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RSpec
|
2
|
+
module RubyContent
|
3
|
+
module Helpers
|
4
|
+
def check_matchings matchings
|
5
|
+
matchings.each do |matching|
|
6
|
+
self.should match /#{Regexp.escape(matching)}/
|
7
|
+
end
|
8
|
+
end
|
9
|
+
alias_method :matchings, :check_matchings
|
10
|
+
|
11
|
+
|
12
|
+
def check_methods methods, type=nil
|
13
|
+
methods.each do |method_name|
|
14
|
+
self.should have_method(method_name, type)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
alias_method :methods, :check_methods
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Generator
|
3
|
+
module Helpers
|
4
|
+
def check_file file, type=nil, &block
|
5
|
+
self.should generate_file(file, type, &block)
|
6
|
+
end
|
7
|
+
alias_method :file, :check_file
|
8
|
+
|
9
|
+
def check_view(file_name, matchings)
|
10
|
+
self.should generate_file(filename, :view) do |content|
|
11
|
+
check_matchings matchings
|
12
|
+
end
|
13
|
+
end
|
14
|
+
alias_method :view, :check_view
|
15
|
+
|
16
|
+
def check_model(name, clazz, options = {})
|
17
|
+
self.should generate_file("#{name.underscore}.rb", :model) do |file_content|
|
18
|
+
file_content.should have_class clazz.camelize do |content|
|
19
|
+
check_matchings options[:matchings]
|
20
|
+
check_methods(options[:methods])
|
21
|
+
check_class_methods(options[:class_methods])
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
alias_method :model, :check_model
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
require 'rspec_for_generators/rails_helpers/rails_app'
|
2
|
+
require 'rspec_for_generators/rails_helpers/rails_model'
|
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,55 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
module App
|
4
|
+
def self.debug!
|
5
|
+
self.extend(Debug)
|
6
|
+
end
|
7
|
+
|
8
|
+
module Debug
|
9
|
+
def rails_files
|
10
|
+
FileList["#{::Rails.root}/**/*.rb"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def rails_app_files
|
14
|
+
FileList["#{::Rails.root}/app/**/*.rb"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def rails_model_files
|
18
|
+
FileList["#{::Rails.root}/app/models/**/*.rb"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def rails_controller_files
|
22
|
+
FileList["#{::Rails.root}/app/controllers/**/*.rb"]
|
23
|
+
end
|
24
|
+
|
25
|
+
def rails_helper_files
|
26
|
+
FileList["#{::Rails.root}/app/helpers/**/*.rb"]
|
27
|
+
end
|
28
|
+
|
29
|
+
def rails_view_files
|
30
|
+
FileList["#{::Rails.root}/app/views/**/*.yml"]
|
31
|
+
end
|
32
|
+
|
33
|
+
def rails_initializer_files
|
34
|
+
FileList["#{::Rails.root}/config/initializers/**/*.rb"]
|
35
|
+
end
|
36
|
+
|
37
|
+
def rails_migration_files
|
38
|
+
FileList["#{::Rails.root}/db/migration/**/*.rb"]
|
39
|
+
end
|
40
|
+
|
41
|
+
def rails_locale_files
|
42
|
+
FileList["#{::Rails.root}/config/locales/**/*.yml"]
|
43
|
+
end
|
44
|
+
|
45
|
+
def rails_javascript_files
|
46
|
+
FileList["#{::Rails.root}/public/javascripts/**/*.yml"]
|
47
|
+
end
|
48
|
+
|
49
|
+
def rails_stylesheet_files
|
50
|
+
FileList["#{::Rails.root}/public/stylesheets/**/*.yml"]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
module Controller
|
4
|
+
def create_controller name
|
5
|
+
file = controller_file_name(name)
|
6
|
+
unless File.exist?(file)
|
7
|
+
FileUtils.mkdir_p File.dirname(file)
|
8
|
+
File.open(file, 'w') do |f|
|
9
|
+
f.puts controller_content
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def controller_content name
|
15
|
+
%Q{class #{name.camelize}Controller < ActionController::Base
|
16
|
+
end}
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove_controller name
|
20
|
+
file = controller_file_name(name)
|
21
|
+
FileUtils.rm_f(file) if File.exist?(file)
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_controllers *names
|
25
|
+
names.each{|name| remove_controller name }
|
26
|
+
end
|
27
|
+
|
28
|
+
def controller_file_name name
|
29
|
+
File.join(::Rails.root, "app/controllers/#{name}.rb")
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Rails
|
3
|
+
module Helper
|
4
|
+
def create_helper name
|
5
|
+
file = helper_file_name(name)
|
6
|
+
unless File.exist?(file)
|
7
|
+
FileUtils.mkdir_p File.dirname(file)
|
8
|
+
File.open(file, 'w') do |f|
|
9
|
+
f.puts helper_content
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def helper_content name
|
15
|
+
%Q{class #{name.camelize}Helper
|
16
|
+
end}
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove_model name
|
20
|
+
file = helper_file_name(name)
|
21
|
+
FileUtils.rm_f(file) if File.exist?(file)
|
22
|
+
end
|
23
|
+
|
24
|
+
def remove_helpers *names
|
25
|
+
names.each{|name| remove_helper name }
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def helper_file_name name
|
31
|
+
File.join(::Rails.root, "app/helpers/#{name}.rb")
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|