require 'spec_helper.rb'

describe AttrRequired do
    before do
        @a, @b, @c = A.new, B.new, C.new
    end

    describe '.attr_required' do
        it 'should define accessible attributes' do
            expect(@a).to respond_to :attr_required_a
            expect(@a).to respond_to :attr_required_a=
            expect(@b).to respond_to :attr_required_b
            expect(@b).to respond_to :attr_required_b=
        end

        it 'should be inherited' do
            expect(@b).to respond_to :attr_required_a
            expect(@b).to respond_to :attr_required_a=
        end

        context 'when already optional' do
            it 'should be optional' do
                expect(@c.attr_required?(:attr_optional_b)).to be_truthy
                expect(@c.attr_optional?(:attr_optional_b)).to be_falsey
            end
        end

        context 'when AttrOptional not included' do
            it 'should do nothing' do
                expect(OnlyRequired.required_attributes).to eq([:only_required])
            end
        end
    end

    describe '.attr_required?' do
        it 'should answer whether the attributes is required or not' do
            expect(A.attr_required?(:attr_required_a)).to be_truthy
            expect(B.attr_required?(:attr_required_a)).to be_truthy
            expect(B.attr_required?(:attr_required_b)).to be_truthy
            expect(B.attr_required?(:to_s)).to be_falsey
        end
    end

    describe '#attr_required?' do
        it 'should answer whether the attributes is required or not' do
            expect(@a.attr_required?(:attr_required_a)).to be_truthy
            expect(@b.attr_required?(:attr_required_a)).to be_truthy
            expect(@b.attr_required?(:attr_required_b)).to be_truthy
            expect(@a.attr_required?(:attr_required_b)).to be_falsey
            expect(@b.attr_required?(:to_s)).to be_falsey
        end
    end

    describe '#attr_missing?' do
        it 'should answer whether any attributes are missing' do
            expect(@a.attr_missing?).to be_truthy
            expect(@b.attr_missing?).to be_truthy
            @a.attr_required_a = 'attr_required_a'
            @b.attr_required_a = 'attr_required_a'
            expect(@a.attr_missing?).to be_falsey
            expect(@b.attr_missing?).to be_truthy
            @b.attr_required_b = 'attr_required_b'
            expect(@b.attr_missing?).to be_falsey
        end
    end

    describe '#attr_missing!' do
        it 'should raise AttrMissing error when any attributes are missing' do
            expect(lambda { @a.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
            expect(lambda { @b.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
            @a.attr_required_a = 'attr_required_a'
            @b.attr_required_a = 'attr_required_a'
            expect(lambda { @a.attr_missing! }).not_to raise_error
            expect(lambda { @b.attr_missing! }).to raise_error(AttrRequired::AttrMissing)
            @b.attr_required_b = 'attr_required_b'
            expect(lambda { @b.attr_missing! }).not_to raise_error
        end
    end

    describe '#attr_missing' do
        it 'should return missing attributes keys' do
            expect(@a.attr_missing).to eq([:attr_required_a])
            expect(@b.attr_missing).to eq([:attr_required_a, :attr_required_b])
            @a.attr_required_a = 'attr_required_a'
            @b.attr_required_a = 'attr_required_a'
            expect(@a.attr_missing).to eq([])
            expect(@b.attr_missing).to eq([:attr_required_b])
        end
    end

    describe '.required_attributes' do
        it 'should return all required attributes keys' do
            expect(A.required_attributes).to eq([:attr_required_a])
            expect(B.required_attributes).to eq([:attr_required_a, :attr_required_b])
        end
    end

    describe '#required_attributes' do
        it 'should return required attributes keys' do
            expect(@a.required_attributes).to eq([:attr_required_a])
            expect(@b.required_attributes).to eq([:attr_required_a, :attr_required_b])
        end
    end

    describe '.undef_required_attributes' do
        it 'should undefine accessors and remove from required attributes' do
            expect(C.required_attributes).not_to include :attr_required_a
            expect(@c.required_attributes).not_to include :attr_required_a
            expect(@c).not_to respond_to :attr_required_a
            expect(@c).not_to respond_to :attr_required_a=
        end
    end
end
