mocha 0.1
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/COPYING +3 -0
- data/README +148 -0
- data/lib/auto_mocha.rb +1 -0
- data/lib/auto_mocha/auto_mock.rb +54 -0
- data/lib/auto_mocha/mock_class.rb +38 -0
- data/lib/mocha.rb +1 -0
- data/lib/mocha/expectation.rb +116 -0
- data/lib/mocha/infinite_range.rb +27 -0
- data/lib/mocha/inspect.rb +37 -0
- data/lib/mocha/metaclass.rb +7 -0
- data/lib/mocha/mock.rb +30 -0
- data/lib/mocha/mock_methods.rb +55 -0
- data/lib/mocha/pretty_parameters.rb +28 -0
- data/lib/stubba.rb +2 -0
- data/lib/stubba/any_instance_method.rb +31 -0
- data/lib/stubba/class_method.rb +61 -0
- data/lib/stubba/instance_method.rb +22 -0
- data/lib/stubba/object.rb +77 -0
- data/lib/stubba/stubba.rb +27 -0
- data/lib/stubba/test_case.rb +65 -0
- data/test/all_tests.rb +100 -0
- data/test/auto_mocha/auto_mock_test.rb +85 -0
- data/test/auto_mocha/mock_class_test.rb +179 -0
- data/test/auto_mock_acceptance_test.rb +36 -0
- data/test/method_definer.rb +18 -0
- data/test/mocha/expectation_test.rb +216 -0
- data/test/mocha/infinite_range_test.rb +50 -0
- data/test/mocha/inspect_test.rb +79 -0
- data/test/mocha/mock_methods_test.rb +141 -0
- data/test/mocha/mock_test.rb +64 -0
- data/test/mocha/pretty_parameters_test.rb +32 -0
- data/test/mocha_acceptance_test.rb +112 -0
- data/test/stubba/any_instance_method_test.rb +113 -0
- data/test/stubba/class_method_test.rb +149 -0
- data/test/stubba/instance_method_test.rb +97 -0
- data/test/stubba/object_test.rb +147 -0
- data/test/stubba/stubba_test.rb +62 -0
- data/test/stubba/test_case_test.rb +41 -0
- data/test/stubba_acceptance_test.rb +107 -0
- data/test/stubba_integration_test.rb +59 -0
- data/test/stubba_replacer.rb +13 -0
- data/test/test_helper.rb +4 -0
- metadata +91 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/infinite_range'
|
3
|
+
|
4
|
+
class InfiniteRangeTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_should_include_values_at_or_above_minimum
|
7
|
+
range = Range.at_least(10)
|
8
|
+
assert(range === 10)
|
9
|
+
assert(range === 11)
|
10
|
+
assert(range === 1000000)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_not_include_values_below_minimum
|
14
|
+
range = Range.at_least(10)
|
15
|
+
assert_false(range === 0)
|
16
|
+
assert_false(range === 9)
|
17
|
+
assert_false(range === -11)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_be_human_readable_description_for_at_least
|
21
|
+
assert_equal "at least 10", Range.at_least(10).to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_should_include_values_at_or_below_maximum
|
25
|
+
range = Range.at_most(10)
|
26
|
+
assert(range === 10)
|
27
|
+
assert(range === 0)
|
28
|
+
assert(range === -1000000)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_not_include_values_above_maximum
|
32
|
+
range = Range.at_most(10)
|
33
|
+
assert_false(range === 11)
|
34
|
+
assert_false(range === 1000000)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_be_human_readable_description_for_at_most
|
38
|
+
assert_equal "at most 10", Range.at_most(10).to_s
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_should_not_break_original_description
|
42
|
+
assert_equal "1..10", (1..10).to_s
|
43
|
+
assert_equal "1...10", (1...10).to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def assert_false(condition)
|
47
|
+
assert(!condition)
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/inspect'
|
3
|
+
|
4
|
+
class InspectTest
|
5
|
+
def self.suite
|
6
|
+
suite = Test::Unit::TestSuite.new('MochaInspectTests')
|
7
|
+
suite << ObjectInspectTest.suite
|
8
|
+
suite << TimeDateInspectTest.suite
|
9
|
+
suite << StringInspectTest.suite
|
10
|
+
suite << ArrayInstanceTest.suite
|
11
|
+
suite << HashInspectTest.suite
|
12
|
+
suite
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ObjectInspectTest < Test::Unit::TestCase
|
17
|
+
|
18
|
+
def test_should_provide_custom_representation_of_object
|
19
|
+
object = Object.new
|
20
|
+
assert_equal "#<#{object.class}: #{object.object_id}>", object.mocha_inspect
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class TimeDateInspectTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def test_should_use_include_date_in_seconds
|
28
|
+
time = Time.now
|
29
|
+
assert_equal "#{time.inspect} (#{time.to_f} secs)", time.mocha_inspect
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_use_to_s_for_date
|
33
|
+
date = Date.new(2006, 1, 1)
|
34
|
+
assert_equal date.to_s, date.mocha_inspect
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_use_to_s_for_datetime
|
38
|
+
datetime = DateTime.new(2006, 1, 1)
|
39
|
+
assert_equal datetime.to_s, datetime.mocha_inspect
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
class StringInspectTest < Test::Unit::TestCase
|
45
|
+
|
46
|
+
def test_should_replace_escaped_quotes_with_single_quote
|
47
|
+
string = "my_string"
|
48
|
+
assert_equal "'my_string'", string.mocha_inspect
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
class ArrayInstanceTest < Test::Unit::TestCase
|
54
|
+
|
55
|
+
def test_should_use_inspect
|
56
|
+
array = [1, 2]
|
57
|
+
assert_equal array.inspect, array.mocha_inspect
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_use_mocha_inspect_on_each_item
|
61
|
+
array = [1, 2, "chris"]
|
62
|
+
assert_equal "[1, 2, 'chris']", array.mocha_inspect
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
class HashInspectTest < Test::Unit::TestCase
|
68
|
+
|
69
|
+
def test_should_keep_spacing_between_key_value
|
70
|
+
hash = {:a => true}
|
71
|
+
assert_equal '{:a => true}', hash.mocha_inspect
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_should_use_mocha_inspect_on_each_item
|
75
|
+
hash = {:a => 'mocha'}
|
76
|
+
assert_equal "{:a => 'mocha'}", hash.mocha_inspect
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/mock_methods'
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
class MockMethodsTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Mocha
|
8
|
+
|
9
|
+
def test_should_create_and_add_expectations
|
10
|
+
mock = Object.new
|
11
|
+
mock.extend(MockMethods)
|
12
|
+
|
13
|
+
expectation1 = mock.expects(:method1)
|
14
|
+
expectation2 = mock.expects(:method2)
|
15
|
+
|
16
|
+
assert_equal [expectation1, expectation2].to_set, mock.expectations.to_set
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_create_and_add_stubs
|
20
|
+
mock = Object.new
|
21
|
+
mock.extend(MockMethods)
|
22
|
+
|
23
|
+
stub1 = mock.stubs(:method1)
|
24
|
+
stub2 = mock.stubs(:method2)
|
25
|
+
|
26
|
+
assert_equal [stub1, stub2].to_set, mock.expectations.to_set
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_should_find_matching_expectation
|
30
|
+
mock = Object.new
|
31
|
+
mock.extend(MockMethods)
|
32
|
+
|
33
|
+
expectation1 = mock.expects(:my_method).with(:argument1, :argument2)
|
34
|
+
expectation2 = mock.expects(:my_method).with(:argument3, :argument4)
|
35
|
+
|
36
|
+
assert_equal expectation2, mock.matching_expectation(:my_method, :argument3, :argument4)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_should_invoke_expectation_and_return_result
|
40
|
+
mock = Object.new
|
41
|
+
mock.extend(MockMethods)
|
42
|
+
mock.expects(:my_method).returns(:result)
|
43
|
+
|
44
|
+
result = mock.my_method
|
45
|
+
|
46
|
+
assert_equal :result, result
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_should_raise_no_method_error
|
50
|
+
mock = Object.new
|
51
|
+
mock.extend(MockMethods)
|
52
|
+
assert_raise(NoMethodError) do
|
53
|
+
mock.super_method_missing(nil)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_raise_assertion_error_for_unexpected_method_call
|
58
|
+
mock = Object.new
|
59
|
+
mock.extend(MockMethods)
|
60
|
+
error = assert_raise(Test::Unit::AssertionFailedError) do
|
61
|
+
mock.unexpected_method_called(:my_method, :argument1, :argument2)
|
62
|
+
end
|
63
|
+
assert_match /my_method/, error.message
|
64
|
+
assert_match /argument1/, error.message
|
65
|
+
assert_match /argument2/, error.message
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_should_indicate_unexpected_method_called
|
69
|
+
mock = Object.new
|
70
|
+
mock.extend(MockMethods)
|
71
|
+
class << mock
|
72
|
+
attr_accessor :symbol, :arguments
|
73
|
+
def unexpected_method_called(symbol, *arguments)
|
74
|
+
self.symbol, self.arguments = symbol, arguments
|
75
|
+
end
|
76
|
+
end
|
77
|
+
mock.my_method(:argument1, :argument2)
|
78
|
+
|
79
|
+
assert_equal :my_method, mock.symbol
|
80
|
+
assert_equal [:argument1, :argument2], mock.arguments
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_should_call_method_missing_for_parent
|
84
|
+
mock = Object.new
|
85
|
+
mock.extend(MockMethods)
|
86
|
+
class << mock
|
87
|
+
attr_accessor :symbol, :arguments
|
88
|
+
def super_method_missing(symbol, *arguments, &block)
|
89
|
+
self.symbol, self.arguments = symbol, arguments
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
mock.my_method(:argument1, :argument2)
|
94
|
+
|
95
|
+
assert_equal :my_method, mock.symbol
|
96
|
+
assert_equal [:argument1, :argument2], mock.arguments
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_should_verify_that_all_expectations_have_been_fulfilled
|
100
|
+
mock = Object.new
|
101
|
+
mock.extend(MockMethods)
|
102
|
+
mock.expects(:method1)
|
103
|
+
mock.expects(:method2)
|
104
|
+
mock.method1
|
105
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
106
|
+
mock.verify
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_should_only_verify_expectations_matching_method_name
|
111
|
+
mock = Object.new
|
112
|
+
mock.extend(MockMethods)
|
113
|
+
mock.expects(:method1)
|
114
|
+
mock.expects(:method2)
|
115
|
+
mock.method1
|
116
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
117
|
+
mock.verify(:method1)
|
118
|
+
end
|
119
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
120
|
+
mock.verify(:method2)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_should_only_verify_expectations_matching_multiple_method_names
|
125
|
+
mock = Object.new
|
126
|
+
mock.extend(MockMethods)
|
127
|
+
mock.expects(:method1)
|
128
|
+
mock.expects(:method2)
|
129
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
130
|
+
mock.verify(:method1, :method2)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_should_report_possible_expectations
|
135
|
+
mock = Object.new.extend(MockMethods)
|
136
|
+
mock.expects(:meth).with(1)
|
137
|
+
exception = assert_raise(Test::Unit::AssertionFailedError) { mock.meth(2) }
|
138
|
+
assert_equal "Unexpected message :meth(2)\nSimilar expectations :meth(1)", exception.message
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/mock'
|
3
|
+
|
4
|
+
class MockTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include Mocha
|
7
|
+
|
8
|
+
def test_should_include_mocha_methods
|
9
|
+
assert Mock.included_modules.include?(MockMethods)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_should_set_single_expectation
|
13
|
+
mock = Mock.new
|
14
|
+
mock.expects(:method1).returns(1)
|
15
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
16
|
+
assert_equal 1, mock.method1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_set_multiple_expectations_in_constructor
|
21
|
+
mock = Mock.new(:method1 => 1, :method2 => 2)
|
22
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
23
|
+
assert_equal 1, mock.method1
|
24
|
+
assert_equal 2, mock.method2
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_should_claim_to_respond_to_any_method
|
29
|
+
mock = Mock.new
|
30
|
+
always_responds = mock.always_responds
|
31
|
+
assert always_responds.respond_to?(:gobble_de_gook)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_should_raise_exception_if_attempting_to_mock_undefined_method
|
35
|
+
mocked = Class.new
|
36
|
+
mock = Mock.new(mocked.new)
|
37
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
38
|
+
mock.expects(:method1)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_not_raise_exception_if_attempting_to_mock_defined_method
|
43
|
+
mocked = Class.new { def method1; end }
|
44
|
+
mock = Mock.new(mocked.new)
|
45
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
46
|
+
mock.expects(:method1)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_should_not_raise_exception_if_attempting_to_mock_method_when_no_class_specified
|
51
|
+
mock = Mock.new
|
52
|
+
assert_nothing_raised(Test::Unit::AssertionFailedError) do
|
53
|
+
mock.expects(:method1)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_build_and_store_expectations
|
58
|
+
mock = Mock.new
|
59
|
+
expectation = mock.expects(:method1)
|
60
|
+
assert_not_nil expectation
|
61
|
+
assert_equal [expectation], mock.expectations
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
require 'mocha/pretty_parameters'
|
3
|
+
|
4
|
+
class PrettyParametersTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
include Mocha
|
7
|
+
|
8
|
+
def test_should_remove_outer_array_braces
|
9
|
+
params = [1, 2, [3, 4]]
|
10
|
+
parameters = PrettyParameters.new(params)
|
11
|
+
assert_equal '1, 2, [3, 4]', parameters.pretty
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_should_display_numeric_arguments_as_is
|
15
|
+
params = [1, 2, 3]
|
16
|
+
parameters = PrettyParameters.new(params)
|
17
|
+
assert_equal '1, 2, 3', parameters.pretty
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_remove_curly_braces_if_hash_is_only_argument
|
21
|
+
params = [{:a => 1, :z => 2}]
|
22
|
+
parameters = PrettyParameters.new(params)
|
23
|
+
assert_match /^:[az] => [12], :[az] => [12]$/, parameters.pretty
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_should_not_remove_curly_braces_if_hash_is_not_the_only_argument
|
27
|
+
params = [1, {:a => 1}]
|
28
|
+
parameters = PrettyParameters.new(params)
|
29
|
+
assert_equal '1, {:a => 1}', parameters.pretty
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'mocha'
|
3
|
+
|
4
|
+
class MochaAcceptanceTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class Rover
|
7
|
+
|
8
|
+
def initialize(left_track, right_track, steps_per_metre, steps_per_degree)
|
9
|
+
@left_track, @right_track, @steps_per_metre, @steps_per_degree = left_track, right_track, steps_per_metre, steps_per_degree
|
10
|
+
end
|
11
|
+
|
12
|
+
def forward(metres)
|
13
|
+
@left_track.move(metres * @steps_per_metre)
|
14
|
+
@right_track.move(metres * @steps_per_metre)
|
15
|
+
wait
|
16
|
+
end
|
17
|
+
|
18
|
+
def backward(metres)
|
19
|
+
forward(-metres)
|
20
|
+
end
|
21
|
+
|
22
|
+
def left(degrees)
|
23
|
+
@left_track.move(-degrees * @steps_per_degree)
|
24
|
+
@right_track.move(+degrees * @steps_per_degree)
|
25
|
+
wait
|
26
|
+
end
|
27
|
+
|
28
|
+
def right(degrees)
|
29
|
+
left(-degrees)
|
30
|
+
end
|
31
|
+
|
32
|
+
def wait
|
33
|
+
while (@left_track.moving? or @right_track.moving?); end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
include Mocha
|
39
|
+
|
40
|
+
def test_should_move_both_tracks_forward_ten_steps
|
41
|
+
left_track = Mock.new
|
42
|
+
right_track = Mock.new
|
43
|
+
steps_per_metre = 5
|
44
|
+
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
|
45
|
+
|
46
|
+
left_track.expects(:move).with(10)
|
47
|
+
right_track.expects(:move).with(10)
|
48
|
+
|
49
|
+
left_track.stubs(:moving?).returns(false)
|
50
|
+
right_track.stubs(:moving?).returns(false)
|
51
|
+
|
52
|
+
rover.forward(2)
|
53
|
+
|
54
|
+
left_track.verify
|
55
|
+
right_track.verify
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_should_move_both_tracks_backward_ten_steps
|
59
|
+
left_track = Mock.new
|
60
|
+
right_track = Mock.new
|
61
|
+
steps_per_metre = 5
|
62
|
+
rover = Rover.new(left_track, right_track, steps_per_metre, nil)
|
63
|
+
|
64
|
+
left_track.expects(:move).with(-10)
|
65
|
+
right_track.expects(:move).with(-10)
|
66
|
+
|
67
|
+
left_track.stubs(:moving?).returns(false)
|
68
|
+
right_track.stubs(:moving?).returns(false)
|
69
|
+
|
70
|
+
rover.backward(2)
|
71
|
+
|
72
|
+
left_track.verify
|
73
|
+
right_track.verify
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_should_move_left_track_forwards_five_steps_and_right_track_backwards_five_steps
|
77
|
+
left_track = Mock.new
|
78
|
+
right_track = Mock.new
|
79
|
+
steps_per_degree = 5.0 / 90.0
|
80
|
+
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
|
81
|
+
|
82
|
+
left_track.expects(:move).with(+5)
|
83
|
+
right_track.expects(:move).with(-5)
|
84
|
+
|
85
|
+
left_track.stubs(:moving?).returns(false)
|
86
|
+
right_track.stubs(:moving?).returns(false)
|
87
|
+
|
88
|
+
rover.right(90)
|
89
|
+
|
90
|
+
left_track.verify
|
91
|
+
right_track.verify
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_should_move_left_track_backwards_five_steps_and_right_track_forwards_five_steps
|
95
|
+
left_track = Mock.new
|
96
|
+
right_track = Mock.new
|
97
|
+
steps_per_degree = 5.0 / 90.0
|
98
|
+
rover = Rover.new(left_track, right_track, nil, steps_per_degree)
|
99
|
+
|
100
|
+
left_track.expects(:move).with(-5)
|
101
|
+
right_track.expects(:move).with(+5)
|
102
|
+
|
103
|
+
left_track.stubs(:moving?).returns(false)
|
104
|
+
right_track.stubs(:moving?).returns(false)
|
105
|
+
|
106
|
+
rover.left(90)
|
107
|
+
|
108
|
+
left_track.verify
|
109
|
+
right_track.verify
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|