forked from expertiza/expertiza
/
view_translation_substitutor_spec.rb
155 lines (126 loc) · 7.49 KB
/
view_translation_substitutor_spec.rb
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Test of controller for handling view translation substitution and generating translation statistics.
describe ViewTranslationSubstitutor do
let(:substitutor) { ViewTranslationSubstitutor.new }
let(:test_directory) { '../../spec/test_folder' }
let(:test_view) { 'example_view' }
let(:test_translations) { { 'hello' => 'Hello, world!' } }
describe '#substitute' do
let(:substitutor) { ViewTranslationSubstitutor.new }
it 'iterates over the locale hash and processes directories' do
# Create a sample locale hash.
locale = {
'dir1' => { 'view1' => { 'key1' => 'val1' } },
'dir2' => { 'view2' => { 'key2' => 'val2' } }
}
# Expect process_files_in_directory to be called for each directory in the locale hash.
expect(substitutor).to receive(:process_files_in_directory).with('dir1', { 'view1' => { 'key1' => 'val1' } }).and_return('stats1')
expect(substitutor).to receive(:process_files_in_directory).with('dir2', { 'view2' => { 'key2' => 'val2' } }).and_return('stats2')
# Expect File.open to be called with a regex matching translation_stats*.yml and write the processed stats to YAML.
expect(File).to receive(:open).with(/^translation_stats.*\.yml$/, 'w').and_yield(file = double)
expect(file).to receive(:write).with({ 'dir1' => 'stats1', 'dir2' => 'stats2' }.to_yaml)
# Call the substitute method with the sample locale hash.
substitutor.substitute(locale)
end
end
describe '#process_files_in_directory' do
let(:substitutor) { ViewTranslationSubstitutor.new }
context 'when view_hash is not empty' do
let(:dir_name) { 'dir1' }
let(:view_hash) { { 'view1' => { 'key1' => 'val1' } } }
it 'processes each view in the view_hash' do
# Stub process_view method to return 'stats1' and ensure it is called with correct arguments.
allow(substitutor).to receive(:process_view).and_return('stats1')
dir_stats = substitutor.send(:process_files_in_directory, dir_name, view_hash)
expect(dir_stats).to eq({ 'view1' => 'stats1' })
expect(substitutor).to have_received(:process_view).with(dir_name, 'view1', { 'key1' => 'val1' })
end
end
context 'when view_hash is empty' do
# Local variables
let(:dir_name) { 'dir2' }
let(:view_hash) { {} }
it 'returns an empty hash' do
# Call the private method process_files_in_directory with an empty view_hash and ensure it returns an empty hash.
dir_stats = substitutor.send(:process_files_in_directory, dir_name, view_hash)
expect(dir_stats).to eq({})
end
end
end
describe '#process_view' do
# Local variables
let(:substitutor) { ViewTranslationSubstitutor.new }
let(:directory_name) { 'test_folder' }
let(:view_name) { 'example_view' }
let(:translations) { { 'key1' => 'value1', 'key2' => 'value2' } }
context 'when the view file exists' do
before do
# Stub File.exist? to return true and File.open to yield "Existing content".
allow(File).to receive(:exist?).with("./#{directory_name}/#{view_name}.html.erb").and_return(true)
allow(File).to receive(:open).with("./#{directory_name}/#{view_name}.html.erb", 'w').and_yield(StringIO.new("Existing content"))
end
it 'reads the file, processes translations, and writes back the contents' do
# Expect process_translation to be called for each translation and return modified content and stats.
expect(substitutor).to receive(:process_translation).with("Existing content", 'key1', 'value1').and_return(['stats1', 'new_content1'])
expect(substitutor).to receive(:process_translation).with('new_content1', 'key2', 'value2').and_return(['stats2', 'new_content2'])
# Call the process_view method and expect it to return processed view stats.
view_stats = substitutor.send(:process_view, directory_name, view_name, translations)
expect(view_stats).to eq({ 'key1' => 'stats1', 'key2' => 'stats2' })
# Expect File.open to be called twice with the view file path.
expect(File).to have_received(:open).with("./#{directory_name}/#{view_name}.html.erb", 'w').twice
end
end
context 'when the view file does not exist' do
it 'returns "<file not found>"' do
# Call the process_view method and expect it to return "<file not found>".
view_stats = substitutor.send(:process_view, directory_name, view_name, translations)
expect(view_stats).to eq('<file not found>')
end
end
context 'when the alternate view file exists' do
before do
# Stub File.exist? to return false for main view file and true for alternate view file.
allow(File).to receive(:exist?).with("./#{directory_name}/#{view_name}.html.erb").and_return(false)
allow(File).to receive(:exist?).with("./#{directory_name}/_#{view_name}.html.erb").and_return(true)
allow(File).to receive(:open).with("./#{directory_name}/_#{view_name}.html.erb", 'w').and_yield(StringIO.new("Existing content"))
end
it 'reads the alternate file, processes translations, and writes back the contents' do
# Expect process_translation to be called for each translation and return modified content and stats.
expect(substitutor).to receive(:process_translation).with("Existing content", 'key1', 'value1').and_return(['stats1', 'new_content1'])
expect(substitutor).to receive(:process_translation).with('new_content1', 'key2', 'value2').and_return(['stats2', 'new_content2'])
# Call the process_view method and expect it to return processed view stats.
view_stats = substitutor.send(:process_view, directory_name, view_name, translations)
expect(view_stats).to eq({ 'key1' => 'stats1', 'key2' => 'stats2' })
# Expect File.open to be called twice with the alternate view file path.
expect(File).to have_received(:open).with("./#{directory_name}/_#{view_name}.html.erb", 'w').twice
end
end
end
describe '#process_translation' do
# Local variables
let(:contents) { 'This is a test string with some "text" to be replaced.' }
let(:key) { 'other word' }
let(:val) { 'text' }
let(:key2) { 'skipped key' }
let(:val2) { 'string' }
let(:key3) { 'non-existent key' }
let(:val3) { 'non-existent val' }
it 'replaces the correct text with translation keys' do
# Call process_translation with a valid key and value and expect correct replacements and modified content.
replacements, new_contents = substitutor.send(:process_translation, contents, key, val)
expect(replacements).to include("replacements" => ["\"#{val}\""])
expect(new_contents).to include("#{key}")
end
it 'adds to skip when one value is not found but another value is' do
# Call process_translation with a key and value that partially match the contents and expect skips and unmodified content.
skips, new_contents = substitutor.send(:process_translation, contents, key2, val2)
expect(skips).to include("skips" => ["test string with"])
expect(new_contents).not_to include("#{key2}")
end
it 'adds <unmatched> to translation_stats when there is no match' do
# Call process_translation with a key and value that do not match any part of the contents and expect <unmatched> and unmodified content.
translation_stats, new_contents = substitutor.send(:process_translation, contents, key3, val3)
expect(translation_stats).to include("<unmatched>")
expect(new_contents).not_to include("#{key3}")
end
end
end