rambling-trie 2.4.0 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. checksums.yaml +4 -4
  2. data/Dockerfile +28 -0
  3. data/Gemfile +20 -8
  4. data/Guardfile +16 -5
  5. data/README.md +38 -32
  6. data/Rakefile +6 -0
  7. data/Steepfile +35 -0
  8. data/lib/rambling/trie/comparable.rb +2 -2
  9. data/lib/rambling/trie/compressible.rb +1 -1
  10. data/lib/rambling/trie/compressor.rb +22 -19
  11. data/lib/rambling/trie/configuration/properties.rb +10 -6
  12. data/lib/rambling/trie/configuration/provider_collection.rb +14 -9
  13. data/lib/rambling/trie/configuration.rb +2 -3
  14. data/lib/rambling/trie/container.rb +32 -24
  15. data/lib/rambling/trie/enumerable.rb +5 -6
  16. data/lib/rambling/trie/nodes/compressed.rb +26 -16
  17. data/lib/rambling/trie/nodes/node.rb +35 -12
  18. data/lib/rambling/trie/nodes/raw.rb +18 -20
  19. data/lib/rambling/trie/nodes.rb +2 -3
  20. data/lib/rambling/trie/readers/plain_text.rb +3 -3
  21. data/lib/rambling/trie/readers.rb +2 -3
  22. data/lib/rambling/trie/serializers/file.rb +1 -3
  23. data/lib/rambling/trie/serializers/marshal.rb +4 -4
  24. data/lib/rambling/trie/serializers/yaml.rb +3 -3
  25. data/lib/rambling/trie/serializers/zip.rb +13 -5
  26. data/lib/rambling/trie/serializers.rb +2 -3
  27. data/lib/rambling/trie/stringifyable.rb +1 -1
  28. data/lib/rambling/trie/version.rb +1 -1
  29. data/lib/rambling/trie.rb +12 -15
  30. data/rambling-trie.gemspec +4 -10
  31. data/sig/lib/rambling/trie/comparable.rbs +17 -0
  32. data/sig/lib/rambling/trie/compressible.rbs +17 -0
  33. data/sig/lib/rambling/trie/compressor.rbs +17 -0
  34. data/sig/lib/rambling/trie/configuration/properties.rbs +28 -0
  35. data/sig/lib/rambling/trie/configuration/provider_collection.rbs +47 -0
  36. data/sig/lib/rambling/trie/container.rbs +69 -0
  37. data/sig/lib/rambling/trie/enumerable.rbs +23 -0
  38. data/sig/lib/rambling/trie/inspectable.rbs +27 -0
  39. data/sig/lib/rambling/trie/invalid_operation.rbs +7 -0
  40. data/sig/lib/rambling/trie/nodes/compressed.rbs +25 -0
  41. data/sig/lib/rambling/trie/nodes/missing.rbs +9 -0
  42. data/sig/lib/rambling/trie/nodes/node.rbs +69 -0
  43. data/sig/lib/rambling/trie/nodes/raw.rbs +27 -0
  44. data/sig/lib/rambling/trie/readers/plain_text.rbs +9 -0
  45. data/sig/lib/rambling/trie/readers/reader.rbs +9 -0
  46. data/sig/lib/rambling/trie/serializers/file.rbs +8 -0
  47. data/sig/lib/rambling/trie/serializers/marshal.rbs +13 -0
  48. data/sig/lib/rambling/trie/serializers/serializer.rbs +10 -0
  49. data/sig/lib/rambling/trie/serializers/yaml.rbs +13 -0
  50. data/sig/lib/rambling/trie/serializers/zip.rbs +21 -0
  51. data/sig/lib/rambling/trie/stringifyable.rbs +21 -0
  52. data/sig/lib/rambling/trie.rbs +27 -0
  53. data/sig/lib/zip/entry.rbs +11 -0
  54. data/sig/lib/zip/file.rbs +11 -0
  55. metadata +34 -123
  56. data/spec/assets/test_words.en_US.txt +0 -23
  57. data/spec/assets/test_words.es_DO.txt +0 -24
  58. data/spec/integration/rambling/trie_spec.rb +0 -116
  59. data/spec/lib/rambling/trie/comparable_spec.rb +0 -87
  60. data/spec/lib/rambling/trie/compressor_spec.rb +0 -111
  61. data/spec/lib/rambling/trie/configuration/properties_spec.rb +0 -75
  62. data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +0 -177
  63. data/spec/lib/rambling/trie/container_spec.rb +0 -466
  64. data/spec/lib/rambling/trie/enumerable_spec.rb +0 -50
  65. data/spec/lib/rambling/trie/inspectable_spec.rb +0 -62
  66. data/spec/lib/rambling/trie/nodes/compressed_spec.rb +0 -43
  67. data/spec/lib/rambling/trie/nodes/node_spec.rb +0 -9
  68. data/spec/lib/rambling/trie/nodes/raw_spec.rb +0 -184
  69. data/spec/lib/rambling/trie/readers/plain_text_spec.rb +0 -26
  70. data/spec/lib/rambling/trie/readers/reader_spec.rb +0 -14
  71. data/spec/lib/rambling/trie/serializers/file_spec.rb +0 -11
  72. data/spec/lib/rambling/trie/serializers/marshal_spec.rb +0 -10
  73. data/spec/lib/rambling/trie/serializers/serializer_spec.rb +0 -21
  74. data/spec/lib/rambling/trie/serializers/yaml_spec.rb +0 -10
  75. data/spec/lib/rambling/trie/serializers/zip_spec.rb +0 -36
  76. data/spec/lib/rambling/trie/stringifyable_spec.rb +0 -89
  77. data/spec/lib/rambling/trie_spec.rb +0 -244
  78. data/spec/spec_helper.rb +0 -42
  79. data/spec/support/config.rb +0 -15
  80. data/spec/support/helpers/add_word.rb +0 -20
  81. data/spec/support/helpers/one_line_heredoc.rb +0 -11
  82. data/spec/support/shared_examples/a_compressible_trie.rb +0 -46
  83. data/spec/support/shared_examples/a_container_partial_word.rb +0 -17
  84. data/spec/support/shared_examples/a_container_scan.rb +0 -14
  85. data/spec/support/shared_examples/a_container_word.rb +0 -43
  86. data/spec/support/shared_examples/a_container_words_within.rb +0 -44
  87. data/spec/support/shared_examples/a_serializable_trie.rb +0 -26
  88. data/spec/support/shared_examples/a_serializer.rb +0 -60
  89. data/spec/support/shared_examples/a_trie_data_structure.rb +0 -45
  90. data/spec/support/shared_examples/a_trie_node.rb +0 -135
  91. data/spec/support/shared_examples/a_trie_node_implementation.rb +0 -149
  92. data/spec/tmp/.gitkeep +0 -0
