sneakin / spec_matcher_helper

Makes creating RSpec Matchers easier.

This URL has Read+Write access

spec_matcher_helper / spec / spec_matcher_spec.rb
100644 119 lines (93 sloc) 2.588 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# The specification for my RSpec matcher helper.
#
# Author:: Nolan Eakins <nolan@eakins.net>
# Copyright:: Public domain
# License:: You are free to use and abuse this in anyway.
require 'spec'
require 'spec_matcher_helper'
 
matcher :test_matcher do
  attr_accessor :expecting, :target
  
  def initialize(*expecting) #:nodoc:all
    @expecting = expecting
  end
  
  def matches?(target) #:nodoc:all
  end
  
  def failure_message #:nodoc:all
  end
  
  def negative_failure_message #:nodoc:all
  end
end
 
class InheritsFrom
  attr_accessor :expecting
  
  def initialize(expecting)
    @expecting = expecting
  end
  
  def was_inherited?
  end
end
 
matcher :test_matcher_with_super_class => InheritsFrom do
  def initialize(expecting) #:nodoc:all
    super(expecting)
  end
  
  def matches?(target) #:nodoc:all
  end
end
 
matcher :test_matcher_with_super_symbol => :inherits_from do
  def initialize(expecting) #:nodoc:all
    super(expecting)
  end
  
  def matches?(target) #:nodoc:all
  end
end
 
describe 'matcher', :shared => true do
  describe 'generates a method using the given name that' do
    it "returns an instance of the generated class" do
      @matcher.should be_kind_of(@klass)
    end
  
    it "passes along its arguments" do
      @matcher.expecting.should == @args
    end
  end
end
 
describe 'matcher', 'with test_matcher' do
  before(:each) do
    @args = [:a, :b]
    @klass = TestMatcher
    @matcher = test_matcher(*@args)
  end
 
  it_should_behave_like 'matcher'
 
  it "responds to all of the methods defined in the block" do
    %W(matches? expecting failure_message negative_failure_message).each do |meth|
      @matcher.should respond_to(meth)
    end
  end
end
 
describe 'matcher with super', :shared => true do
  before(:each) do
    @args = :arg
    @klass = Object.const_get(@matcher_name.to_s.camelize)
    @method = method(@matcher_name)
    @matcher = @method.call(*@args)
  end
 
  it_should_behave_like 'matcher'
  
  it "responds to all of the methods defined in the block" do
    %W(matches? expecting).each do |meth|
      @matcher.should respond_to(meth)
    end
  end
 
  it "subclasses InheritsFrom" do
    @matcher.should be_kind_of(InheritsFrom)
  end
end
 
describe 'matcher', 'with test_matcher_with_super_class' do
  it_should_behave_like 'matcher with super'
  
  before(:all) do
    @matcher_name = :test_matcher_with_super_class
  end
end
 
describe 'matcher', 'with test_matcher_with_super_symbol' do
  before(:all) do
    @matcher_name = :test_matcher_with_super_symbol
  end
 
  it_should_behave_like 'matcher with super'
end