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
|
# frozen_string_literal: true
require 'tempfile'
require_relative '../lib/action_file_executor'
RSpec.describe ActionFileExecutor do
let(:invalid_file_path) { 'non_existent_file.txt' }
let(:family_tree) { instance_double('FamilyTree') }
let(:tempfile) { Tempfile.new('actions.txt') }
before do
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
tempfile.close
tempfile.unlink
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
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
end
end
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)
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 '#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
context 'with an empty line' do
it 'does not execute the action' do
action_file_executor = ActionFileExecutor.new(tempfile.path)
expect(action_file_executor.send(:process_line, '')).to be_nil
end
end
end
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.send(:execute_action, 'ADD_CHILD', ["Mother's Name", "Child's Name"])
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'])
end
end
context 'with an unsupported action' do
it 'prints an error message' do
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
end
end
end
|