summaryrefslogtreecommitdiff
path: root/spec/family_spec.rb
blob: 615bbc6c6ef2c0c925ee7bf107748fe117e2aaf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

require_relative '../lib/family'
require_relative '../lib/person'
require_relative '../lib/nil_person'
require_relative '../lib/gender'

RSpec.describe Family do
  describe '#initialize' do
    it 'creates a new family with a mother, father, and children' do
      family = Family.new
      expect(family.mother).to be_a(NilPerson)
      expect(family.father).to be_a(NilPerson)
      expect(family.children).to eq([])
    end

    it 'raises an error if the mother is not a female' do
      jack = Person.new('Jack', Gender::MALE)

      expect { Family.new(jack) }.to raise_error(ArgumentError).with_message('Mother must be female')
    end

    it 'raises an error if the father is not a male' do
      jill = Person.new('Jill', Gender::FEMALE)

      expect { Family.new(NilPerson.new, jill) }.to raise_error(ArgumentError).with_message('Father must be male')
    end
  end

  describe 'assign_mother' do
    it 'raises an error if the mother is not a female' do
      family = Family.new
      jack = Person.new('Jack', Gender::MALE)

      expect { family.assign_mother(jack) }.to raise_error(ArgumentError).with_message('Mother must be female')
    end
  end

  describe 'assign_father' do
    it 'raises an error if the mother is not a female' do
      family = Family.new
      jill = Person.new('Jill', Gender::FEMALE)

      expect { family.assign_father(jill) }.to raise_error(ArgumentError).with_message('Father must be male')
    end
  end

  describe '#add_child' do
    it 'adds a child to the family' do
      family = Family.new
      child = Person

      family.add_child(child)
      expect(family.children).to eq([child])
    end
  end

  describe '#get_siblings' do
    it 'returns all siblings of a person' do
      family = Family.new
      child1 = Person.new('Jack', Gender::MALE)
      child2 = Person.new('Jill', Gender::FEMALE)
      child3 = Person.new('Phil', Gender::MALE)

      family.add_child(child1)
      family.add_child(child2)
      family.add_child(child3)

      expect(family.get_siblings(child1)).to eq([child2, child3])
    end
  end
end