mocha 0.5.5 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +3 -1
- data/examples/misc.rb +44 -36
- data/examples/stubba.rb +1 -1
- data/lib/mocha/auto_verify.rb +38 -31
- data/lib/mocha/central.rb +1 -1
- data/lib/mocha/class_method.rb +5 -1
- data/lib/mocha/expectation.rb +63 -61
- data/lib/mocha/expectation_error.rb +9 -0
- data/lib/mocha/expectation_list.rb +11 -10
- data/lib/mocha/method_matcher.rb +21 -0
- data/lib/mocha/missing_expectation.rb +5 -15
- data/lib/mocha/mock.rb +28 -26
- data/lib/mocha/parameter_matchers.rb +17 -1
- data/lib/mocha/parameter_matchers/all_of.rb +6 -3
- data/lib/mocha/parameter_matchers/any_of.rb +6 -3
- data/lib/mocha/parameter_matchers/any_parameters.rb +40 -0
- data/lib/mocha/parameter_matchers/anything.rb +5 -2
- data/lib/mocha/parameter_matchers/base.rb +15 -0
- data/lib/mocha/parameter_matchers/equals.rb +42 -0
- data/lib/mocha/parameter_matchers/has_entries.rb +42 -0
- data/lib/mocha/parameter_matchers/has_entry.rb +20 -4
- data/lib/mocha/parameter_matchers/has_key.rb +5 -2
- data/lib/mocha/parameter_matchers/has_value.rb +5 -2
- data/lib/mocha/parameter_matchers/includes.rb +5 -2
- data/lib/mocha/parameter_matchers/instance_of.rb +5 -2
- data/lib/mocha/parameter_matchers/is_a.rb +42 -0
- data/lib/mocha/parameter_matchers/kind_of.rb +5 -2
- data/lib/mocha/parameter_matchers/not.rb +42 -0
- data/lib/mocha/parameter_matchers/object.rb +9 -0
- data/lib/mocha/parameter_matchers/optionally.rb +33 -0
- data/lib/mocha/parameter_matchers/regexp_matches.rb +5 -2
- data/lib/mocha/parameters_matcher.rb +37 -0
- data/lib/mocha/pretty_parameters.rb +1 -1
- data/lib/mocha/return_values.rb +7 -4
- data/lib/mocha/sequence.rb +42 -0
- data/lib/mocha/yield_parameters.rb +3 -3
- data/test/acceptance/expected_invocation_count_acceptance_test.rb +8 -8
- data/test/acceptance/mock_with_initializer_block_acceptance_test.rb +44 -0
- data/test/acceptance/optional_parameters_acceptance_test.rb +63 -0
- data/test/acceptance/parameter_matcher_acceptance_test.rb +38 -2
- data/test/acceptance/partial_mocks_acceptance_test.rb +40 -0
- data/test/acceptance/sequence_acceptance_test.rb +179 -0
- data/test/integration/mocha_test_result_integration_test.rb +3 -3
- data/test/integration/stubba_integration_test.rb +2 -2
- data/test/integration/stubba_test_result_integration_test.rb +2 -2
- data/test/test_runner.rb +2 -2
- data/test/unit/any_instance_method_test.rb +2 -0
- data/test/unit/auto_verify_test.rb +10 -3
- data/test/unit/central_test.rb +1 -1
- data/test/unit/class_method_test.rb +4 -0
- data/test/unit/expectation_error_test.rb +24 -0
- data/test/unit/expectation_list_test.rb +6 -0
- data/test/unit/expectation_test.rb +111 -27
- data/test/unit/method_matcher_test.rb +23 -0
- data/test/unit/missing_expectation_test.rb +24 -27
- data/test/unit/mock_test.rb +29 -22
- data/test/unit/object_inspect_test.rb +4 -2
- data/test/unit/parameter_matchers/all_of_test.rb +2 -2
- data/test/unit/parameter_matchers/any_of_test.rb +2 -2
- data/test/unit/parameter_matchers/anything_test.rb +2 -2
- data/test/unit/parameter_matchers/has_entries_test.rb +30 -0
- data/test/unit/parameter_matchers/has_entry_test.rb +20 -5
- data/test/unit/parameter_matchers/has_key_test.rb +2 -2
- data/test/unit/parameter_matchers/has_value_test.rb +2 -2
- data/test/unit/parameter_matchers/includes_test.rb +2 -2
- data/test/unit/parameter_matchers/instance_of_test.rb +2 -2
- data/test/unit/parameter_matchers/is_a_test.rb +25 -0
- data/test/unit/parameter_matchers/kind_of_test.rb +3 -3
- data/test/unit/parameter_matchers/not_test.rb +26 -0
- data/test/unit/parameter_matchers/regexp_matches_test.rb +2 -2
- data/test/unit/parameter_matchers/stub_matcher.rb +2 -1
- data/test/unit/parameters_matcher_test.rb +121 -0
- data/test/unit/sequence_test.rb +104 -0
- metadata +35 -6
- data/test/unit/pretty_parameters_test.rb +0 -32
@@ -11,7 +11,7 @@ class ObjectInspectTest < Test::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
object.attribute = 'instance_variable'
|
13
13
|
assert_match Regexp.new("^#<Object:0x[0-9A-Fa-f]{1,8}.*>$"), object.mocha_inspect
|
14
|
-
assert_no_match
|
14
|
+
assert_no_match(/instance_variable/, object.mocha_inspect)
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_should_return_customized_string_representation_of_object
|
@@ -27,7 +27,9 @@ class ObjectInspectTest < Test::Unit::TestCase
|
|
27
27
|
object.define_instance_accessor(:called)
|
28
28
|
object.called = false
|
29
29
|
object.replace_instance_method(:object_id) { self.called = true; 1 }
|
30
|
-
|
30
|
+
if RUBY_VERSION < '1.9'
|
31
|
+
object.replace_instance_method(:id) { self.called = true; 1 }
|
32
|
+
end
|
31
33
|
object.mocha_inspect
|
32
34
|
assert_equal false, object.called
|
33
35
|
end
|
@@ -10,12 +10,12 @@ class AllOfTest < Test::Unit::TestCase
|
|
10
10
|
|
11
11
|
def test_should_match_if_all_matchers_match
|
12
12
|
matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(true), Stub::Matcher.new(true))
|
13
|
-
assert matcher
|
13
|
+
assert matcher.matches?(['any_old_value'])
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_should_not_match_if_any_matcher_does_not_match
|
17
17
|
matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(false), Stub::Matcher.new(true))
|
18
|
-
assert matcher
|
18
|
+
assert !matcher.matches?(['any_old_value'])
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_should_describe_matcher
|
@@ -10,12 +10,12 @@ class AnyOfTest < Test::Unit::TestCase
|
|
10
10
|
|
11
11
|
def test_should_match_if_any_matchers_match
|
12
12
|
matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(true), Stub::Matcher.new(false))
|
13
|
-
assert matcher
|
13
|
+
assert matcher.matches?(['any_old_value'])
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_should_not_match_if_no_matchers_match
|
17
17
|
matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(false), Stub::Matcher.new(false))
|
18
|
-
assert matcher
|
18
|
+
assert !matcher.matches?(['any_old_value'])
|
19
19
|
end
|
20
20
|
|
21
21
|
def test_should_describe_matcher
|
@@ -9,8 +9,8 @@ class AnythingTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_anything
|
11
11
|
matcher = anything
|
12
|
-
assert matcher
|
13
|
-
assert matcher
|
12
|
+
assert matcher.matches?([:something])
|
13
|
+
assert matcher.matches?([{'x' => 'y'}])
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_should_describe_matcher
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
|
3
|
+
require 'mocha/parameter_matchers/has_entries'
|
4
|
+
require 'mocha/inspect'
|
5
|
+
|
6
|
+
class HasEntriesTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Mocha::ParameterMatchers
|
9
|
+
|
10
|
+
def test_should_match_hash_including_specified_entries
|
11
|
+
matcher = has_entries(:key_1 => 'value_1', :key_2 => 'value_2')
|
12
|
+
assert matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2', :key_3 => 'value_3' }])
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_not_match_hash_not_including_specified_entries
|
16
|
+
matcher = has_entries(:key_1 => 'value_2', :key_2 => 'value_2', :key_3 => 'value_3')
|
17
|
+
assert !matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_describe_matcher
|
21
|
+
matcher = has_entries(:key_1 => 'value_1', :key_2 => 'value_2')
|
22
|
+
description = matcher.mocha_inspect
|
23
|
+
matches = /has_entries\((.*)\)/.match(description)
|
24
|
+
assert_not_nil matches[0]
|
25
|
+
entries = eval(matches[1])
|
26
|
+
assert_equal 'value_1', entries[:key_1]
|
27
|
+
assert_equal 'value_2', entries[:key_2]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -7,19 +7,34 @@ class HasEntryTest < Test::Unit::TestCase
|
|
7
7
|
|
8
8
|
include Mocha::ParameterMatchers
|
9
9
|
|
10
|
-
def
|
10
|
+
def test_should_match_hash_including_specified_key_value_pair
|
11
11
|
matcher = has_entry(:key_1, 'value_1')
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def test_should_not_match_hash_not_including_specified_key_value_pair
|
16
16
|
matcher = has_entry(:key_1, 'value_2')
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_match_hash_including_specified_entry
|
21
|
+
matcher = has_entry(:key_1 => 'value_1')
|
22
|
+
assert matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
18
23
|
end
|
19
24
|
|
20
|
-
def
|
25
|
+
def test_should_not_match_hash_not_including_specified_entry
|
26
|
+
matcher = has_entry(:key_1 => 'value_2')
|
27
|
+
assert !matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_should_describe_matcher_with_key_value_pair
|
21
31
|
matcher = has_entry(:key_1, 'value_1')
|
22
32
|
assert_equal "has_entry(:key_1, 'value_1')", matcher.mocha_inspect
|
23
33
|
end
|
24
34
|
|
35
|
+
def test_should_describe_matcher_with_entry
|
36
|
+
matcher = has_entry(:key_1 => 'value_1')
|
37
|
+
assert_equal "has_entry(:key_1, 'value_1')", matcher.mocha_inspect
|
38
|
+
end
|
39
|
+
|
25
40
|
end
|
@@ -9,12 +9,12 @@ class HasKeyTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_hash_including_specified_key
|
11
11
|
matcher = has_key(:key_1)
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?([{ :key_1 => 1, :key_2 => 2 }])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_hash_not_including_specified_key
|
16
16
|
matcher = has_key(:key_1)
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?([{ :key_2 => 2 }])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -9,12 +9,12 @@ class HasValueTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_hash_including_specified_value
|
11
11
|
matcher = has_value('value_1')
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?([{ :key_1 => 'value_1', :key_2 => 'value_2' }])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_hash_not_including_specified_value
|
16
16
|
matcher = has_value('value_1')
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?([{ :key_2 => 'value_2' }])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -9,12 +9,12 @@ class IncludesTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_object_including_value
|
11
11
|
matcher = includes(:x)
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?([[:x, :y, :z]])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_object_that_does_not_include_value
|
16
16
|
matcher = includes(:not_included)
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?([[:x, :y, :z]])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -9,12 +9,12 @@ class InstanceOfTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_object_that_is_an_instance_of_specified_class
|
11
11
|
matcher = instance_of(String)
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?(['string'])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_object_that_is_not_an_instance_of_specified_class
|
16
16
|
matcher = instance_of(String)
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?([99])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
|
3
|
+
require 'mocha/parameter_matchers/is_a'
|
4
|
+
require 'mocha/inspect'
|
5
|
+
|
6
|
+
class IsATest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
include Mocha::ParameterMatchers
|
9
|
+
|
10
|
+
def test_should_match_object_that_is_a_specified_class
|
11
|
+
matcher = is_a(Integer)
|
12
|
+
assert matcher.matches?([99])
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_not_match_object_that_is_not_a_specified_class
|
16
|
+
matcher = is_a(Integer)
|
17
|
+
assert !matcher.matches?(['string'])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_describe_matcher
|
21
|
+
matcher = is_a(Integer)
|
22
|
+
assert_equal "is_a(Integer)", matcher.mocha_inspect
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -3,18 +3,18 @@ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
|
3
3
|
require 'mocha/parameter_matchers/kind_of'
|
4
4
|
require 'mocha/inspect'
|
5
5
|
|
6
|
-
class
|
6
|
+
class KindOfTest < Test::Unit::TestCase
|
7
7
|
|
8
8
|
include Mocha::ParameterMatchers
|
9
9
|
|
10
10
|
def test_should_match_object_that_is_a_kind_of_specified_class
|
11
11
|
matcher = kind_of(Integer)
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?([99])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_object_that_is_not_a_kind_of_specified_class
|
16
16
|
matcher = kind_of(Integer)
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?(['string'])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
|
2
|
+
|
3
|
+
require 'mocha/parameter_matchers/not'
|
4
|
+
require 'mocha/inspect'
|
5
|
+
require 'stub_matcher'
|
6
|
+
|
7
|
+
class NotTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
include Mocha::ParameterMatchers
|
10
|
+
|
11
|
+
def test_should_match_if_matcher_does_not_match
|
12
|
+
matcher = Not(Stub::Matcher.new(false))
|
13
|
+
assert matcher.matches?(['any_old_value'])
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_should_not_match_if_matcher_does_match
|
17
|
+
matcher = Not(Stub::Matcher.new(true))
|
18
|
+
assert !matcher.matches?(['any_old_value'])
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_should_describe_matcher
|
22
|
+
matcher = Not(Stub::Matcher.new(true))
|
23
|
+
assert_equal 'Not(matcher(true))', matcher.mocha_inspect
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -9,12 +9,12 @@ class MatchesTest < Test::Unit::TestCase
|
|
9
9
|
|
10
10
|
def test_should_match_parameter_matching_regular_expression
|
11
11
|
matcher = regexp_matches(/oo/)
|
12
|
-
assert matcher
|
12
|
+
assert matcher.matches?(['foo'])
|
13
13
|
end
|
14
14
|
|
15
15
|
def test_should_not_match_parameter_not_matching_regular_expression
|
16
16
|
matcher = regexp_matches(/oo/)
|
17
|
-
assert matcher
|
17
|
+
assert !matcher.matches?(['bar'])
|
18
18
|
end
|
19
19
|
|
20
20
|
def test_should_describe_matcher
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/parameters_matcher'
|
3
|
+
|
4
|
+
class ParametersMatcherTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include Mocha
|
7
|
+
|
8
|
+
def test_should_match_any_actual_parameters_if_no_expected_parameters_specified
|
9
|
+
parameters_matcher = ParametersMatcher.new
|
10
|
+
assert parameters_matcher.match?(actual_parameters = [1, 2, 3])
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_match_if_actual_parameters_are_same_as_expected_parameters
|
14
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, 6])
|
15
|
+
assert parameters_matcher.match?(actual_parameters = [4, 5, 6])
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_should_not_match_if_actual_parameters_are_different_from_expected_parameters
|
19
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, 6])
|
20
|
+
assert !parameters_matcher.match?(actual_parameters = [1, 2, 3])
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_should_not_match_if_there_are_less_actual_parameters_than_expected_parameters
|
24
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, 6])
|
25
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 5])
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_not_match_if_there_are_more_actual_parameters_than_expected_parameters
|
29
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5])
|
30
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 5, 6])
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_not_match_if_not_all_required_parameters_are_supplied
|
34
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
35
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
36
|
+
assert !parameters_matcher.match?(actual_parameters = [4])
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_match_if_all_required_parameters_match_and_no_optional_parameters_are_supplied
|
40
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
41
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
42
|
+
assert parameters_matcher.match?(actual_parameters = [4, 5])
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_should_match_if_all_required_and_optional_parameters_match_and_some_optional_parameters_are_supplied
|
46
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
47
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
48
|
+
assert parameters_matcher.match?(actual_parameters = [4, 5, 6])
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_should_match_if_all_required_and_optional_parameters_match_and_all_optional_parameters_are_supplied
|
52
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
53
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
54
|
+
assert parameters_matcher.match?(actual_parameters = [4, 5, 6, 7])
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_not_match_if_all_required_and_optional_parameters_match_but_too_many_optional_parameters_are_supplied
|
58
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
59
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
60
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 5, 6, 7, 8])
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_not_match_if_all_required_parameters_match_but_some_optional_parameters_do_not_match
|
64
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
65
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
66
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 5, 6, 0])
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_should_not_match_if_some_required_parameters_do_not_match_although_all_optional_parameters_do_match
|
70
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
71
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
72
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 0, 6])
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_should_not_match_if_all_required_parameters_match_but_no_optional_parameters_match
|
76
|
+
optionals = ParameterMatchers::Optionally.new(6, 7)
|
77
|
+
parameters_matcher = ParametersMatcher.new(expected_parameters = [4, 5, optionals])
|
78
|
+
assert !parameters_matcher.match?(actual_parameters = [4, 5, 0, 0])
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_should_match_if_actual_parameters_satisfy_matching_block
|
82
|
+
parameters_matcher = ParametersMatcher.new { |x, y| x + y == 3 }
|
83
|
+
assert parameters_matcher.match?(actual_parameters = [1, 2])
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_should_not_match_if_actual_parameters_do_not_satisfy_matching_block
|
87
|
+
parameters_matcher = ParametersMatcher.new { |x, y| x + y == 3 }
|
88
|
+
assert !parameters_matcher.match?(actual_parameters = [2, 3])
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_should_remove_outer_array_braces
|
92
|
+
params = [1, 2, [3, 4]]
|
93
|
+
parameters_matcher = ParametersMatcher.new(params)
|
94
|
+
assert_equal '(1, 2, [3, 4])', parameters_matcher.mocha_inspect
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_should_display_numeric_arguments_as_is
|
98
|
+
params = [1, 2, 3]
|
99
|
+
parameters_matcher = ParametersMatcher.new(params)
|
100
|
+
assert_equal '(1, 2, 3)', parameters_matcher.mocha_inspect
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_should_remove_curly_braces_if_hash_is_only_argument
|
104
|
+
params = [{:a => 1, :z => 2}]
|
105
|
+
parameters_matcher = ParametersMatcher.new(params)
|
106
|
+
assert_nil parameters_matcher.mocha_inspect.index('{')
|
107
|
+
assert_nil parameters_matcher.mocha_inspect.index('}')
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_should_not_remove_curly_braces_if_hash_is_not_the_only_argument
|
111
|
+
params = [1, {:a => 1}]
|
112
|
+
parameters_matcher = ParametersMatcher.new(params)
|
113
|
+
assert_equal '(1, {:a => 1})', parameters_matcher.mocha_inspect
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_should_indicate_that_matcher_will_match_any_actual_parameters
|
117
|
+
parameters_matcher = ParametersMatcher.new
|
118
|
+
assert_equal '(any_parameters)', parameters_matcher.mocha_inspect
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/sequence'
|
3
|
+
require 'mocha/expectation'
|
4
|
+
|
5
|
+
class SequenceTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mocha
|
8
|
+
|
9
|
+
class FakeExpectation
|
10
|
+
|
11
|
+
attr_reader :ordering_constraints
|
12
|
+
|
13
|
+
def initialize(satisfied = false)
|
14
|
+
@satisfied = satisfied
|
15
|
+
@ordering_constraints = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_ordering_constraint(ordering_constraint)
|
19
|
+
@ordering_constraints << ordering_constraint
|
20
|
+
end
|
21
|
+
|
22
|
+
def satisfied?
|
23
|
+
@satisfied
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_be_satisfied_if_no_expectations_added
|
29
|
+
sequence = Sequence.new('name')
|
30
|
+
assert sequence.satisfied_to_index?(0)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_should_be_satisfied_if_one_unsatisfied_expectations_added_but_it_is_not_included_by_index
|
34
|
+
sequence = Sequence.new('name')
|
35
|
+
expectation = FakeExpectation.new(satisfied = false)
|
36
|
+
sequence.constrain_as_next_in_sequence(expectation)
|
37
|
+
assert sequence.satisfied_to_index?(0)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_should_not_be_satisfied_if_one_unsatisfied_expectations_added_and_it_is_included_by_index
|
41
|
+
sequence = Sequence.new('name')
|
42
|
+
expectation = FakeExpectation.new(satisfied = false)
|
43
|
+
sequence.constrain_as_next_in_sequence(expectation)
|
44
|
+
assert !sequence.satisfied_to_index?(1)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_be_satisfied_if_one_satisfied_expectations_added_and_it_is_included_by_index
|
48
|
+
sequence = Sequence.new('name')
|
49
|
+
expectation = FakeExpectation.new(satisfied = true)
|
50
|
+
sequence.constrain_as_next_in_sequence(expectation)
|
51
|
+
assert sequence.satisfied_to_index?(1)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_should_not_be_satisfied_if_one_satisfied_and_one_unsatisfied_expectation_added_and_both_are_included_by_index
|
55
|
+
sequence = Sequence.new('name')
|
56
|
+
expectation_one = FakeExpectation.new(satisfied = true)
|
57
|
+
expectation_two = FakeExpectation.new(satisfied = false)
|
58
|
+
sequence.constrain_as_next_in_sequence(expectation_one)
|
59
|
+
sequence.constrain_as_next_in_sequence(expectation_two)
|
60
|
+
assert !sequence.satisfied_to_index?(2)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_should_be_satisfied_if_two_satisfied_expectations_added_and_both_are_included_by_index
|
64
|
+
sequence = Sequence.new('name')
|
65
|
+
expectation_one = FakeExpectation.new(satisfied = true)
|
66
|
+
expectation_two = FakeExpectation.new(satisfied = true)
|
67
|
+
sequence.constrain_as_next_in_sequence(expectation_one)
|
68
|
+
sequence.constrain_as_next_in_sequence(expectation_two)
|
69
|
+
assert sequence.satisfied_to_index?(2)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_add_ordering_constraint_to_expectation
|
73
|
+
sequence = Sequence.new('name')
|
74
|
+
expectation = FakeExpectation.new
|
75
|
+
sequence.constrain_as_next_in_sequence(expectation)
|
76
|
+
assert_equal 1, expectation.ordering_constraints.length
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_should_not_allow_invocation_of_second_method_when_first_n_sequence_has_not_been_invoked
|
80
|
+
sequence = Sequence.new('name')
|
81
|
+
expectation_one = FakeExpectation.new(satisfied = false)
|
82
|
+
expectation_two = FakeExpectation.new(satisfied = false)
|
83
|
+
sequence.constrain_as_next_in_sequence(expectation_one)
|
84
|
+
sequence.constrain_as_next_in_sequence(expectation_two)
|
85
|
+
assert !expectation_two.ordering_constraints[0].allows_invocation_now?
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_should_allow_invocation_of_second_method_when_first_in_sequence_has_been_invoked
|
89
|
+
sequence = Sequence.new('name')
|
90
|
+
expectation_one = FakeExpectation.new(satisfied = true)
|
91
|
+
expectation_two = FakeExpectation.new(satisfied = false)
|
92
|
+
sequence.constrain_as_next_in_sequence(expectation_one)
|
93
|
+
sequence.constrain_as_next_in_sequence(expectation_two)
|
94
|
+
assert expectation_two.ordering_constraints[0].allows_invocation_now?
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_should_describe_ordering_constraint_as_being_part_of_named_sequence
|
98
|
+
sequence = Sequence.new('wibble')
|
99
|
+
expectation = FakeExpectation.new
|
100
|
+
sequence.constrain_as_next_in_sequence(expectation)
|
101
|
+
assert_equal "in sequence 'wibble'", expectation.ordering_constraints[0].mocha_inspect
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|