diff options
Diffstat (limited to 'spec')
| -rw-r--r-- | spec/action_file_executor_spec.rb | 94 | ||||
| -rw-r--r-- | spec/family_spec.rb | 71 | ||||
| -rw-r--r-- | spec/family_tree_manager_spec.rb | 35 | ||||
| -rw-r--r-- | spec/family_tree_spec.rb | 34 |
4 files changed, 138 insertions, 96 deletions
diff --git a/spec/action_file_executor_spec.rb b/spec/action_file_executor_spec.rb index cab34b3..ce2fa11 100644 --- a/spec/action_file_executor_spec.rb +++ b/spec/action_file_executor_spec.rb @@ -2,17 +2,16 @@ require 'tempfile' require_relative '../lib/action_file_executor' -require_relative '../lib/family_tree_manager' RSpec.describe ActionFileExecutor do let(:invalid_file_path) { 'non_existent_file.txt' } - let(:family_tree_manager) { instance_double('FamilyTreeManager') } + let(:family_tree) { instance_double('FamilyTree') } let(:tempfile) { Tempfile.new('actions.txt') } before do - allow(FamilyTreeManager).to receive(:instance).and_return(family_tree_manager) - allow(family_tree_manager).to receive(:add_child) - allow(family_tree_manager).to receive(:query_hierarchy) + allow(FamilyTree).to receive(:instance).and_return(family_tree) + allow(family_tree).to receive(:add_child) + allow(family_tree).to receive(:get_relationship).and_return([]) end after do @@ -21,6 +20,13 @@ RSpec.describe ActionFileExecutor do end describe '#initialize' do + context 'when the file exists' do + it 'initializes successfully' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + expect(action_file_executor).to be_an_instance_of(ActionFileExecutor) + end + end + context 'when the file does not exist' do it 'prints an error message and exits' do expect do @@ -28,49 +34,83 @@ RSpec.describe ActionFileExecutor do end.to output("Error: The file 'non_existent_file.txt' does not exist.\n").to_stdout.and raise_error(SystemExit) end end + end - context 'when the file exists' do - it 'initializes successfully' do - tempfile.puts('ADD_CHILD Mother Child Male') + 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("# A comment\n") + tempfile.write("\n") + tempfile.write("INVALID ACTION\n") + tempfile.write("GET_RELATIONSHIP \"Mother's Name\" \"Child's Name\"\n") tempfile.rewind + end + it 'processes all lines in the file' do action_file_executor = ActionFileExecutor.new(tempfile.path) - expect(action_file_executor).to be_an_instance_of(ActionFileExecutor) + allow(action_file_executor).to receive(:process_line) + + action_file_executor.execute_actions + expect(action_file_executor).to have_received(:process_line).exactly(5).times + end + + it 'executes actions for non-empty and non-comment lines' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + 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 end end end - describe '#execute_actions' do - context 'with valid actions' do - before do - tempfile.puts('ADD_CHILD Mother Child Male') - tempfile.puts('GET_RELATIONSHIP Child Maternal-Uncle') - tempfile.rewind + describe '#process_line' do + context 'with a comment line' do + it 'does not execute the action' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + expect(action_file_executor.send(:process_line, '# This is a comment')).to be_nil end + end - it 'calls the add_child method on FamilyTreeManager when it encounters the ADD_CHILD action' do + context 'with an empty line' do + it 'does not execute the action' do action_file_executor = ActionFileExecutor.new(tempfile.path) - action_file_executor.execute_actions - expect(family_tree_manager).to have_received(:add_child).with('Mother', 'Child', 'Male') + expect(action_file_executor.send(:process_line, '')).to be_nil end + end + end - it 'calls the query_hierarchy method on FamilyTreeManager when it encounters the GET_RELATIONSHIP action' do + describe '#extract_action_and_params' do + it 'returns the action and params from the line' do + action_file_executor = ActionFileExecutor.new(tempfile.path) + expect(action_file_executor.send(:extract_action_and_params, + 'ADD_CHILD "Mother\'s Name" "Child\'s Name"')).to eq(['ADD_CHILD', + ["Mother's Name", + "Child's Name"]]) + end + end + + describe '#execute_action' 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.execute_actions - expect(family_tree_manager).to have_received(:query_hierarchy).with('Child', 'Maternal-Uncle') + action_file_executor.send(:execute_action, 'ADD_CHILD', ["Mother's Name", "Child's Name"]) end end - context 'with unsupported actions' do - before do - tempfile.puts('ADD_MOTHER Child Mother') - tempfile.rewind + 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']) end + end - it 'prints an error message when it encounters an unsupported action' do + context 'with an unsupported action' do + it 'prints an error message' do expect do action_file_executor = ActionFileExecutor.new(tempfile.path) - action_file_executor.execute_actions + 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 end diff --git a/spec/family_spec.rb b/spec/family_spec.rb new file mode 100644 index 0000000..7247648 --- /dev/null +++ b/spec/family_spec.rb @@ -0,0 +1,71 @@ +# frozen_string_literal: true + +require_relative '../lib/family' +require_relative '../lib/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 diff --git a/spec/family_tree_manager_spec.rb b/spec/family_tree_manager_spec.rb deleted file mode 100644 index a1161ae..0000000 --- a/spec/family_tree_manager_spec.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -require_relative '../lib/family_tree_manager' - -RSpec.describe FamilyTreeManager do - let(:family_tree_manager) { FamilyTreeManager.instance } - let(:family_tree) { instance_double(FamilyTree) } - - before do - family_tree_manager.instance_variable_set(:@family_tree, family_tree) - end - - describe '#initialize' do - it 'creates a family tree' do - expect(family_tree_manager.instance_variable_get(:@family_tree)).to eq(family_tree) - end - end - - describe '#add_child' do - it 'is defined' do - expect(family_tree_manager).to respond_to(:add_child) - end - - it 'calls add_child on the family tree' do - expect(family_tree).to receive(:add_child).with('Child', 'Mother', 'Father') - family_tree_manager.add_child('Child', 'Mother', 'Father') - end - end - - describe '#query_hierarchy' do - it 'is defined' do - expect(family_tree_manager).to respond_to(:query_hierarchy) - end - end -end diff --git a/spec/family_tree_spec.rb b/spec/family_tree_spec.rb index 2059a89..ba05aa3 100644 --- a/spec/family_tree_spec.rb +++ b/spec/family_tree_spec.rb @@ -3,38 +3,4 @@ require_relative '../lib/family_tree' RSpec.describe FamilyTree do - describe '#initialize' do - it 'creates an empty array' do - family_tree = FamilyTree.new - expect(family_tree.instance_variable_get(:@people)).to eq([]) - end - end - - describe '#add_child' do - it 'is defined' do - family_tree = FamilyTree.new - expect(family_tree).to respond_to(:add_child) - end - - it 'prints the params' do - family_tree = FamilyTree.new - expect do - family_tree.add_child('Child', 'Mother', 'Father') - end.to output("Adding Child with params: Child, Mother, Father\n").to_stdout - end - end - - describe '#query_hierarchy' do - it 'is defined' do - family_tree = FamilyTree.new - expect(family_tree).to respond_to(:query_hierarchy) - end - - it 'prints the params' do - family_tree = FamilyTree.new - expect do - family_tree.query_hierarchy('Child') - end.to output("Querying Hierarcy with params: Child\n").to_stdout - end - end end |
