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
data/README.markdown
CHANGED
@@ -2,22 +2,24 @@
|
|
2
2
|
|
3
3
|
This project contains RSpec 2 matchers, helpers and various utilities to assist in writing Rails 3 generator specs.
|
4
4
|
|
5
|
-
Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators
|
6
|
-
of TestCase and also added some extra goodies to the mix. It should now be pretty easy to test your Rails 3 generators with RSpec 2 :)
|
5
|
+
Rails 3 has a Rails::Generators::TestCase class for use with Test-Unit, to help test generators. This TestCase contains specific custom assertion methods that can be used to assert generator behavior. To create an RSpec 2 equivalent, I wrapped Rails::Generators::TestCase for use with RSpec 2 and created some RSpec 2 matchers that mimic the assertion methods of the Test-Unit TestCase. I have also a bunch of "extra goodies" to the mix. The new intuitive DSL RSpec DSL for specifying/testing generators should make it very easy and intuitive to test your Rails 3 generators with RSpec 2 :)
|
7
6
|
|
8
|
-
Please advice if you find any issues or have suggestions for improvements. The code is not as pretty as it could be, so feel free to refactor and improve
|
7
|
+
Please advice if you find any issues or have suggestions for improvements. The code is not as pretty as it could be, so feel free to refactor and improve this gem to your hearts desire!
|
9
8
|
|
10
9
|
## Install
|
11
10
|
|
12
|
-
<code>gem install
|
11
|
+
<code>gem install generator-spec</code>
|
13
12
|
|
14
|
-
|
13
|
+
### Install from code
|
14
|
+
|
15
|
+
The gem is a jewel based on [jeweler](http://github.com/technicalpickles/jeweler).
|
16
|
+
To install the gem from the code, simply use the jeweler rake task:
|
15
17
|
|
16
18
|
<code>rake install</code>
|
17
19
|
|
18
20
|
## Usage
|
19
21
|
|
20
|
-
The following demonstrates
|
22
|
+
The following demonstrates usage of this gem. There are many more options and DSL convenience methods.
|
21
23
|
|
22
24
|
### Configuration
|
23
25
|
|
@@ -26,32 +28,59 @@ First setup the *spec_helper.rb*. Here is an example configuration.
|
|
26
28
|
<pre># spec/spec_helper.rb
|
27
29
|
|
28
30
|
require 'rspec'
|
29
|
-
require '
|
31
|
+
require 'generators_spec'
|
30
32
|
|
31
33
|
# configure it like this to use default settings
|
32
|
-
RSpec::Generator.
|
34
|
+
RSpec::Generator.configure do |config|
|
35
|
+
config.debug = false
|
36
|
+
config.remove_temp_dir = true
|
37
|
+
config.default_rails_root(__FILE__)
|
38
|
+
end
|
33
39
|
|
34
40
|
# or customize the location of the temporary Rails 3 app dir used
|
35
|
-
RSpec::Generator.
|
41
|
+
RSpec::Generator.configure do |config|
|
42
|
+
config.debug = false
|
43
|
+
config.remove_temp_dir = true
|
44
|
+
config.rails_root = '~/my/rails/folder'
|
45
|
+
end
|
46
|
+
|
36
47
|
</pre>
|
37
48
|
|
38
49
|
### Specs for generators
|
39
50
|
|
40
|
-
I recommend having a separate spec for each generator
|
41
|
-
|
51
|
+
I recommend having a separate spec for each generator called a 'generator spec'.
|
52
|
+
You can use the special *require_generator* statement to ensure that one or more generators are loaded and made available for the spec.
|
42
53
|
|
43
54
|
<pre>require_generator :canable</pre>
|
44
55
|
|
45
56
|
This will load the generator : `generators/canabale_generator.rb`
|
46
57
|
|
47
|
-
If the generator is
|
58
|
+
If the generator is in a namespace, use a nested approach like this:
|
48
59
|
|
49
60
|
<pre>require_generators :canable => ['model', 'user']</pre>
|
50
61
|
|
51
62
|
This will load the generators: `generators/canable/model_generator.rb` and `generators/canable/user_generator.rb`
|
52
63
|
|
53
|
-
|
54
|
-
|
64
|
+
You can also load generators from multiple namespaces and mix and match like this
|
65
|
+
|
66
|
+
<pre>require_generators [:canable => ['model', 'user'], :other => :side, :simple]</pre>
|
67
|
+
|
68
|
+
### Auto-require all generators
|
69
|
+
|
70
|
+
You can also require all generators or all within a specific namespace like this:
|
71
|
+
|
72
|
+
require_generators :all
|
73
|
+
require_generators :canable => :all
|
74
|
+
|
75
|
+
## Using a shared generator loader file
|
76
|
+
|
77
|
+
If you have multiple generators in a shared namespace, the following alternative approach can be used.
|
78
|
+
Create a file that loads all the generators you are specifying in this folder, fx all generators in a specific namespace.
|
79
|
+
Then include this 'generator load file' in each spec.
|
80
|
+
|
81
|
+
This uses the 'require_all' gem functionality
|
82
|
+
|
83
|
+
## Example usage: require generators
|
55
84
|
|
56
85
|
<pre># spec/generators/canable.rb
|
57
86
|
require_generators :canable => ['model', 'user']</pre>
|
@@ -76,9 +105,7 @@ describe 'user_generator' do
|
|
76
105
|
end
|
77
106
|
</pre>
|
78
107
|
|
79
|
-
|
80
|
-
|
81
|
-
### Example of full generator spec
|
108
|
+
### Example: full generator spec
|
82
109
|
|
83
110
|
<pre># spec/generators/model_generator_spec.rb
|
84
111
|
|
@@ -99,29 +126,27 @@ describe 'model_generator' do
|
|
99
126
|
tests Canable::Generators::ModelGenerator
|
100
127
|
end
|
101
128
|
# ensure clean state before run
|
102
|
-
remove_model
|
129
|
+
remove_model :account
|
103
130
|
end
|
104
131
|
|
105
132
|
after :each do
|
106
|
-
# ensure clean state after run
|
107
|
-
remove_model
|
133
|
+
# ensure clean state after each run
|
134
|
+
remove_model :account
|
108
135
|
end
|
109
136
|
|
110
137
|
it "should not work without an existing Account model file" do
|
111
138
|
with_generator do |g|
|
112
|
-
|
113
|
-
g.
|
114
|
-
g.should_not generate_file name, :model
|
139
|
+
g.run_generator :account.args
|
140
|
+
g.should_not generate_file :account, :model
|
115
141
|
end
|
116
142
|
end
|
117
143
|
|
118
144
|
it "should decorate an existing Account model file with 'include Canable:Ables'" do
|
119
145
|
with_generator do |g|
|
120
|
-
|
121
|
-
|
122
|
-
g.
|
123
|
-
|
124
|
-
content.should have_class name do |klass|
|
146
|
+
create_model :account
|
147
|
+
g.run_generator 'account'.args
|
148
|
+
g.should generate_model :account do |content|
|
149
|
+
content.should have_class :account do |klass|
|
125
150
|
klass.should include_module 'Canable::Ables'
|
126
151
|
end
|
127
152
|
end
|
@@ -129,15 +154,6 @@ describe 'model_generator' do
|
|
129
154
|
end
|
130
155
|
end</pre>
|
131
156
|
|
132
|
-
## TODO
|
133
|
-
|
134
|
-
Need to create specs to test matchers/helpers for generated:
|
135
|
-
* migration
|
136
|
-
* controller
|
137
|
-
* helper
|
138
|
-
* view
|
139
|
-
* ...
|
140
|
-
|
141
157
|
## Note on Patches/Pull Requests
|
142
158
|
|
143
159
|
* Fork the project.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.4
|
data/lib/{rspec_for_generators/rails_spec_helper.rb → generator_spec/helpers/configure_rails.rb}
RENAMED
@@ -1,5 +1,3 @@
|
|
1
|
-
require_all File.dirname(__FILE__) + '/rails_helpers'
|
2
|
-
|
3
1
|
module TmpRails
|
4
2
|
class << self
|
5
3
|
attr_accessor :root
|
@@ -22,8 +20,6 @@ end
|
|
22
20
|
|
23
21
|
module Rails
|
24
22
|
def self.root
|
25
|
-
# raise StandardError, "You must define a location for Rails.config_root_dir" if !Rails.config_root_dir
|
26
|
-
# @root ||= File.expand_path(File.join(Rails.config_root_dir, '..', 'tmp', 'rails'))
|
27
23
|
@root ||= File.join(Rails.application.config.root_dir, 'rails')
|
28
24
|
end
|
29
25
|
end
|
File without changes
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Generator
|
3
|
+
end
|
4
|
+
end
|
5
|
+
|
6
|
+
module RSpec
|
7
|
+
module RubyContentMatchers
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module RSpec
|
12
|
+
module FileMatchers
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
require_all File.dirname(__FILE__) + '/extensions'
|
17
|
+
require_all File.dirname(__FILE__) + '/rails_helpers'
|
18
|
+
require_all File.dirname(__FILE__) + '/helpers'
|
19
|
+
require_all File.dirname(__FILE__) + '/rspec_generator'
|
20
|
+
|
21
|
+
|
22
|
+
module RSpec::Core
|
23
|
+
class ExampleGroup
|
24
|
+
def with_generator &block
|
25
|
+
RSpec::Generator.with_generator &block
|
26
|
+
end
|
27
|
+
|
28
|
+
def setup_generator test_method_name=nil, &block
|
29
|
+
RSpec::Generator.setup_generator test_method_name, &block
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'generator_spec/require_generator'
|
35
|
+
|
36
|
+
require 'generator_spec/matchers/content/content_matcher'
|
37
|
+
require_all File.dirname(__FILE__) + '/matchers/content'
|
38
|
+
|
39
|
+
require_all File.dirname(__FILE__) + '/matchers/file'
|
40
|
+
require_all File.dirname(__FILE__) + '/matchers/migration'
|
41
|
+
|
42
|
+
require_all File.dirname(__FILE__) + '/matchers/helpers'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RSpec
|
2
|
+
class RubyContentMatcher
|
3
|
+
attr_reader :content
|
4
|
+
|
5
|
+
def matches?(content)
|
6
|
+
@content = content
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure_message
|
10
|
+
debug_content
|
11
|
+
end
|
12
|
+
|
13
|
+
def negative_failure_message
|
14
|
+
debug_content
|
15
|
+
end
|
16
|
+
|
17
|
+
def debug_content
|
18
|
+
puts "Content: #{content}" if RSpec::Generator.debug?
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RSpec::RubyContentMatchers
|
2
|
-
class HaveBlock
|
3
|
-
attr_reader :name, :args, :block_args
|
2
|
+
class HaveBlock < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :name, :args, :block_args
|
4
4
|
|
5
5
|
def initialize(name, options={})
|
6
6
|
@name = name.to_s
|
@@ -8,9 +8,9 @@ module RSpec::RubyContentMatchers
|
|
8
8
|
@block_args = options[:block_args]
|
9
9
|
end
|
10
10
|
|
11
|
-
def matches?(content)
|
12
|
-
|
13
|
-
match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)
|
11
|
+
def matches?(content)
|
12
|
+
super
|
13
|
+
match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)#{end_expr}/m) != nil
|
14
14
|
block_content = $1 || $3
|
15
15
|
if block_given? && block_content
|
16
16
|
ruby_content = block_content.strip.extend(RSpec::RubyContent::Helpers)
|
@@ -20,17 +20,21 @@ module RSpec::RubyContentMatchers
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def failure_message
|
23
|
-
|
23
|
+
super
|
24
24
|
return "Expected there to be a block #{name} with arguments #{args}, but there wasn't"
|
25
25
|
end
|
26
26
|
|
27
27
|
def negative_failure_message
|
28
|
-
|
28
|
+
super
|
29
29
|
return "Did not expect there to be a block #{name} with arguments #{args}, but there was"
|
30
30
|
end
|
31
31
|
|
32
32
|
protected
|
33
33
|
|
34
|
+
def end_expr
|
35
|
+
"(end|# #{name})"
|
36
|
+
end
|
37
|
+
|
34
38
|
def args_expr
|
35
39
|
args ? '(\()?\s*' + args + '\s*(\))?\s+' : ''
|
36
40
|
end
|
@@ -42,5 +46,10 @@ module RSpec::RubyContentMatchers
|
|
42
46
|
|
43
47
|
def have_block(name, options={})
|
44
48
|
HaveBlock.new(name, options)
|
45
|
-
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def have_class_self(name, options={})
|
52
|
+
HaveBlock.new('class', :args => '<< self')
|
53
|
+
end
|
54
|
+
|
46
55
|
end
|
@@ -6,8 +6,8 @@
|
|
6
6
|
# say_hello_file_content.should have_method "hello"
|
7
7
|
#
|
8
8
|
module RSpec::RubyContentMatchers
|
9
|
-
class HaveCall
|
10
|
-
attr_reader :method, :args
|
9
|
+
class HaveCall < RSpec::RubyContentMatcher
|
10
|
+
attr_reader :method, :args
|
11
11
|
|
12
12
|
def initialize(method, args = nil)
|
13
13
|
@method = method.to_s
|
@@ -15,18 +15,18 @@ module RSpec::RubyContentMatchers
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def matches?(content)
|
18
|
-
|
18
|
+
super
|
19
19
|
(content =~ /#{method}#{args_expr}/m) != nil
|
20
20
|
end
|
21
21
|
|
22
22
|
def failure_message
|
23
|
-
|
23
|
+
super
|
24
24
|
return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
|
25
25
|
"Expected there to be a call to #{method}, but there wasn't"
|
26
26
|
end
|
27
27
|
|
28
|
-
def negative_failure_message
|
29
|
-
|
28
|
+
def negative_failure_message
|
29
|
+
super
|
30
30
|
return "Did not expect there to be a call to #{method} with args #{args}, but there was" if args
|
31
31
|
"Did not expect there to be a call to #{method}, but there was"
|
32
32
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module RSpec::RubyContentMatchers
|
2
|
-
class HaveCalls
|
3
|
-
attr_reader :calls, :method, :args
|
2
|
+
class HaveCalls < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :calls, :method, :args
|
4
4
|
|
5
5
|
def initialize(calls)
|
6
6
|
@calls = calls
|
7
7
|
end
|
8
8
|
|
9
9
|
def matches?(content)
|
10
|
-
|
10
|
+
super
|
11
11
|
calls.each_pair do |method, args|
|
12
12
|
@method = method.to_s
|
13
13
|
@args = args
|
@@ -17,13 +17,13 @@ module RSpec::RubyContentMatchers
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def failure_message
|
20
|
-
|
20
|
+
super
|
21
21
|
return "Expected there to be a call to #{method} with args #{args}, but there wasn't" if args
|
22
22
|
"Expected there to be a call to #{method}, but there wasn't"
|
23
23
|
end
|
24
24
|
|
25
25
|
def negative_failure_message
|
26
|
-
|
26
|
+
super
|
27
27
|
return "Did not expect there to be a call to #{method} with args #{args}, but there was" if args
|
28
28
|
"Did not expect there to be a call to #{method}, but there was"
|
29
29
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module RSpec::RubyContentMatchers
|
2
|
-
class HaveClass
|
3
|
-
attr_reader :klass, :type
|
2
|
+
class HaveClass < RSpec::RubyContentMatcher
|
3
|
+
attr_reader :klass, :type
|
4
4
|
|
5
5
|
def initialize(klass, type=nil)
|
6
6
|
@klass = klass.to_s.camelize
|
@@ -8,8 +8,8 @@ module RSpec::RubyContentMatchers
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def matches?(content)
|
11
|
-
|
12
|
-
match_res = (content =~ /class\s+#{klass}#{type}\s+(.*)
|
11
|
+
super
|
12
|
+
match_res = (content =~ /class\s+#{klass}#{type}\s+(.*)#{end_expr}/m)
|
13
13
|
if block_given? && $1
|
14
14
|
ruby_content = $1.strip.extend(RSpec::RubyContent::Helpers)
|
15
15
|
yield ruby_content
|
@@ -18,28 +18,36 @@ module RSpec::RubyContentMatchers
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def failure_message
|
21
|
-
|
21
|
+
super
|
22
22
|
"Expected there to be the class #{klass}"
|
23
23
|
end
|
24
24
|
|
25
25
|
def negative_failure_message
|
26
|
-
|
26
|
+
super
|
27
27
|
"Did no expected there to be the class #{klass}"
|
28
28
|
end
|
29
|
+
|
30
|
+
def end_expr
|
31
|
+
"(end|# #{klass})"
|
32
|
+
end
|
29
33
|
|
30
34
|
end
|
31
35
|
|
32
36
|
def have_class(klass, type = nil)
|
33
37
|
HaveClass.new(klass, type)
|
34
38
|
end
|
39
|
+
alias_method :be_class, :have_class
|
35
40
|
|
36
41
|
def have_helper_class(klass)
|
37
42
|
HaveClass.new(klass, :helper)
|
38
43
|
end
|
44
|
+
alias_method :be_helper_class, :have_helper_class
|
45
|
+
|
39
46
|
|
40
47
|
def have_controller_class(klass)
|
41
48
|
HaveClass.new(klass, :controller)
|
42
49
|
end
|
50
|
+
alias_method :be_controller_class, :have_controller_class
|
43
51
|
end
|
44
52
|
|
45
53
|
|