genspec 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,9 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- genspec (0.2.2)
4
+ genspec (0.2.4)
5
5
  i18n (>= 0.5.0)
6
- rspec (~> 2.6.0)
6
+ rspec (~> 2)
7
7
  sc-core-ext (~> 1.2.1)
8
8
  thor (~> 0.14.6)
9
9
 
@@ -79,14 +79,14 @@ GEM
79
79
  rake (0.9.2.2)
80
80
  rdoc (3.11)
81
81
  json (~> 1.4)
82
- rspec (2.6.0)
83
- rspec-core (~> 2.6.0)
84
- rspec-expectations (~> 2.6.0)
85
- rspec-mocks (~> 2.6.0)
86
- rspec-core (2.6.4)
87
- rspec-expectations (2.6.0)
82
+ rspec (2.8.0)
83
+ rspec-core (~> 2.8.0)
84
+ rspec-expectations (~> 2.8.0)
85
+ rspec-mocks (~> 2.8.0)
86
+ rspec-core (2.8.0)
87
+ rspec-expectations (2.8.0)
88
88
  diff-lcs (~> 1.1.2)
89
- rspec-mocks (2.6.0)
89
+ rspec-mocks (2.8.0)
90
90
  sc-core-ext (1.2.1)
91
91
  activesupport (>= 2.3.5)
92
92
  sprockets (2.0.3)
data/README.rdoc CHANGED
@@ -199,6 +199,23 @@ Finally, you can also choose to use +with_args+ without a block, in which case i
199
199
  # . . .
200
200
  end
201
201
  end
202
+
203
+
204
+ === Passing Options
205
+
206
+ Sometimes you need to change the behavior of the generator itself, by passing options directly into the generator instance that you couldn't normally pass from the command line. A perfect example is when you want to test what would happen when the generator's behavior is set to :revoke, which is equivalent to the +rails destroy+ command.
207
+
208
+ Here's an example that verifies that a file is created by the generator, but that the same file is deleted when the generator's behavior is set to +:revoke+:
209
+
210
+ describe :controller do
211
+ with_args "welcome" do
212
+ it { should generate("app/controllers/welcome_controller.rb") }
213
+
214
+ with_options :behavior => :revoke do
215
+ it { should delete("app/controllers/welcome_controller.rb") }
216
+ end
217
+ end
218
+ end
202
219
 
203
220
  === Fixtures
204
221
 
@@ -5,7 +5,36 @@ module GenSpec
5
5
 
6
6
  def self.included(base)
7
7
  base.send(:extend, GenSpec::GeneratorExampleGroup::ClassMethods)
8
- base.send(:subject) { self.class.generator_descriptor }
8
+ base.send(:subject) { generator_descriptor }
9
+ end
10
+
11
+ def within_source_root(&block)
12
+ generator_init_blocks << block
13
+ end
14
+
15
+ def generator_init_blocks
16
+ @generator_init_blocks ||= self.class.generator_init_blocks.dup
17
+ end
18
+
19
+ # A hash containing the following:
20
+ #
21
+ # :described - the generator to be tested, or the string/symbol representing it
22
+ # :args - any arguments to be used when invoking the generator
23
+ # :input - a string to be used as an input stream, or nil
24
+ # :init_blocks - an array of blocks to be invoked prior to running the generator
25
+ # :generator_options - a hash of options to be passed into the generator
26
+ #
27
+ # This hash represents the +subject+ of the spec and this is the object that will
28
+ # ultimately be passed into the GenSpec matchers.
29
+ #
30
+ def generator_descriptor
31
+ {
32
+ :described => self.class.target_generator,
33
+ :args => self.class.generator_args,
34
+ :input => self.class.generator_input,
35
+ :init_blocks => generator_init_blocks,
36
+ :generator_options => self.class.generator_options
37
+ }
9
38
  end
10
39
 
11
40
  module ClassMethods
@@ -41,6 +70,27 @@ module GenSpec
41
70
  end
42
71
  end
43
72
 
73
+ # Allows you to pass options directly into the generator, such as
74
+ # :shell, :behavior, etc.
75
+ #
76
+ # Ex:
77
+ #
78
+ # # simulate a destroy generator, per `rails destroy controller ...`
79
+ # with_generator_options :behavior => :revoke do
80
+ # # . . .
81
+ # end
82
+ #
83
+ def with_generator_options(options, &block)
84
+ if block_given?
85
+ context "with generator options #{options.inspect}" do
86
+ with_generator_options options
87
+ instance_eval &block
88
+ end
89
+ else
90
+ generator_options.merge! options
91
+ end
92
+ end
93
+
44
94
  # Sets the input stream for this generator.
45
95
  #
46
96
  # Ex:
@@ -113,6 +163,17 @@ module GenSpec
113
163
  end
