-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
record result of invocation before 'returning' it
This helps us avoid rescuing an exception raised by code elsewhere in the Mocha code. Also, enables us to handle the missed scenario where Expectaction#throws is called, 'coz without it, we'd need a generic way to catch any thrown tag and that might be quite awkward.
- Loading branch information
Showing
9 changed files
with
68 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,45 @@ | ||
require File.expand_path('../../test_helper', __FILE__) | ||
|
||
require 'mocha/invocation' | ||
require 'mocha/exception_raiser' | ||
require 'timeout' | ||
|
||
class ExceptionRaiserTest < Mocha::TestCase | ||
include Mocha | ||
|
||
def new_invocation | ||
Invocation.new(:irrelevant) | ||
end | ||
|
||
def test_should_raise_exception_with_specified_class_and_default_message | ||
exception_class = Class.new(StandardError) | ||
raiser = ExceptionRaiser.new(exception_class, nil) | ||
exception = assert_raises(exception_class) { raiser.evaluate } | ||
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } | ||
assert_equal exception_class.to_s, exception.message | ||
end | ||
|
||
def test_should_raise_exception_with_specified_class_and_message | ||
exception_class = Class.new(StandardError) | ||
raiser = ExceptionRaiser.new(exception_class, 'message') | ||
exception = assert_raises(exception_class) { raiser.evaluate } | ||
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } | ||
assert_equal 'message', exception.message | ||
end | ||
|
||
def test_should_raise_exception_instance | ||
exception_class = Class.new(StandardError) | ||
raiser = ExceptionRaiser.new(exception_class.new('message'), nil) | ||
exception = assert_raises(exception_class) { raiser.evaluate } | ||
exception = assert_raises(exception_class) { raiser.evaluate(new_invocation) } | ||
assert_equal 'message', exception.message | ||
end | ||
|
||
def test_should_raise_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6 | ||
raiser = ExceptionRaiser.new(Interrupt, nil) | ||
assert_raises(Interrupt) { raiser.evaluate } | ||
assert_raises(Interrupt) { raiser.evaluate(new_invocation) } | ||
end | ||
|
||
def test_should_raise_subclass_of_interrupt_exception_with_default_message_so_it_works_in_ruby_1_8_6 | ||
exception_class = Class.new(Interrupt) | ||
raiser = ExceptionRaiser.new(exception_class, nil) | ||
assert_raises(exception_class) { raiser.evaluate } | ||
assert_raises(exception_class) { raiser.evaluate(new_invocation) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,66 @@ | ||
require File.expand_path('../../test_helper', __FILE__) | ||
|
||
require 'mocha/invocation' | ||
require 'mocha/return_values' | ||
|
||
class ReturnValuesTest < Mocha::TestCase | ||
include Mocha | ||
|
||
def new_invocation | ||
Invocation.new(:irrelevant) | ||
end | ||
|
||
def test_should_return_nil | ||
values = ReturnValues.new | ||
assert_nil values.next | ||
assert_nil values.next(new_invocation) | ||
end | ||
|
||
def test_should_keep_returning_nil | ||
values = ReturnValues.new | ||
values.next | ||
assert_nil values.next | ||
assert_nil values.next | ||
values.next(new_invocation) | ||
assert_nil values.next(new_invocation) | ||
assert_nil values.next(new_invocation) | ||
end | ||
|
||
def test_should_return_evaluated_single_return_value | ||
values = ReturnValues.new(SingleReturnValue.new('value')) | ||
assert_equal 'value', values.next | ||
assert_equal 'value', values.next(new_invocation) | ||
end | ||
|
||
def test_should_keep_returning_evaluated_single_return_value | ||
values = ReturnValues.new(SingleReturnValue.new('value')) | ||
values.next | ||
assert_equal 'value', values.next | ||
assert_equal 'value', values.next | ||
values.next(new_invocation) | ||
assert_equal 'value', values.next(new_invocation) | ||
assert_equal 'value', values.next(new_invocation) | ||
end | ||
|
||
def test_should_return_consecutive_evaluated_single_return_values | ||
values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2')) | ||
assert_equal 'value_1', values.next | ||
assert_equal 'value_2', values.next | ||
assert_equal 'value_1', values.next(new_invocation) | ||
assert_equal 'value_2', values.next(new_invocation) | ||
end | ||
|
||
def test_should_keep_returning_last_of_consecutive_evaluated_single_return_values | ||
values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2')) | ||
values.next | ||
values.next | ||
assert_equal 'value_2', values.next | ||
assert_equal 'value_2', values.next | ||
values.next(new_invocation) | ||
values.next(new_invocation) | ||
assert_equal 'value_2', values.next(new_invocation) | ||
assert_equal 'value_2', values.next(new_invocation) | ||
end | ||
|
||
def test_should_build_single_return_values_for_each_values | ||
values = ReturnValues.build('value_1', 'value_2', 'value_3').values | ||
assert_equal 'value_1', values[0].evaluate | ||
assert_equal 'value_2', values[1].evaluate | ||
assert_equal 'value_3', values[2].evaluate | ||
assert_equal 'value_1', values[0].evaluate(new_invocation) | ||
assert_equal 'value_2', values[1].evaluate(new_invocation) | ||
assert_equal 'value_3', values[2].evaluate(new_invocation) | ||
end | ||
|
||
def test_should_combine_two_sets_of_return_values | ||
values1 = ReturnValues.build('value_1') | ||
values2 = ReturnValues.build('value_2a', 'value_2b') | ||
values = (values1 + values2).values | ||
assert_equal 'value_1', values[0].evaluate | ||
assert_equal 'value_2a', values[1].evaluate | ||
assert_equal 'value_2b', values[2].evaluate | ||
assert_equal 'value_1', values[0].evaluate(new_invocation) | ||
assert_equal 'value_2a', values[1].evaluate(new_invocation) | ||
assert_equal 'value_2b', values[2].evaluate(new_invocation) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
require File.expand_path('../../test_helper', __FILE__) | ||
|
||
require 'mocha/invocation' | ||
require 'mocha/single_return_value' | ||
|
||
class SingleReturnValueTest < Mocha::TestCase | ||
include Mocha | ||
|
||
def new_invocation | ||
Invocation.new(:irrelevant) | ||
end | ||
|
||
def test_should_return_value | ||
value = SingleReturnValue.new('value') | ||
assert_equal 'value', value.evaluate | ||
assert_equal 'value', value.evaluate(new_invocation) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,23 @@ | ||
require File.expand_path('../../test_helper', __FILE__) | ||
|
||
require 'mocha/invocation' | ||
require 'mocha/thrower' | ||
|
||
class ThrowerTest < Mocha::TestCase | ||
include Mocha | ||
|
||
def new_invocation | ||
Invocation.new(:irrelevant) | ||
end | ||
|
||
def test_should_throw_tag | ||
thrower = Thrower.new(:tag) | ||
assert_throws(:tag) { thrower.evaluate } | ||
assert_throws(:tag) { thrower.evaluate(new_invocation) } | ||
end | ||
|
||
def test_should_throw_tag_with_return_value | ||
thrower = Thrower.new(:tag, 'return-value') | ||
return_value = catch(:tag) { thrower.evaluate } | ||
return_value = catch(:tag) { thrower.evaluate(new_invocation) } | ||
assert_equal 'return-value', return_value | ||
end | ||
end |