mocha 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/Rakefile +28 -10
  2. data/examples/stubba.rb +1 -1
  3. data/lib/mocha/auto_verify.rb +6 -6
  4. data/lib/mocha/deprecation.rb +22 -0
  5. data/lib/mocha/exception_raiser.rb +17 -0
  6. data/lib/mocha/expectation.rb +167 -84
  7. data/lib/mocha/infinite_range.rb +4 -6
  8. data/lib/mocha/inspect.rb +3 -1
  9. data/lib/mocha/is_a.rb +9 -0
  10. data/lib/mocha/missing_expectation.rb +27 -0
  11. data/lib/mocha/mock.rb +191 -5
  12. data/lib/mocha/multiple_yields.rb +20 -0
  13. data/lib/mocha/no_yields.rb +11 -0
  14. data/lib/mocha/object.rb +11 -1
  15. data/lib/mocha/parameter_matchers.rb +9 -0
  16. data/lib/mocha/parameter_matchers/all_of.rb +39 -0
  17. data/lib/mocha/parameter_matchers/any_of.rb +44 -0
  18. data/lib/mocha/parameter_matchers/anything.rb +30 -0
  19. data/lib/mocha/parameter_matchers/has_entry.rb +39 -0
  20. data/lib/mocha/parameter_matchers/has_key.rb +39 -0
  21. data/lib/mocha/parameter_matchers/has_value.rb +39 -0
  22. data/lib/mocha/parameter_matchers/includes.rb +37 -0
  23. data/lib/mocha/return_values.rb +31 -0
  24. data/lib/mocha/single_return_value.rb +24 -0
  25. data/lib/mocha/single_yield.rb +18 -0
  26. data/lib/mocha/standalone.rb +2 -0
  27. data/lib/mocha/stub.rb +18 -0
  28. data/lib/mocha/yield_parameters.rb +31 -0
  29. data/test/{mocha_acceptance_test.rb → acceptance/mocha_acceptance_test.rb} +1 -1
  30. data/test/acceptance/mocked_methods_dispatch_acceptance_test.rb +72 -0
  31. data/test/acceptance/parameter_matcher_acceptance_test.rb +63 -0
  32. data/test/{standalone_acceptance_test.rb → acceptance/standalone_acceptance_test.rb} +22 -1
  33. data/test/{stubba_acceptance_test.rb → acceptance/stubba_acceptance_test.rb} +1 -1
  34. data/test/deprecation_disabler.rb +15 -0
  35. data/test/{mocha_test_result_integration_test.rb → integration/mocha_test_result_integration_test.rb} +1 -1
  36. data/test/{stubba_integration_test.rb → integration/stubba_integration_test.rb} +1 -1
  37. data/test/{stubba_test_result_integration_test.rb → integration/stubba_test_result_integration_test.rb} +1 -1
  38. data/test/test_helper.rb +8 -0
  39. data/test/test_runner.rb +31 -0
  40. data/test/{mocha → unit}/any_instance_method_test.rb +0 -0
  41. data/test/unit/array_inspect_test.rb +16 -0
  42. data/test/{mocha → unit}/auto_verify_test.rb +0 -0
  43. data/test/{mocha → unit}/central_test.rb +0 -0
  44. data/test/{mocha → unit}/class_method_test.rb +0 -0
  45. data/test/unit/date_time_inspect_test.rb +21 -0
  46. data/test/unit/expectation_raiser_test.rb +28 -0
  47. data/test/{mocha → unit}/expectation_test.rb +105 -63
  48. data/test/unit/hash_inspect_test.rb +16 -0
  49. data/test/{mocha → unit}/infinite_range_test.rb +8 -5
  50. data/test/{mocha → unit}/metaclass_test.rb +0 -0
  51. data/test/unit/missing_expectation_test.rb +51 -0
  52. data/test/unit/mock_test.rb +351 -0
  53. data/test/unit/multiple_yields_test.rb +18 -0
  54. data/test/unit/no_yield_test.rb +18 -0
  55. data/test/unit/object_inspect_test.rb +35 -0
  56. data/test/{mocha → unit}/object_test.rb +0 -0
  57. data/test/unit/parameter_matchers/all_of_test.rb +26 -0
  58. data/test/unit/parameter_matchers/any_of_test.rb +26 -0
  59. data/test/unit/parameter_matchers/anything_test.rb +21 -0
  60. data/test/unit/parameter_matchers/has_entry_test.rb +25 -0
  61. data/test/unit/parameter_matchers/has_key_test.rb +25 -0
  62. data/test/unit/parameter_matchers/has_value_test.rb +25 -0
  63. data/test/unit/parameter_matchers/includes_test.rb +25 -0
  64. data/test/unit/parameter_matchers/stub_matcher.rb +22 -0
  65. data/test/{mocha → unit}/pretty_parameters_test.rb +0 -0
  66. data/test/unit/return_values_test.rb +63 -0
  67. data/test/{mocha → unit}/setup_and_teardown_test.rb +0 -0
  68. data/test/unit/single_return_value_test.rb +33 -0
  69. data/test/unit/single_yield_test.rb +18 -0
  70. data/test/unit/string_inspect_test.rb +11 -0
  71. data/test/unit/stub_test.rb +24 -0
  72. data/test/unit/yield_parameters_test.rb +93 -0
  73. metadata +117 -73
  74. data/lib/mocha/mock_methods.rb +0 -122
  75. data/test/all_tests.rb +0 -75
  76. data/test/mocha/inspect_test.rb +0 -90
  77. data/test/mocha/mock_methods_test.rb +0 -235
  78. data/test/mocha/mock_test.rb +0 -84
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/multiple_yields'
4
+
5
+ class MultipleYieldsTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ def test_should_provide_parameters_for_multiple_yields_in_single_invocation
10
+ parameter_group = MultipleYields.new([1, 2, 3], [4, 5])
11
+ parameter_groups = []
12
+ parameter_group.each do |parameters|
13
+ parameter_groups << parameters
14
+ end
15
+ assert_equal [[1, 2, 3], [4, 5]], parameter_groups
16
+ end
17
+
18
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/no_yields'
4
+
5
+ class NoYieldsTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ def test_should_provide_parameters_for_no_yields_in_single_invocation
10
+ parameter_group = NoYields.new
11
+ parameter_groups = []
12
+ parameter_group.each do |parameters|
13
+ parameter_groups << parameters
14
+ end
15
+ assert_equal [], parameter_groups
16
+ end
17
+
18
+ end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/inspect'
3
+ require 'method_definer'
4
+
5
+ class ObjectInspectTest < Test::Unit::TestCase
6
+
7
+ def test_should_return_default_string_representation_of_object_not_including_instance_variables
8
+ object = Object.new
9
+ class << object
10
+ attr_accessor :attribute
11
+ end
12
+ object.attribute = 'instance_variable'
13
+ assert_match Regexp.new("^#<Object:0x[0-9A-Fa-f]{1,8}.*>$"), object.mocha_inspect
14
+ assert_no_match /instance_variable/, object.mocha_inspect
15
+ end
16
+
17
+ def test_should_return_customized_string_representation_of_object
18
+ object = Object.new
19
+ class << object
20
+ define_method(:inspect) { 'custom_inspect' }
21
+ end
22
+ assert_equal 'custom_inspect', object.mocha_inspect
23
+ end
24
+
25
+ def test_should_use_underscored_id_instead_of_object_id_or_id_so_that_they_can_be_stubbed
26
+ object = Object.new
27
+ object.define_instance_accessor(:called)
28
+ object.called = false
29
+ object.replace_instance_method(:object_id) { self.called = true; 1 }
30
+ object.replace_instance_method(:id) { self.called = true; 1 }
31
+ object.mocha_inspect
32
+ assert_equal false, object.called
33
+ end
34
+
35
+ end
File without changes
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/all_of'
4
+ require 'mocha/inspect'
5
+ require 'stub_matcher'
6
+
7
+ class AllOfTest < Test::Unit::TestCase
8
+
9
+ include Mocha::ParameterMatchers
10
+
11
+ def test_should_match_if_all_matchers_match
12
+ matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(true), Stub::Matcher.new(true))
13
+ assert matcher == 'any_old_value'
14
+ end
15
+
16
+ def test_should_not_match_if_any_matcher_does_not_match
17
+ matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(false), Stub::Matcher.new(true))
18
+ assert matcher != 'any_old_value'
19
+ end
20
+
21
+ def test_should_describe_matcher
22
+ matcher = all_of(Stub::Matcher.new(true), Stub::Matcher.new(false), Stub::Matcher.new(true))
23
+ assert_equal 'all_of(matcher(true), matcher(false), matcher(true))', matcher.mocha_inspect
24
+ end
25
+
26
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/any_of'
4
+ require 'mocha/inspect'
5
+ require 'stub_matcher'
6
+
7
+ class AnyOfTest < Test::Unit::TestCase
8
+
9
+ include Mocha::ParameterMatchers
10
+
11
+ def test_should_match_if_any_matchers_match
12
+ matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(true), Stub::Matcher.new(false))
13
+ assert matcher == 'any_old_value'
14
+ end
15
+
16
+ def test_should_not_match_if_no_matchers_match
17
+ matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(false), Stub::Matcher.new(false))
18
+ assert matcher != 'any_old_value'
19
+ end
20
+
21
+ def test_should_describe_matcher
22
+ matcher = any_of(Stub::Matcher.new(false), Stub::Matcher.new(true), Stub::Matcher.new(false))
23
+ assert_equal 'any_of(matcher(false), matcher(true), matcher(false))', matcher.mocha_inspect
24
+ end
25
+
26
+ end
@@ -0,0 +1,21 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/anything'
4
+ require 'mocha/inspect'
5
+
6
+ class AnythingTest < Test::Unit::TestCase
7
+
8
+ include Mocha::ParameterMatchers
9
+
10
+ def test_should_match_anything
11
+ matcher = anything
12
+ assert matcher == :something
13
+ assert matcher == {'x' => 'y'}
14
+ end
15
+
16
+ def test_should_describe_matcher
17
+ matcher = anything
18
+ assert_equal "anything", matcher.mocha_inspect
19
+ end
20
+
21
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/has_entry'
4
+ require 'mocha/inspect'
5
+
6
+ class HasEntryTest < Test::Unit::TestCase
7
+
8
+ include Mocha::ParameterMatchers
9
+
10
+ def test_should_match_hash_including_specified_entry
11
+ matcher = has_entry(:key_1, 'value_1')
12
+ assert matcher == { :key_1 => 'value_1', :key_2 => 'value_2' }
13
+ end
14
+
15
+ def test_should_not_match_hash_not_including_specified_entry
16
+ matcher = has_entry(:key_1, 'value_2')
17
+ assert matcher != { :key_1 => 'value_1', :key_2 => 'value_2' }
18
+ end
19
+
20
+ def test_should_describe_matcher
21
+ matcher = has_entry(:key_1, 'value_1')
22
+ assert_equal "has_entry(:key_1, 'value_1')", matcher.mocha_inspect
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/has_key'
4
+ require 'mocha/inspect'
5
+
6
+ class HasKeyTest < Test::Unit::TestCase
7
+
8
+ include Mocha::ParameterMatchers
9
+
10
+ def test_should_match_hash_including_specified_key
11
+ matcher = has_key(:key_1)
12
+ assert matcher == { :key_1 => 1, :key_2 => 2 }
13
+ end
14
+
15
+ def test_should_not_match_hash_not_including_specified_key
16
+ matcher = has_key(:key_1)
17
+ assert matcher != { :key_2 => 2 }
18
+ end
19
+
20
+ def test_should_describe_matcher
21
+ matcher = has_key(:key)
22
+ assert_equal 'has_key(:key)', matcher.mocha_inspect
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/has_value'
4
+ require 'mocha/inspect'
5
+
6
+ class HasValueTest < Test::Unit::TestCase
7
+
8
+ include Mocha::ParameterMatchers
9
+
10
+ def test_should_match_hash_including_specified_value
11
+ matcher = has_value('value_1')
12
+ assert matcher == { :key_1 => 'value_1', :key_2 => 'value_2' }
13
+ end
14
+
15
+ def test_should_not_match_hash_not_including_specified_value
16
+ matcher = has_value('value_1')
17
+ assert matcher != { :key_2 => 'value_2' }
18
+ end
19
+
20
+ def test_should_describe_matcher
21
+ matcher = has_value('value_1')
22
+ assert_equal "has_value('value_1')", matcher.mocha_inspect
23
+ end
24
+
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.join(File.dirname(__FILE__), "..", "..", "test_helper")
2
+
3
+ require 'mocha/parameter_matchers/includes'
4
+ require 'mocha/inspect'
5
+
6
+ class IncludesTest < Test::Unit::TestCase
7
+
8
+ include Mocha::ParameterMatchers
9
+
10
+ def test_should_match_object_including_value
11
+ matcher = includes(:x)
12
+ assert matcher == [:x, :y, :z]
13
+ end
14
+
15
+ def test_should_not_match_object_that_does_not_include_value
16
+ matcher = includes(:not_included)
17
+ assert matcher != [:x, :y, :z]
18
+ end
19
+
20
+ def test_should_describe_matcher
21
+ matcher = includes(:x)
22
+ assert_equal "includes(:x)", matcher.mocha_inspect
23
+ end
24
+
25
+ end
@@ -0,0 +1,22 @@
1
+ module Stub
2
+
3
+ class Matcher
4
+
5
+ attr_accessor :value
6
+
7
+ def initialize(matches)
8
+ @matches = matches
9
+ end
10
+
11
+ def ==(value)
12
+ @value = value
13
+ @matches
14
+ end
15
+
16
+ def mocha_inspect
17
+ "matcher(#{@matches})"
18
+ end
19
+
20
+ end
21
+
22
+ end
File without changes
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/return_values'
4
+
5
+ class ReturnValuesTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ def test_should_return_nil
10
+ values = ReturnValues.new
11
+ assert_nil values.next
12
+ end
13
+
14
+ def test_should_keep_returning_nil
15
+ values = ReturnValues.new
16
+ values.next
17
+ assert_nil values.next
18
+ assert_nil values.next
19
+ end
20
+
21
+ def test_should_return_evaluated_single_return_value
22
+ values = ReturnValues.new(SingleReturnValue.new('value'))
23
+ assert_equal 'value', values.next
24
+ end
25
+
26
+ def test_should_keep_returning_evaluated_single_return_value
27
+ values = ReturnValues.new(SingleReturnValue.new('value'))
28
+ values.next
29
+ assert_equal 'value', values.next
30
+ assert_equal 'value', values.next
31
+ end
32
+
33
+ def test_should_return_consecutive_evaluated_single_return_values
34
+ values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2'))
35
+ assert_equal 'value_1', values.next
36
+ assert_equal 'value_2', values.next
37
+ end
38
+
39
+ def test_should_keep_returning_last_of_consecutive_evaluated_single_return_values
40
+ values = ReturnValues.new(SingleReturnValue.new('value_1'), SingleReturnValue.new('value_2'))
41
+ values.next
42
+ values.next
43
+ assert_equal 'value_2', values.next
44
+ assert_equal 'value_2', values.next
45
+ end
46
+
47
+ def test_should_build_single_return_values_for_each_values
48
+ values = ReturnValues.build('value_1', 'value_2', 'value_3').values
49
+ assert_equal 'value_1', values[0].evaluate
50
+ assert_equal 'value_2', values[1].evaluate
51
+ assert_equal 'value_3', values[2].evaluate
52
+ end
53
+
54
+ def test_should_combine_two_sets_of_return_values
55
+ values_1 = ReturnValues.build('value_1')
56
+ values_2 = ReturnValues.build('value_2a', 'value_2b')
57
+ values = (values_1 + values_2).values
58
+ assert_equal 'value_1', values[0].evaluate
59
+ assert_equal 'value_2a', values[1].evaluate
60
+ assert_equal 'value_2b', values[2].evaluate
61
+ end
62
+
63
+ end
File without changes
@@ -0,0 +1,33 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/single_return_value'
4
+ require 'deprecation_disabler'
5
+
6
+ class SingleReturnValueTest < Test::Unit::TestCase
7
+
8
+ include Mocha
9
+ include DeprecationDisabler
10
+
11
+ def test_should_return_value
12
+ value = SingleReturnValue.new('value')
13
+ assert_equal 'value', value.evaluate
14
+ end
15
+
16
+ def test_should_return_result_of_calling_proc
17
+ proc = lambda { 'value' }
18
+ value = SingleReturnValue.new(proc)
19
+ result = nil
20
+ disable_deprecations { result = value.evaluate }
21
+ assert_equal 'value', result
22
+ end
23
+
24
+ def test_should_indicate_deprecated_use_of_expectation_returns_method
25
+ proc = lambda {}
26
+ value = SingleReturnValue.new(proc)
27
+ Deprecation.messages = []
28
+ disable_deprecations { value.evaluate }
29
+ expected_message = "use of Expectation#returns with instance of Proc - see Expectation#returns RDoc for alternatives"
30
+ assert_equal [expected_message], Deprecation.messages
31
+ end
32
+
33
+ end
@@ -0,0 +1,18 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+
3
+ require 'mocha/single_yield'
4
+
5
+ class SingleYieldTest < Test::Unit::TestCase
6
+
7
+ include Mocha
8
+
9
+ def test_should_provide_parameters_for_single_yield_in_single_invocation
10
+ parameter_group = SingleYield.new(1, 2, 3)
11
+ parameter_groups = []
12
+ parameter_group.each do |parameters|
13
+ parameter_groups << parameters
14
+ end
15
+ assert_equal [[1, 2, 3]], parameter_groups
16
+ end
17
+
18
+ end
@@ -0,0 +1,11 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/inspect'
3
+
4
+ class StringInspectTest < Test::Unit::TestCase
5
+
6
+ def test_should_replace_escaped_quotes_with_single_quote
7
+ string = "my_string"
8
+ assert_equal "'my_string'", string.mocha_inspect
9
+ end
10
+
11
+ end
@@ -0,0 +1,24 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha/stub'
3
+
4
+ class StubTest < Test::Unit::TestCase
5
+
6
+ include Mocha
7
+
8
+ def test_should_always_verify_successfully
9
+ stub = Stub.new(nil, :expected_method)
10
+ assert stub.verify
11
+ stub.invoke
12
+ assert stub.verify
13
+ end
14
+
15
+ def test_should_match_successfully_for_any_number_of_invocations
16
+ stub = Stub.new(nil, :expected_method)
17
+ assert stub.match?(:expected_method)
18
+ stub.invoke
19
+ assert stub.match?(:expected_method)
20
+ stub.invoke
21
+ assert stub.match?(:expected_method)
22
+ end
23
+
24
+ end