spec/lib/sinclair/method_definition_spec.rb in sinclair-1.10.0 vs spec/lib/sinclair/method_definition_spec.rb in sinclair-1.11.0

- old
+ new

@@ -3,10 +3,52 @@ require 'spec_helper' describe Sinclair::MethodDefinition do let(:method_name) { :the_method } + describe '.build' do + subject(:definition) do + definition_class.new(method_name, **options) + end + + let(:definition_class) { Class.new(described_class) } + let(:options) { {} } + let(:klass) { Class.new } + let(:type) { Sinclair::MethodBuilder::CLASS_METHOD } + + context 'when the builder has not been defined' do + it do + expect { definition.build(klass, type) } + .to raise_error(NotImplementedError) + end + end + + context 'when the builder has been defined' do + let(:definition_class) do + Class.new(described_class) do + def code_line + '10' + end + end + end + + before do + definition_class.build_with(Sinclair::MethodBuilder::StringMethodBuilder) + end + + it do + expect { definition.build(klass, type) } + .not_to raise_error + end + + it 'builds the method using the builder' do + expect { definition.build(klass, type) } + .to add_class_method(method_name).to(klass) + end + end + end + describe '.default_value' do subject(:klass) { Class.new(described_class) } let(:value) { Random.rand } let(:instance) { klass.new(:other_method) } @@ -42,10 +84,12 @@ end end end describe '.for' do + let(:klass) { Class.new } + context 'when there are no options nor block' do let(:type) { :call } let(:arguments) { %i[attr_reader some_attribute other_attribute] } it do @@ -57,11 +101,32 @@ expect(described_class.for(type, *arguments)) .to be_a(described_class::CallDefinition) end it 'initializes it correctly' do - expect(described_class.for(type, *arguments).code_string) - .to eq('attr_reader :some_attribute, :other_attribute') + expect { klass.module_eval(&described_class.for(type, *arguments).code_block) } + .to add_method(:some_attribute).to(klass) + end + end + + context 'when type is not given' do + let(:type) { nil } + let(:method_name) { :the_method } + let(:block) { proc { 10 } } + + it do + expect(described_class.for(type, method_name, &block)) + .to be_a(described_class) + end + + it 'infers the definition from arguments' do + expect(described_class.for(type, method_name, &block)) + .to be_a(described_class::BlockDefinition) + end + + it 'initializes it correctly' do + expect(described_class.for(type, method_name, &block).name) + .to eq(method_name) end end context 'when a block is given' do let(:type) { :block }