Module: GenSpec::GeneratorExampleGroup::ClassMethods
- Defined in:
- lib/genspec/generator_example_group.rb
Instance Method Summary collapse
-
#generator(name = nil) ⇒ Object
Traverses up the context tree to find the topmost description, which represents the controller to be tested or the string/symbol representing it.
-
#generator_args ⇒ Object
(also: #generator_arguments)
Returns the generator arguments to be used for this context.
-
#generator_init_blocks ⇒ Object
Returns an array of all init blocks from the topmost context down to this one, in that order.
-
#generator_input ⇒ Object
Returns the input stream to be used for this context.
-
#generator_options ⇒ Object
Returns the hash of options to be passed into the generator in this context.
-
#generator_output ⇒ Object
Returns the generator output string or IO, or nil.
-
#genspec_subclass? ⇒ Boolean
Returns true if this object’s superclass is also a GenSpec.
-
#with_args(*args, &block) ⇒ Object
(also: #with_arguments)
Sets the list of arguments for this generator.
-
#with_generator_options(options, &block) ⇒ Object
Allows you to pass options directly into the generator, such as :shell, :behavior, etc.
-
#with_input(string, &block) ⇒ Object
Sets the input stream for this generator.
-
#within_source_root(&block) ⇒ Object
(also: #before_generation)
Executes some code within the generator’s source root prior to the generator actually running.
Instance Method Details
#generator(name = nil) ⇒ Object
Traverses up the context tree to find the topmost description, which represents the controller to be tested or the string/symbol representing it.
If name is specified, it will be used instead and subsequent calls to this method will return the specified name.
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/genspec/generator_example_group.rb', line 213 def generator(name = nil) [:generator_name] = name.to_s if name return [:generator_name] if [:generator_name] if genspec_subclass? superclass.generator else describes || description end end |
#generator_args ⇒ Object Also known as: generator_arguments
Returns the generator arguments to be used for this context. If this context doesn’t have any generator arguments, its superclass is checked, and so on until either the parent isn’t a GenSpec or a set of arguments is found. Only the closest argument set is used; any sets specified above the discovered argument set are ignored.
157 158 159 160 161 162 163 164 165 |
# File 'lib/genspec/generator_example_group.rb', line 157 def generator_args return [:generator_args] if [:generator_args] [:generator_args] = if genspec_subclass? superclass.generator_args else [] end end |
#generator_init_blocks ⇒ Object
Returns an array of all init blocks from the topmost context down to this one, in that order. These blocks will be executed sequentially prior to each run of the generator.
145 146 147 148 149 150 |
# File 'lib/genspec/generator_example_group.rb', line 145 def generator_init_blocks result = [] result.concat superclass.generator_init_blocks if genspec_subclass? result << [:generator_init_block] if [:generator_init_block] result end |
#generator_input ⇒ Object
Returns the input stream to be used for this context. If this context doesn’t have an input stream, its superclass is checked, and so on until either the parent isn’t a GenSpec or an input stream is found. Only the closest input stream is used; any streams specified above the discovered input stream are ignored.
194 195 196 197 198 199 200 201 202 |
# File 'lib/genspec/generator_example_group.rb', line 194 def generator_input return [:generator_input] if [:generator_input] [:generator_input] = if genspec_subclass? superclass.generator_input else nil end end |
#generator_options ⇒ Object
Returns the hash of options to be passed into the generator in this context.
168 169 170 171 172 173 174 175 176 |
# File 'lib/genspec/generator_example_group.rb', line 168 def return [:generator_options] if [:generator_options] [:generator_options] = if genspec_subclass? superclass..dup else { } end end |
#generator_output ⇒ Object
Returns the generator output string or IO, or nil.
179 180 181 182 183 184 185 186 187 |
# File 'lib/genspec/generator_example_group.rb', line 179 def generator_output return [:generator_output] if [:generator_output] [:generator_output] = if genspec_subclass? superclass.generator_output else nil end end |
#genspec_subclass? ⇒ Boolean
Returns true if this object’s superclass is also a GenSpec.
When a context is created, rspec creates a class inheriting from the context’s parent. Therefore, this method can be used to recurse up to the highest-level spec that still tests a generator.
229 230 231 |
# File 'lib/genspec/generator_example_group.rb', line 229 def genspec_subclass? superclass.include?(GenSpec::GeneratorExampleGroup) end |
#with_args(*args, &block) ⇒ Object Also known as: with_arguments
Sets the list of arguments for this generator.
-
All arguments will be converted to Strings, because that’s how they’d enter the generator from a command line. To avoid this, pass :object => true at the end;
Ex:
with_args '--orm', 'active_record' do
it "should use activerecord" do
# . . .
end
end
with_args '--size', 5, :object => true do
# . . .
end
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/genspec/generator_example_group.rb', line 60 def with_args(*args, &block) = args. args = args.flatten.collect { |c| c.to_s } unless [:object] if block_given? context "with arguments #{args.inspect}" do with_args(generator_args + args, ) instance_eval(&block) end else [:generator_args] = args end end |
#with_generator_options(options, &block) ⇒ Object
Allows you to pass options directly into the generator, such as :shell, :behavior, etc.
Ex:
# simulate a destroy generator, per `rails destroy controller ...`
:behavior => :revoke do
# . . .
end
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/genspec/generator_example_group.rb', line 84 def (, &block) if block_given? context "with generator options #{options.inspect}" do instance_eval &block end else .merge! end end |
#with_input(string, &block) ⇒ Object
Sets the input stream for this generator.
Ex:
with_input " y\n n\n a\n" do
it "should overwrite, then skip, then overwrite all" do
# . . .
end
end
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/genspec/generator_example_group.rb', line 109 def with_input(string, &block) if block_given? context "with input string #{string.inspect}" do with_input string instance_eval &block end else [:generator_input] = string end end |
#within_source_root(&block) ⇒ Object Also known as: before_generation
Executes some code within the generator’s source root prior to the generator actually running. Useful for setting up fixtures.
Ex:
within_source_root do
touch "Gemfile"
end
Optionally, the block may receive a single argument, which is the full path to the temporary directory representing the source root:
within_source_root do |tempdir|
# . . .
end
138 139 140 |
# File 'lib/genspec/generator_example_group.rb', line 138 def within_source_root(&block) [:generator_init_block] = block end |