Skip to content

WIP: Removing string evals #848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 14, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions lib/rspec/rails/adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ def assertion_instance
assertion_modules.each do |mod|
mod.public_instance_methods.each do |method|
next if method == :method_missing || method == "method_missing"
class_eval <<-EOM, __FILE__, __LINE__ + 1
def #{method}(*args, &block)
assertion_instance.send(:#{method}, *args, &block)
end
EOM
define_method(method.to_sym) do |*args, &block|
assertion_instance.send(method.to_sym, *args, &block)
end
end
end
end
Expand Down Expand Up @@ -142,11 +140,9 @@ def assertion_method_names
# @api private
def define_assertion_delegators
assertion_method_names.each do |m|
class_eval <<-CODE, __FILE__, __LINE__ + 1
def #{m}(*args, &block)
assertion_delegator.send :#{m}, *args, &block
end
CODE
define_method(m.to_sym) do |*args, &block|
assertion_delegator.send(m.to_sym, *args, &block)
end
end
end
end
Expand Down
54 changes: 28 additions & 26 deletions lib/rspec/rails/mocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,39 +110,41 @@ def self.primary_key; :id; end
end
end
end
m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
def @object.is_a?(other)
#{model_class}.ancestors.include?(other)
end unless #{stubs.has_key?(:is_a?)}

def @object.kind_of?(other)
#{model_class}.ancestors.include?(other)
end unless #{stubs.has_key?(:kind_of?)}
msingleton = m.singleton_class
msingleton.__send__(:define_method, :is_a?) do |other|
model_class.ancestors.include?(other)
end unless stubs.has_key?(:is_a?)

def @object.instance_of?(other)
other == #{model_class}
end unless #{stubs.has_key?(:instance_of?)}
msingleton.__send__(:define_method, :kind_of?) do |other|
model_class.ancestors.include?(other)
end unless stubs.has_key?(:kind_of?)

def @object.__model_class_has_column?(method_name)
#{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s)
end
msingleton.__send__(:define_method, :instance_of?) do |other|
other == model_class
end unless stubs.has_key?(:instance_of?)

def @object.respond_to?(method_name, include_private=false)
__model_class_has_column?(method_name) ? true : super
end unless #{stubs.has_key?(:respond_to?)}
msingleton.__send__(:define_method, :__model_class_has_column?) do |method_name|
model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
end

def @object.method_missing(m, *a, &b)
respond_to?(m) ? null_object? ? self : nil : super
end
msingleton.__send__(:define_method, :respond_to?) do |method_name, *args|
include_private = args.first || false
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
end unless stubs.has_key?(:respond_to?)

msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
end

def @object.class
#{model_class}
end unless #{stubs.has_key?(:class)}
msingleton.__send__(:define_method, :class) do
model_class
end unless stubs.has_key?(:class)

def @object.to_s
"#{model_class.name}_#{to_param}"
end unless #{stubs.has_key?(:to_s)}
CODE
mock_param = to_param
msingleton.__send__(:define_method, :to_s) do
"#{model_class.name}_#{mock_param}"
end unless stubs.has_key?(:to_s)
yield m if block_given?
end
end
Expand Down
33 changes: 16 additions & 17 deletions spec/rspec/rails/mocks/mock_model_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe "mock_model(RealModel)" do

context "given a String" do
context "that does not represent an existing constant" do
it "class says it's name" do
Expand Down Expand Up @@ -121,15 +120,15 @@
before(:each) do
@model = mock_model(SubMockableModel)
end

it "says it is_a?(RealModel)" do
@model.is_a?(SubMockableModel).should be(true)
end

it "says it is_a?(OtherModel) if RealModel is an ancestors" do
@model.is_a?(MockableModel).should be(true)
end

it "can be stubbed" do
mock_model(MockableModel, :is_a? => true).is_a?(:Foo).should be_truthy
end
Expand All @@ -139,15 +138,15 @@
before(:each) do
@model = mock_model(SubMockableModel)
end

it "says it is kind_of? if RealModel is" do
@model.kind_of?(SubMockableModel).should be(true)
end

it "says it is kind_of? if RealModel's ancestor is" do
@model.kind_of?(MockableModel).should be(true)
end

it "can be stubbed" do
mock_model(MockableModel, :kind_of? => true).kind_of?(:Foo).should be_truthy
end
Expand All @@ -157,15 +156,15 @@
before(:each) do
@model = mock_model(SubMockableModel)
end

it "says it is instance_of? if RealModel is" do
@model.instance_of?(SubMockableModel).should be(true)
end

it "does not say it instance_of? if RealModel isn't, even if it's ancestor is" do
@model.instance_of?(MockableModel).should be(false)
end

it "can be stubbed" do
mock_model(MockableModel, :instance_of? => true).instance_of?(:Foo).should be_truthy
end
Expand Down Expand Up @@ -223,7 +222,7 @@
@model.respond_to?("title_before_type_cast").should be(false)
end
end

context "with as_null_object" do
it "says it will respond_to?(key) if RealModel has the attribute 'key'" do
@model.as_null_object.respond_to?("column_a").should be(true)
Expand All @@ -247,25 +246,25 @@
model = NonActiveRecordModel.new
model.should respond_to(:to_param)
end

context "with as_null_object" do
it "says it will not respond_to?(xxx_before_type_cast)" do
model = NonActiveRecordModel.new.as_null_object
model.respond_to?("title_before_type_cast").should be(false)
end
end
end

it "can be stubbed" do
mock_model(MockableModel, :respond_to? => true).respond_to?(:foo).should be_truthy
end
end

describe "#class" do
it "returns the mocked model" do
mock_model(MockableModel).class.should eq(MockableModel)
end

it "can be stubbed" do
mock_model(MockableModel, :class => String).class.should be(String)
end
Expand All @@ -275,7 +274,7 @@
it "returns (model.name)_(model#to_param)" do
mock_model(MockableModel).to_s.should == "MockableModel_#{to_param}"
end

it "can be stubbed" do
mock_model(MockableModel, :to_s => "this string").to_s.should == "this string"
end
Expand Down Expand Up @@ -325,7 +324,7 @@
mock_model(MockableModel).should be_valid
end
end

context "stubbed with false" do
it "returns false" do
mock_model(MockableModel, :valid? => false).should_not be_valid
Expand Down