stubborn 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -51,9 +51,17 @@ Which produces this output:
51
51
  GreatClass.singleton.stub!(:slow_method).with(123, AClass, "hi").and_return(another_class_instance)
52
52
  GreatClass.singleton.stub!(:slow_method).and_return(another_class_instance)
53
53
 
54
- So all unstubbed class methods of the Api class and its instances will raise this special exception while running the tests. If you prefer you can use it on a single instance like this:
54
+ So all unstubbed class methods of the Api class and its instances will raise this special exception while running the tests.
55
+ If you prefer you can use it on a single instance like this:
55
56
 
56
- Stubborn.should_be_stubbed(Api.new)
57
+ api = Stubborn.should_be_stubbed(Api.new)
58
+
59
+ === Filters
60
+
61
+ You can control which methods to check with the options :except and :only. Both accept either strings or arrays:
62
+
63
+ Stubborn.should_be_stubbed(Api.new, :except => :safe_method)
64
+ Stubborn.should_be_stubbed(Api.new, :only => [:slow_method, :external_service])
57
65
 
58
66
  == Installation
59
67
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
data/lib/stubborn.rb CHANGED
@@ -16,41 +16,29 @@ module Stubborn
16
16
  end
17
17
 
18
18
  def initialize(proxy_target, options = {})
19
+ @proxy_target = proxy_target
20
+
21
+ options[:except] = [options[:except]].flatten.compact.map{|m| m.to_s}
22
+ options[:only] = [options[:only]].flatten.compact.map{|m| m.to_s}
19
23
  options = {:class => proxy_target.class}.merge(options)
20
24
  @label = options[:label]
21
- @proxy_target = proxy_target
22
- @klass = options[:class]
23
- @methods_to_skip = ["respond_to?", "is_a?", "kind_of?", "equal?", "eql?", "==", "==="]
25
+ @class = options[:class]
26
+ @methods_to_skip = ["respond_to?", "is_a?", "kind_of?", "equal?", "eql?", "==", "==="] + options[:except]
27
+ @only_methods = options[:only]
24
28
  end
25
29
 
26
30
  def class
27
- @klass
31
+ @class
28
32
  end
29
33
 
30
34
  def method_missing(method_name, *args, &block)
31
- comes_from_another = comes_from_another_method_missing?
32
- begin
33
- set_comes_from_another
34
- result = @proxy_target.send(method_name, *args, &block)
35
- return result if @methods_to_skip.include?(method_name.to_s) || comes_from_another
36
- raise MissedStubException.new(@label || @proxy_target, method_name, args, result, Suggesters::RSpecSuggester)
37
- ensure
38
- clear_comes_from_another unless comes_from_another
39
- end
40
- end
41
-
42
- private
43
- def comes_from_another_method_missing?
44
- Thread.current["stubborn_objects_ids"] = {} if Thread.current["stubborn_objects_ids"].nil?
45
- Thread.current["stubborn_objects_ids"][self.__id__]
46
- end
47
-
48
- def set_comes_from_another
49
- Thread.current["stubborn_objects_ids"][self.__id__] = true
50
- end
51
-
52
- def clear_comes_from_another
53
- Thread.current["stubborn_objects_ids"].delete(self.__id__)
35
+ were_we_already_processing_a_missed_stub = Thread.current["inside_missed_stub"]
36
+ Thread.current["inside_missed_stub"] = true
37
+ result = @proxy_target.send(method_name, *args, &block)
38
+ return result if !@only_methods.include?(method_name.to_s) && !@only_methods.empty? || @methods_to_skip.include?(method_name.to_s) || were_we_already_processing_a_missed_stub
39
+ raise MissedStubException.new(@label || @proxy_target, method_name, args, result, Suggesters::RSpecSuggester)
40
+ ensure
41
+ Thread.current["inside_missed_stub"] = false unless were_we_already_processing_a_missed_stub
54
42
  end
55
43
  end
56
44
 
data/stubborn.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{stubborn}
5
- s.version = "0.1.5"
5
+ s.version = "0.2.0"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Daniel Cadenas"]
9
- s.date = %q{2009-10-02}
9
+ s.date = %q{2009-10-05}
10
10
  s.email = %q{dcadenas@gmail.com}
