Skip to content

Commit d21509b

Browse files
author
Sam Phippen
committed
Merge pull request #848 from rspec/remove-string-evals
WIP: Removing string evals
2 parents c1b4ad4 + d0d2162 commit d21509b

File tree

3 files changed

+50
-53
lines changed

3 files changed

+50
-53
lines changed

lib/rspec/rails/adapters.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ def assertion_instance
3636
assertion_modules.each do |mod|
3737
mod.public_instance_methods.each do |method|
3838
next if method == :method_missing || method == "method_missing"
39-
class_eval <<-EOM, __FILE__, __LINE__ + 1
40-
def #{method}(*args, &block)
41-
assertion_instance.send(:#{method}, *args, &block)
42-
end
43-
EOM
39+
define_method(method.to_sym) do |*args, &block|
40+
assertion_instance.send(method.to_sym, *args, &block)
41+
end
4442
end
4543
end
4644
end
@@ -142,11 +140,9 @@ def assertion_method_names
142140
# @api private
143141
def define_assertion_delegators
144142
assertion_method_names.each do |m|
145-
class_eval <<-CODE, __FILE__, __LINE__ + 1
146-
def #{m}(*args, &block)
147-
assertion_delegator.send :#{m}, *args, &block
148-
end
149-
CODE
143+
define_method(m.to_sym) do |*args, &block|
144+
assertion_delegator.send(m.to_sym, *args, &block)
145+
end
150146
end
151147
end
152148
end

lib/rspec/rails/mocks.rb

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,39 +110,41 @@ def self.primary_key; :id; end
110110
end
111111
end
112112
end
113-
m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
114-
def @object.is_a?(other)
115-
#{model_class}.ancestors.include?(other)
116-
end unless #{stubs.has_key?(:is_a?)}
117113

118-
def @object.kind_of?(other)
119-
#{model_class}.ancestors.include?(other)
120-
end unless #{stubs.has_key?(:kind_of?)}
114+
msingleton = m.singleton_class
115+
msingleton.__send__(:define_method, :is_a?) do |other|
116+
model_class.ancestors.include?(other)
117+
end unless stubs.has_key?(:is_a?)
121118

122-
def @object.instance_of?(other)
123-
other == #{model_class}
124-
end unless #{stubs.has_key?(:instance_of?)}
119+
msingleton.__send__(:define_method, :kind_of?) do |other|
120+
model_class.ancestors.include?(other)
121+
end unless stubs.has_key?(:kind_of?)
125122

126-
def @object.__model_class_has_column?(method_name)
127-
#{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s)
128-
end
123+
msingleton.__send__(:define_method, :instance_of?) do |other|
124+
other == model_class
125+
end unless stubs.has_key?(:instance_of?)
129126

130-
def @object.respond_to?(method_name, include_private=false)
131-
__model_class_has_column?(method_name) ? true : super
132-
end unless #{stubs.has_key?(:respond_to?)}
127+
msingleton.__send__(:define_method, :__model_class_has_column?) do |method_name|
128+
model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
129+
end
133130

134-
def @object.method_missing(m, *a, &b)
135-
respond_to?(m) ? null_object? ? self : nil : super
136-
end
131+
msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
132+
include_private = args.first || false
133+
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
134+
end unless stubs.has_key?(:respond_to?)
135+
136+
msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
137+
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
138+
end
137139

138-
def @object.class
139-
#{model_class}
140-
end unless #{stubs.has_key?(:class)}
140+
msingleton.__send__(:define_method, :class) do
141+
model_class
142+
end unless stubs.has_key?(:class)
141143

142-
def @object.to_s
143-
"#{model_class.name}_#{to_param}"
144-
end unless #{stubs.has_key?(:to_s)}
145-
CODE
144+
mock_param = to_param
145+
msingleton.__send__(:define_method, :to_s) do
146+
"#{model_class.name}_#{mock_param}"
147+
end unless stubs.has_key?(:to_s)
146148
yield m if block_given?
147149
end
148150
end

spec/rspec/rails/mocks/mock_model_spec.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'spec_helper'
22

33
describe "mock_model(RealModel)" do
4-
54
context "given a String" do
65
context "that does not represent an existing constant" do
76
it "class says it's name" do
@@ -121,15 +120,15 @@
121120
before(:each) do
122121
@model = mock_model(SubMockableModel)
123122
end
124-
123+
125124
it "says it is_a?(RealModel)" do
126125
@model.is_a?(SubMockableModel).should be(true)
127126
end
128-
127+
129128
it "says it is_a?(OtherModel) if RealModel is an ancestors" do
130129
@model.is_a?(MockableModel).should be(true)
131130
end
132-
131+
133132
it "can be stubbed" do
134133
mock_model(MockableModel, :is_a? => true).is_a?(:Foo).should be_truthy
135134
end
@@ -139,15 +138,15 @@
139138
before(:each) do
140139
@model = mock_model(SubMockableModel)
141140
end
142-
141+
143142
it "says it is kind_of? if RealModel is" do
144143
@model.kind_of?(SubMockableModel).should be(true)
145144
end
146-
145+
147146
it "says it is kind_of? if RealModel's ancestor is" do
148147
@model.kind_of?(MockableModel).should be(true)
149148
end
150-
149+
151150
it "can be stubbed" do
152151
mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo).should be_truthy
153152
end
@@ -157,15 +156,15 @@
157156
before(:each) do
158157
@model = mock_model(SubMockableModel)
159158
end
160-
159+
161160
it "says it is instance_of? if RealModel is" do
162161
@model.instance_of?(SubMockableModel).should be(true)
163162
end
164-
163+
165164
it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
166165
@model.instance_of?(MockableModel).should be(false)
167166
end
168-
167+
169168
it "can be stubbed" do
170169
mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo).should be_truthy
171170
end
@@ -223,7 +222,7 @@
223222
@model.respond_to?("title_before_type_cast").should be(false)
224223
end
225224
end
226-
225+
227226
context "with as_null_object" do
228227
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
229228
@model.as_null_object.respond_to?("column_a").should be(true)
@@ -247,25 +246,25 @@
247246
model = NonActiveRecordModel.new
248247
model.should respond_to(:to_param)
249248
end
250-
249+
251250
context "with as_null_object" do
252251
it "says it will not respond_to?(xxx_before_type_cast)" do
253252
model = NonActiveRecordModel.new.as_null_object
254253
model.respond_to?("title_before_type_cast").should be(false)
255254
end
256255
end
257256
end
258-
257+
259258
it "can be stubbed" do
260259
mock_model(MockableModel, :respond_to? => true).respond_to?(:foo).should be_truthy
261260
end
262261
end
263-
262+
264263
describe "#class" do
265264
it "returns the mocked model" do
266265
mock_model(MockableModel).class.should eq(MockableModel)
267266
end
268-
267+
269268
it "can be stubbed" do
270269
mock_model(MockableModel, :class => String).class.should be(String)
271270
end
@@ -275,7 +274,7 @@
275274
it "returns (model.name)_(model#to_param)" do
276275
mock_model(MockableModel).to_s.should == "MockableModel_#{to_param}"
277276
end
278-
277+
279278
it "can be stubbed" do
280279
mock_model(MockableModel, :to_s => "this string").to_s.should == "this string"
281280
end
@@ -325,7 +324,7 @@
325324
mock_model(MockableModel).should be_valid
326325
end
327326
end
328-
327+
329328
context "stubbed with false" do
330329
it "returns false" do
331330
mock_model(MockableModel, :valid? => false).should_not be_valid

0 commit comments

Comments
 (0)