generator-spec 0.4.3 → 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +53 -37
- data/VERSION +1 -1
- data/lib/{rspec_for_generators.rb → generator_spec.rb} +1 -1
- data/lib/generator_spec/extensions/core_ext.rb +17 -0
- data/lib/{rspec_for_generators/rails_spec_helper.rb → generator_spec/helpers/configure_rails.rb} +0 -4
- data/lib/{rspec_for_generators → generator_spec/helpers}/migration_helper.rb +0 -0
- data/lib/generator_spec/main.rb +42 -0
- data/lib/generator_spec/matchers/content/content_matcher.rb +21 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_block.rb +17 -8
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_call.rb +6 -6
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_calls.rb +5 -5
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_class.rb +14 -6
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_method.rb +17 -8
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_module.rb +7 -6
- data/lib/generator_spec/matchers/content/have_region.rb +49 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/have_subclass.rb +19 -7
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/include_module.rb +5 -5
- data/lib/{rspec_for_generators → generator_spec}/matchers/content/inherit_from.rb +7 -3
- data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_directory.rb +1 -1
- data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_file.rb +6 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/file/generate_migration.rb +1 -1
- data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/content.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/file.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/helpers/migration.rb +1 -1
- data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_column.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_index.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_table.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_tbl_column.rb +0 -0
- data/lib/{rspec_for_generators → generator_spec}/matchers/migration/have_up_down.rb +0 -0
- data/lib/generator_spec/rails_helpers/rails_app.rb +159 -0
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_controller.rb +4 -2
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_helper.rb +4 -2
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_mailer.rb +4 -2
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_migration.rb +1 -1
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_model.rb +4 -2
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_observer.rb +4 -2
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_orm.rb +37 -17
- data/lib/{rspec_for_generators → generator_spec}/rails_helpers/rails_view.rb +3 -1
- data/lib/generator_spec/require_generator.rb +67 -0
- data/lib/generator_spec/rspec_generator/configure.rb +51 -0
- data/lib/generator_spec/rspec_generator/generator_util.rb +57 -0
- data/lib/generator_spec/rspec_generator/routes.rb +2 -0
- data/lib/generator_spec/rspec_generator/test_case.rb +25 -0
- data/spec/{rspec_for_generators → generator_spec}/controller_generator_spec.rb +0 -0
- data/spec/{rspec_for_generators → generator_spec}/helper_generator_spec.rb +10 -10
- data/spec/{rspec_for_generators → generator_spec}/migration_generator_spec.rb +0 -0
- data/spec/{rspec_for_generators → generator_spec}/model_generator_spec.rb +0 -0
- data/spec/{rspec_for_generators → generator_spec}/observer_generator_spec.rb +0 -0
- data/spec/{rspec_for_generators → generator_spec}/view_generator_spec.rb +0 -0
- data/spec/spec_helper.rb +12 -9
- metadata +55 -51
- data/generator-spec.gemspec +0 -128
- data/lib/rspec_for_generators/fixtures/routes.rb +0 -2
- data/lib/rspec_for_generators/generator_spec_helper.rb +0 -151
- data/lib/rspec_for_generators/main.rb +0 -19
- data/lib/rspec_for_generators/rails_helpers/rails_app.rb +0 -119
- data/lib/rspec_for_generators/rspec_test_case.rb +0 -26
@@ -1,6 +1,8 @@
|
|
1
1
|
module RSpec
|
2
2
|
module Rails
|
3
|
-
module Controller
|
3
|
+
module Controller
|
4
|
+
include RSpec::Rails::App
|
5
|
+
|
4
6
|
def create_controller name, content=nil
|
5
7
|
file = controller_file_name(name)
|
6
8
|
unless File.exist?(file)
|
@@ -28,7 +30,7 @@ module RSpec
|
|
28
30
|
end
|
29
31
|
|
30
32
|
def controller_file_name name
|
31
|
-
File.join(
|
33
|
+
File.join(controller_dir, "#{name}_controller.rb")
|
32
34
|
end
|
33
35
|
|
34
36
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec::Rails
|
2
|
-
module Helper
|
2
|
+
module Helper
|
3
|
+
include RSpec::Rails::App
|
4
|
+
|
3
5
|
def create_helper name, content=nil
|
4
6
|
file = helper_file_name(name)
|
5
7
|
unless File.exist?(file)
|
@@ -29,7 +31,7 @@ end}
|
|
29
31
|
protected
|
30
32
|
|
31
33
|
def helper_file_name name
|
32
|
-
File.join(
|
34
|
+
File.join(helper_dir, "#{name}_helper.rb")
|
33
35
|
end
|
34
36
|
|
35
37
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec::Rails
|
2
|
-
module Mailer
|
2
|
+
module Mailer
|
3
|
+
include RSpec::Rails::App
|
4
|
+
|
3
5
|
def create_mailer name, content=nil
|
4
6
|
file = mailer_file_name(name)
|
5
7
|
unless File.exist?(file)
|
@@ -22,7 +24,7 @@ module RSpec::Rails
|
|
22
24
|
end
|
23
25
|
|
24
26
|
def mailer_file_name name
|
25
|
-
File.join(
|
27
|
+
File.join(mailer_dir, "#{name}.rb")
|
26
28
|
end
|
27
29
|
|
28
30
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec::Rails
|
2
|
-
module Model
|
2
|
+
module Model
|
3
|
+
include RSpec::Rails::App
|
4
|
+
|
3
5
|
def create_model name, type=nil, &block
|
4
6
|
file = model_file_name(name)
|
5
7
|
unless File.exist?(file)
|
@@ -21,7 +23,7 @@ module RSpec::Rails
|
|
21
23
|
end
|
22
24
|
|
23
25
|
def model_file_name name
|
24
|
-
File.join(
|
26
|
+
File.join(model_dir, "#{name}.rb")
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec::Rails
|
2
|
-
module Observer
|
2
|
+
module Observer
|
3
|
+
include RSpec::Rails::App
|
4
|
+
|
3
5
|
def create_observer name, content=nil
|
4
6
|
file = observer_file_name(name)
|
5
7
|
unless File.exist?(file)
|
@@ -27,7 +29,7 @@ end}
|
|
27
29
|
end
|
28
30
|
|
29
31
|
def observer_file_name name
|
30
|
-
File.join(
|
32
|
+
File.join(observer_dir, "#{name}_observer.rb")
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'generator_spec/rails_helpers/rails_model'
|
2
2
|
|
3
3
|
module RSpec::Rails
|
4
4
|
module Orm
|
@@ -7,6 +7,10 @@ module RSpec::Rails
|
|
7
7
|
|
8
8
|
protected
|
9
9
|
|
10
|
+
def document_name type
|
11
|
+
type_content = type == :document ? "#{orm_name}::Document" : "#{orm_name}::EmbeddedDocument"
|
12
|
+
end
|
13
|
+
|
10
14
|
def clazz name
|
11
15
|
"class #{name.to_s.camelize}"
|
12
16
|
end
|
@@ -24,13 +28,18 @@ module RSpec::Rails
|
|
24
28
|
def file_w_inherit name
|
25
29
|
%Q{#{clazz name} < #{yield if block_given?}
|
26
30
|
end}
|
27
|
-
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def field name, type = nil
|
34
|
+
return "#{field_name} :#{name}, #{type}" if type
|
35
|
+
"#{field_name} :#{name}"
|
36
|
+
end
|
28
37
|
end
|
29
38
|
|
30
39
|
module ActiveRecord
|
31
40
|
include RSpec::Rails::Orm::Base
|
32
41
|
|
33
|
-
def file_content
|
42
|
+
def file_content name, type=nil
|
34
43
|
file_w_inherit(name) {'ActiveRecord::Base'}
|
35
44
|
end
|
36
45
|
end
|
@@ -38,41 +47,52 @@ module RSpec::Rails
|
|
38
47
|
module MongoMapper
|
39
48
|
include RSpec::Rails::Orm::Base
|
40
49
|
|
41
|
-
def
|
42
|
-
|
43
|
-
|
50
|
+
def orm_name
|
51
|
+
'MongoMapper'
|
52
|
+
end
|
53
|
+
def file_content name, type = :document
|
54
|
+
file_w_include(name) { document_name type }
|
44
55
|
end
|
45
56
|
|
46
|
-
def
|
47
|
-
|
48
|
-
"key :#{name}"
|
57
|
+
def field_name
|
58
|
+
'key'
|
49
59
|
end
|
50
60
|
end
|
51
61
|
|
52
62
|
module Mongoid
|
53
63
|
include RSpec::Rails::Orm::Base
|
54
64
|
|
65
|
+
def orm_name
|
66
|
+
'Mongoid'
|
67
|
+
end
|
68
|
+
|
69
|
+
def field_name
|
70
|
+
'field'
|
71
|
+
end
|
72
|
+
|
55
73
|
def file_content name, type = :document
|
56
|
-
|
57
|
-
file_w_include(name) { type_content }
|
74
|
+
file_w_include(name) { document_name type }
|
58
75
|
end
|
59
76
|
|
60
77
|
def field name, type = nil
|
61
|
-
return "
|
62
|
-
"
|
78
|
+
return "#{field_name} :#{name}, :type => #{type}" if type
|
79
|
+
"#{field_name} :#{name}"
|
63
80
|
end
|
64
81
|
end
|
65
82
|
|
66
83
|
module DataMapper
|
67
84
|
include RSpec::Rails::Orm::Base
|
68
85
|
|
86
|
+
def orm_name
|
87
|
+
'DataMapper'
|
88
|
+
end
|
89
|
+
|
69
90
|
def file_content name
|
70
|
-
file_w_include(name) {
|
91
|
+
file_w_include(name) { "#{orm_name}::Resource" }
|
71
92
|
end
|
72
93
|
|
73
|
-
def
|
74
|
-
|
75
|
-
"property :#{name}"
|
94
|
+
def field_name
|
95
|
+
'property'
|
76
96
|
end
|
77
97
|
end
|
78
98
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module RSpec::Rails
|
2
2
|
module View
|
3
|
+
include RSpec::Rails::App
|
4
|
+
|
3
5
|
def create_view name, action, type, content=nil
|
4
6
|
file = view_file_name(name, action, type)
|
5
7
|
unless File.exist?(file)
|
@@ -18,7 +20,7 @@ module RSpec::Rails
|
|
18
20
|
end
|
19
21
|
|
20
22
|
def view_file_name name, action, type
|
21
|
-
File.join(
|
23
|
+
File.join(view_dir, name.to_s, "#{action}.#{type}")
|
22
24
|
end
|
23
25
|
|
24
26
|
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# require the generators
|
2
|
+
def require_generators *generator_list
|
3
|
+
req = RSpec::Generator::Require
|
4
|
+
generator_list.each do |name, generators|
|
5
|
+
case name
|
6
|
+
when Hash
|
7
|
+
req.parse_generator_hash name
|
8
|
+
when Symbol
|
9
|
+
req.require_all_generators and return if name == :all
|
10
|
+
if !generators || generators.empty?
|
11
|
+
req.require_generator! name
|
12
|
+
else
|
13
|
+
req.parse_generator_list name, generators
|
14
|
+
end
|
15
|
+
else
|
16
|
+
raise ArgumentError, "required generator(s) must be stated either using a symbol or array of symbols"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
alias :require_generator :require_generators
|
21
|
+
|
22
|
+
module RSpec::Generator
|
23
|
+
module Require
|
24
|
+
class << self
|
25
|
+
attr_accessor :lib
|
26
|
+
|
27
|
+
def debug?
|
28
|
+
RSpec::Generator.debug?
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_generator_hash hash
|
32
|
+
parse_generator_list hash.keys.first, hash.values.flatten
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_generator_list name, generators
|
36
|
+
if generators.empty?
|
37
|
+
require_generator! name
|
38
|
+
else
|
39
|
+
generators.each do |generator_name|
|
40
|
+
require_generator! name, generator_name
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def require_all_generators name=nil
|
46
|
+
raise Error, "lib must be set to lib directory for RSpec::Generator::Require" if !lib
|
47
|
+
base_path = File.join(lib, 'generators')
|
48
|
+
path = name ? File.join(base_path, name) : base_path
|
49
|
+
path = File.expand_path path
|
50
|
+
puts "require all generators from: #{path}" if debug?
|
51
|
+
require_all path
|
52
|
+
end
|
53
|
+
|
54
|
+
def require_generator! name, generator=nil
|
55
|
+
require_all_generators(name) and return if generator == :all
|
56
|
+
|
57
|
+
file = (require_file_name name, generator)
|
58
|
+
puts "Loading generator: #{file}" if debug?
|
59
|
+
require file
|
60
|
+
end
|
61
|
+
|
62
|
+
def require_file_name name, name_space=nil
|
63
|
+
File.join('generators', name.to_s, "#{name_space}", "#{name_space || name}_generator")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RSpec::Generator
|
2
|
+
def self.configure &block
|
3
|
+
conf = RSpec::Generator::Configure
|
4
|
+
if block
|
5
|
+
block.arity < 1 ? conf.instance_eval(&block) : block.call(conf, self)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Configure
|
10
|
+
class << self
|
11
|
+
def remove_temp_dir= bool
|
12
|
+
RSpec::Generator.remove_temp_dir = bool
|
13
|
+
end
|
14
|
+
|
15
|
+
def debug= bool
|
16
|
+
RSpec::Generator.debug = bool
|
17
|
+
end
|
18
|
+
|
19
|
+
def default_rails_root path, options = {}
|
20
|
+
configure_root_dir path, options
|
21
|
+
end
|
22
|
+
|
23
|
+
def rails_root= path, options = {}
|
24
|
+
configure_root_dir path, :custom
|
25
|
+
end
|
26
|
+
|
27
|
+
def lib= path
|
28
|
+
RSpec::Generator::Require.lib = path
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def configure_root_dir path, options = {}
|
34
|
+
::Rails.application.configure do
|
35
|
+
config.root_dir = options == :custom ? TmpRails.root_dir(File.dirname(path) + '/../tmp', :custom) : TmpRails.root_dir(path)
|
36
|
+
end
|
37
|
+
|
38
|
+
gen = RSpec::Generator
|
39
|
+
|
40
|
+
::RSpec.configure do |config|
|
41
|
+
config.after(:suite) do
|
42
|
+
gen.remove_rails_dir! if gen.remove_temp_dir
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
::RSpec::Generator::TestCase.destination ::Rails.root
|
47
|
+
::Rails::Generators.configure!
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end # Configure
|
51
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rails/all'
|
2
|
+
require 'rails/generators'
|
3
|
+
require 'rails/generators/test_case'
|
4
|
+
require 'generator_spec/rspec_generator/configure'
|
5
|
+
|
6
|
+
module RSpec::Generator
|
7
|
+
class << self
|
8
|
+
attr_accessor :generator, :test_method_name, :remove_temp_dir, :debug
|
9
|
+
|
10
|
+
def debug?
|
11
|
+
debug
|
12
|
+
end
|
13
|
+
|
14
|
+
def remove_rails_dir!
|
15
|
+
FileUtils.rm_rf ::TmpRails.root
|
16
|
+
end
|
17
|
+
|
18
|
+
def run_generator *args, &block
|
19
|
+
generator.run_generator *args
|
20
|
+
if block
|
21
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_generator &block
|
26
|
+
with(get_generator, &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def setup_generator test_method_name=nil, &block
|
30
|
+
clean! if test_method_name
|
31
|
+
generator = get_generator(test_method_name).extend(RSpec::Generator::Helpers)
|
32
|
+
if block
|
33
|
+
block.arity < 1 ? generator.class.instance_eval(&block) : block.call(generator.class)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
def with(generator, &block)
|
40
|
+
if block
|
41
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self, generator.class)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def clean!
|
46
|
+
if generator
|
47
|
+
generator.class.generator_class = nil
|
48
|
+
end
|
49
|
+
@generator = nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_generator test_method_name=nil
|
53
|
+
@generator ||= RSpec::Generator::TestCase.new(test_method_name + '_spec')
|
54
|
+
end
|
55
|
+
|
56
|
+
end # class self
|
57
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
module RSpec::Generator
|
4
|
+
class TestCase < ::Rails::Generators::TestCase
|
5
|
+
setup :prepare_destination
|
6
|
+
|
7
|
+
def initialize(test_method_name)
|
8
|
+
@method_name = test_method_name
|
9
|
+
@test_passed = true
|
10
|
+
@interrupted = false
|
11
|
+
routes_file = File.join(File.dirname(__FILE__), 'routes.rb')
|
12
|
+
copy_routes routes_file
|
13
|
+
end
|
14
|
+
|
15
|
+
def copy_routes routes_file
|
16
|
+
routes = File.expand_path(routes_file)
|
17
|
+
raise ArgumentError, "No routes file exists at #{routes_file}" if !File.exist?(routes)
|
18
|
+
destination = File.join(::Rails.root, "config")
|
19
|
+
FileUtils.mkdir_p(destination) # create dir
|
20
|
+
FileUtils.cp routes, destination # copy
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
File without changes
|
@@ -1,8 +1,10 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
2
|
|
3
|
-
RSpec::Generator.
|
3
|
+
RSpec::Generator.configure do |config|
|
4
|
+
config.debug = true
|
5
|
+
end
|
4
6
|
|
5
|
-
|
7
|
+
require_generators :all
|
6
8
|
|
7
9
|
module RSpec::Core
|
8
10
|
class ExampleGroup
|
@@ -15,30 +17,28 @@ describe 'helper_generator' do
|
|
15
17
|
setup_generator 'helper_generator' do
|
16
18
|
tests HelperGenerator
|
17
19
|
end
|
18
|
-
remove_helper
|
20
|
+
remove_helper :account
|
19
21
|
end
|
20
22
|
|
21
23
|
after :each do
|
22
|
-
remove_helper
|
24
|
+
remove_helper :account
|
23
25
|
end
|
24
26
|
|
25
27
|
it "should not work without an Account helper file" do
|
26
|
-
with_generator do |g|
|
27
|
-
|
28
|
-
g.
|
29
|
-
g.should_not generate_helper name
|
28
|
+
with_generator do |g|
|
29
|
+
g.run_generator :account.args
|
30
|
+
g.should_not generate_helper :account
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
34
|
it "should decorate an existing Account helper file with a 'help_me' method" do
|
34
35
|
with_generator do |g|
|
35
|
-
name = :account
|
36
36
|
create_helper :account do
|
37
37
|
'# helper content'
|
38
38
|
end
|
39
39
|
g.run_generator [:account].args
|
40
40
|
g.should generate_helper :account do |content|
|
41
|
-
content.should have_helper_class
|
41
|
+
content.should have_helper_class :account do |klass|
|
42
42
|
klass.should have_method :help_me
|
43
43
|
end
|
44
44
|
end
|