Matchy::Expectations::TestCaseExtensions

Public Instance Methods

be(*obj) click to toggle source

Simply checks if the receiver matches the expected object. TODO: Fill this out to implement much of the RSpec functionality (and then some)

Examples

  "hello".should be("hello")
  (13 < 20).should be(true)
    # File lib/matchy/built_in/truth_expectations.rb, line 12
12:       def be(*obj)
13:         build_matcher(:be, obj) do |receiver, matcher, args|
14:           @receiver, expected = receiver, args[0]
15:           matcher.positive_failure_message = "Expected #{@receiver.inspect} to be #{expected.inspect}."
16:           matcher.negative_failure_message = "Expected #{@receiver.inspect} to not be #{expected.inspect}."
17:           expected == @receiver
18:         end
19:       end
be_close(obj, delta = 0.3) click to toggle source

Checks if the given object is within a given object and delta.

Examples

  (20.0 - 2.0).should be_close(18.0)
  (13.0 - 4.0).should be_close(9.0, 0.5)
    # File lib/matchy/built_in/truth_expectations.rb, line 28
28:       def be_close(obj, delta = 0.3)
29:         build_matcher(:be_close, [obj, delta]) do |receiver, matcher, args|
30:           @receiver, expected, delta = receiver, args[0], args[1]
31:           matcher.positive_failure_message = "Expected #{@receiver.inspect} to be close to #{expected.inspect} (delta: #{delta})."
32:           matcher.negative_failure_message = "Expected #{@receiver.inspect} to not be close to #{expected.inspect} (delta: #{delta})."
33:           (@receiver - expected).abs < delta
34:         end
35:       end
be_success() click to toggle source

Asks given for success?(). This is necessary because Rails Integration::Session overides method_missing without grace.

Examples

  @response.should be_success
     # File lib/matchy/built_in/truth_expectations.rb, line 110
110:       def be_success
111:         ask_for(:success, :with_arg => nil)
112:       end
change(&block) click to toggle source

Checks if the given block alters the value of the block attached to change

Examples

  lambda {var += 1}.should change {var}.by(1)
  lambda {var += 2}.should change {var}.by_at_least(1)
  lambda {var += 1}.should change {var}.by_at_most(1)
  lambda {var += 2}.should change {var}.from(1).to(3) if var = 1
    # File lib/matchy/built_in/change_expectations.rb, line 11
11:       def change(&block)
12:         build_matcher(:change) do |receiver, matcher, args|
13:           before, done, after = block.call, receiver.call, block.call
14:           comparison = after != before
15:           if list = matcher.chained_messages
16:             comparison = case list[0].name
17:               # todo: provide meaningful messages
18:             when :by          then (after == before + list[0].args[0] || after == before - list[0].args[0])
19:             when :by_at_least then (after >= before + list[0].args[0] || after <= before - list[0].args[0])
20:             when :by_at_most  then (after <= before + list[0].args[0] && after >= before - list[0].args[0])
21:             when :from        then (before == list[0].args[0]) && (after == list[1].args[0])
22:             end
23:           end
24:           matcher.positive_failure_message = "given block shouldn't alter the block attached to change"
25:           matcher.negative_failure_message = "given block should alter the block attached to change"
26:           comparison
27:         end
28:       end
eql(*obj) click to toggle source

Calls eql? on the given object (i.e., are the objects the same value?)

Examples

  
   1.should_not eql(1.0)
   (12 / 6).should eql(6)
    # File lib/matchy/built_in/truth_expectations.rb, line 55
55:       def eql(*obj)
56:         ask_for(:eql, :with_arg => obj)
57:       end
equal(*obj) click to toggle source

Calls equal? on the given object (i.e., do the two objects have the same object_id?)

Examples

  x = [1,2,3]
  y = [1,2,3]

  # Different object_id's...
  x.should_not equal(y)

  # The same object_id
  x[0].should equal(y[0])
    # File lib/matchy/built_in/truth_expectations.rb, line 72
72:       def equal(*obj)
73:         ask_for(:equal, :with_arg => obj)
74:       end
exclude(*obj) click to toggle source

Expects the receiver to exclude the given object(s). You can provide multiple arguments to see if all of them are included.

Examples

  
  [1,2,3].should exclude(16)
  [7,8,8].should_not exclude(7)
  ['a', 'b', 'c'].should exclude('e', 'f', 'g')
    # File lib/matchy/built_in/enumerable_expectations.rb, line 27
27:       def exclude(*obj)
28:         _clude(:exclude, obj)
29:       end
exist() click to toggle source

Calls exist? on the given object.

Examples

  # found_user.exist?
  found_user.should exist
    # File lib/matchy/built_in/truth_expectations.rb, line 44
44:       def exist
45:         ask_for(:exist, :with_arg => nil)
46:       end
include(*obj) click to toggle source

Calls include? on the receiver for any object. You can also provide multiple arguments to see if all of them are included.

Examples

  
  [1,2,3].should include(1)
  [7,8,8].should_not include(3)
  ['a', 'b', 'c'].should include('a', 'c')
    # File lib/matchy/built_in/enumerable_expectations.rb, line 14
14:       def include(*obj)
15:         _clude(:include, obj)
16:       end
method_missing(name, *args, &block) click to toggle source

be_*something(*args)

This method_missing acts as a matcher builder.

If a call to be_xyz() reaches this method_missing (say: obj.should be_xyz), a matcher with the name xyz will be built, whose defining property is that it returns the value of obj.xyz? for matches?.

Examples

  nil.should be_nil
  17.should be_kind_of(Fixnum)
  obj.something? #=> true
  obj.should be_something
     # File lib/matchy/built_in/truth_expectations.rb, line 127
