diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/family_tree_spec.rb | 123 |
1 files changed, 56 insertions, 67 deletions
diff --git a/spec/family_tree_spec.rb b/spec/family_tree_spec.rb index 4dbe83e..dcff803 100644 --- a/spec/family_tree_spec.rb +++ b/spec/family_tree_spec.rb @@ -9,50 +9,37 @@ 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 - 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 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::MALE) + 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') - 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::MALE) - result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::MALE) + 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') - expect(family.children.count { |child| child.name.casecmp('Charlie').zero? }).to eq(1) end end context 'when the mother is not present' do it 'fails to add a child and returns PERSON_NOT_FOUND' do - FamilyTree.instance.families.clear # Clear existing families result = FamilyTree.instance.add_child('Unknown Mother', 'Charlie', Gender::MALE) expect(result).to eq('PERSON_NOT_FOUND') end @@ -62,96 +49,98 @@ RSpec.describe FamilyTree do describe '#get_relationship' do context 'finding parents' do it 'returns the mother\'s name if present' do - expect(FamilyTree.instance.get_relationship('Anna', 'mother')).to eq(mother.name) + 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('Anna', 'father')).to eq(father.name) + expect(FamilyTree.instance.get_relationship('Charlie', 'father')).to eq('King Arthur') end it 'returns PERSON_NOT_FOUND if the person has no mother' do - orphan_family = Family.new(NilPerson.new, father, [child1]) - FamilyTree.instance.families.clear - FamilyTree.instance.add_family(orphan_family) - - expect(FamilyTree.instance.get_relationship('Anna', 'mother')).to eq('PERSON_NOT_FOUND') + 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 - orphan_family = Family.new(mother, NilPerson.new, [child1]) - FamilyTree.instance.families.clear - FamilyTree.instance.add_family(orphan_family) - - expect(FamilyTree.instance.get_relationship('Anna', 'father')).to eq('PERSON_NOT_FOUND') + 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('Anna', 'siblings')).to eq(child2.name) + expect(FamilyTree.instance.get_relationship('Draco', 'siblings')).to eq('Aster') end it 'returns NONE if there are no siblings' do - single_child_family = Family.new(mother, father, [child1]) - FamilyTree.instance.families.clear - FamilyTree.instance.add_family(single_child_family) - - expect(FamilyTree.instance.get_relationship('Anna', 'siblings')).to eq('NONE') + 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(father.name, 'child')).to eq("#{child1.name} #{child2.name}") + expect(FamilyTree.instance.get_relationship('Percy', 'child')).to eq('Molly Lucy') 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") + expect(FamilyTree.instance.get_relationship('Alice', 'son')).to eq('Ron') end it 'returns only daughters when queried' do - expect(FamilyTree.instance.get_relationship(father.name, 'daughter')).to eq(child1.name) + expect(FamilyTree.instance.get_relationship('Alice', 'daughter')).to eq('Ginny') 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') + expect(FamilyTree.instance.get_relationship('Charlie', '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) + it 'returns NONE if there are no sons' do + expect(FamilyTree.instance.get_relationship('Charlie', 'son')).to eq('NONE') end - it 'returns paternal aunts' do - expect(FamilyTree.instance.get_relationship(child1.name, 'paternal-aunt')).to eq(paternal_aunt.name) + it 'returns NONE if there are no daugthers' do + expect(FamilyTree.instance.get_relationship('Charlie', 'daughter')).to eq('NONE') end + end + end - it 'returns maternal uncles' do - expect(FamilyTree.instance.get_relationship(child1.name, 'maternal-uncle')).to eq(maternal_uncle.name) - 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 uncles' do - expect(FamilyTree.instance.get_relationship(child1.name, 'paternal-uncle')).to eq(paternal_uncle.name) - end + it 'returns paternal aunts' do + expect(FamilyTree.instance.get_relationship('William', 'paternal-aunt')).to eq('Lily') end - # TODO: Add tests for finding in-laws - context 'finding in-laws' do - it 'returns sister-in-law' do; end + it 'returns maternal uncles' do + expect(FamilyTree.instance.get_relationship('Aster', 'maternal-uncle')).to eq('Hugo') + end - it 'returns brother-in-law' do; end + it 'returns paternal uncles' do + expect(FamilyTree.instance.get_relationship('Ginny', 'paternal-uncle')).to eq('James') end + end - context 'invalid relationships' do - it 'returns false for unsupported types' do - expect(FamilyTree.instance.get_relationship('Anna', 'uncle')).to eq(false) - 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 |
