spec/lib/sinclair/method_definition_spec.rb in sinclair-1.3.0 vs spec/lib/sinclair/method_definition_spec.rb in sinclair-1.3.1

- old
+ new

@@ -1,105 +1,64 @@ # frozen_string_literal: true require 'spec_helper' -shared_examples 'MethodDefinition#build' do - it do - expect { method_definition.build(klass) }.to add_method(method_name).to(klass) - end - - describe 'after build' do - before do - method_definition.build(klass) - end - - it 'adds the method to the klass instance' do - expect(instance).to respond_to(method_name) - end - - it 'evaluates return of the method within the instance context' do - expect(instance.the_method).to eq(1) - end - - it 'evaluates in the context of the instance' do - expect { instance.the_method } - .to change { instance.instance_variable_get(:@x) } - .from(nil).to(1) - end - end -end - describe Sinclair::MethodDefinition do let(:klass) { Class.new } let(:instance) { klass.new } describe '#build' do let(:method_name) { :the_method } + context 'when instantiating the class itself' do + subject(:method_definition) do + described_class.new(method_name) + end + + it do + expect { method_definition.build(klass) }.to raise_error( + RuntimeError, + 'Build is implemented in subclasses. ' \ + "Use #{described_class}.from to initialize a proper object" + ) + end + end + context 'when method was defined with a string' do subject(:method_definition) do - described_class.new(method_name, code) + described_class.from(method_name, code) end let(:code) { '@x = @x.to_i + 1' } - it_behaves_like 'MethodDefinition#build' + it_behaves_like 'MethodDefinition#build without cache' - it 'creates a dynamic method' do - method_definition.build(klass) - expect { instance.the_method }.to change(instance, :the_method) - .from(1).to(3) - end - context 'with cached options' do subject(:method_definition) do - described_class.new(method_name, code, cached: true) + described_class.from(method_name, code, cached: cached_option) end - it_behaves_like 'MethodDefinition#build' - - it 'creates a semi-dynamic method' do - method_definition.build(klass) - expect { instance.the_method }.not_to change(instance, :the_method) - end + it_behaves_like 'MethodDefinition#build with cache options' end end context 'when method was defined with a block' do subject(:method_definition) do - described_class.new(method_name) do + described_class.from(method_name) do @x = @x.to_i + 1 end end - it_behaves_like 'MethodDefinition#build' + it_behaves_like 'MethodDefinition#build without cache' - it 'creates a dynamic method' do - method_definition.build(klass) - expect { instance.the_method }.to change(instance, :the_method) - .from(1).to(3) - end - context 'with cached options' do subject(:method_definition) do - described_class.new(method_name, cached: true) do + described_class.from(method_name, cached: cached_option) do @x = @x.to_i + 1 end end - it_behaves_like 'MethodDefinition#build' - - it 'creates a dynamic method' do - method_definition.build(klass) - expect { instance.the_method }.not_to change(instance, :the_method) - end - - it 'sets the instance variable' do - method_definition.build(klass) - expect { instance.the_method } - .to change { instance.instance_variable_get("@#{method_name}") } - .from(nil).to(1) - end + it_behaves_like 'MethodDefinition#build with cache options' end end end end