Module: GenSpec::Matchers
- Included in:
- GeneratorExampleGroup
- Defined in:
- lib/genspec/matchers.rb,
lib/genspec/matchers/base.rb,
lib/genspec/matchers/output_matcher.rb,
lib/genspec/matchers/result_matcher.rb
Defined Under Namespace
Classes: Base, GenerationMethodMatcher, OutputMatcher, ResultMatcher
Class Method Summary collapse
Instance Method Summary collapse
-
#call_action(kind, *args, &block) ⇒ Object
ex: subject.should call_action(:create_file, …).
-
#delete(filename) ⇒ Object
Makes sure that the generator deletes the named file.
-
#generate(kind = nil, *args, &block) ⇒ Object
Valid types: :dependency, :class_collisions, :file, :template, :complex_template, :directory, :readme, :migration_template, :route_resources.
-
#output(text_or_regexp) ⇒ Object
This tests the content sent to the command line, instead of the generated product.
Class Method Details
.add_shorthand_methods(base) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/genspec/matchers.rb', line 55 def add_shorthand_methods(base) instance_methods = base.instance_methods.collect { |m| m.to_s } # ex: # subject.should create_file(...) # equivalent to: # subject.should call_action(:create_file, ...) GenSpec::Matchers::GenerationMethodMatcher.generation_methods.each do |method_name| # don't overwrite existing methods. since the user expects this to fire FIRST, # it's as if this method's been "overridden". See #included and #extended. next if instance_methods.include?(method_name.to_s) base.class_eval " def \#{method_name}(*args, &block) # def create_file(*args, &block)\n call_action(\#{method_name.inspect}, *args, &block) # call_action('create_file', *args, &block)\n end # end\n end_code\n end \nend\n" |
Instance Method Details
#call_action(kind, *args, &block) ⇒ Object
ex:
subject.should call_action(:create_file, ...)
44 45 46 |
# File 'lib/genspec/matchers.rb', line 44 def call_action(kind, *args, &block) GenSpec::Matchers::GenerationMethodMatcher.for_method(kind, *args, &block) end |
#delete(filename) ⇒ Object
Makes sure that the generator deletes the named file. This is done by first ensuring that the file exists in the first place, and then ensuring that it does not exist after the generator completes its run.
Example:
subject.should delete("path/to/file")
32 33 34 35 36 37 38 39 |
# File 'lib/genspec/matchers.rb', line 32 def delete(filename) within_source_root do FileUtils.mkdir_p File.dirname(filename) FileUtils.touch filename end generate { File.should_not exist(filename) } end |
#generate(kind = nil, *args, &block) ⇒ Object
Valid types: :dependency, :class_collisions, :file, :template, :complex_template, :directory, :readme, :migration_template, :route_resources
Examples:
subject.should generate(:file, ...)
subject.should generate("vendor/plugins/will_paginate/init.rb")
15 16 17 18 19 20 21 22 23 |
# File 'lib/genspec/matchers.rb', line 15 def generate(kind = nil, *args, &block) if kind.kind_of?(Symbol) # subject.should generate(:file, ...) call_action(kind, *args, &block) else # subject.should generate("vendor/plugins/will_paginate/init.rb") GenSpec::Matchers::ResultMatcher.new(kind, &block) end end |
#output(text_or_regexp) ⇒ Object
This tests the content sent to the command line, instead of the generated product. Useful for testing help messages, etc.
50 51 52 |
# File 'lib/genspec/matchers.rb', line 50 def output(text_or_regexp) GenSpec::Matchers::OutputMatcher.new(text_or_regexp) end |