Module: Matchy::Expectations::TestCaseExtensions
- Defined in:
- lib/matchy/built_in/error_expectations.rb,
lib/matchy/built_in/truth_expectations.rb,
lib/matchy/built_in/enumerable_expectations.rb
Instance Method Summary collapse
-
#be(obj) ⇒ Object
Simply checks if the receiver matches the expected object.
-
#be_close(obj, delta = 0.3) ⇒ Object
Checks if the given object is within a given object and delta.
-
#be_kind_of(klass) ⇒ Object
Checks if the given object is kind_of? the expected class.
-
#eql(obj) ⇒ Object
Calls
eql?on the given object (i.e., are the objects the same value?). -
#equal(obj) ⇒ Object
Calls
equal?on the given object (i.e., do the two objects have the sameobject_id?). -
#exclude(*obj) ⇒ Object
Expects the receiver to exclude the given object(s).
-
#exist ⇒ Object
Calls
exist?on the given object. -
#include(*obj) ⇒ Object
Calls
include?on the receiver for any object. -
#raise_error(obj = StandardError) ⇒ Object
Expects a lambda to raise an error.
-
#respond_to(meth) ⇒ Object
Checks if the given object responds to the given method.
-
#satisfy(obj) ⇒ Object
A last ditch way to implement your testing logic.
-
#throw_symbol(obj) ⇒ Object
Expects a lambda to throw an error.
Instance Method Details
#be(obj) ⇒ Object
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)
144 145 146 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 144 def be(obj) Matchy::Expectations::BeExpectation.new(obj, self) end |
#be_close(obj, delta = 0.3) ⇒ Object
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)
165 166 167 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 165 def be_close(obj, delta = 0.3) Matchy::Expectations::BeCloseExpectation.new(obj, delta, self) end |
#be_kind_of(klass) ⇒ Object
Checks if the given object is kind_of? the expected class
Examples
"hello".should be_kind_of(String)
3.should be_kind_of(Fixnum)
154 155 156 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 154 def be_kind_of(klass) Matchy::Expectations::BeKindOfExpectation.new(klass, self) end |
#eql(obj) ⇒ Object
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)
187 188 189 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 187 def eql(obj) Matchy::Expectations::EqlExpectation.new(obj, self) end |
#equal(obj) ⇒ Object
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])
204 205 206 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 204 def equal(obj) Matchy::Expectations::EqualExpectation.new(obj, self) end |
#exclude(*obj) ⇒ Object
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')
64 65 66 |
# File 'lib/matchy/built_in/enumerable_expectations.rb', line 64 def exclude(*obj) Matchy::Expectations::ExcludeExpectation.new(obj, self) end |
#exist ⇒ Object
Calls exist? on the given object.
Examples
# found_user.exist?
found_user.should exist
176 177 178 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 176 def exist Matchy::Expectations::ExistExpectation.new(self) end |
#include(*obj) ⇒ Object
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')
51 52 53 |
# File 'lib/matchy/built_in/enumerable_expectations.rb', line 51 def include(*obj) Matchy::Expectations::IncludeExpectation.new(obj, self) end |
#raise_error(obj = StandardError) ⇒ Object
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)
74 75 76 |
# File 'lib/matchy/built_in/error_expectations.rb', line 74 def raise_error(obj = StandardError) Matchy::Expectations::RaiseErrorExpectation.new(obj, self) end |
#respond_to(meth) ⇒ Object
Checks if the given object responds to the given method
Examples
"foo".should respond_to(:length)
{}.should respond_to(:has_key?)
226 227 228 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 226 def respond_to(meth) Matchy::Expectations::RespondToExpectation.new(meth, self) end |
#satisfy(obj) ⇒ Object
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/})
216 217 218 |
# File 'lib/matchy/built_in/truth_expectations.rb', line 216 def satisfy(obj) Matchy::Expectations::SatisfyExpectation.new(obj, self) end |
#throw_symbol(obj) ⇒ Object
Expects a lambda to throw an error.
Examples
lambda { throw :thing }.should throw_symbol(:thing)
lambda { "not this time" }.should_not throw_symbol(:hello)
85 86 87 |
# File 'lib/matchy/built_in/error_expectations.rb', line 85 def throw_symbol(obj) Matchy::Expectations::ThrowSymbolExpectation.new(obj, self) end |