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,30 @@
1
+ module Mocha
2
+
3
+ module ParameterMatchers
4
+
5
+ # :call-seq: anything -> parameter_matcher
6
+ #
7
+ # Matches any object.
8
+ # object = mock()
9
+ # object.expects(:method_1).with(anything)
10
+ # object.method_1('foo')
11
+ # # no error raised
12
+ def anything
13
+ Anything.new
14
+ end
15
+
16
+ class Anything # :nodoc:
17
+
18
+ def ==(parameter)
19
+ return true
20
+ end
21
+
22
+ def mocha_inspect
23
+ "anything"
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+
30
+ end
@@ -0,0 +1,39 @@
1
+ module Mocha
2
+
3
+ module ParameterMatchers
4
+
5
+ # :call-seq: has_entry(key, value) -> parameter_matcher
6
+ #
7
+ # Matches +Hash+ containing entry with +key+ and +value+.
8
+ # object = mock()
9
+ # object.expects(:method_1).with(has_entry('key_1', 1))
10
+ # object.method_1('key_1' => 1, 'key_2' => 2)
11
+ # # no error raised
12
+ #
13
+ # object = mock()
14
+ # object.expects(:method_1).with(has_entry('key_1', 1))
15
+ # object.method_1('key_1' => 2, 'key_2' => 1)
16
+ # # error raised, because method_1 was not called with Hash containing entry: 'key_1' => 1
17
+ def has_entry(key, value)
18
+ HasEntry.new(key, value)
19
+ end
20
+
21
+ class HasEntry # :nodoc:
22
+
23
+ def initialize(key, value)
24
+ @key, @value = key, value
25
+ end
26
+
27
+ def ==(parameter)
28
+ parameter[@key] == @value
29
+ end
30
+
31
+ def mocha_inspect
32
+ "has_entry(#{@key.mocha_inspect}, #{@value.mocha_inspect})"
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,39 @@
1
+ module Mocha
2
+
3
+ module ParameterMatchers
4
+
5
+ # :call-seq: has_key(key) -> parameter_matcher
6
+ #
7
+ # Matches +Hash+ containing +key+.
8
+ # object = mock()
9
+ # object.expects(:method_1).with(has_key('key_1'))
10
+ # object.method_1('key_1' => 1, 'key_2' => 2)
11
+ # # no error raised
12
+ #
13
+ # object = mock()
14
+ # object.expects(:method_1).with(has_key('key_1'))
15
+ # object.method_1('key_2' => 2)
16
+ # # error raised, because method_1 was not called with Hash containing key: 'key_1'
17
+ def has_key(key)
18
+ HasKey.new(key)
19
+ end
20
+
21
+ class HasKey # :nodoc:
22
+
23
+ def initialize(key)
24
+ @key = key
25
+ end
26
+
27
+ def ==(parameter)
28
+ parameter.keys.include?(@key)
29
+ end
30
+
31
+ def mocha_inspect
32
+ "has_key(#{@key.mocha_inspect})"
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,39 @@
1
+ module Mocha
2
+
3
+ module ParameterMatchers
4
+
5
+ # :call-seq: has_value(value) -> parameter_matcher
6
+ #
7
+ # Matches +Hash+ containing +value+.
8
+ # object = mock()
9
+ # object.expects(:method_1).with(has_value(1))
10
+ # object.method_1('key_1' => 1, 'key_2' => 2)
11
+ # # no error raised
12
+ #
13
+ # object = mock()
14
+ # object.expects(:method_1).with(has_value(1))
15
+ # object.method_1('key_2' => 2)
16
+ # # error raised, because method_1 was not called with Hash containing value: 1
17
+ def has_value(value)
18
+ HasValue.new(value)
19
+ end
20
+
21
+ class HasValue # :nodoc:
22
+
23
+ def initialize(value)
24
+ @value = value
25
+ end
26
+
27
+ def ==(parameter)
28
+ parameter.values.include?(@value)
29
+ end
30
+
31
+ def mocha_inspect
32
+ "has_value(#{@value.mocha_inspect})"
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,37 @@
1
+ module Mocha
2
+
3
+ module ParameterMatchers
4
+
5
+ # :call-seq: includes(item) -> parameter_matcher
6
+ #
7
+ # Matches any object that responds true to include?(item)
8
+ # object = mock()
9
+ # object.expects(:method_1).with(includes('foo'))
10
+ # object.method_1(['foo', 'bar'])
11
+ # # no error raised
12
+ #
13
+ # object.method_1(['baz'])
14
+ # # error raised, because ['baz'] does not include 'foo'.
15
+ def includes(item)
16
+ Includes.new(item)
17
+ end
18
+
19
+ class Includes # :nodoc:
20
+
21
+ def initialize(item)
22
+ @item = item
23
+ end
24
+
25
+ def ==(parameter)
26
+ return parameter.include?(@item)
27
+ end
28
+
29
+ def mocha_inspect
30
+ "includes(#{@item.mocha_inspect})"
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+
37
+ end
@@ -0,0 +1,31 @@
1
+ require 'mocha/single_return_value'
2
+
3
+ module Mocha # :nodoc:
4
+
5
+ class ReturnValues # :nodoc:
6
+
7
+ def self.build(*values)
8
+ new(*values.map { |value| SingleReturnValue.new(value) })
9
+ end
10
+
11
+ attr_accessor :values
12
+
13
+ def initialize(*values)
14
+ @values = values
15
+ end
16
+
17
+ def next
18
+ case @values.size
19
+ when 0: nil
20
+ when 1: @values.first.evaluate
21
+ else @values.shift.evaluate
22
+ end
23
+ end
24
+
25
+ def +(other)
26
+ self.class.new(*(@values + other.values))
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -0,0 +1,24 @@
1
+ require 'mocha/is_a'
2
+ require 'mocha/deprecation'
3
+
4
+ module Mocha # :nodoc:
5
+
6
+ class SingleReturnValue # :nodoc:
7
+
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def evaluate
13
+ if @value.__is_a__(Proc) then
14
+ message = 'use of Expectation#returns with instance of Proc - see Expectation#returns RDoc for alternatives'
15
+ Deprecation.warning(message)
16
+ @value.call
17
+ else
18
+ @value
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,18 @@
1
+ module Mocha # :nodoc:
2
+
3
+ class SingleYield # :nodoc:
4
+
5
+ attr_reader :parameters
6
+
7
+ def initialize(*parameters)
8
+ @parameters = parameters
9
+ end
10
+
11
+ def each
12
+ yield(@parameters)
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+
@@ -1,4 +1,5 @@
1
1
  require 'mocha/auto_verify'
