spec/rspec/substitute_for_spec.rb in surrogate-0.5.5 vs spec/rspec/substitute_for_spec.rb in surrogate-0.6.0

- old
+ new

@@ -100,13 +100,13 @@ specify 'when combined' do surrogate = Surrogate.endow(Class.new) { define :surrogate_class_meth }.define :surrogate_instance_meth klass = Class.new { def self.api_class_meth()end; def api_instance_meth() end } expect { surrogate.should substitute_for klass }.to \ - raise_error(RSpec::Expectations::ExpectationNotMetError, "Was not substitutable because surrogate has extra instance methods: [:surrogate_instance_meth], "\ - "has extra class methods: [:surrogate_class_meth], "\ - "is missing instance methods: [:api_instance_meth], "\ + raise_error(RSpec::Expectations::ExpectationNotMetError, "Was not substitutable because surrogate has extra instance methods: [:surrogate_instance_meth]\n"\ + "has extra class methods: [:surrogate_class_meth]\n"\ + "is missing instance methods: [:api_instance_meth]\n"\ "is missing class methods: [:api_class_meth]") end specify "when negated (idk why you'd ever want this, though)" do expect { surrogate.should_not substitute_for Class.new }.to \ @@ -143,10 +143,96 @@ example 'false when other is missing instance and class methods' do klass = Class.new { def self.extra_method()end; def extra_method()end } expect { Surrogate.endow(Class.new) { define :class_meth }.define(:instance_meth).should substitute_for klass, subset: true }.to \ raise_error(RSpec::Expectations::ExpectationNotMetError, - "Was not substitutable because surrogate has extra instance methods: [:instance_meth], has extra class methods: [:class_meth]") + "Was not substitutable because surrogate has extra instance methods: [:instance_meth]\nhas extra class methods: [:class_meth]") + end + end + end + + + + context 'type substitutability -- specified with types: true/false option (DEFAULTS TO TRUE)' do + it 'is turned on by default' do + klass = Class.new { def instance_meth(a) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { } + surrogate.should_not substitute_for klass + surrogate.should substitute_for klass, types: false + end + + it 'disregards when argument names differ' do + klass = Class.new { def instance_meth(a) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |b| } + surrogate.should substitute_for klass, names: false, types: true + end + + it 'disregards when surrogate has no body for an api method' do + klass = Class.new { def instance_meth(a) end } + surrogate = Surrogate.endow(Class.new).define :instance_meth + surrogate.should substitute_for klass, types: true + end + + it 'disregards when real object has natively implemented methods that cannot be reflected on' do + Array.method(:[]).parameters.should == [[:rest]] # make sure Array signatures aren't changing across versions or something + Array.instance_method(:insert).parameters.should == [[:rest]] + surrogate = Surrogate.endow(Class.new) { define(:[]) { |a,b,c| } }.define(:insert) { |a,b,c| } + surrogate.should substitute_for Array, subset: true, types: true + end + + context 'returns true if argument types match exactly. Examples:' do + example 'true when exact match' do + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should substitute_for klass, types: true + end + + example 'false when missing block' do + klass = Class.new { def instance_meth(a, b=1, *c, d) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d| } + surrogate.should_not substitute_for klass, types: true + end + + example 'false when missing splatted args' do + klass = Class.new { def instance_meth(a, b=1, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, d, &e| } + surrogate.should_not substitute_for klass + end + + example 'false when missing optional args' do + klass = Class.new { def instance_meth(a, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should_not substitute_for klass + + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + end + + example 'false when missing required args' do + klass = Class.new { def instance_meth(b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |b=1, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + + klass = Class.new { def instance_meth(a, b=1, *c, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, d, &e| } + surrogate.should_not substitute_for klass, types: true + + klass = Class.new { def instance_meth(a, b=1, *c, d, &e) end } + surrogate = Surrogate.endow(Class.new).define(:instance_meth) { |a, b=1, *c, &e| } + surrogate.should_not substitute_for klass, types: true end end end end