module Cog::SpecHelpers::Matchers

Extra should or should_not matchers for RSpec. Check out {#match_maker} for help writing new matchers.

Public Instance Methods

complain() click to toggle source

The target {Invocation} should write something to STDERR, indicating an error @return [nil]

# File lib/cog/spec_helpers/matchers.rb, line 13
def complain
  match_maker do
    message { "to [write something|not write anything] to STDERR" }
    test { !error.empty? }
  end
end
do_something() click to toggle source

The target {Invocation} should do something, as determined by standard output @return [nil]

# File lib/cog/spec_helpers/matchers.rb, line 37
def do_something
  match_maker do
    message { "to [write something|not write anything] to STDOUT" }
    test { !lines.empty? }
  end
end
make(path) click to toggle source

The target {Invocation} should create a file at the given path @param path [String] path to check for a file after the invocation @return [nil]

# File lib/cog/spec_helpers/matchers.rb, line 23
def make(path)
  match_maker do
    message { "to [create|not create] #{path}" }
    before do
      @existed = File.exists? path
    end
    test do
      !@existed && File.exists?(path)
    end
  end
end
match_maker(&block) click to toggle source

Makes it easier to write RSpec matchers for testing cog command invocations @yield self is set to an instance of {MatchMaker} @example

def show_help
  match_maker do
    message { "to [show|not show] the default help text, got #{lines.first.inspect}" }
    test { (/help.*code gen/ =~ lines[1]) }
  end
end
# File lib/cog/spec_helpers/matchers/match_maker.rb, line 109
def match_maker(&block)
  m = MatchMaker.new
  m.instance_eval &block
  m
end
output(x) click to toggle source

The target {Invocation} should write the given list of lines to standard output @param x [Array<String>, Regexp] a list of lines to match against standard output @return [nil]

# File lib/cog/spec_helpers/matchers.rb, line 47
def output(x)
  match_maker do
    message do
      if x.is_a? Regexp
        "to [write|not write] #{x.inspect} to STDOUT"
      else
        "to [write|not write] #{x.join "\n"} to STDOUT"
      end
    end
    test do
      if x.is_a? Regexp
        x =~ lines.join("\n")
      else
        lines.zip(x).all? {|a, b| a.strip == b.to_s.strip}
      end
    end
  end
end
show_help() click to toggle source

The target {Invocation} should output the default help text @return [nil]

# File lib/cog/spec_helpers/matchers.rb, line 68
def show_help
  match_maker do
    message { "to [show|not show] the default help text, got #{lines.first.inspect}" }
    test { (/help.*code gen/ =~ lines[1]) }
  end
end