Module: RSpecial::Matchers

Included in:
Test::Matchers
Defined in:
lib/rspecial/matchers.rb

Overview

This module provides matchers for RSpec-compatiblity.

The set is not fully compataible, but provides most RSpec matchers. The most notable exlusion for the moment is the have matchers.

Compatability will improve with time. Feel obligated to submit a patch if you really need it. ;)

Instance Method Summary collapse

Instance Method Details

#be_close(delta, criterion) ⇒ Object

Passes if expected and actual are nto equal within delta tolerance.

value.should be_close(delta, criterion)

Raises:

  • WithinAssay



42
43
44
# File 'lib/rspecial/matchers.rb', line 42

def be_close(delta, criterion)
  WithinAssay.assertor(criterion, delta)
end

#be_empty ⇒ Object

Passes if object is empty.

object.should be_empty

Raises:

  • EmptyAssay



52
53
54
# File 'lib/rspecial/matchers.rb', line 52

def be_empty
  EmptyAssay.assertor
end

#be_false ⇒ Object

Passed if object is false.

value.should be_false

Raises:

  • FalseAssay



88
89
90
# File 'lib/rspecial/matchers.rb', line 88

def be_false
  FalseAssay.assertor
end

#be_instance_of(cls) ⇒ Object Also known as: be_an_instance_of

Passes if object is an instance of class.

object.should be_instance_of(class)

Raises:

  • InstanceAssay



123
124
125
# File 'lib/rspecial/matchers.rb', line 123

def be_instance_of(cls)
  InstanceAssay.assertor(cls)
end

#be_kind_of(cls) ⇒ Object Also known as: be_a_kind_of, be_a, be_an

Pass if object is a kind of class.

object.should be_kind_of(class)

Raises:

  • KindAssay



136
137
138
# File 'lib/rspecial/matchers.rb', line 136

def be_kind_of(cls)
  KindAssay.assertor(cls)
end

#be_like(criterion) ⇒ Object

Passes if the expected and actual are alike.

object.should be_like(criterion)

There is no equivalant for this in RSpec, we simply add it here to cover all Assays available.

Raises:

  • LikeAssay



32
33
34
# File 'lib/rspecial/matchers.rb', line 32

def be_like(criterion)
  LikeAssay.assertor(criterion)
end

#be_nil ⇒ Object

Pass if object is nil.

value.should be_nil

Raises:

  • NilAssay



162
163
164
# File 'lib/rspecial/matchers.rb', line 162

def be_nil
  NilAssay.assertor
end

#be_true ⇒ Object

Passed if object is true.

object.should be_true

Raises:

  • TrueAssay



223
224
225
# File 'lib/rspecial/matchers.rb', line 223

def be_true
  TrueAssay.assertor
end

#eq(obj) ⇒ Object

Passes if actual == expected.

object.should eq(object)

Raises:

  • EqualAssay



112
113
114
# File 'lib/rspecial/matchers.rb', line 112

def eq(obj)
  EqualAssay.assertor(obj)
end

#eql(exp) ⇒ Object

Pass if two objects are equal using #eql? method.

object1.should eql(object2)

Raises:

  • EqualityAssay



198
199
200
# File 'lib/rspecial/matchers.rb', line 198

def eql(exp)
  EqualityAssay.assertor(exp)
end

#equal(obj) ⇒ Object Also known as: be_identical_to

Passes if actual is the same exact object as expected.

object.should be_identical_to(object)

Raises:

  • IdentityAssay



99
100
101
# File 'lib/rspecial/matchers.rb', line 99

def equal(obj)
  IdentityAssay.assertor(obj)
end

#equate_to(exp) ⇒ Object

Passes if expected == actual.

'MY STRING'.should equate_to('my string'.upcase)
'MY STRING'.should_not equate_to('another string')

This matcher is not supported by RSpec, but is added so that the EqualityAssay has an explict matcher available.

Raises:

  • EqualityAssay



66
67
68
# File 'lib/rspecial/matchers.rb', line 66

def equate_to(exp)
  EqualityAssay.assertor(exp)
end

#expect(target) ⇒ Object



252
253
254
# File 'lib/rspecial/matchers.rb', line 252

def expect(target)
  Expect.new(target)
end

#have(n) ⇒ Object Also known as: have_exactly



230
231
232
# File 'lib/rspecial/matchers.rb', line 230

def have(n)
  Have.new(n)
end

#have_at_least(n) ⇒ Object



239
240
241
# File 'lib/rspecial/matchers.rb', line 239

def have_at_least(n)
  Have.new(n, :at_least)
end

#have_at_most(n) ⇒ Object



246
247
248
# File 'lib/rspecial/matchers.rb', line 246

def have_at_most(n)
  Have.new(n, :at_most)
end

#match(regexp) ⇒ Object

Pass if object matches pattern using #=~ method.

object.should match(regexp)

Raises:

  • MatchAssay



151
152
153
# File 'lib/rspecial/matchers.rb', line 151

def match(regexp)
  MatchAssay.assertor(regexp)
end

#raise_error(exception = Exception) ⇒ Object

TODO:

Support for message matching ?

Pass if an exception is raised.

lambda { do_something_risky }.should raise_error
lambda { do_something_risky }.should raise_error(PoorRiskDecisionError)

Raises:

  • RaiseAssay



176
177
178
# File 'lib/rspecial/matchers.rb', line 176

def raise_error(exception=Exception)
  RaiseAssay.assertor(exception)
end

#respond_to(method) ⇒ Object

Pass if object responds to message.

object.should respond_to(:method_name)

Raises:

  • RespondAssay



187
188
189
# File 'lib/rspecial/matchers.rb', line 187

def respond_to(method)
  RespondAssay.assertor(method)
end

#satisfy(&block) ⇒ Object

Passes if block is satisfied given target object as argument.

5.should satisfy{ |n| n > 3}

Raises:

  • ExecutionAssay



77
78
79
# File 'lib/rspecial/matchers.rb', line 77

def satisfy(&block)
  ExecutionAssay.assertor(&block)
end

#throw_symbol(sym = nil) ⇒ Object

TODO:

Support for throw argument. (Does RSpec even support this?)

Pass if procedure throws a specified symbol.

lambda { do_something_risky }.should throw_symbol
lambda { do_something_risky }.should throw_symbol(:that_was_risky)

Raises:

  • ThrowAssay



212
213
214
# File 'lib/rspecial/matchers.rb', line 212

def throw_symbol(sym=nil) #, arg=nil)
  ThrowAssay.assertor(sym)
end