rambling-trie 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/README.md +16 -10
- data/lib/rambling/trie/compressor.rb +2 -0
- data/lib/rambling/trie/configuration/properties.rb +5 -2
- data/lib/rambling/trie/configuration/provider_collection.rb +2 -2
- data/lib/rambling/trie/container.rb +12 -1
- data/lib/rambling/trie/enumerable.rb +1 -1
- data/lib/rambling/trie/nodes/node.rb +4 -4
- data/lib/rambling/trie/nodes/raw.rb +2 -1
- data/lib/rambling/trie/readers/plain_text.rb +1 -1
- data/lib/rambling/trie/serializers/file.rb +1 -3
- data/lib/rambling/trie/serializers/marshal.rb +3 -3
- data/lib/rambling/trie/serializers/yaml.rb +2 -2
- data/lib/rambling/trie/serializers/zip.rb +2 -0
- data/lib/rambling/trie/version.rb +1 -1
- data/lib/rambling/trie.rb +1 -1
- data/rambling-trie.gemspec +3 -9
- metadata +7 -122
- data/spec/assets/test_words.en_US.txt +0 -23
- data/spec/assets/test_words.es_DO.txt +0 -24
- data/spec/integration/rambling/trie_spec.rb +0 -116
- data/spec/lib/rambling/trie/comparable_spec.rb +0 -87
- data/spec/lib/rambling/trie/compressor_spec.rb +0 -111
- data/spec/lib/rambling/trie/configuration/properties_spec.rb +0 -75
- data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +0 -177
- data/spec/lib/rambling/trie/container_spec.rb +0 -466
- data/spec/lib/rambling/trie/enumerable_spec.rb +0 -50
- data/spec/lib/rambling/trie/inspectable_spec.rb +0 -62
- data/spec/lib/rambling/trie/nodes/compressed_spec.rb +0 -43
- data/spec/lib/rambling/trie/nodes/node_spec.rb +0 -9
- data/spec/lib/rambling/trie/nodes/raw_spec.rb +0 -184
- data/spec/lib/rambling/trie/readers/plain_text_spec.rb +0 -26
- data/spec/lib/rambling/trie/readers/reader_spec.rb +0 -14
- data/spec/lib/rambling/trie/serializers/file_spec.rb +0 -11
- data/spec/lib/rambling/trie/serializers/marshal_spec.rb +0 -10
- data/spec/lib/rambling/trie/serializers/serializer_spec.rb +0 -21
- data/spec/lib/rambling/trie/serializers/yaml_spec.rb +0 -10
- data/spec/lib/rambling/trie/serializers/zip_spec.rb +0 -36
- data/spec/lib/rambling/trie/stringifyable_spec.rb +0 -89
- data/spec/lib/rambling/trie_spec.rb +0 -244
- data/spec/spec_helper.rb +0 -42
- data/spec/support/config.rb +0 -15
- data/spec/support/helpers/add_word.rb +0 -20
- data/spec/support/helpers/one_line_heredoc.rb +0 -11
- data/spec/support/shared_examples/a_compressible_trie.rb +0 -46
- data/spec/support/shared_examples/a_container_partial_word.rb +0 -17
- data/spec/support/shared_examples/a_container_scan.rb +0 -14
- data/spec/support/shared_examples/a_container_word.rb +0 -43
- data/spec/support/shared_examples/a_container_words_within.rb +0 -44
- data/spec/support/shared_examples/a_serializable_trie.rb +0 -26
- data/spec/support/shared_examples/a_serializer.rb +0 -60
- data/spec/support/shared_examples/a_trie_data_structure.rb +0 -45
- data/spec/support/shared_examples/a_trie_node.rb +0 -135
- data/spec/support/shared_examples/a_trie_node_implementation.rb +0 -149
- data/spec/tmp/.gitkeep +0 -0
@@ -1,184 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Nodes::Raw do
|
6
|
-
let(:node) { described_class.new }
|
7
|
-
|
8
|
-
describe '#new' do
|
9
|
-
it 'is not a word' do
|
10
|
-
expect(node).not_to be_word
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
it_behaves_like 'a trie node implementation' do
|
15
|
-
def add_word_to_tree word
|
16
|
-
add_word node, word
|
17
|
-
end
|
18
|
-
|
19
|
-
def add_words_to_tree words
|
20
|
-
add_words node, words
|
21
|
-
end
|
22
|
-
|
23
|
-
def assign_letter letter
|
24
|
-
node.letter = letter
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe '#compressed?' do
|
29
|
-
it 'returns false' do
|
30
|
-
expect(node).not_to be_compressed
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe '#add' do
|
35
|
-
context 'when the node has no branches' do
|
36
|
-
before { add_word node, 'abc' }
|
37
|
-
|
38
|
-
it 'adds only one child' do
|
39
|
-
expect(node.children.size).to eq 1
|
40
|
-
end
|
41
|
-
|
42
|
-
# rubocop:disable RSpec/MultipleExpectations
|
43
|
-
it 'adds the full subtree' do
|
44
|
-
expect(node[:a]).not_to be_nil
|
45
|
-
expect(node[:a][:b]).not_to be_nil
|
46
|
-
expect(node[:a][:b][:c]).not_to be_nil
|
47
|
-
end
|
48
|
-
# rubocop:enable RSpec/MultipleExpectations
|
49
|
-
|
50
|
-
# rubocop:disable RSpec/MultipleExpectations
|
51
|
-
it 'marks only the last child as terminal' do
|
52
|
-
expect(node).not_to be_terminal
|
53
|
-
expect(node[:a]).not_to be_terminal
|
54
|
-
expect(node[:a][:b]).not_to be_terminal
|
55
|
-
expect(node[:a][:b][:c]).to be_terminal
|
56
|
-
end
|
57
|
-
# rubocop:enable RSpec/MultipleExpectations
|
58
|
-
end
|
59
|
-
|
60
|
-
context 'when a word is added more than once' do
|
61
|
-
before do
|
62
|
-
add_word node, 'ack'
|
63
|
-
add_word node, 'ack'
|
64
|
-
end
|
65
|
-
|
66
|
-
# rubocop:disable RSpec/MultipleExpectations
|
67
|
-
it 'only counts it once' do
|
68
|
-
expect(node.children.size).to eq 1
|
69
|
-
expect(node[:a].children.size).to eq 1
|
70
|
-
expect(node[:a][:c].children.size).to eq 1
|
71
|
-
expect(node[:a][:c][:k].children.size).to eq 0
|
72
|
-
end
|
73
|
-
# rubocop:enable RSpec/MultipleExpectations
|
74
|
-
|
75
|
-
# rubocop:disable RSpec/MultipleExpectations
|
76
|
-
it 'does not change the terminal nodes in the tree' do
|
77
|
-
expect(node).not_to be_terminal
|
78
|
-
expect(node[:a]).not_to be_terminal
|
79
|
-
expect(node[:a][:c]).not_to be_terminal
|
80
|
-
expect(node[:a][:c][:k]).to be_terminal
|
81
|
-
end
|
82
|
-
# rubocop:enable RSpec/MultipleExpectations
|
83
|
-
|
84
|
-
it 'still returns the "added" node' do
|
85
|
-
child = add_word node, 'ack'
|
86
|
-
expect(child.letter).to eq :a
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context 'when the word does not exist in the tree but the letters do' do
|
91
|
-
before { add_words node, %w(ack a) }
|
92
|
-
|
93
|
-
it 'does not add another branch' do
|
94
|
-
expect(node.children.size).to eq 1
|
95
|
-
end
|
96
|
-
|
97
|
-
# rubocop:disable RSpec/MultipleExpectations
|
98
|
-
it 'marks the corresponding node as terminal' do
|
99
|
-
expect(node).not_to be_terminal
|
100
|
-
expect(node[:a]).to be_terminal
|
101
|
-
expect(node[:a][:c]).not_to be_terminal
|
102
|
-
expect(node[:a][:c][:k]).to be_terminal
|
103
|
-
end
|
104
|
-
# rubocop:enable RSpec/MultipleExpectations
|
105
|
-
|
106
|
-
it 'returns the added node' do
|
107
|
-
child = add_word node, 'a'
|
108
|
-
expect(child.letter).to eq :a
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context 'when the node has a letter and a parent' do
|
113
|
-
let(:parent) { described_class.new }
|
114
|
-
let(:node) { described_class.new :a, parent }
|
115
|
-
|
116
|
-
context 'when adding an empty string' do
|
117
|
-
before { add_word node, '' }
|
118
|
-
|
119
|
-
it 'does not alter the node letter' do
|
120
|
-
expect(node.letter).to eq :a
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'does not change the node children' do
|
124
|
-
expect(node.children.size).to eq 0
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'changes the node to terminal' do
|
128
|
-
expect(node).to be_terminal
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context 'when adding a one letter word' do
|
133
|
-
before { add_word node, 'b' }
|
134
|
-
|
135
|
-
it 'does not alter the node letter' do
|
136
|
-
expect(node.letter).to eq :a
|
137
|
-
end
|
138
|
-
|
139
|
-
# rubocop:disable RSpec/MultipleExpectations
|
140
|
-
it 'adds a child with the expected letter' do
|
141
|
-
expect(node.children.size).to eq 1
|
142
|
-
expect(node.children.first.letter).to eq :b
|
143
|
-
end
|
144
|
-
# rubocop:enable RSpec/MultipleExpectations
|
145
|
-
|
146
|
-
it 'reports it has the expected letter a key' do
|
147
|
-
expect(node).to have_key(:b)
|
148
|
-
end
|
149
|
-
|
150
|
-
it 'returns the child corresponding to the key' do
|
151
|
-
expect(node[:b]).to eq node.children_tree[:b]
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'does not mark itself as terminal' do
|
155
|
-
expect(node).not_to be_terminal
|
156
|
-
end
|
157
|
-
|
158
|
-
it 'marks the first child as terminal' do
|
159
|
-
expect(node[:b]).to be_terminal
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
context 'when adding a large word' do
|
164
|
-
before { add_word node, 'mplified' }
|
165
|
-
|
166
|
-
it 'marks the last letter as terminal' do
|
167
|
-
expect(node[:m][:p][:l][:i][:f][:i][:e][:d]).to be_terminal
|
168
|
-
end
|
169
|
-
|
170
|
-
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
|
171
|
-
it 'does not mark any other letter as terminal' do
|
172
|
-
expect(node[:m][:p][:l][:i][:f][:i][:e]).not_to be_terminal
|
173
|
-
expect(node[:m][:p][:l][:i][:f][:i]).not_to be_terminal
|
174
|
-
expect(node[:m][:p][:l][:i][:f]).not_to be_terminal
|
175
|
-
expect(node[:m][:p][:l][:i]).not_to be_terminal
|
176
|
-
expect(node[:m][:p][:l]).not_to be_terminal
|
177
|
-
expect(node[:m][:p]).not_to be_terminal
|
178
|
-
expect(node[:m]).not_to be_terminal
|
179
|
-
end
|
180
|
-
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Readers::PlainText do
|
6
|
-
subject(:reader) { described_class.new }
|
7
|
-
|
8
|
-
describe '#each_word' do
|
9
|
-
let(:filepath) { File.join(::SPEC_ROOT, 'assets', 'test_words.en_US.txt') }
|
10
|
-
let(:words) { File.readlines(filepath).map(&:chomp) }
|
11
|
-
|
12
|
-
it 'yields every word yielded by the file' do
|
13
|
-
yielded = []
|
14
|
-
reader.each_word(filepath) { |word| yielded << word }
|
15
|
-
expect(yielded).to eq words
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'returns an enumerator when no block is given' do
|
19
|
-
expect(reader.each_word filepath).to be_an Enumerator
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'returns the enumerable when a block is given' do
|
23
|
-
expect(reader.each_word(filepath) { |_| }).to eq reader
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Readers::Reader do
|
6
|
-
subject(:reader) { described_class.new }
|
7
|
-
|
8
|
-
describe '#load' do
|
9
|
-
it 'is an abstract method that raises NotImplementedError' do
|
10
|
-
expect { reader.each_word('any-file.zip') }
|
11
|
-
.to raise_exception NotImplementedError
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,11 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Serializers::File do
|
6
|
-
it_behaves_like 'a serializer' do
|
7
|
-
let(:file_format) { :file }
|
8
|
-
let(:content) { trie.to_a.join ' ' }
|
9
|
-
let(:format_content) { ->(content) { content } }
|
10
|
-
end
|
11
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Serializers::Serializer do
|
6
|
-
subject(:serializer) { described_class.new }
|
7
|
-
|
8
|
-
describe '#load' do
|
9
|
-
it 'is an abstract method that raises NotImplementedError' do
|
10
|
-
expect { serializer.load('any-file.zip') }
|
11
|
-
.to raise_exception NotImplementedError
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
describe '#dump' do
|
16
|
-
it 'is an abstract method that raises NotImplementedError' do
|
17
|
-
expect { serializer.dump('any contents', 'any-file.zip') }
|
18
|
-
.to raise_exception NotImplementedError
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Serializers::Zip do
|
6
|
-
{
|
7
|
-
yaml: YAML.method(:dump),
|
8
|
-
yml: YAML.method(:dump),
|
9
|
-
marshal: Marshal.method(:dump),
|
10
|
-
file: Marshal.method(:dump),
|
11
|
-
}.each do |file_format, dump_method|
|
12
|
-
context "with '.#{file_format}'" do
|
13
|
-
it_behaves_like 'a serializer' do
|
14
|
-
let(:properties) { Rambling::Trie::Configuration::Properties.new }
|
15
|
-
let(:serializer) { described_class.new properties }
|
16
|
-
let(:file_format) { :zip }
|
17
|
-
|
18
|
-
let(:filepath) { File.join tmp_path, "trie-root.#{file_format}.zip" }
|
19
|
-
let(:format_content) { ->(content) { zip dump_method.call content } }
|
20
|
-
let(:filename) { File.basename(filepath).gsub %r{\.zip}, '' }
|
21
|
-
|
22
|
-
before { properties.tmp_path = tmp_path }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def zip content
|
28
|
-
cursor = Zip::OutputStream.write_buffer do |io|
|
29
|
-
io.put_next_entry filename
|
30
|
-
io.write content
|
31
|
-
end
|
32
|
-
|
33
|
-
cursor.rewind
|
34
|
-
cursor.read
|
35
|
-
end
|
36
|
-
end
|
@@ -1,89 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe Rambling::Trie::Stringifyable do
|
6
|
-
describe '#as_word' do
|
7
|
-
let(:node) { Rambling::Trie::Nodes::Raw.new }
|
8
|
-
|
9
|
-
context 'with an empty node' do
|
10
|
-
before { add_word node, '' }
|
11
|
-
|
12
|
-
it 'returns nil' do
|
13
|
-
expect(node.as_word).to be_empty
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'with one letter' do
|
18
|
-
before do
|
19
|
-
node.letter = :a
|
20
|
-
add_word node, ''
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'returns the expected one letter word' do
|
24
|
-
expect(node.as_word).to eq 'a'
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'with a small word' do
|
29
|
-
before do
|
30
|
-
node.letter = :a
|
31
|
-
add_word node, 'll'
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'returns the expected small word' do
|
35
|
-
expect(node[:l][:l].as_word).to eq 'all'
|
36
|
-
end
|
37
|
-
|
38
|
-
it 'raises an error for a non terminal node' do
|
39
|
-
expect { node[:l].as_word }
|
40
|
-
.to raise_error Rambling::Trie::InvalidOperation
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
context 'with a long word' do
|
45
|
-
before do
|
46
|
-
node.letter = :b
|
47
|
-
add_word node, 'eautiful'
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'returns the expected long word' do
|
51
|
-
expect(node[:e][:a][:u][:t][:i][:f][:u][:l].as_word).to eq 'beautiful'
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
context 'with a node with nil letter' do
|
56
|
-
let(:node) { Rambling::Trie::Nodes::Raw.new nil }
|
57
|
-
|
58
|
-
it 'returns nil' do
|
59
|
-
expect(node.as_word).to be_empty
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context 'with a compressed node' do
|
64
|
-
let(:compressor) { Rambling::Trie::Compressor.new }
|
65
|
-
let(:compressed_node) { compressor.compress node }
|
66
|
-
|
67
|
-
before do
|
68
|
-
node.letter = :a
|
69
|
-
add_words node, %w(m dd)
|
70
|
-
end
|
71
|
-
|
72
|
-
[
|
73
|
-
[:m, 'am'],
|
74
|
-
[:d, 'add'],
|
75
|
-
].each do |test_params|
|
76
|
-
key, expected = test_params
|
77
|
-
|
78
|
-
it "returns the words for terminal nodes (#{key} => #{expected})" do
|
79
|
-
expect(compressed_node[key].as_word).to eq expected
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'raises an error for non terminal nodes' do
|
84
|
-
expect { compressed_node.as_word }
|
85
|
-
.to raise_error Rambling::Trie::InvalidOperation
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
@@ -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
|