From c8b917b1e0f217bf529918d540c49d07a69dba3e Mon Sep 17 00:00:00 2001 From: Szymon Szukalski Date: Fri, 25 Oct 2024 01:51:28 +1100 Subject: Write test cases for new relationships --- lib/family_tree.rb | 2 +- spec/family_tree_spec.rb | 72 ++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/lib/family_tree.rb b/lib/family_tree.rb index 0c9ecef..0909ed1 100644 --- a/lib/family_tree.rb +++ b/lib/family_tree.rb @@ -83,7 +83,7 @@ class FamilyTree case relationship.downcase when 'child' - children.map(&:name).join(' ') + children.empty? ? 'NONE' : children.map(&:name).join(' ') when 'son' sons = children.select { |child| child.gender == Gender::MALE } sons.empty? ? 'NONE' : sons.map(&:name).join(' ') diff --git a/spec/family_tree_spec.rb b/spec/family_tree_spec.rb index 3228214..90d08b3 100644 --- a/spec/family_tree_spec.rb +++ b/spec/family_tree_spec.rb @@ -9,38 +9,42 @@ RSpec.describe FamilyTree do let(:father) { Person.new('John', Gender::MALE) } let(:child1) { Person.new('Anna', Gender::FEMALE) } let(:child2) { Person.new('Bob', Gender::MALE) } + let(:child3) { Person.new('Charlie', Gender::MALE) } + let(:maternal_aunt) { Person.new('Alice', Gender::FEMALE) } + let(:paternal_aunt) { Person.new('Catherine', Gender::FEMALE) } + let(:maternal_uncle) { Person.new('Mark', Gender::MALE) } + let(:paternal_uncle) { Person.new('David', Gender::MALE) } + let(:family) { Family.new(mother, father, [child1, child2]) } before do - # Reset FamilyTree singleton before each test FamilyTree.instance.families.clear + FamilyTree.instance.add_family(Family.new(NilPerson.new, NilPerson.new, [mother, maternal_aunt, maternal_uncle])) + FamilyTree.instance.add_family(Family.new(NilPerson.new, NilPerson.new, [father, paternal_aunt, paternal_uncle])) + FamilyTree.instance.add_family(family) end 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 - before { FamilyTree.instance.add_family(family) } - context 'when the mother is present' do it 'successfully adds a child to the family' do - result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::FEMALE) + result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::MALE) expect(result).to eq('CHILD_ADDED') expect(family.children.last.name).to eq('Charlie') end it 'does not add a duplicate child' do - FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::FEMALE) - result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::FEMALE) + FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::MALE) + result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::MALE) expect(result).to eq('CHILD_ADDITION_FAILED') expect(family.children.count { |child| child.name.casecmp('Charlie').zero? }).to eq(1) end @@ -49,15 +53,13 @@ RSpec.describe FamilyTree do context 'when the mother is not present' do it 'fails to add a child and returns CHILD_ADDITION_FAILED' do FamilyTree.instance.families.clear # Clear existing families - result = FamilyTree.instance.add_child('Unknown Mother', 'Charlie', Gender::FEMALE) + result = FamilyTree.instance.add_child('Unknown Mother', 'Charlie', Gender::MALE) expect(result).to eq('CHILD_ADDITION_FAILED') end end end describe '#get_relationship' do - before { FamilyTree.instance.add_family(family) } - context 'finding parents' do it 'returns the mother\'s name if present' do expect(FamilyTree.instance.get_relationship('Anna', 'mother')).to eq(mother.name) @@ -98,6 +100,54 @@ RSpec.describe FamilyTree do end end + context 'finding children' do + it 'returns all children when queried' do + expect(FamilyTree.instance.get_relationship(father.name, 'child')).to eq("#{child1.name} #{child2.name}") + end + + it 'returns only sons when queried' do + FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::MALE) + expect(FamilyTree.instance.get_relationship(father.name, 'son')).to eq("#{child2.name} Charlie") + end + + it 'returns only daughters when queried' do + expect(FamilyTree.instance.get_relationship(father.name, 'daughter')).to eq(child1.name) + end + + it 'returns NONE if there are no children' do + single_child_family = Family.new(mother, father, []) + FamilyTree.instance.families.clear + FamilyTree.instance.add_family(single_child_family) + + expect(FamilyTree.instance.get_relationship(father.name, 'child')).to eq('NONE') + end + end + + context 'finding aunts and uncles' do + it 'returns maternal aunts' do + expect(FamilyTree.instance.get_relationship(child1.name, 'maternal-aunt')).to eq(maternal_aunt.name) + end + + it 'returns paternal aunts' do + expect(FamilyTree.instance.get_relationship(child1.name, 'paternal-aunt')).to eq(paternal_aunt.name) + end + + it 'returns maternal uncles' do + expect(FamilyTree.instance.get_relationship(child1.name, 'maternal-uncle')).to eq(maternal_uncle.name) + end + + it 'returns paternal uncles' do + expect(FamilyTree.instance.get_relationship(child1.name, 'paternal-uncle')).to eq(paternal_uncle.name) + end + end + + # TODO: Add tests for finding in-laws + context 'finding in-laws' do + it 'returns sister-in-law' do; end + + it 'returns brother-in-law' do; end + end + context 'invalid relationships' do it 'returns UNSUPPORTED_RELATIONSHIP for unsupported types' do expect(FamilyTree.instance.get_relationship('Anna', 'uncle')).to eq('UNSUPPORTED_RELATIONSHIP') -- cgit v1.2.3