stubborn 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -15,12 +15,12 @@ module Stubborn
15
15
  private
16
16
  def friendly_name(object)
17
17
  return "\"#{object}\"" if object.respond_to?(:to_str)
18
- return object.inspect if object.respond_to?(:to_int) || object.is_a?(Hash)
18
+ return object.inspect if object.respond_to?(:to_int) || object.is_a?(Hash) || object.nil? || object == true || object == false
19
19
 
20
20
  if object.is_a?(Class)
21
21
  object.name
22
22
  else
23
- "#{object.class.name.downcase}_instance"
23
+ "#{object.class.name.downcase.gsub(/\W+/, '_')}_instance"
24
24
  end
25
25
  end
26
26
  end
data/lib/stubborn.rb CHANGED
@@ -28,16 +28,36 @@ module Stubborn
28
28
  end
29
29
 
30
30
  def method_missing(method_name, *args, &block)
31
- result = @proxy_target.send(method_name, *args, &block)
32
- return result if @methods_to_skip.include?(method_name.to_s)
33
- raise MissedStubException.new(@label || @proxy_target, method_name, args, result, Suggesters::RSpecSuggester)
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__)
34
54
  end
35
55
  end
36
56
 
37
57
  class ProxyForClass < ProxyForInstance
38
58
  def initialize(proxy_target, options = {})
39
59
  super
40
- redefine_const(proxy_target.name, self) unless proxy_target.name.strip.empty?
60
+ redefine_const(proxy_target, self) unless proxy_target.name.strip.empty?
41
61
  end
42
62
 
43
63
  def name
@@ -50,9 +70,13 @@ module Stubborn
50
70
  end
51
71
 
52
72
  private
53
- def redefine_const(name, value)
54
- Object.__send__(:remove_const, name) if Object.const_defined?(name)
55
- Object.const_set(name, value)
73
+ def redefine_const(const, value)
74
+ const_parts = const.name.split('::')
75
+ const_name = const_parts.pop
76
+ parent_const = const_parts.inject(Object){|a, c| a.const_get(c) }
77
+
78
+ parent_const.__send__(:remove_const, const_name) if parent_const.const_defined?(const_name)
79
+ parent_const.const_set(const_name, value)
56
80
  end
57
81
  end
58
82
  end
@@ -3,7 +3,7 @@ module Stubborn
3
3
  module RSpecSuggester
4
4
  def self.suggestions(object_label, method_name, args, result)
5
5
  with = args.strip.empty? ? nil : ".with(#{args})"
6
- and_return = result.nil? ? nil : ".and_return(#{result})"
6
+ and_return = result == "nil" ? nil : ".and_return(#{result})"
7
7
  suggestions = []
8
8
  suggestions << "#{object_label}.stub!(:#{method_name})#{with}#{and_return}"
9
9
  suggestions << "#{object_label}.stub!(:#{method_name})#{and_return}"
data/stubborn.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{stubborn}
5
- s.version = "0.1.4"
5
+ s.version = "0.1.5"
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"]
@@ -1,29 +1,42 @@
1
1
  require 'test_helper'
2
2
 
3
+ module Test; end
4
+
3
5
  def reset_test_api_class
4
- name = "TestApi"
5
- Object.__send__(:remove_const, name) if Object.const_defined?(name)
6
+ name = "Api"
7
+ Test.__send__(:remove_const, name) if Test.const_defined?(name)
6
8
  klass = Class.new do
7
9
  def plus_one(number)
8
10
  number + 1
9
11
  end
10
12
 
13
+ def plus_three(number)
14
+ number = plus_one(number)
15
+ number = plus_one(number)
16
+ plus_one(number)
17
+ end
18
+
11
19
  def self.plus_two(number)
12
20
  number + 2
13
21
  end
22
+
23
+ def self.plus_four(number)
24
+ number = Test::Api.plus_two(number)
25
+ Test::Api.plus_two(number)
26
+ end
14
27
  end