@@ -1,244 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- describe Rambling::Trie do
6
- describe '.create' do
7
- let(:root) { Rambling::Trie::Nodes::Raw.new }
8
- let(:compressor) { Rambling::Trie::Compressor.new }
9
- let!(:container) { Rambling::Trie::Container.new root, compressor }
10
-
11
- before do
12
- allow(Rambling::Trie::Container).to receive(:new)
13
- .and_yield(container)
14
- .and_return container
15
- end
16
-
17
- it 'returns a new instance of the trie container' do
18
- expect(described_class.create).to eq container
19
- end
20
-
21
- context 'with a block' do
22
- it 'yields the new container' do
23
- yielded = nil
24
- described_class.create { |trie| yielded = trie }
25
- expect(yielded).to eq container
26
- end
27
- end
28
-
29
- context 'with a filepath' do
30
- let(:filepath) { 'a test filepath' }
31
- let :reader do
32
- instance_double 'Rambling::Trie::Readers::PlainText', :reader
33
- end
34
- let(:words) { %w(a couple of test words over here) }
35
-
36
- before do
37
- receive_and_yield = receive(:each_word)
38
- words.inject receive_and_yield do |yielder, word|
39
- yielder.and_yield word
40
- end
41
-
42
- allow(reader).to receive_and_yield
43
- allow(container).to receive :<<
44
- end
45
-
46
- it 'loads every word' do
47
- described_class.create filepath, reader
48
-
49
- words.each do |word|
50
- expect(container).to have_received(:<<).with word
51
- end
52
- end
53
- end
54
-
55
- context 'without any reader' do
56
- let(:filepath) { 'a test filepath' }
57
- let :reader do
58
- instance_double(
59
- 'Rambling::Trie::Readers::PlainText',
60
- :reader,
61
- each_word: nil,
62
- )
63
- end
64
-
65
- before do
66
- described_class.config do |c|
67
- c.readers.add :default, reader
68
- c.readers.default = reader
69
- end
70
- end
71
-
72
- it 'defaults to a plain text reader' do
73
- described_class.create filepath, nil
74
-
75
- expect(reader).to have_received(:each_word).with filepath
76
- end
77
- end
78
- end
79
-
80
- describe '.load' do
81
- let(:filepath) { 'a path to a file' }
82
- let(:root) { Rambling::Trie::Nodes::Raw.new }
83
- let(:compressor) { Rambling::Trie::Compressor.new }
84
- let(:container) { Rambling::Trie::Container.new root, compressor }
85
- let :serializer do
86
- instance_double(
87
- 'Rambling::True::Serializers::File',
88
- :serializer,
89
- load: root,
90
- )
91
- end
92
-
93
- it 'returns a new container with the loaded root node' do
94
- trie = described_class.load filepath, serializer
95
- expect(trie).to eq container
96
- end
97
-
98
- it 'uses the serializer to load the root node from the given filepath' do
99
- described_class.load filepath, serializer
100
- expect(serializer).to have_received(:load).with filepath
101
- end
102
-
103
- context 'without a serializer' do
104
- let :marshal_serializer do
105
- instance_double(
106
- 'Rambling::Trie::Serializers::Marshal',
107
- :marshal_serializer,
108
- load: nil,
109
- )
110
- end
111
- let :default_serializer do
112
- instance_double(
113
- 'Rambling::Trie::Serializers::File',
114
- :default_serializer,
115
- load: nil,
116
- )
117
- end
118
- let :yaml_serializer do
119
- instance_double(
120
- 'Rambling::Trie::Serializers::Yaml',
121
- :yaml_serializer,
122
- load: nil,
123
- )
124
- end
125
-
126
- before do
127
- described_class.config do |c|
128
- c.serializers.add :default, default_serializer
129
- c.serializers.add :marshal, marshal_serializer
130
- c.serializers.add :yml, yaml_serializer
131
- c.serializers.add :yaml, yaml_serializer
132
-
133
- c.serializers.default = default_serializer
134
- end
135
- end
136
-
137
- [
138
- ['.marshal', :marshal_serializer],
139
- ['.yml', :yaml_serializer],
140
- ['.yaml', :yaml_serializer],
141
- ['', :default_serializer],
142
- ].each do |test_params|
143
- extension, serializer = test_params
144
- filepath = "test#{extension}"
145
-
146
- it "uses extension-based serializer (#{filepath} -> #{serializer})" do
147
- serializer_instance = public_send serializer
148
-
149
- described_class.load filepath
150
- expect(serializer_instance).to have_received(:load).with filepath
151
- end
152
- end
153
- end
154
-
155
- context 'with a block' do
156
- it 'yields the new container' do
157
- yielded = nil
158
-
159
- described_class.load filepath, serializer do |trie|
160
- yielded = trie
161
- end
162
-
163
- expect(yielded).to eq container
164
- end
165
- end
166
- end
167
-
168
- describe '.dump' do
169
- let(:filename) { 'a trie' }
170
- let(:root) { instance_double 'Rambling::Trie::Serializers::Marshal', :root }
171
- let :compressor do
172
- instance_double 'Rambling::Trie::Serializers::Marshal', :compressor
173
- end
174
- let(:trie) { Rambling::Trie::Container.new root, compressor }
175
-
176
- let :marshal_serializer do
177
- instance_double(
178
- 'Rambling::Trie::Serializers::Marshal',
179
- :marshal_serializer,
180
- dump: nil,
181
- )
182
- end
183
- let :yaml_serializer do
184
- instance_double(
185
- 'Rambling::Trie::Serializers::Yaml',
186
- :yaml_serializer,
187
- dump: nil,
188
- )
189
- end
190
- let :default_serializer do
191
- instance_double(
192
- 'Rambling::Trie::Serializers::File',
193
- :default_serializer,
194
- dump: nil,
195
- )
196
- end
197
-
198
- before do
199
- described_class.config do |c|
200
- c.serializers.add :default, default_serializer
201
- c.serializers.add :marshal, marshal_serializer
202
- c.serializers.add :yml, yaml_serializer
203
-
204
- c.serializers.default = default_serializer
205
- end
206
- end
207
-
208
- it 'uses the configured default serializer by default' do
209
- described_class.dump trie, filename
210
- expect(default_serializer).to have_received(:dump).with root, filename
211
- end
212
-
213
- context 'when provided with a format' do
214
- [
215
- ['.marshal', :marshal_serializer],
216
- ['.yml', :yaml_serializer],
217
- ['', :default_serializer],
218
- ].each do |test_params|
219
- extension, serializer = test_params
220
-
221
- it 'uses the corresponding serializer' do
222
- filepath = "#{filename}#{extension}"
223
- serializer_instance = public_send serializer
224
-
225
- described_class.dump trie, filepath
226
- expect(serializer_instance).to have_received(:dump)
227
- .with root, filepath
228
- end
229
- end
230
- end
231
- end
232
-
233
- describe '.config' do
234
- it 'returns the properties' do
235
- expect(described_class.config).to eq described_class.send :properties
236
- end
237
-
238
- it 'yields the properties' do
239
- yielded = nil
240
- described_class.config { |c| yielded = c }
241
- expect(yielded).to eq described_class.send :properties
242
- end
243
- end
244
- end
data/spec/spec_helper.rb DELETED
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'yaml'
4
- require 'simplecov'
5
-
6
- COVERAGE_FILTER = %r{/spec/}.freeze
7
-
8
- if ENV.key? 'COVERALLS_REPO_TOKEN'
9
- require 'coveralls'
10
-
11
- SimpleCov.formatters = [
12
- SimpleCov::Formatter::HTMLFormatter,
13
- Coveralls::SimpleCov::Formatter,
14
- ]
15
-
16
- Coveralls.wear! { add_filter COVERAGE_FILTER }
17
- else
18
- SimpleCov.start { add_filter COVERAGE_FILTER }
19
- end
20
-
21
- require 'rspec'
22
- require 'rambling-trie'
23
- ::SPEC_ROOT = File.dirname __FILE__
24
-
25
- RSpec.configure do |config|
26
- config.color = true
27
- config.tty = true
28
- config.formatter = :documentation
29
- config.order = :random
30
- config.filter_run_when_matching :focus
31
- config.raise_errors_for_deprecations!
32
- end
33
-
34
- require 'support/config'
35
-
36
- %w(
37
- a_compressible_trie a_serializable_trie a_serializer a_trie_data_structure
38
- a_trie_node a_trie_node_implementation a_container_scan a_container_word
39
- a_container_partial_word a_container_words_within
40
- ).each do |name|
41
- require File.join('support', 'shared_examples', name)
42
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'helpers/add_word'
4
- require_relative 'helpers/one_line_heredoc'
5
-
6
- RSpec.configure do |c|
7
- c.before do
8
- Rambling::Trie.config.reset
9
- end
10
-
11
- c.include Support::Helpers::AddWord
12
- c.include Support::Helpers::OneLineHeredoc
13
-
14
- RSpec::Matchers.define_negated_matcher :not_change, :change
15
- end
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Support
4
- module Helpers
5
- module AddWord
6
- def add_words node, words
7
- words.each { |word| add_word node, word }
8
- end
9
-
10
- def add_word node, word
11
- case node
12
- when Rambling::Trie::Container
13
- node.add word
14
- else
15
- node.add word.chars.reverse.map(&:to_sym)
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Support
4
- module Helpers
5
- module OneLineHeredoc
6
- def one_line heredoc
7
- heredoc.strip.tr "\n", ' '
8
- end
9
- end
10
- end
11
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a compressible trie' do
4
- context 'with an uncompressed trie' do
5
- it_behaves_like 'a trie data structure'
6
-
7
- it 'does not alter the input' do
8
- word = 'string'
9
- add_word trie, word
10
-
11
- expect(word).to eq 'string'
12
- end
13
-
14
- it 'is marked as not compressed' do
15
- expect(trie).not_to be_compressed
16
- end
17
- end
18
-
19
- context 'with an compressed trie' do
20
- let!(:original_root) { trie.root }
21
- let!(:original_keys) { original_root.children_tree.keys }
22
- let!(:original_values) { original_root.children_tree.values }
23
-
24
- before do
25
- trie.compress!
26
- end
27
-
28
- it_behaves_like 'a trie data structure'
29
-
30
- it 'is marked as compressed' do
31
- expect(trie).to be_compressed
32
- end
33
-
34
- it 'leaves the original root keys intact' do
35
- expect(original_root.children_tree.keys).to eq original_keys
36
- end
37
-
38
- it 'leaves the original trie keys intact' do
39
- expect(trie.children_tree.keys).to eq original_keys
40
- end
41
-
42
- it 'leaves the original trie values intact' do
43
- expect(trie.children_tree.values).not_to eq original_values
44
- end
45
- end
46
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a matching container#partial_word' do
4
- %w(h he hell hello hi hig high).each do |prefix|
5
- it 'matches part of the word' do
6
- expect(container.partial_word? prefix).to be true
7
- end
8
- end
9
- end
10
-
11
- shared_examples_for 'a non-matching container#partial_word' do
12
- it 'does not match any part of the word' do
13
- %w(ha hal al).each do |word|
14
- expect(container.partial_word? word).to be false
15
- end
16
- end
17
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a matching container#scan' do
4
- [
5
- ['hi', %w(hi high highlight histerical)],
6
- ['hig', %w(high highlight)],
7
- ].each do |test_params|
8
- prefix, expected = test_params
9
-
10
- it "returns an array with the words that match '#{prefix}'" do
11
- expect(container.scan prefix).to eq expected
12
- end
13
- end
14
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a matching container#word' do
4
- %w(hello high).each do |word|
5
- it 'matches the whole word' do
6
- expect(container.word? word).to be true
7
- end
8
- end
9
- end
10
-
11
- shared_examples_for 'a non-matching container#word' do
12
- %w(halt al).each do |word|
13
- it 'does not match the whole word' do
14
- expect(container.word? word).to be false
15
- end
16
- end
17
- end
18
-
19
- shared_examples_for 'a propagating node' do
20
- [
21
- [true, 'Rambling::Trie::Nodes::Compressed'],
22
- [false, 'Rambling::Trie::Nodes::Raw'],
23
- ].each do |test_params|
24
- compressed_value, instance_double_class = test_params
25
-
26
- context "when root has compressed=#{compressed_value}" do
27
- let :root do
28
- instance_double(
29
- instance_double_class,
30
- :root,
31
- compressed?: compressed_value,
32
- word?: nil,
33
- partial_word?: nil,
34
- )
35
- end
36
-
37
- it 'calls the root with the word characters' do
38
- container.public_send method_name, 'words'
39
- expect(root).to have_received(method_name).with %w(w o r d s)
40
- end
41
- end
42
- end
43
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a matching container#words_within' do
4
- [
5
- ['word', %w(word)],
6
- ['wordxyz', %w(word)],
7
- ].each do |test_params|
8
- phrase, expected = test_params
9
-
10
- it "returns an array with the word found in the phrase '#{phrase}'" do
11
- expect(container.words_within phrase).to match_array expected
12
- end
13
- end
14
- end
15
-
16
- shared_examples_for 'a non-matching container#words_within' do
17
- it 'returns an array with all words found in the phrase' do
18
- expect(container.words_within 'xyzword otherzxyone')
19
- .to match_array %w(word other one)
20
- end
21
- end
22
-
23
- shared_examples_for 'a matching container#words_within?' do
24
- context 'when phrase does not contain any words' do
25
- it 'returns false' do
26
- expect(container.words_within? 'xyz').to be false
27
- end
28
- end
29
-
30
- context 'when phrase contains any word' do
31
- ['xyz words', 'xyzone word'].each do |phrase|
32
- it "returns true for '#{phrase}'" do
33
- expect(container.words_within? phrase).to be true
34
- end
35
- end
36
- end
37
- end
38
-
39
- shared_examples_for 'a non-matching container#words_within?' do
40
- it 'returns an array with all words found in the phrase' do
41
- expect(container.words_within 'xyzword otherzxyone')
42
- .to match_array %w(word other one)
43
- end
44
- end
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a serializable trie' do
4
- let(:tmp_path) { File.join ::SPEC_ROOT, 'tmp' }
5
- let(:filepath) { File.join tmp_path, "trie-root.#{file_format}" }
6
-
7
- context 'with an uncompressed trie' do
8
- before { Rambling::Trie.dump trie_to_serialize, filepath }
9
-
10
- it_behaves_like 'a compressible trie' do
11
- let(:trie) { Rambling::Trie.load filepath }
12
- end
13
- end
14
-
15
- context 'with an compressed trie' do
16
- let(:trie) { Rambling::Trie.load filepath }
17
-
18
- before { Rambling::Trie.dump trie_to_serialize.compress!, filepath }
19
-
20
- it_behaves_like 'a trie data structure'
21
-
22
- it 'is marked as compressed' do
23
- expect(trie).to be_compressed
24
- end
25
- end
26
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a serializer' do
4
- subject(:serializer) { described_class.new }
5
-
6
- let(:trie) { Rambling::Trie.create }
7
- let(:tmp_path) { File.join ::SPEC_ROOT, 'tmp' }
8
- let(:filepath) { File.join tmp_path, "trie-root.#{file_format}" }
9
- let(:content) { trie.root }
10
-
11
- before do
12
- trie.concat %w(a few words to validate that load and dump are working)
13
- FileUtils.rm_f filepath
14
- end
15
-
16
- describe '#dump' do
17
- [true, false].each do |compress_value|
18
- context "with compressed=#{compress_value} trie" do
19
- let(:formatted_content) { format_content.call content }
20
-
21
- before { trie.compress! if compress_value }
22
-
23
- it 'returns the size in bytes of the file dumped' do
24
- total_bytes = serializer.dump content, filepath
25
- expect(total_bytes).to be_within(20).of formatted_content.size
26
- end
27
-
28
- it 'creates the file with the provided path' do
29
- serializer.dump content, filepath
30
- expect(File.exist? filepath).to be true
31
- end
32
-
33
- it 'converts the contents to the appropriate format' do
34
- serializer.dump content, filepath
35
- expect(File.size filepath).to be_within(20).of formatted_content.size
36
- end
37
- end
38
- end
39
- end
40
-
41
- describe '#load' do
42
- [true, false].each do |compress_value|
43
- context "with compressed=#{compress_value} trie" do
44
- before do
45
- trie.compress! if compress_value
46
- serializer.dump content, filepath
47
- end
48
-
49
- it 'loads the dumped object back into memory' do
50
- expect(serializer.load filepath).to eq content
51
- end
52
-
53
- it "loads a compressed=#{compress_value} object" do
54
- loaded = serializer.load filepath
55
- expect(loaded.compressed?).to be compress_value unless :file == file_format
56
- end
57
- end
58
- end
59
- end
60
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- shared_examples_for 'a trie data structure' do
4
- it 'contains all the words previously provided' do
5
- words.each { |word| expect(trie).to include word }
6
- end
7
-
8
- it 'returns true for #word? for all words previously provided' do
9
- words.each { |word| expect(trie.word? word).to be true }
10
- end
11
-
12
- it 'matches the full word for all words in file' do
13
- words.each { |word| expect(trie.match? word).to be true }
14
- end
15
-
16
- it 'matches the start of all the words in file' do
17
- words.each { |word| expect(trie.match? word[0..-2]).to be true }
18
- end
19
-
20
- it 'returns true for #partial_word? with full word for all words in file' do
21
- words.each { |word| expect(trie.partial_word? word).to be true }
22
- end
23
-
24
- it 'returns true for #partial_word? with the start of all words in file' do
25
- words.each { |word| expect(trie.partial_word? word[0..-2]).to be true }
26
- end
27
-
28
- it 'extracts words within larger strings' do
29
- words.each do |word|
30
- phrase = "x#{word}y"
31
- expect(trie.words_within phrase).to include word
32
- end
33
- end
34
-
35
- it 'identifies words within larger strings' do
36
- words.each do |word|
37
- phrase = "x#{word}y"
38
- expect(trie.words_within? phrase).to be true
39
- end
40
- end
41
-
42
- it 'allows iterating over all the words' do
43
- expect(trie.to_a.sort).to eq words.sort
44
- end
45
- end