114
164
  end
115
165
 
166
+ # Returns the hash of options to be passed into the generator in this context.
167
+ def generator_options
168
+ return metadata[:generator_options] if metadata[:generator_options]
169
+
170
+ metadata[:generator_options] = if genspec_subclass?
171
+ superclass.generator_options.dup
172
+ else
173
+ { }
174
+ end
175
+ end
176
+
116
177
  # Returns the input stream to be used for this context. If this context doesn't
117
178
  # have an input stream, its superclass is checked, and so on until either the
118
179
  # parent isn't a GenSpec or an input stream is found. Only the closest input
@@ -132,25 +193,6 @@ module GenSpec
132
193
  alias with_arguments with_args
133
194
  alias generator_arguments generator_args
134
195
 
135
- # A hash containing the following:
136
- #
137
- # :described - the generator to be tested, or the string/symbol representing it
138
- # :args - any arguments to be used when invoking the generator
139
- # :input - a string to be used as an input stream, or nil
140
- # :init_blocks - an array of blocks to be invoked prior to running the generator
141
- #
142
- # This hash represents the +subject+ of the spec and this is the object that will
143
- # ultimately be passed into the GenSpec matchers.
144
- #
145
- def generator_descriptor
146
- {
147
- :described => target_generator,
148
- :args => generator_args,
149
- :input => generator_input,
150
- :init_blocks => generator_init_blocks
151
- }
152
- end
153
-
154
196
  # Traverses up the context tree to find the topmost description, which represents
155
197
  # the controller to be tested or the string/symbol representing it.
156
198
  def target_generator
@@ -22,6 +22,22 @@ module GenSpec
22
22
  end
23
23
  end
24
24
 
25
+ # Makes sure that the generator deletes the named file. This is done by first ensuring that the
26
+ # file exists in the first place, and then ensuring that it does not exist after the generator
27
+ # completes its run.
28
+ #
29
+ # Example:
30
+ # subject.should delete("path/to/file")
31
+ #
32
+ def delete(filename)
33
+ within_source_root do
34
+ FileUtils.mkdir_p File.dirname(filename)
35
+ FileUtils.touch filename
36
+ end
37
+
38
+ generate { File.should_not exist(filename) }
39
+ end
40
+
25
41
  # ex:
26
42
  # subject.should call_action(:create_file, ...)
27
43
  #
@@ -21,6 +21,7 @@ module GenSpec
21
21
  def matches?(generator)
22
22
  @described = generator[:described]
23
23
  @args = generator[:args]
24
+ @generator_options = generator[:generator_options]
24
25
  @shell = GenSpec::Shell.new("", generator[:input] || "")
25
26
  @init_blocks = generator[:init_blocks]
26
27
 
@@ -103,11 +104,10 @@ module GenSpec
103
104
  end
104
105
 
105
106
  @destination_root = tempdir
106
- @generator.start(@args || [], {
107
+ @generator.start(@args || [], @generator_options.reverse_merge(
107
108
  :shell => @shell,
108
109
  :destination_root => destination_root
109
- })
110
-
110
+ ))
111
111
  check_for_errors
112
112
  generated
113
113
  end
@@ -2,7 +2,7 @@ module GenSpec
2
2
  class Version
3
3
  MAJOR = 0
4
4
  MINOR = 2
5
- PATCH = 4
5
+ PATCH = 5
6
6
  RELEASE = nil
7
7
 
8
8
  STRING = (RELEASE ? [MAJOR, MINOR, PATCH, RELEASE] : [MAJOR, MINOR, PATCH]).join('.')
@@ -80,12 +80,30 @@ describe :test_rails3 do
80
80
  end
81
81
  end
82
82
 
83
- # FIXME: Uh, how best to write a spec around this? I'm actually trying to test #with_args with a block...
84
83
  with_args :test_arg do
85
84
  it "should generate file 'test_arg'" do
86
85
  subject.should generate('test_arg')
87
86
  end
88
87
 
88
+ it "should not generate template_name" do
89
+ # because that option hasn't been given in this context.
90
+ subject.should_not generate('template_name')
91
+ end
92
+
93
+ with_generator_options :behavior => :revoke do
94
+ it "should delete file 'test_arg'" do
95
+ subject.should generate {
96
+ File.should_not exist("test_arg")
97
+ }
98
+ end
99
+
100
+ # demonstrate use of the `delete` matcher, which is equivalent to
101
+ # above:
102
+ it "should destroy file 'test_arg'" do
103
+ subject.should delete("test_arg")
104
+ end
105
+ end
106
+
89
107
  # ...and a test of nested args
90
108
  with_args "template_name" do
91
109
  it "should generate file 'test_arg'" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: genspec
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.4
5
+ version: 0.2.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Colin MacKenzie IV