127:       def method_missing(name, *args, &block)
128:         if (name.to_s =~ /^be_(.+)/)
129:           ask_for($1, :with_arg => args)
130:         else
131:           old_missing(name, *args, &block)
132:         end
133:       end
Also aliased as: old_missing
old_missing(name, *args, &block) click to toggle source
Alias for: method_missing
raise_error(*obj) click to toggle source

Expects a lambda to raise an error. You can specify the error or leave it blank to encompass any error.

Examples

  lambda { raise "FAILURE." }.should raise_error
  lambda { puts i_dont_exist }.should raise_error(NameError)
    # File lib/matchy/built_in/error_expectations.rb, line 12
12:       def raise_error(*obj)
13:         build_matcher(:raise_error, obj) do |receiver, matcher, args|
14:           expected = args[0] || Exception
15:           raised = false
16:           error = nil
17:           begin
18:             receiver.call
19:           rescue Exception => e
20:             raised = true
21:             error = e
22:           end
23:           if expected.respond_to?(:ancestors) && expected.ancestors.include?(Exception)
24:             matcher.positive_failure_message = "Expected #{receiver.inspect} to raise #{expected.name}, " + 
25:               (error ? "but #{error.class.name} was raised instead." : "but none was raised.")
26:             matcher.negative_failure_message = "Expected #{receiver.inspect} to not raise #{expected.name}."
27:             comparison = (raised && error.class.ancestors.include?(expected))
28:           else
29:             message = error ? error.message : "none"
30:             matcher.positive_failure_message = "Expected #{receiver.inspect} to raise error with message matching '#{expected}', but '#{message}' was raised."
31:             matcher.negative_failure_message = "Expected #{receiver.inspect} to raise error with message not matching '#{expected}', but '#{message}' was raised."
32:             comparison = (raised && (expected.kind_of?(Regexp) ? ((error.message =~ expected) ? true : false) : expected == error.message))
33:           end
34:           comparison
35:         end
36:       end
respond_to(*meth) click to toggle source

Checks if the given object responds to the given method

Examples

  "foo".should respond_to(:length)
  {}.should respond_to(:has_key?)
     # File lib/matchy/built_in/truth_expectations.rb, line 99
 99:       def respond_to(*meth)
100:         ask_for(:respond_to, :with_arg => meth)
101:       end
satisfy(*obj) click to toggle source

A last ditch way to implement your testing logic. You probably shouldn’t use this unless you have to.

Examples

  (13 - 4).should satisfy(lambda {|i| i < 20})
  "hello".should_not satisfy(lambda {|s| s =~ /hi/})
    # File lib/matchy/built_in/truth_expectations.rb, line 84
84:       def satisfy(*obj)
85:         build_matcher(:satisfy, obj) do |receiver, matcher, args|
86:           @receiver, expected = receiver, args[0]
87:           matcher.positive_failure_message = "Expected #{@receiver.inspect} to satisfy given block."
88:           matcher.negative_failure_message = "Expected #{@receiver.inspect} to not satisfy given block."
89:           expected.call(@receiver) == true
90:         end
91:       end
throw_symbol(*obj) click to toggle source

Expects a lambda to throw an error.

Examples

  lambda { throw :thing }.should throw_symbol(:thing)
  lambda { "not this time" }.should_not throw_symbol(:hello)
    # File lib/matchy/built_in/error_expectations.rb, line 45
45:       def throw_symbol(*obj)
46:         build_matcher(:throw_symbol, obj) do |receiver, matcher, args|
47:           raised, thrown_symbol, expected = false, nil, args[0]
48:           begin
49:             receiver.call
50:           rescue NameError => e
51:             raise e unless e.message =~ /uncaught throw/
52:             raised = true
53:             thrown_symbol = e.name.to_sym if e.respond_to?(:name)
54:           rescue ArgumentError => e
55:             raise e unless e.message =~ /uncaught throw/
56:             thrown_symbol = e.message.match(/uncaught throw :(.+)/)[1].to_sym
57:           end
58:           matcher.positive_failure_message = "Expected #{receiver.inspect} to throw :#{expected}, but " +
59:             "#{thrown_symbol ? ':' + thrown_symbol.to_s + ' was thrown instead' : 'no symbol was thrown'}."
60:           matcher.negative_failure_message = "Expected #{receiver.inspect} to not throw :#{expected}."
61:           expected == thrown_symbol
62:         end
63:       end

Private Instance Methods

_clude(sym, obj) click to toggle source
    # File lib/matchy/built_in/enumerable_expectations.rb, line 32
32:       def _clude(sym, obj)
33:         build_matcher(sym, obj) do |given, matcher, args|
34:           matcher.positive_failure_message = "Expected #{given.inspect} to #{sym} #{args.inspect}."
35:           matcher.negative_failure_message = "Expected #{given.inspect} to not #{sym} #{args.inspect}."
36:           args.inject(true) {|m,o| m && (given.include?(o) == (sym == :include)) }
37:         end
38:       end
ask_for(sym, option={}) click to toggle source
     # File lib/matchy/built_in/truth_expectations.rb, line 136
136:       def ask_for(sym, option={})
137:         build_matcher(sym, (option[:with_arg] || [])) do |receiver, matcher, args|
138:           expected, meth = args[0], (sym.to_s + "?" ).to_sym
139:           matcher.positive_failure_message = "Expected #{receiver.inspect} to return true for #{sym}?, with '#{(expected && expected.inspect) || 'no args'}'."
140:           matcher.negative_failure_message = "Expected #{receiver.inspect} to not return true for #{sym}?, with '#{(expected && expected.inspect) || 'no args'}'."
141:           expected ? receiver.send(meth, expected) : receiver.send(meth)
142:         end
143:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.