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

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
74
75
76
# File 'lib/genspec/matchers.rb', line 55

def add_shorthand_methods(base)
  instance_methods = base.instance_methods.collect { |m| m.to_s }
  
  # ex:
  #   expect(subject).to create_file(...)
  # equivalent to:
  #   expect(subject).to 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 <<-end_code
      # def create_file(*args, &block)
      #   call_action('create_file', *args, &block)
      # end
      def #{method_name}(*args, &block)
        call_action(#{method_name.inspect}, *args, &block)
      end
    end_code
  end 
end

Instance Method Details

#call_action(kind, *args, &block) ⇒ Object

ex:

expect(subject).to 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:

expect(subject).to 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 { expect(File).not_to 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:

expect(subject).to generate(:file, ...)
expect(subject).to 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)
    # expect(subject).to generate(:file, ...)
    call_action(kind, *args, &block)
  else
    # expect(subject).to 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