Feature/with istance initializer - #4
Open
Kadaverin wants to merge 9 commits into
Open
Conversation
hf6cTXNPfyPgOBK6
suggested changes
Jun 26, 2018
There was a problem hiding this comment.
Hello, @Kadaverin. Please use this spec to adjust your factory and make all specs green. Thanks.
# frozen_string_literal: true
require_relative '../lib/factory'
RSpec.describe 'Factory' do
before do
if Object.constants.include?(:Customer)
Object.send(:remove_const, :Customer)
end
end
it 'creates factory in a namespace' do
Factory.new('Customer', :name, :address)
customer = Factory::Customer.new('Dave', '123 Main')
expect(customer.name).to eq('Dave')
expect(customer.address).to eq('123 Main')
end
it 'creates standalone class' do
Customer = Factory.new(:name, :address) do
def greeting
"Hello #{name}!"
end
end
customer = Customer.new('Dave', '123 Main')
expect(customer.greeting).to eq('Hello Dave!')
end
it 'raises ArgumentError when extra args passed' do
Customer = Factory.new(:name, :address) do
def greeting
"Hello #{name}!"
end
end
expect { Customer.new('Dave', '123 Main', 123) }.to raise_error(ArgumentError)
end
it 'equality operator works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
joejr = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
jane = Customer.new('Jane Doe', '456 Elm, Anytown NC', 12_345)
expect(joe).to eq(joejr)
expect(joe).not_to eq(jane)
end
it 'attribute reference operator works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
expect(joe['name']).to eq('Joe Smith')
expect(joe[:name]).to eq('Joe Smith')
expect(joe[0]).to eq('Joe Smith')
expect { joe[-123] }.to raise_error(IndexError)
expect { joe[+123] }.to raise_error(IndexError)
expect { joe[:a] }.to raise_error(NameError)
expect(joe[1.5]).to eq('123 Maple, Anytown NC')
end
it 'attribute assignment operator works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
joe['name'] = 'Luke'
joe[:zip] = '90210'
expect(joe.name).to eq('Luke')
expect(joe.zip).to eq('90210')
expect { joe[:abc] = '123' }.to raise_error(NameError)
expect { joe[-123] = '123' }.to raise_error(IndexError)
expect { joe[+123] = '123' }.to raise_error(IndexError)
end
it 'dig works as expected' do
Customer = Factory.new(:a)
c = Customer.new(Customer.new(b: [1, 2, 3]))
expect(c.dig(:a, :a, :b, 0)).to eq(1)
expect(c.dig(:b, 0)).to be_nil
expect { c.dig(:a, :a, :b, :c) }.to raise_error(TypeError)
end
it 'each works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
each_elements = []
joe.each { |x| each_elements << x }
expect(each_elements).to match_array(['Joe Smith', '123 Maple, Anytown NC', 12_345])
end
it 'each_pair works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
each_elements = []
joe.each_pair { |name, value| each_elements << "#{name} => #{value}" }
expect(each_elements).to match_array(['name => Joe Smith', 'address => 123 Maple, Anytown NC', 'zip => 12345'])
end
it 'length (size) works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
expect(joe.length).to eq(3)
expect(joe.size).to eq(3)
end
it 'members works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
expect(joe.members).to match_array(%i[name address zip])
end
it 'selects works as expected' do
Customer = Factory.new(:a, :b, :c, :d, :e, :f)
l = Customer.new(11, 22, 33, 44, 55, 66)
result = l.select(&:even?)
expect(result).to match_array([22, 44, 66])
end
it 'to_a works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
expect(joe.to_a[1]).to eq('123 Maple, Anytown NC')
end
it 'values_at works as expected' do
Customer = Factory.new(:name, :address, :zip)
joe = Customer.new('Joe Smith', '123 Maple, Anytown NC', 12_345)
expect(joe.values_at(0, 2)).to eq(['Joe Smith', 12_345])
expect { joe.values_at(-123) }.to raise_error(IndexError)
expect { joe.values_at(+123) }.to raise_error(IndexError)
end
end
hf6cTXNPfyPgOBK6
approved these changes
Jun 26, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Если этот вариант не понравится , то глянь в develop ветку.
Feature/with istance initializer - здесь интересный способ использования модуля. Но скорее just for fun. В develop код может показаться более адекватным , но не таким красивым , как с использованием этой фичи