diff options
| author | Szymon Szukalski <szymon@skas.io> | 2024-10-25 01:19:25 +1100 |
|---|---|---|
| committer | Szymon Szukalski <szymon@skas.io> | 2024-10-25 01:19:25 +1100 |
| commit | fa50df797d8fcb1e3636683cf2ff1582f35bc181 (patch) | |
| tree | d5a68c46854fcfecf548e0f13a438bc01f8e490c /spec | |
| parent | a89fa6a27bb0e2b9797dd01052aaf22b38296821 (diff) | |
Implement extended relationships
- Support uncle, aunt, in-law, son, daughter relationships
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/action_file_executor_spec.rb | 20 | ||||
| -rw-r--r-- | spec/family_tree_spec.rb | 75 |
2 files changed, 36 insertions, 59 deletions
diff --git a/spec/action_file_executor_spec.rb b/spec/action_file_executor_spec.rb index ce2fa11..1d97f6b 100644 --- a/spec/action_file_executor_spec.rb +++ b/spec/action_file_executor_spec.rb @@ -31,7 +31,7 @@ RSpec.describe ActionFileExecutor do it 'prints an error message and exits' do expect do ActionFileExecutor.new(invalid_file_path) - end.to output("Error: The file 'non_existent_file.txt' does not exist.\n").to_stdout.and raise_error(SystemExit) + end.to output("Error: The file 'non_existent_file.txt' does not exist.\n").to_stderr.and raise_error(SystemExit) end end end @@ -39,11 +39,11 @@ RSpec.describe ActionFileExecutor do describe '#execute_actions' do context 'with a valid file' do before do - tempfile.write("ADD_CHILD \"Mother's Name\" \"Child's Name\"\n") + tempfile.write("ADD_CHILD \"Mother's Name\" \"Child's Name\" Male\n") tempfile.write("# A comment\n") tempfile.write("\n") tempfile.write("INVALID ACTION\n") - tempfile.write("GET_RELATIONSHIP \"Mother's Name\" \"Child's Name\"\n") + tempfile.write("GET_RELATIONSHIP \"Mother's Name\" \"Son\"\n") tempfile.rewind end @@ -60,7 +60,7 @@ RSpec.describe ActionFileExecutor do allow(action_file_executor).to receive(:execute_action) action_file_executor.execute_actions - expect(action_file_executor).to have_received(:execute_action).exactly(3).times + expect(action_file_executor).to have_received(:execute_action).exactly(2).times end end end @@ -95,23 +95,25 @@ RSpec.describe ActionFileExecutor do context 'with the ADD_CHILD action' do it 'calls the add_child method on FamilyTree' do action_file_executor = ActionFileExecutor.new(tempfile.path) - action_file_executor.send(:execute_action, 'ADD_CHILD', ["Mother's Name", "Child's Name"]) + action_file_executor.send(:execute_action, 'ADD_CHILD', ["Mother's Name", "Child's Name", 'Male']) + expect(family_tree).to have_received(:add_child).with("Mother's Name", "Child's Name", 'Male') end end context 'with the GET_RELATIONSHIP action' do it 'calls the get_relationship method on FamilyTree' do action_file_executor = ActionFileExecutor.new(tempfile.path) - action_file_executor.send(:execute_action, 'GET_RELATIONSHIP', ["Child's Name", 'Maternal-Uncle']) + action_file_executor.send(:execute_action, 'GET_RELATIONSHIP', ["Mother's Name", 'Son']) + expect(family_tree).to have_received(:get_relationship).with("Mother's Name", 'Son') end end context 'with an unsupported action' do - it 'prints an error message' do + it 'does nothing' do + action_file_executor = ActionFileExecutor.new(tempfile.path) expect do - action_file_executor = ActionFileExecutor.new(tempfile.path) action_file_executor.send(:execute_action, 'ADD_MOTHER', ["Child's Name", "Mother's Name"]) - end.to output("Ignoring unsupported action: [ADD_MOTHER]\n").to_stdout + end.not_to raise_error end end end diff --git a/spec/family_tree_spec.rb b/spec/family_tree_spec.rb index dc91719..834acca 100644 --- a/spec/family_tree_spec.rb +++ b/spec/family_tree_spec.rb @@ -30,11 +30,31 @@ RSpec.describe FamilyTree do end describe '#add_child' do - it 'outputs the correct parameters when adding a child' do - expect do - FamilyTree.instance.add_child('Jane', 'Sam', - 'Male') - end.to output("Adding Child with params: Jane, Sam, Male\n").to_stdout + before do + FamilyTree.instance.add_family(family) + end + + context 'when the mother is present' do + it 'adds a child to the family' do + result = FamilyTree.instance.add_child(mother.name, 'Charlie', Gender::FEMALE) + 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) + 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 'returns CHILD_ADDITION_FAILED' do + FamilyTree.instance.families.clear # Remove existing families + result = FamilyTree.instance.add_child('Unknown Mother', 'Charlie', Gender::FEMALE) + expect(result).to eq('CHILD_ADDITION_FAILED') + end end end @@ -98,49 +118,4 @@ RSpec.describe FamilyTree do end end end - - describe '#find_mother' do - it 'returns the mother if present' do - expect(FamilyTree.instance.find_mother(family)).to eq(mother) - end - - it 'returns NilPerson if no mother is present' do - orphan_family = Family.new(NilPerson.new, father, [child1]) - expect(FamilyTree.instance.find_mother(orphan_family)).to be_a(NilPerson) - end - end - - describe '#find_father' do - it 'returns the father if present' do - expect(FamilyTree.instance.find_father(family)).to eq(father) - end - - it 'returns NilPerson if no father is present' do - orphan_family = Family.new(mother, NilPerson.new, [child1]) - expect(FamilyTree.instance.find_father(orphan_family)).to be_a(NilPerson) - end - end - - describe '#find_siblings' do - it 'returns siblings in the same family' do - result = FamilyTree.instance.find_siblings(family, 'Anna') - expect(result).to eq([child2]) - end - - it 'returns empty array if there are no siblings' do - single_child_family = Family.new(mother, father, [child1]) - result = FamilyTree.instance.find_siblings(single_child_family, 'Anna') - expect(result).to eq([]) - end - end - - describe '#child_of_family?' do - it 'returns true if the person is a child of the family' do - expect(FamilyTree.instance.child_of_family?(family, 'Anna')).to be true - end - - it 'returns false if the person is not a child of the family' do - expect(FamilyTree.instance.child_of_family?(family, 'Unknown')).to be false - end - end end |