11
11
  s.extra_rdoc_files = [
12
12
  "LICENSE",
@@ -1,8 +1,10 @@
1
1
  require 'test_helper'
2
2
 
3
- def reset_test_api_class
4
- name = "TestApi"
5
- Object.__send__(:remove_const, name) if Object.const_defined?(name)
3
+ module FilteringTest; end
4
+
5
+ def reset_FilteringTest_api_class
6
+ name = "Api"
7
+ FilteringTest.send(:remove_const, name) if FilteringTest.const_defined?(name)
6
8
  klass = Class.new do
7
9
  def instance_method_1
8
10
  "instance_method_1 called"
@@ -20,34 +22,56 @@ def reset_test_api_class
20
22
  "class_method_2 called"
21
23
  end
22
24
  end
23
- Object.const_set(name, klass)
25
+ FilteringTest.const_set(name, klass)
24
26
  end
25
27
 
26
- __END__
27
- PENDING
28
-
29
28
  Expectations do
30
29
  expect "instance_method_1 called" do
31
- api = TestApi.new
32
- Stubborn.should_be_stubbed(api, :except => :instance_method_1)
30
+ reset_FilteringTest_api_class
31
+ api = FilteringTest::Api.new
32
+ api = Stubborn.should_be_stubbed(api, :except => [:instance_method_1, :instance_method_2])
33
33
  api.instance_method_1
34
34
  end
35
35
 
36
+ expect "instance_method_2 called" do
37
+ reset_FilteringTest_api_class
38
+ api = FilteringTest::Api.new
39
+ api = Stubborn.should_be_stubbed(api, :except => [:instance_method_1, :instance_method_2])
40
+ api.instance_method_2
41
+ end
42
+
36
43
  expect Stubborn::MissedStubException do
37
- api = TestApi.new
38
- Stubborn.should_be_stubbed(api, :except => :instance_method_1)
44
+ reset_FilteringTest_api_class
45
+ api = FilteringTest::Api.new
46
+ api = Stubborn.should_be_stubbed(api, :except => :instance_method_1)
39
47
  api.instance_method_2
40
48
  end
41
49
 
42
50
  expect "instance_method_2 called" do
43
- api = TestApi.new
44
- Stubborn.should_be_stubbed(api, :only => :instance_method_1)
51
+ reset_FilteringTest_api_class
52
+ api = FilteringTest::Api.new
53
+ api = Stubborn.should_be_stubbed(api, :only => :instance_method_1)
45
54
  api.instance_method_2
46
55
  end
47
56
 
48
57
  expect Stubborn::MissedStubException do
49
- api = TestApi.new
50
- Stubborn.should_be_stubbed(api, :only => :instance_method_1)
58
+ reset_FilteringTest_api_class
59
+ api = FilteringTest::Api.new
60
+ api = Stubborn.should_be_stubbed(api, :only => :instance_method_1)
61
+ api.instance_method_1
62
+ end
63
+
64
+ expect Stubborn::MissedStubException do
65
+ reset_FilteringTest_api_class
66
+ api = FilteringTest::Api.new
67
+ api = Stubborn.should_be_stubbed(api, :only => [:instance_method_1, :instance_method_2])
51
68
  api.instance_method_1
52
69
  end
70
+
71
+ expect Stubborn::MissedStubException do
72
+ reset_FilteringTest_api_class
73
+ api = FilteringTest::Api.new
74
+ api = Stubborn.should_be_stubbed(api, :only => [:instance_method_1, :instance_method_2])
75
+ api.instance_method_2
76
+ end
53
77
  end
@@ -4,7 +4,7 @@ module Test; end
4
4
 
5
5
  def reset_test_api_class
6
6
  name = "Api"
7
- Test.__send__(:remove_const, name) if Test.const_defined?(name)
7
+ Test.send(:remove_const, name) if Test.const_defined?(name)
8
8
  klass = Class.new do
9
9
  def plus_one(number)
10
10
  number + 1
@@ -84,11 +84,10 @@ Expectations do
84
84
  reset_test_api_class
85
85
  api = Test::Api.new
86
86
  api = Stubborn.should_be_stubbed(api)
87
- message = ""
88
87
  begin
89
88
  api.plus_one(0)
90
89
  rescue => e
91
- message = e.message
90
+ e.message
92
91
  end
93
92
  end
94
93
 
@@ -96,11 +95,10 @@ Expectations do
96
95
  reset_test_api_class
97
96
  api = Test::Api.new
98
97
  api = Stubborn.should_be_stubbed(api, :label => "Api.singleton")
99
- message = ""
100
98
  begin
101
99
  api.plus_one(0)
102
100
  rescue => e
103
- message = e.message
101
+ e.message
104
102
  end
105
103
  end
106
104
 
@@ -141,35 +139,32 @@ Expectations do
141
139
  expect "You've missed adding a stub. Consider this suggestions:\ntest_api_instance.stub!(:plus_one).with(0).and_return(1)\ntest_api_instance.stub!(:plus_one).and_return(1)" do
142
140
  reset_test_api_class
143
141
  Stubborn.should_be_stubbed(Test::Api)
144
- message = ""
145
142
  api = Test::Api.new
146
143
  begin
147
144
  api.plus_one(0)
148
145
  rescue => e
149
- message = e.message
146
+ e.message
150
147
  end
151
148
  end
152
149
 
153
150
  expect "You've missed adding a stub. Consider this suggestions:\ntest_api_instance.stub!(:plus_three).with(1).and_return(4)\ntest_api_instance.stub!(:plus_three).and_return(4)" do
154
151
  reset_test_api_class
155
152
  Stubborn.should_be_stubbed(Test::Api)
156
- message = ""
157
153
  api = Test::Api.new
158
154
  begin
159
155
  api.plus_three(1)
160
156
  rescue => e
161
- message = e.message
157
+ e.message
162
158
  end
163
159
  end
164
160
 
165
161
  expect "You've missed adding a stub. Consider this suggestions:\nTest::Api.stub!(:plus_four).with(1).and_return(5)\nTest::Api.stub!(:plus_four).and_return(5)" do
166
162
  reset_test_api_class
167
163
  Stubborn.should_be_stubbed(Test::Api)
168
- message = ""
169
164
  begin
170
165
  Test::Api.plus_four(1)
171
166
  rescue => e
172
- message = e.message
167
+ e.message
173
168
  end
174
169
  end
175
170
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stubborn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Cadenas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-02 00:00:00 -03:00
12
+ date: 2009-10-05 00:00:00 -02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15