2
+ require 'mocha/parameter_matchers'
2
3
  require 'mocha/setup_and_teardown'
3
4
 
4
5
  module Mocha
@@ -6,6 +7,7 @@ module Mocha
6
7
  module Standalone
7
8
 
8
9
  include AutoVerify
10
+ include ParameterMatchers
9
11
  include SetupAndTeardown
10
12
 
11
13
  def mocha_setup
data/lib/mocha/stub.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'mocha/expectation'
2
+
3
+ module Mocha # :nodoc:
4
+
5
+ class Stub < Expectation # :nodoc:
6
+
7
+ def initialize(mock, method_name, backtrace = nil)
8
+ super
9
+ @expected_count = Range.at_least(0)
10
+ end
11
+
12
+ def verify
13
+ true
14
+ end
15
+
16
+ end
17
+
18
+ end
@@ -0,0 +1,31 @@
1
+ require 'mocha/no_yields'
2
+ require 'mocha/single_yield'
3
+ require 'mocha/multiple_yields'
4
+
5
+ module Mocha # :nodoc:
6
+
7
+ class YieldParameters # :nodoc:
8
+
9
+ def initialize
10
+ @parameter_groups = []
11
+ end
12
+
13
+ def next_invocation
14
+ case @parameter_groups.size
15
+ when 0: NoYields.new
16
+ when 1: @parameter_groups.first
17
+ else @parameter_groups.shift
18
+ end
19
+ end
20
+
21
+ def add(*parameters)
22
+ @parameter_groups << SingleYield.new(*parameters)
23
+ end
24
+
25
+ def multiple_add(*parameter_groups)
26
+ @parameter_groups << MultipleYields.new(*parameter_groups)
27
+ end
28
+
29
+ end
30
+
31
+ end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
2
  require 'mocha'
3
3
 
4
4
  class MochaAcceptanceTest < Test::Unit::TestCase
