/
container_spec.rb
496 lines (381 loc) · 12.7 KB
/
container_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# frozen_string_literal: true
require 'spec_helper'
describe Rambling::Trie::Container do
subject(:container) { described_class.new root, compressor }
let(:compressor) { Rambling::Trie::Compressor.new }
let(:root) { Rambling::Trie::Nodes::Raw.new }
describe '.new' do
it 'uses the provided node as root' do
expect(container.root).to be root
end
context 'with a block' do
it 'yields the container' do
yielded = nil
container = described_class.new root, compressor do |c|
yielded = c
end
expect(yielded).to be container
end
end
end
describe '#add' do
# rubocop:disable RSpec/MultipleExpectations
it 'adds the word to the root node' do
add_word container, 'hello'
expect(root.children.size).to eq 1
expect(root.to_a).to eq %w(hello)
end
it 'does nothing with empty strings' do
add_word container, ''
expect(root.children.size).to eq 0
expect(root.to_a).to be_empty
end
# rubocop:enable RSpec/MultipleExpectations
end
describe '#concat' do
# rubocop:disable RSpec/MultipleExpectations
it 'adds all the words to the root node' do
container.concat %w(other words)
expect(root.children.size).to eq 2
expect(root.to_a).to eq %w(other words)
end
it 'returns all the corresponding nodes' do
nodes = container.concat %w(other words)
expect(nodes.first.letter).to eq :o
expect(nodes.last.letter).to eq :w
end
# rubocop:enable RSpec/MultipleExpectations
end
describe '#compress' do
let(:node) { Rambling::Trie::Nodes::Compressed.new }
before do
allow(compressor).to receive(:compress).and_return node
add_word root, 'yes'
node[:yes] = Rambling::Trie::Nodes::Compressed.new
end
it 'compresses the trie using the compressor' do
container.compress
expect(compressor).to have_received(:compress).with root
end
# rubocop:disable RSpec/MultipleExpectations
it 'returns a container with the new root' do
new_container = container.compress
expect(new_container.root).not_to eq root
expect(new_container.root).to eq node
end
# rubocop:enable RSpec/MultipleExpectations
it 'returns a new container' do
expect(container.compress).not_to eq container
end
it 'can compress multiple times' do
container.compress
container.compress
expect(compressor).to have_received(:compress).twice
end
it 'cannot compress the result' do
new_container = container.compress
new_container.compress
expect(compressor).to have_received(:compress).once
end
end
describe '#compress!' do
let(:node) { Rambling::Trie::Nodes::Compressed.new }
context 'with a mocked result' do
before do
allow(compressor).to receive(:compress).and_return node
add_word root, 'yes'
node[:yes] = Rambling::Trie::Nodes::Compressed.new
end
it 'compresses the trie using the compressor' do
container.compress!
expect(compressor).to have_received(:compress).with root
end
# rubocop:disable RSpec/MultipleExpectations
it 'changes to the root returned by the compressor' do
container.compress!
expect(container.root).not_to eq root
expect(container.root).to eq node
end
# rubocop:enable RSpec/MultipleExpectations
it 'returns itself' do
expect(container.compress!).to eq container
end
it 'does not compress multiple times' do
container.compress!
allow(node).to receive(:compressed?).and_return(true)
container.compress!
expect(compressor).to have_received(:compress).once
end
# rubocop:disable RSpec/MultipleExpectations
it 'gets a new root from the compressor' do
container.compress!
expect(container.root).not_to be root
expect(container.root).to be_compressed
expect(root).not_to be_compressed
end
# rubocop:enable RSpec/MultipleExpectations
end
it 'generates a new root with the words from the passed root' do
words = %w(a few words hello hell)
add_words container, words
container.compress!
words.each { |word| expect(container).to include word }
end
describe 'and trying to add a word' do
it 'raises an error' do
add_words container, %w(repay rest repaint)
container.compress!
expect do
add_word container, 'restaurant'
end.to raise_error Rambling::Trie::InvalidOperation
end
end
end
describe '#push' do
# rubocop:disable RSpec/MultipleExpectations
it 'adds all the words to the root node' do
container.push 'other', 'words'
expect(root.children.size).to eq 2
expect(root.to_a).to eq %w(other words)
end
it 'returns all the corresponding nodes' do
nodes = container.push 'other', 'words'
expect(nodes.first.letter).to eq :o
expect(nodes.last.letter).to eq :w
end
# rubocop:enable RSpec/MultipleExpectations
end
describe '#partial_word?' do
context 'with underlying node' do
it_behaves_like 'a propagating node' do
let(:method_name) { :partial_word? }
end
end
context 'when word is contained' do
before { add_words container, %w(hello high) }
it_behaves_like 'a matching container#partial_word'
context 'with compressed root' do
before { container.compress! }
it_behaves_like 'a matching container#partial_word'
end
end
context 'when word is not contained' do
before { add_word container, 'hello' }
it_behaves_like 'a non-matching container#partial_word'
context 'with compressed root' do
before { container.compress! }
it_behaves_like 'a non-matching container#partial_word'
end
end
end
describe '#word?' do
it_behaves_like 'a propagating node' do
let(:method_name) { :word? }
end
context 'when word is contained' do
before { add_words container, %w(hello high) }
it_behaves_like 'a matching container#word'
context 'with compressed root' do
before { container.compress! }
it_behaves_like 'a matching container#word'
end
end
context 'when word is not contained' do
before { add_word container, 'hello' }
it_behaves_like 'a non-matching container#word'
context 'with compressed root' do
before { container.compress! }
it_behaves_like 'a non-matching container#word'
end
end
end
describe '#scan' do
context 'when words that match are contained' do
before do
add_words container, %w(hi hello high hell highlight histerical)
end
it_behaves_like 'a matching container#scan'
context 'with compressed root' do
before { container.compress! }
it_behaves_like 'a matching container#scan'
end
end
context 'when words that match are not contained' do
before { add_word container, 'hello' }
it 'returns an empty array' do
expect(container.scan 'hi').to eq %w()
end
context 'with compressed root' do
before { container.compress! }
it 'returns an empty array' do
expect(container.scan 'hi').to eq %w()
end
end
end
end
describe '#words_within' do
before { add_words container, %w(one word and other words) }
context 'when phrase does not contain any words' do
it 'returns an empty array' do
expect(container.words_within 'xyz').to be_empty
end
context 'with compressed node' do
before { container.compress! }
it 'returns an empty array' do
expect(container.words_within 'xyz').to be_empty
end
end
end
context 'when phrase contains one word at the start of the phrase' do
it_behaves_like 'a matching container#words_within'
context 'with compressed node' do
before { container.compress! }
it_behaves_like 'a matching container#words_within'
end
end
context 'when phrase contains one word at the end of the phrase' do
it 'returns an array with the word found in the phrase' do
expect(container.words_within 'xyz word').to match_array %w(word)
end
context 'with compressed node' do
before { container.compress! }
it 'returns an array with the word found in the phrase' do
expect(container.words_within 'xyz word').to match_array %w(word)
end
end
end
context 'when phrase contains a few words' do
it_behaves_like 'a non-matching container#words_within'
context 'with compressed node' do
before { container.compress! }
it_behaves_like 'a non-matching container#words_within'
end
end
end
describe '#words_within?' do
before { add_words container, %w(one word and other words) }
it_behaves_like 'a matching container#words_within?'
context 'with compressed node' do
before { container.compress! }
it_behaves_like 'a non-matching container#words_within?'
end
end
describe '#==' do
context 'when the root nodes are the same' do
let :other_container do
described_class.new container.root, compressor
end
it 'returns true' do
expect(container).to eq other_container
end
end
context 'when the root nodes are not the same' do
let(:other_root) { Rambling::Trie::Nodes::Raw.new }
let :other_container do
described_class.new other_root, compressor
end
before { add_word other_container, 'hola' }
it 'returns false' do
expect(container).not_to eq other_container
end
end
end
describe '#each' do
before { add_words container, %w(yes no why) }
it 'yields every word previously added' do
yielded = []
container.each { |word| yielded << word }
expect(yielded).to eq %w(yes no why)
end
it 'returns an enumerator when no block is given' do
expect(container.each).to be_instance_of Enumerator
end
it 'iterates through all words contained' do
expect(container.each.to_a).to eq %w(yes no why)
end
end
describe '#inspect' do
before { add_words container, %w(a few words hello hell) }
it 'returns the container class name plus the root inspection' do
expect(container.inspect).to eq one_line <<~CONTAINER
#<Rambling::Trie::Container root: #<Rambling::Trie::Nodes::Raw letter: nil,
terminal: nil,
children: [:a, :f, :w, :h]>>
CONTAINER
end
end
describe 'delegates and aliases' do
before do
allow(root).to receive_messages(
:[] => nil,
add: nil,
as_word: nil,
children: nil,
children_tree: nil,
compressed?: nil,
each: nil,
key?: nil,
inspect: nil,
letter: nil,
parent: nil,
partial_word?: nil,
scan: nil,
size: nil,
to_s: nil,
word?: nil,
)
end
it 'aliases `#include?` to `#word?`' do
container.include? 'words'
expect(root).to have_received(:word?).with %w(w o r d s)
end
it 'aliases `#match?` to `#partial_word?`' do
container.match? 'words'
expect(root).to have_received(:partial_word?).with %w(w o r d s)
end
it 'aliases `#words` to `#scan`' do
container.words 'hig'
expect(root).to have_received(:scan).with %w(h i g)
end
it 'aliases `#<<` to `#add`' do
container << 'words'
expect(root).to have_received(:add).with %i(s d r o w)
end
it 'delegates `#[]` to the root node' do
container[:yep]
expect(root).to have_received(:[]).with :yep
end
it 'delegates `#children` to the root node' do
container.children
expect(root).to have_received :children
end
it 'delegates `#children_tree` to the root node' do
container.children_tree
expect(root).to have_received :children_tree
end
it 'delegates `#compressed?` to the root node' do
container.compressed?
expect(root).to have_received :compressed?
end
it 'delegates `#key?` to the root node' do
container.key? :yup
expect(root).to have_received(:key?).with :yup
end
it 'aliases `#has_key?` to `#key?`' do
container.has_key? :yup
expect(root).to have_received(:key?).with :yup
end
it 'aliases `#has_letter?` to `#has_key?`' do
container.has_letter? :yup
expect(root).to have_received(:key?).with :yup
end
it 'delegates `#inspect` to the root node' do
container.inspect
expect(root).to have_received :inspect
end
it 'delegates `#size` to the root node' do
container.size
expect(root).to have_received :size
end
end
end