15
- Object.const_set(name, klass)
28
+ Test.const_set(name, klass)
16
29
  end
17
30
 
18
31
  Expectations do
19
32
  expect 2 do
20
33
  reset_test_api_class
21
- TestApi.new.plus_one(1)
34
+ Test::Api.new.plus_one(1)
22
35
  end
23
36
 
24
- expect "TestApi" do
37
+ expect "Test::Api" do
25
38
  reset_test_api_class
26
- api = TestApi.new
39
+ api = Test::Api.new
27
40
  api = Stubborn.should_be_stubbed(api)
28
41
  api.class.name
29
42
  end
@@ -31,16 +44,16 @@ Expectations do
31
44
  %w[is_a? kind_of?].each do |method|
32
45
  expect true do
33
46
  reset_test_api_class
34
- api = TestApi.new
47
+ api = Test::Api.new
35
48
  api = Stubborn.should_be_stubbed(api)
36
- api.send(method, TestApi)
49
+ api.send(method, Test::Api)
37
50
  end
38
51
  end
39
52
 
40
53
  %w[equal? eql? == ===].each do |method|
41
54
  expect true do
42
55
  reset_test_api_class
43
- api = TestApi.new
56
+ api = Test::Api.new
44
57
  proxied_api = Stubborn.should_be_stubbed(api)
45
58
  proxied_api.send(method, api)
46
59
  end
@@ -48,28 +61,28 @@ Expectations do
48
61
 
49
62
  expect true do
50
63
  reset_test_api_class
51
- api = TestApi.new
64
+ api = Test::Api.new
52
65
  api = Stubborn.should_be_stubbed(api)
53
66
  api.respond_to?(:plus_one)
54
67
  end
55
68
 
56
69
  expect Stubborn::MissedStubException do
57
70
  reset_test_api_class
58
- api = TestApi.new
71
+ api = Test::Api.new
59
72
  api = Stubborn.should_be_stubbed(api)
60
73
  api.plus_one(1)
61
74
  end
62
75
 
63
76
  expect 3 do
64
77
  reset_test_api_class
65
- api = TestApi.new
78
+ api = Test::Api.new
66
79
  api = Stubborn.should_be_stubbed(api)
67
80
  api.class.plus_two(1)
68
81
  end
69
82
 
70
- expect "You've missed adding a stub. Consider this suggestions:\ntestapi_instance.stub!(:plus_one).with(0).and_return(1)\ntestapi_instance.stub!(:plus_one).and_return(1)" do
83
+ 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
71
84
  reset_test_api_class
72
- api = TestApi.new
85
+ api = Test::Api.new
73
86
  api = Stubborn.should_be_stubbed(api)
74
87
  message = ""
75
88
  begin
@@ -81,7 +94,7 @@ Expectations do
81
94
 
82
95
  expect "You've missed adding a stub. Consider this suggestions:\nApi.singleton.stub!(:plus_one).with(0).and_return(1)\nApi.singleton.stub!(:plus_one).and_return(1)" do
83
96
  reset_test_api_class
84
- api = TestApi.new
97
+ api = Test::Api.new
85
98
  api = Stubborn.should_be_stubbed(api, :label => "Api.singleton")
86
99
  message = ""
87
100
  begin
@@ -91,45 +104,74 @@ Expectations do
91
104
  end
92
105
  end
93
106
 
94
- expect "TestApi" do
107
+ expect "Test::Api" do
95
108
  reset_test_api_class
96
- Stubborn.should_be_stubbed(TestApi)
97
- api = TestApi.new
109
+ Stubborn.should_be_stubbed(Test::Api)
110
+ api = Test::Api.new
98
111
  api.class.name
99
112
  end
100
113
 
101
114
  expect true do
102
115
  reset_test_api_class
103
- Stubborn.should_be_stubbed(TestApi)
104
- api = TestApi.new
116
+ Stubborn.should_be_stubbed(Test::Api)
117
+ api = Test::Api.new
105
118
  api.respond_to?(:plus_one)
106
119
  end
107
120
 