@@ -0,0 +1,72 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class MockedMethodDispatchAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_find_latest_matching_expectation
10
+ test_result = run_test do
11
+ mock = mock()
12
+ mock.stubs(:method).returns(1)
13
+ mock.stubs(:method).returns(2)
14
+ assert_equal 2, mock.method
15
+ assert_equal 2, mock.method
16
+ assert_equal 2, mock.method
17
+ end
18
+ assert_passed(test_result)
19
+ end
20
+
21
+ def test_should_find_latest_expectation_which_has_not_stopped_matching
22
+ test_result = run_test do
23
+ mock = mock()
24
+ mock.stubs(:method).returns(1)
25
+ mock.stubs(:method).once.returns(2)
26
+ assert_equal 2, mock.method
27
+ assert_equal 1, mock.method
28
+ assert_equal 1, mock.method
29
+ end
30
+ assert_passed(test_result)
31
+ end
32
+
33
+ def test_should_keep_finding_later_stub_and_so_never_satisfy_earlier_expectation
34
+ test_result = run_test do
35
+ mock = mock()
36
+ mock.expects(:method).returns(1)
37
+ mock.stubs(:method).returns(2)
38
+ assert_equal 2, mock.method
39
+ assert_equal 2, mock.method
40
+ assert_equal 2, mock.method
41
+ end
42
+ assert_failed(test_result)
43
+ end
44
+
45
+ def test_should_find_later_expectation_until_it_stops_matching_then_find_earlier_stub
46
+ test_result = run_test do
47
+ mock = mock()
48
+ mock.stubs(:method).returns(1)
49
+ mock.expects(:method).returns(2)
50
+ assert_equal 2, mock.method
51
+ assert_equal 1, mock.method
52
+ assert_equal 1, mock.method
53
+ end
54
+ assert_passed(test_result)
55
+ end
56
+
57
+ def test_should
58
+ # flunk
59
+ test_result = run_test do
60
+ mock = mock()
61
+ mock.stubs(:method).returns(1)
62
+ mock.stubs(:method).times(2..3).returns(2)
63
+ assert_equal 2, mock.method
64
+ assert_equal 2, mock.method
65
+ assert_equal 2, mock.method
66
+ assert_equal 1, mock.method
67
+ assert_equal 1, mock.method
68
+ end
69
+ assert_passed(test_result)
70
+ end
71
+
72
+ end
@@ -0,0 +1,63 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'mocha'
3
+ require 'test_runner'
4
+
5
+ class ParameterMatcherAcceptanceTest < Test::Unit::TestCase
6
+
7
+ include TestRunner
8
+
9
+ def test_should_match_hash_parameter_with_specified_key
10
+ test_result = run_test do
11
+ mock = mock()
12
+ mock.expects(:method).with(has_key(:key_1))
13
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
14
+ end
15
+ assert_passed(test_result)
16
+ end
17
+
18
+ def test_should_not_match_hash_parameter_with_specified_key
19
+ test_result = run_test do
20
+ mock = mock()
21
+ mock.expects(:method).with(has_key(:key_1))
22
+ mock.method(:key_2 => 'value_2')
23
+ end
24
+ assert_failed(test_result)
25
+ end
26
+
27
+ def test_should_match_hash_parameter_with_specified_value
28
+ test_result = run_test do
29
+ mock = mock()
30
+ mock.expects(:method).with(has_value('value_1'))
31
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
32
+ end
33
+ assert_passed(test_result)
34
+ end
35
+
36
+ def test_should_not_match_hash_parameter_with_specified_value
37
+ test_result = run_test do
38
+ mock = mock()
39
+ mock.expects(:method).with(has_value('value_1'))
40
+ mock.method(:key_2 => 'value_2')
41
+ end
42
+ assert_failed(test_result)
43
+ end
44
+
45
+ def test_should_match_hash_parameter_with_specified_entry
46
+ test_result = run_test do
47
+ mock = mock()
48
+ mock.expects(:method).with(has_entry(:key_1, 'value_1'))
49
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
50
+ end
51
+ assert_passed(test_result)
52
+ end
53
+
54
+ def test_should_not_match_hash_parameter_with_specified_entry
55
+ test_result = run_test do
56
+ mock = mock()
57
+ mock.expects(:method).with(has_entry(:key_1, 'value_2'))
58
+ mock.method(:key_1 => 'value_1', :key_2 => 'value_2')
59
+ end
60
+ assert_failed(test_result)
61
+ end
62
+
63
+ end