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.
Files changed (43) hide show
  1. data/COPYING +3 -0
  2. data/README +148 -0
  3. data/lib/auto_mocha.rb +1 -0
  4. data/lib/auto_mocha/auto_mock.rb +54 -0
  5. data/lib/auto_mocha/mock_class.rb +38 -0
  6. data/lib/mocha.rb +1 -0
  7. data/lib/mocha/expectation.rb +116 -0
  8. data/lib/mocha/infinite_range.rb +27 -0
  9. data/lib/mocha/inspect.rb +37 -0
  10. data/lib/mocha/metaclass.rb +7 -0
  11. data/lib/mocha/mock.rb +30 -0
  12. data/lib/mocha/mock_methods.rb +55 -0
  13. data/lib/mocha/pretty_parameters.rb +28 -0
  14. data/lib/stubba.rb +2 -0
  15. data/lib/stubba/any_instance_method.rb +31 -0
  16. data/lib/stubba/class_method.rb +61 -0
  17. data/lib/stubba/instance_method.rb +22 -0
  18. data/lib/stubba/object.rb +77 -0
  19. data/lib/stubba/stubba.rb +27 -0
  20. data/lib/stubba/test_case.rb +65 -0
  21. data/test/all_tests.rb +100 -0
  22. data/test/auto_mocha/auto_mock_test.rb +85 -0
  23. data/test/auto_mocha/mock_class_test.rb +179 -0
  24. data/test/auto_mock_acceptance_test.rb +36 -0
  25. data/test/method_definer.rb +18 -0
  26. data/test/mocha/expectation_test.rb +216 -0
  27. data/test/mocha/infinite_range_test.rb +50 -0
  28. data/test/mocha/inspect_test.rb +79 -0
  29. data/test/mocha/mock_methods_test.rb +141 -0
  30. data/test/mocha/mock_test.rb +64 -0
  31. data/test/mocha/pretty_parameters_test.rb +32 -0
  32. data/test/mocha_acceptance_test.rb +112 -0
  33. data/test/stubba/any_instance_method_test.rb +113 -0
  34. data/test/stubba/class_method_test.rb +149 -0
  35. data/test/stubba/instance_method_test.rb +97 -0
  36. data/test/stubba/object_test.rb +147 -0
  37. data/test/stubba/stubba_test.rb +62 -0
  38. data/test/stubba/test_case_test.rb +41 -0
  39. data/test/stubba_acceptance_test.rb +107 -0
  40. data/test/stubba_integration_test.rb +59 -0
  41. data/test/stubba_replacer.rb +13 -0
  42. data/test/test_helper.rb +4 -0
  43. metadata +91 -0
