Module: Cog::SpecHelpers::Matchers

Defined in:
lib/cog/spec_helpers/matchers.rb,
lib/cog/spec_helpers/matchers/match_maker.rb

Overview

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

Defined Under Namespace

Classes: MatchMaker

Instance Method Summary collapse

Instance Method Details

#complainnil

The target Invocation should write something to STDERR, indicating an error



13
14
15
16
17
18
# 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_somethingnil

The target Invocation should do something, as determined by standard output



37
38
39
40
41
42
# 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) ⇒ nil

The target Invocation should create a file at the given path



23
24
25
26
27
28
29
30
31
32
33
# 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 { ... } ⇒ Object

Makes it easier to write RSpec matchers for testing cog command invocations

Examples:

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

Yields:



109
110
111
112
113
# 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) ⇒ nil

The target Invocation should write the given list of lines to standard output



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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_helpnil

The target Invocation should output the default help text



68
69
70
71
72
73
# 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