generator-spec 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +144 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/generator-spec.gemspec +106 -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 +95 -0
- data/lib/rspec_for_generators/main.rb +18 -0
- data/lib/rspec_for_generators/matchers/content/have_block.rb +47 -0
- data/lib/rspec_for_generators/matchers/content/have_call.rb +42 -0
- data/lib/rspec_for_generators/matchers/content/have_class.rb +32 -0
- data/lib/rspec_for_generators/matchers/content/have_method.rb +41 -0
- data/lib/rspec_for_generators/matchers/content/have_module.rb +32 -0
- data/lib/rspec_for_generators/matchers/content/include_module.rb +32 -0
- data/lib/rspec_for_generators/matchers/content/inherit_from.rb +31 -0
- data/lib/rspec_for_generators/matchers/file/generate_directory.rb +71 -0
- data/lib/rspec_for_generators/matchers/file/generate_file.rb +71 -0
- data/lib/rspec_for_generators/matchers/file/generate_migration.rb +50 -0
- data/lib/rspec_for_generators/matchers/helpers/content.rb +18 -0
- data/lib/rspec_for_generators/matchers/helpers/file.rb +26 -0
- data/lib/rspec_for_generators/matchers/helpers/migration.rb +102 -0
- data/lib/rspec_for_generators/matchers/migration/have_column.rb +13 -0
- data/lib/rspec_for_generators/matchers/migration/have_index.rb +9 -0
- data/lib/rspec_for_generators/matchers/migration/have_migration.rb +5 -0
- data/lib/rspec_for_generators/matchers/migration/have_table.rb +18 -0
- data/lib/rspec_for_generators/matchers/migration/have_tbl_column.rb +25 -0
- data/lib/rspec_for_generators/matchers/migration/have_up_down.rb +13 -0
- data/lib/rspec_for_generators/rails_helpers/all.rb +6 -0
- data/lib/rspec_for_generators/rails_helpers/rails_app.rb +53 -0
- data/lib/rspec_for_generators/rails_helpers/rails_controller.rb +36 -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 +27 -0
- data/lib/rspec_for_generators/rails_helpers/rails_orm.rb +77 -0
- data/lib/rspec_for_generators/rails_helpers/rails_view.rb +29 -0
- data/lib/rspec_for_generators/rails_spec_helper.rb +35 -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 +41 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +6 -0
- data/wiki/Custom Rails 3 Generators.textile +110 -0
- data/wiki/rails_view_helpers.textile +167 -0
- metadata +205 -0
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rake'
|
3
|
+
require 'rails'
|
4
|
+
require 'require_all'
|
5
|
+
require 'rails/generators'
|
6
|
+
require 'rspec_for_generators/main'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.include(RSpec::FileMatchers)
|
10
|
+
config.include(RSpec::RubyContentMatchers)
|
11
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'rails/all'
|
3
|
+
require 'rails/generators'
|
4
|
+
require 'rails/generators/test_case'
|
5
|
+
|
6
|
+
require 'rspec_for_generators/rails_spec_helper'
|
7
|
+
|
8
|
+
# Call configure to load the settings from
|
9
|
+
# Rails.application.config.generators to Rails::Generators
|
10
|
+
|
11
|
+
# require the generators
|
12
|
+
def require_generators *generator_list
|
13
|
+
generator_list.each do |name, generators|
|
14
|
+
if !generators || generators.empty
|
15
|
+
require File.join('generators', name.to_s, "#{name.to_s}_generator")
|
16
|
+
else
|
17
|
+
generators.each do |generator_name|
|
18
|
+
require File.join('generators', name.to_s, generator_name.to_s, "#{generator_name}_generator")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
alias :require_generator :require_generators
|
24
|
+
|
25
|
+
module RSpec
|
26
|
+
module Generator
|
27
|
+
class << self
|
28
|
+
attr_accessor :generator, :test_method_name
|
29
|
+
|
30
|
+
def remove_rails_dir!
|
31
|
+
FileUtils.rm_rf ::TmpRails.root
|
32
|
+
end
|
33
|
+
|
34
|
+
def configure_root_dir path, options = {}
|
35
|
+
::Rails.application.configure do
|
36
|
+
config.root_dir = options == :custom ? TmpRails.root_dir(File.dirname(path) + '/../tmp', :custom) : TmpRails.root_dir(path)
|
37
|
+
end
|
38
|
+
|
39
|
+
::RSpec::Generators::TestCase.destination ::Rails.root
|
40
|
+
::Rails::Generators.configure!
|
41
|
+
|
42
|
+
::RSpec.configure do |config|
|
43
|
+
config.after(:suite) do
|
44
|
+
::RSpec::Generator.remove_rails_dir!
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def run_generator *args, &block
|
50
|
+
generator.run_generator *args
|
51
|
+
if block
|
52
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def check(&block)
|
57
|
+
if block
|
58
|
+
block.arity < 1 ? self.instance_eval(&block) : block.call(self)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def with(generator, &block)
|
63
|
+
if block
|
64
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self, generator.class)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def with_generator &block
|
69
|
+
with(get_generator, &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def setup_generator test_method_name=nil, &block
|
73
|
+
clean! if test_method_name
|
74
|
+
generator = get_generator(test_method_name).extend(RSpec::Generator::Helpers)
|
75
|
+
if block
|
76
|
+
block.arity < 1 ? generator.class.instance_eval(&block) : block.call(generator.class)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
protected
|
81
|
+
|
82
|
+
def clean!
|
83
|
+
if generator
|
84
|
+
generator.class.generator_class = nil
|
85
|
+
end
|
86
|
+
@generator = nil
|
87
|
+
end
|
88
|
+
|
89
|
+
def get_generator test_method_name=nil
|
90
|
+
@generator ||= RSpec::Generators::TestCase.new(test_method_name + '_spec')
|
91
|
+
end
|
92
|
+
|
93
|
+
end # class self
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rspec_for_generators/rails_spec_helper'
|
2
|
+
require 'rspec_for_generators/rspec_test_case'
|
3
|
+
require 'rspec_for_generators/generator_spec_helper'
|
4
|
+
|
5
|
+
module RSpec
|
6
|
+
module RubyContentMatchers
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module RSpec
|
11
|
+
module FileMatchers
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
require_all File.dirname(__FILE__) + '/matchers/helpers'
|
16
|
+
require_all File.dirname(__FILE__) + '/matchers/content'
|
17
|
+
require_all File.dirname(__FILE__) + '/matchers/file'
|
18
|
+
require_all File.dirname(__FILE__) + '/matchers/migration'
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class HaveBlock
|
3
|
+
attr_reader :name, :args, :block_args
|
4
|
+
|
5
|
+
def initialize(name, options={})
|
6
|
+
@name = name
|
7
|
+
@args = options[:args]
|
8
|
+
@block_args = options[:block_args]
|
9
|
+
end
|
10
|
+
|
11
|
+
def matches?(content)
|
12
|
+
match_res = match(content)
|
13
|
+
if block_given? && $1
|
14
|
+
ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
|
15
|
+
yield ruby_content
|
16
|
+
else
|
17
|
+
match_res
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure_message
|
22
|
+
return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
|
23
|
+
end
|
24
|
+
|
25
|
+
def negative_failure_message
|
26
|
+
return "Did not expect there to be a block #{name} with arguments #{args}, but there was"
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def match content
|
32
|
+
content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m
|
33
|
+
end
|
34
|
+
|
35
|
+
def args_expr
|
36
|
+
args ? "\(?\s*#{args}\s*\)?\s+" : ''
|
37
|
+
end
|
38
|
+
|
39
|
+
def block_args_expr
|
40
|
+
args ? "\|\s*#{block_args}\s*\|" : ''
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def have_block(name, options={})
|
45
|
+
HaveBlock.new(name, options)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,42 @@
|
|
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::RubyContentMatchers
|
9
|
+
class HaveCall
|
10
|
+
attr_reader :method, :args
|
11
|
+
|
12
|
+
def initialize(method, args = nil)
|
13
|
+
@method = method.to_s
|
14
|
+
@args = args
|
15
|
+
end
|
16
|
+
|
17
|
+
def matches?(content)
|
18
|
+
content =~ /#{method}\s+#{args_expr}/m
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure_message
|
22
|
+
return "Expected there to be the call to #{method} with args #{args}, but there wasn't" if args
|
23
|
+
"Expected there to be the call to #{method}, but there wasn't"
|
24
|
+
end
|
25
|
+
|
26
|
+
def negative_failure_message
|
27
|
+
return "Did not expect there to be the call to #{method} with args #{args}, but there was" if args
|
28
|
+
"Did not expect there to be the call to #{method}, but there was"
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def args_expr
|
34
|
+
args ? "\(?\s*#{args}\s*\)?\s+" : ''
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
def have_call(method, args = nil)
|
40
|
+
HaveCall.new(method, args)
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class HaveClass
|
3
|
+
attr_reader :klass
|
4
|
+
|
5
|
+
def initialize(klass)
|
6
|
+
@klass = klass.to_s.camelize
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(content)
|
10
|
+
match_res = (content =~ /class\s+#{klass}\s+(.*)end/m)
|
11
|
+
if block_given? && $1
|
12
|
+
ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
|
13
|
+
yield ruby_content
|
14
|
+
else
|
15
|
+
match_res
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message
|
20
|
+
"Expected there to be the class #{klass}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"Did no expected there to be the class #{klass}"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def have_class(klass)
|
30
|
+
HaveClass.new(klass)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
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::RubyContentMatchers
|
9
|
+
class HaveMethod
|
10
|
+
attr_reader :method
|
11
|
+
|
12
|
+
def initialize(method, type)
|
13
|
+
@type = type
|
14
|
+
@method = method.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def matches?(content)
|
18
|
+
case @type
|
19
|
+
when :class
|
20
|
+
content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)/m
|
21
|
+
else
|
22
|
+
content =~ /def\s+#{method}\s*(\(.+\))?(.*?)/m
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def failure_message
|
27
|
+
return "Expected there to be the class method #{method}, but there wasn't" if @type == :class
|
28
|
+
"Expected there to be the method #{method}, but there wasn't"
|
29
|
+
end
|
30
|
+
|
31
|
+
def negative_failure_message
|
32
|
+
return "Did not expect there to be the method #{method}, but there was" if @type == :class
|
33
|
+
"Did not expect there to be the method #{method}, but there was"
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def have_method(method, type = nil)
|
39
|
+
HaveMethod.new(method, type)
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class HaveModule
|
3
|
+
attr_reader :module_name
|
4
|
+
|
5
|
+
def initialize(module_name)
|
6
|
+
@module_name = module_name.to_s.camelize
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(content)
|
10
|
+
match_res =~ /module\s+#{module_name}\s+(.*)end/m
|
11
|
+
if block_given? && match_res
|
12
|
+
ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
|
13
|
+
yield ruby_content
|
14
|
+
else
|
15
|
+
match_res
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message
|
20
|
+
"Expected there to be the module #{module_name}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"Did no expected there to be the module #{module_name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def have_module(module_name)
|
30
|
+
HaveModule.new(module_name)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class IncludeModule
|
3
|
+
attr_reader :module_name
|
4
|
+
|
5
|
+
def initialize module_name
|
6
|
+
@module_name = module_name.to_s.camelize
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(content)
|
10
|
+
match_res = (content =~ /include\s+#{module_name}/)
|
11
|
+
if block_given? && match_res
|
12
|
+
ruby_content = content.extend(RSpec::RubyContent::Helpers)
|
13
|
+
yield ruby_content
|
14
|
+
else
|
15
|
+
match_res
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message
|
20
|
+
"Expected there to be an inclusion of module #{module_name}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"Did not expect there to be an inclusion of module #{module_name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def include_module(module_name)
|
30
|
+
IncludeModule.new(module_name)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RSpec::RubyContentMatchers
|
2
|
+
class InheritFrom
|
3
|
+
attr_reader :klass
|
4
|
+
|
5
|
+
def initialize klass
|
6
|
+
@klass = klass.to_s.camelize
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(content)
|
10
|
+
match_res =~ /class\s+(.*?)<\s+#{klass}(.*)end/
|
11
|
+
if block_given? && match_res
|
12
|
+
ruby_content = $2.strip.extend(RSpec::RubyContent::Helpers)
|
13
|
+
yield ruby_content
|
14
|
+
else
|
15
|
+
match_res
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def failure_message
|
20
|
+
"Expected the class to inherit from #{klass}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_failure_message
|
24
|
+
"Did not expect the class to inherit from #{klass}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def inherit_from(klass)
|
29
|
+
InheritFrom.new(klass)
|
30
|
+
end
|
31
|
+
end
|
@@ -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 GenerateDir
|
9
|
+
|
10
|
+
def initialize(relative_path, type = nil)
|
11
|
+
@relative_path = relative_rails_dir 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_dir path, type = nil
|
37
|
+
return File.join(::Rails.root, base_dir(type), folder(type), "#{path.to_s}") 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_dir(relative, type = nil)
|
67
|
+
GenerateDir.new(relative, type)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|