@@ -0,0 +1,113 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+ require 'mocha/mock'
4
+
5
+ require 'stubba/any_instance_method'
6
+
7
+ class AnyInstanceMethodTest < Test::Unit::TestCase
8
+
9
+ include Stubba
10
+ include Mocha
11
+
12
+ def test_should_hide_original_method
13
+ klass = Class.new { def method_x; end }
14
+ method = AnyInstanceMethod.new(klass, :method_x)
15
+ hidden_method_x = method.hidden_method.to_sym
16
+
17
+ method.hide_original_method
18
+
19
+ assert klass.method_defined?(hidden_method_x)
20
+ end
21
+
22
+ def test_should_not_hide_original_method_if_it_is_not_defined
23
+ klass = Class.new
24
+ method = AnyInstanceMethod.new(klass, :method_x)
25
+ hidden_method_x = method.hidden_method.to_sym
26
+
27
+ method.hide_original_method
28
+
29
+ assert_equal false, klass.method_defined?(hidden_method_x)
30
+ end
31
+
32
+ def test_should_define_a_new_method
33
+ klass = Class.new { def method_x; end }
34
+ method = AnyInstanceMethod.new(klass, :method_x)
35
+ mocha = Mock.new
36
+ mocha.expects(:method_x).with(:param1, :param2).returns(:result)
37
+ any_instance = Mock.new(:mocha => mocha)
38
+ klass.define_instance_method(:any_instance) { any_instance }
39
+
40
+ method.define_new_method
41
+
42
+ instance = klass.new
43
+ result = instance.method_x(:param1, :param2)
44
+
45
+ assert_equal :result, result
46
+ mocha.verify
47
+ end
48
+
49
+ def test_should_restore_original_method
50
+ klass = Class.new { def method_x; end }
51
+ method = AnyInstanceMethod.new(klass, :method_x)
52
+ hidden_method_x = method.hidden_method.to_sym
53
+ klass.send(:define_method, hidden_method_x, Proc.new { :original_result })
54
+
55
+ method.restore_original_method
56
+
57
+ instance = klass.new
58
+ assert_equal :original_result, instance.method_x
59
+ assert !klass.method_defined?(hidden_method_x)
60
+ end
61
+
62
+ def test_should_not_restore_original_method_if_hidden_method_not_defined
63
+ klass = Class.new { def method_x; :new_result; end }
64
+ method = AnyInstanceMethod.new(klass, :method_x)
65
+
66
+ method.restore_original_method
67
+
68
+ instance = klass.new
69
+ assert_equal :new_result, instance.method_x
70
+ end
71
+
72
+ def test_should_call_remove_new_method
73
+ klass = Class.new { def method_x; end }
74
+ any_instance = Mock.new(:reset_mocha => nil)
75
+ klass.define_instance_method(:any_instance) { any_instance }
76
+ method = AnyInstanceMethod.new(klass, :method_x)
77
+ method.replace_instance_method(:restore_original_method) { }
78
+ method.define_instance_accessor(:remove_called)
79
+ method.replace_instance_method(:remove_new_method) { self.remove_called = true }
80
+
81
+ method.unstub
82
+
83
+ assert method.remove_called
84
+ end
85
+
86
+ def test_should_call_restore_original_method
87
+ klass = Class.new { def method_x; end }
88
+ any_instance = Mock.new(:reset_mocha => nil)
89
+ klass.define_instance_method(:any_instance) { any_instance }
90
+ method = AnyInstanceMethod.new(klass, :method_x)
91
+ method.replace_instance_method(:remove_new_method) { }
92
+ method.define_instance_accessor(:restore_called)
93
+ method.replace_instance_method(:restore_original_method) { self.restore_called = true }
94
+
95
+ method.unstub
96
+
97
+ assert method.restore_called
98
+ end
99
+
100
+ def test_should_call_reset_mocha
101
+ klass = Class.new { def method_x; end }
102
+ any_instance = Mock.new(:reset_mocha => nil)
103
+ klass.define_instance_method(:any_instance) { any_instance }
104
+ method = AnyInstanceMethod.new(klass, :method_x)
105
+ method.replace_instance_method(:remove_new_method) { }
106
+ method.replace_instance_method(:restore_original_method) { }
107
+
108
+ method.unstub
109
+
110
+ any_instance.verify(:reset_mocha)
111
+ end
112
+
113
+ end
@@ -0,0 +1,149 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+ require 'mocha/mock'
4
+
5
+ require 'stubba/class_method'
6
+
7
+ class ClassMethodTest < Test::Unit::TestCase
8
+
9
+ include Stubba
10
+ include Mocha
11
+
12
+ def test_should_provide_hidden_version_of_method_name
13
+ method = ClassMethod.new(nil, :original_method_name)
14
+ assert_equal '__stubba__original_method_name__stubba__', method.hidden_method
15
+ end
16
+
17
+ def test_should_provide_hidden_version_of_method_name_with_question_mark
18
+ method = ClassMethod.new(nil, :original_method_name?)
19
+ assert_equal '__stubba__original_method_name_question_mark__stubba__', method.hidden_method
20
+ end
21
+
22
+ def test_should_provide_hidden_version_of_method_name_with_exclamation_mark
23
+ method = ClassMethod.new(nil, :original_method_name!)
24
+ assert_equal '__stubba__original_method_name_exclamation_mark__stubba__', method.hidden_method
25
+ end
26
+
27
+ def test_should_hide_original_method
28
+ klass = Class.new { def self.method_x; end }
29
+ method = ClassMethod.new(klass, :method_x)
30
+ hidden_method_x = method.hidden_method
31
+
32
+ method.hide_original_method
33
+
34
+ assert klass.respond_to?(hidden_method_x)
35
+ end
36
+
37
+ def test_should_not_hide_original_method_if_method_not_defined
38
+ klass = Class.new
39
+ method = ClassMethod.new(klass, :method_x)
40
+ hidden_method_x = method.hidden_method
41
+
42
+ method.hide_original_method
43
+
44
+ assert_equal false, klass.respond_to?(hidden_method_x)
45
+ end
46
+
47
+ def test_should_define_a_new_method_which_should_call_mocha_method_missing
48
+ klass = Class.new { def self.method_x; end }
49
+ mocha = Mock.new
50
+ klass.define_instance_method(:mocha) { mocha }
51
+ mocha.expects(:method_x).with(:param1, :param2).returns(:result)
52
+ method = ClassMethod.new(klass, :method_x)
53
+
54
+ method.define_new_method
55
+ result = klass.method_x(:param1, :param2)
56
+
57
+ assert_equal :result, result
58
+ mocha.verify
59
+ end
60
+
61
+ def test_should_remove_new_method
62
+ klass = Class.new { def self.method_x; end }
63
+ method = ClassMethod.new(klass, :method_x)
64
+
65
+ method.remove_new_method
66
+
67
+ assert_equal false, klass.respond_to?(:method_x)
68
+ end
69
+
70
+ def test_should_restore_original_method
71
+ klass = Class.new { def self.method_x; end }
72
+ method = ClassMethod.new(klass, :method_x)
73
+ hidden_method_x = method.hidden_method.to_sym
74
+ klass.define_instance_method(hidden_method_x) { :original_result }
75
+
76
+ method.restore_original_method
77
+
78
+ assert_equal :original_result, klass.method_x
79
+ assert !klass.respond_to?(hidden_method_x)
80
+ end
81
+
82
+ def test_should_not_restore_original_method_if_hidden_method_is_not_defined
83
+ klass = Class.new { def self.method_x; :new_result; end }
84
+ method = ClassMethod.new(klass, :method_x)
85
+
86
+ method.restore_original_method
87
+
88
+ assert_equal :new_result, klass.method_x
89
+ end
90
+
91
+ def test_should_call_hide_original_method
92
+ klass = Class.new { def self.method_x; end }
93
+ method = ClassMethod.new(klass, :method_x)
94
+ method.define_instance_accessor(:hide_called)
95
+ method.replace_instance_method(:hide_original_method) { self.hide_called = true }
96
+
97
+ method.stub
98
+
99
+ assert method.hide_called
100
+ end
101
+
102
+ def test_should_call_define_new_method
103
+ klass = Class.new { def self.method_x; end }
104
+ method = ClassMethod.new(klass, :method_x)
105
+ method.define_instance_accessor(:define_called)
106
+ method.replace_instance_method(:define_new_method) { self.define_called = true }
107
+
108
+ method.stub
109
+
110
+ assert method.define_called
111
+ end
112
+
113
+ def test_should_call_remove_new_method
114
+ klass = Class.new { def self.method_x; end }
115
+ klass.define_instance_method(:reset_mocha) { }
116
+ method = ClassMethod.new(klass, :method_x)
117
+ method.define_instance_accessor(:remove_called)
118
+ method.replace_instance_method(:remove_new_method) { self.remove_called = true }
119
+
120
+ method.unstub
121
+
122
+ assert method.remove_called
123
+ end
124
+
125
+ def test_should_call_restore_original_method
126
+ klass = Class.new { def self.method_x; end }
127
+ klass.define_instance_method(:reset_mocha) { }
128
+ method = ClassMethod.new(klass, :method_x)
129
+ method.define_instance_accessor(:restore_called)
130
+ method.replace_instance_method(:restore_original_method) { self.restore_called = true }
131
+
132
+ method.unstub
133
+
134
+ assert method.restore_called
135
+ end
136
+
137
+ def test_should_call_reset_mocha
138
+ klass = Class.new { def self.method_x; end }
139
+ klass.define_instance_accessor(:reset_called)
140
+ klass.define_instance_method(:reset_mocha) { self.reset_called = true }
141
+ method = ClassMethod.new(klass, :method_x)
142
+ method.replace_instance_method(:restore_original_method) { }
143
+
144
+ method.unstub
145
+
146
+ assert klass.reset_called
147
+ end
148
+
149
+ end
@@ -0,0 +1,97 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'method_definer'
3
+
4
+ require 'stubba/instance_method'
5
+
6
+ class InstanceMethodTest < Test::Unit::TestCase
7
+
8
+ include Stubba
9
+
10
+ def test_should_exist
11
+ klass = Class.new { def method_x; end }
12
+ instance = klass.new
13
+ method = InstanceMethod.new(instance, :method_x)
14
+
15
+ assert method.exists?
16
+ end
17
+
18
+ def test_should_not_exist
19
+ klass = Class.new { }
20
+ instance = klass.new
21
+ method = InstanceMethod.new(instance, :non_existent_method)
22
+
23
+ assert !method.exists?
24
+ end
25
+
26
+ def test_should_raise_assertion_failed_error_when_attempting_to_stub_non_existent_method
27
+ klass = Class.new
28
+ instance = klass.new
29
+ method = InstanceMethod.new(instance, :non_existent_method)
30
+ assert_raise(Test::Unit::AssertionFailedError) { method.stub }
31
+ end
32
+
33
+ def test_should_call_define_new_method
34
+ klass = Class.new { def method_x; end }
35
+ instance = klass.new
36
+ method = InstanceMethod.new(instance, :method_x)
37
+ method.define_instance_accessor(:define_called)
38
+ method.replace_instance_method(:define_new_method) { self.define_called = true }
39
+
40
+ method.stub
41
+
42
+ assert method.define_called
43
+ end
44
+
45
+ def test_should_not_call_hide_original_method
46
+ klass = Class.new { def method_x; end }
47
+ instance = klass.new
48
+ method = InstanceMethod.new(instance, :method_x)
49
+ method.replace_instance_method(:define_new_method) { }
50
+ method.define_instance_accessor(:hide_called)
51
+ method.replace_instance_method(:hide_original_method) { self.hide_called = true }
52
+
53
+ method.stub
54
+
55
+ assert !method.hide_called
56
+ end
57
+
58
+ def test_should_not_call_restore_original_method
59
+ klass = Class.new { def method_x; end }
60
+ instance = klass.new
61
+ method = InstanceMethod.new(instance, :method_x)
62
+ method.replace_instance_method(:define_new_method) { }
63
+ method.define_instance_accessor(:restore_called)
64
+ method.replace_instance_method(:restore_original_method) { self.restore_called = true }
65
+
66
+ method.unstub
67
+
68
+ assert !method.restore_called
69
+ end
70
+
71
+ def test_should_not_call_remove_new_method
72
+ klass = Class.new { def method_x; end }
73
+ instance = klass.new
74
+ method = InstanceMethod.new(instance, :method_x)
75
+ method.replace_instance_method(:define_new_method) { }
76
+ method.define_instance_accessor(:remove_called)
77
+ method.replace_instance_method(:remove_new_method) { self.remove_called = true }
78
+
79
+ method.unstub
80
+
81
+ assert !method.remove_called
82
+ end
83
+
84
+ def test_should_not_call_reset_mocha
85
+ klass = Class.new { def method_x; end }
86
+ instance = klass.new
87
+ instance.define_instance_accessor(:reset_called)
88
+ instance.define_instance_method(:reset_mocha) { self.reset_called = true }
89
+ method = InstanceMethod.new(instance, :method_x)
90
+ method.replace_instance_method(:define_new_method) { }
91
+
92
+ method.unstub
93
+
94
+ assert !instance.reset_called
95
+ end
96
+
97
+ end
@@ -0,0 +1,147 @@
1
+ require File.join(File.dirname(__FILE__), "..", "test_helper")
2
+ require 'stubba_replacer'
3
+
4
+ require 'stubba/object'
5
+
6
+ class ObjectTest < Test::Unit::TestCase
7
+
8
+ include Stubba
9
+ include Mocha
10
+
11
+ def test_should_build_mocha
12
+ instance = Object.new
13
+ mocha = instance.mocha
14
+ assert_not_nil mocha
15
+ assert mocha.is_a?(Mock)
16
+ end
17
+
18
+ def test_should_reuse_existing_mocha
19
+ instance = Object.new
20
+ mocha_1 = instance.mocha
21
+ mocha_2 = instance.mocha
22
+ assert_equal mocha_1, mocha_2
23
+ end
24
+
25
+ def test_should_reset_mocha
26
+ instance = Object.new
27
+ assert_nil instance.reset_mocha
28
+ end
29
+
30
+ def test_should_stub_instance_method
31
+ instance = Object.new
32
+ stubba = Mock.new
33
+ stubba.expects(:stub).with(InstanceMethod.new(instance, :method1))
34
+ replace_stubba(stubba) do
35
+ instance.expects(:method1)
36
+ end
37
+ stubba.verify(:stub)
38
+ end
39
+
40
+ def test_should_build_and_store_expectation
41
+ instance = Object.new
42
+ stubba = Mock.new(:stub => nil)
43
+ replace_stubba(stubba) do
44
+ expectation = instance.expects(:method1)
45
+ assert_equal [expectation], instance.mocha.expectations
46
+ end
47
+ end
48
+
49
+ def test_should_verify_expectations
50
+ instance = Object.new
51
+ stubba = Mock.new(:stub => nil)
52
+ replace_stubba(stubba) do
53
+ instance.expects(:method1).with(:value1, :value2)
54
+ end
55
+ assert_raise(Test::Unit::AssertionFailedError) { instance.verify }
56
+ end
57
+
58
+ def test_should_build_any_instance_object
59
+ klass = Class.new
60
+ any_instance = klass.any_instance
61
+ assert_not_nil any_instance
62
+ assert any_instance.is_a?(Class::AnyInstance)
63
+ end
64
+
65
+ def test_should_return_same_any_instance_object
66
+ klass = Class.new
67
+ any_instance_1 = klass.any_instance
68
+ any_instance_2 = klass.any_instance
69
+ assert_equal any_instance_1, any_instance_2
70
+ end
71
+
72
+ def test_should_stub_class_method
73
+ klass = Class.new
74
+ stubba = Mock.new
75
+ stubba.expects(:stub).with(ClassMethod.new(klass, :method1))
76
+ replace_stubba(stubba) do
77
+ klass.expects(:method1)
78
+ end
79
+ stubba.verify(:stub)
80
+ end
81
+
82
+ def test_should_build_and_store_class_method_expectation
83
+ klass = Class.new
84
+ stubba = Mock.new(:stub => nil)
85
+ replace_stubba(stubba) do
86
+ expectation = klass.expects(:method1)
87
+ assert_equal [expectation], klass.mocha.expectations
88
+ end
89
+ end
90
+
91
+ def test_should_stub_module_method
92
+ mod = Module.new
93
+ stubba = Mock.new
94
+ stubba.expects(:stub).with(ClassMethod.new(mod, :method1))
95
+ replace_stubba(stubba) do
96
+ mod.expects(:method1)
97
+ end
98
+ stubba.verify(:stub)
99
+ end
100
+
101
+ def test_should_build_and_store_module_method_expectation
102
+ mod = Module.new
103
+ stubba = Mock.new(:stub => nil)
104
+ replace_stubba(stubba) do
105
+ expectation = mod.expects(:method1)
106
+ assert_equal [expectation], mod.mocha.expectations
107
+ end
108
+ end
109
+
110
+ def test_should_use_stubba_instance_method_for_object
111
+ assert_equal InstanceMethod, Object.new.stubba_method
112
+ end
113
+
114
+ def test_should_use_stubba_class_method_for_module
115
+ assert_equal ClassMethod, Module.new.stubba_method
116
+ end
117
+
118
+ def test_should_use_stubba_class_method_for_class
119
+ assert_equal ClassMethod, Class.new.stubba_method
120
+ end
121
+
122
+ def test_should_use_stubba_class_method_for_any_instance
123
+ assert_equal AnyInstanceMethod, Class::AnyInstance.new(nil).stubba_method
124
+ end
125
+
126
+ def test_should_stub_self_for_object
127
+ object = Object.new
128
+ assert_equal object, object.stubba_object
129
+ end
130
+
131
+ def test_should_stub_self_for_module
132
+ mod = Module.new
133
+ assert_equal mod, mod.stubba_object
134
+ end
135
+
136
+ def test_should_stub_self_for_class
137
+ klass = Class.new
138
+ assert_equal klass, klass.stubba_object
139
+ end
140
+
141
+ def test_should_stub_relevant_class_for_any_instance
142
+ klass = Class.new
143
+ any_instance = Class::AnyInstance.new(klass)
144
+ assert_equal klass, any_instance.stubba_object
145
+ end
146
+
147
+ end