108
121
  expect Stubborn::MissedStubException do
109
122
  reset_test_api_class
110
- Stubborn.should_be_stubbed(TestApi)
111
- api = TestApi.new
123
+ Stubborn.should_be_stubbed(Test::Api)
124
+ api = Test::Api.new
112
125
  api.plus_one(1)
113
126
  end
114
127
 
115
128
  expect Stubborn::MissedStubException do
116
129
  reset_test_api_class
117
- Stubborn.should_be_stubbed(TestApi)
118
- api = TestApi.new
130
+ Stubborn.should_be_stubbed(Test::Api)
131
+ api = Test::Api.new
119
132
  api.class.plus_two(1)
120
133
  end
121
134
 
122
- expect "You've missed adding a stub. Consider this suggestions:\ntestapi_instance.stub!(:plus_one).with(0).and_return(1)\ntestapi_instance.stub!(:plus_one).and_return(1)" do
135
+ expect Stubborn::MissedStubException do
123
136
  reset_test_api_class
124
- Stubborn.should_be_stubbed(TestApi)
137
+ Stubborn.should_be_stubbed(Test::Api)
138
+ Test::Api.plus_two(1)
139
+ end
140
+
141
+ 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
+ reset_test_api_class
143
+ Stubborn.should_be_stubbed(Test::Api)
125
144
  message = ""
126
- api = TestApi.new
145
+ api = Test::Api.new
127
146
  begin
128
147
  api.plus_one(0)
129
148
  rescue => e
130
149
  message = e.message
131
150
  end
132
151
  end
152
+
153
+ 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
+ reset_test_api_class
155
+ Stubborn.should_be_stubbed(Test::Api)
156
+ message = ""
157
+ api = Test::Api.new
158
+ begin
159
+ api.plus_three(1)
160
+ rescue => e
161
+ message = e.message
162
+ end
163
+ end
164
+
165
+ 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
+ reset_test_api_class
167
+ Stubborn.should_be_stubbed(Test::Api)
168
+ message = ""
169
+ begin
170
+ Test::Api.plus_four(1)
171
+ rescue => e
172
+ message = e.message
173
+ end
174
+ end
133
175
  end
134
176
 
135
177
 
@@ -6,8 +6,8 @@ class Api; end
6
6
  include Stubborn
7
7
 
8
8
  Expectations do
9
- expect "You've missed adding a stub. Consider this suggestions:\napi_instance.stub!(:method).with(123, AClass, \"hi\", {1=>2}).and_return(aclass_instance)\napi_instance.stub!(:method).and_return(aclass_instance)" do
10
- MissedStubException.new(Api.new, :method, [123, AClass,"hi", {1 => 2}], AClass.new, Suggesters::RSpecSuggester).message
9
+ expect "You've missed adding a stub. Consider this suggestions:\napi_instance.stub!(:method).with(true, false, 123, AClass, \"hi\", {1=>2}).and_return(aclass_instance)\napi_instance.stub!(:method).and_return(aclass_instance)" do
10
+ MissedStubException.new(Api.new, :method, [true, false, 123, AClass,"hi", {1 => 2}], AClass.new, Suggesters::RSpecSuggester).message
11
11
  end
12
12
 
13
13
  expect "You've missed adding a stub. Consider this suggestion:\napi_instance.stub!(:method).and_return(aclass_instance)" do
@@ -17,5 +17,9 @@ Expectations do
17
17
  expect "You've missed adding a stub. Consider this suggestion:\nApi.singleton.stub!(:method).and_return(aclass_instance)" do
18
18
  MissedStubException.new("Api.singleton", :method, [], AClass.new, Suggesters::RSpecSuggester).message
19
19
  end
20
+
21
+ expect "You've missed adding a stub. Consider this suggestion:\nApi.singleton.stub!(:method)" do
22
+ MissedStubException.new("Api.singleton", :method, [], nil, Suggesters::RSpecSuggester).message
23
+ end
20
24
  end
21
25
 
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.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Cadenas