summaryrefslogtreecommitdiff
path: root/spec/family_tree_spec.rb
blob: dcff80394553e8564a2cb843d79aa865e6c15f35 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true

require_relative '../lib/family_tree'
require_relative '../lib/person'
require_relative '../lib/family'

RSpec.describe FamilyTree do
  let(:mother) { Person.new('Jane', Gender::FEMALE) }
  let(:father) { Person.new('John', Gender::MALE) }
  let(:child1) { Person.new('Anna', Gender::FEMALE) }
  let(:child2) { Person.new('Bob', Gender::MALE) }

  let(:family) { Family.new(mother, father, [child1, child2]) }

  describe '#add_family' do
    it 'adds a family to the family list' do
      FamilyTree.instance.add_family(family)
      expect(FamilyTree.instance.families).to include(family)
    end

    it 'does not add a duplicate family' do
      FamilyTree.instance.add_family(family)
      expect { FamilyTree.instance.add_family(family) }.not_to(change { FamilyTree.instance.families.count })
    end
  end

  describe '#add_child' do
    context 'when the mother is present' do
      it 'successfully adds a child to the family and returns CHILD_ADDED' do
        result = FamilyTree.instance.add_child('Queen Margaret', 'Jonathan', Gender::MALE)
        expect(result).to eq('CHILD_ADDED')
      end

      it 'does not add a duplicate child and returns CHILD_ADDITION_FAILED' do
        FamilyTree.instance.add_child('Queen Margaret', 'Charlie', Gender::MALE)
        result = FamilyTree.instance.add_child('Queen Margaret', 'Charlie', Gender::MALE)
        expect(result).to eq('CHILD_ADDITION_FAILED')
      end
    end

    context 'when the mother is not present' do
      it 'fails to add a child and returns PERSON_NOT_FOUND' do
        result = FamilyTree.instance.add_child('Unknown Mother', 'Charlie', Gender::MALE)
        expect(result).to eq('PERSON_NOT_FOUND')
      end
    end
  end

  describe '#get_relationship' do
    context 'finding parents' do
      it 'returns the mother\'s name if present' do
        expect(FamilyTree.instance.get_relationship('Charlie', 'mother')).to eq('Queen Margaret')
      end

      it 'returns the father\'s name if present' do
        expect(FamilyTree.instance.get_relationship('Charlie', 'father')).to eq('King Arthur')
      end

      it 'returns PERSON_NOT_FOUND if the person has no mother' do
        expect(FamilyTree.instance.get_relationship('King Arthur', 'mother')).to eq('PERSON_NOT_FOUND')
      end

      it 'returns PERSON_NOT_FOUND if the person has no father' do
        expect(FamilyTree.instance.get_relationship('Queen Margaret', 'father')).to eq('PERSON_NOT_FOUND')
      end
    end

    context 'finding siblings' do
      it 'returns sibling\'s name if present' do
        expect(FamilyTree.instance.get_relationship('Draco', 'siblings')).to eq('Aster')
      end

      it 'returns NONE if there are no siblings' do
        expect(FamilyTree.instance.get_relationship('Remus', 'siblings')).to eq('NONE')
      end
    end

    context 'finding children' do
      it 'returns all children when queried' do
        expect(FamilyTree.instance.get_relationship('Percy', 'child')).to eq('Molly Lucy')
      end

      it 'returns only sons when queried' do
        expect(FamilyTree.instance.get_relationship('Alice', 'son')).to eq('Ron')
      end

      it 'returns only daughters when queried' do
        expect(FamilyTree.instance.get_relationship('Alice', 'daughter')).to eq('Ginny')
      end

      it 'returns NONE if there are no children' do
        expect(FamilyTree.instance.get_relationship('Charlie', 'child')).to eq('NONE')
      end

      it 'returns NONE if there are no sons' do
        expect(FamilyTree.instance.get_relationship('Charlie', 'son')).to eq('NONE')
      end

      it 'returns NONE if there are no daugthers' do
        expect(FamilyTree.instance.get_relationship('Charlie', 'daughter')).to eq('NONE')
      end
    end
  end

  context 'finding aunts and uncles' do
    it 'returns maternal aunts' do
      expect(FamilyTree.instance.get_relationship('Remus', 'maternal-aunt')).to eq('Dominique')
    end

    it 'returns paternal aunts' do
      expect(FamilyTree.instance.get_relationship('William', 'paternal-aunt')).to eq('Lily')
    end

    it 'returns maternal uncles' do
      expect(FamilyTree.instance.get_relationship('Aster', 'maternal-uncle')).to eq('Hugo')
    end

    it 'returns paternal uncles' do
      expect(FamilyTree.instance.get_relationship('Ginny', 'paternal-uncle')).to eq('James')
    end
  end

  context 'finding in-laws' do
    it 'returns sister-in-law via spouse' do
      expect(FamilyTree.instance.get_relationship('Alice', 'sister-in-law')).to eq('Lily')
    end

    it 'returns sister-in-law via sibling' do
      expect(FamilyTree.instance.get_relationship('Percy', 'sister-in-law')).to eq('Flora Helen')
    end

    it 'returns brother-in-law via spouse' do
      expect(FamilyTree.instance.get_relationship('Malfoy', 'brother-in-law')).to eq('Hugo')
    end

    it 'returns brother-in-law via sibling' do
      expect(FamilyTree.instance.get_relationship('Percy', 'brother-in-law')).to eq('Harry')
    end
  end

  context 'invalid relationships' do
    it 'returns false for unsupported types' do
      expect(FamilyTree.instance.get_relationship('King Arthur', 'pet')).to eq(false)
    end
  end
end