rambling-trie 2.3.0 → 2.3.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.
- checksums.yaml +4 -4
- data/Gemfile +4 -0
- data/LICENSE +1 -1
- data/README.md +25 -11
- data/lib/rambling/trie/comparable.rb +2 -2
- data/lib/rambling/trie/compressible.rb +3 -3
- data/lib/rambling/trie/configuration/properties.rb +10 -9
- data/lib/rambling/trie/configuration/provider_collection.rb +15 -13
- data/lib/rambling/trie/container.rb +17 -19
- data/lib/rambling/trie/enumerable.rb +3 -0
- data/lib/rambling/trie/nodes/compressed.rb +3 -3
- data/lib/rambling/trie/nodes/node.rb +14 -14
- data/lib/rambling/trie/nodes/raw.rb +2 -2
- data/lib/rambling/trie/readers/plain_text.rb +9 -4
- data/lib/rambling/trie/readers/reader.rb +21 -0
- data/lib/rambling/trie/readers.rb +1 -1
- data/lib/rambling/trie/serializers/file.rb +1 -1
- data/lib/rambling/trie/serializers/marshal.rb +3 -2
- data/lib/rambling/trie/serializers/serializer.rb +27 -0
- data/lib/rambling/trie/serializers/yaml.rb +3 -2
- data/lib/rambling/trie/serializers/zip.rb +9 -5
- data/lib/rambling/trie/serializers.rb +1 -1
- data/lib/rambling/trie/stringifyable.rb +1 -1
- data/lib/rambling/trie/version.rb +1 -1
- data/lib/rambling/trie.rb +11 -9
- data/rambling-trie.gemspec +5 -1
- data/spec/integration/rambling/trie_spec.rb +13 -16
- data/spec/lib/rambling/trie/comparable_spec.rb +29 -39
- data/spec/lib/rambling/trie/compressor_spec.rb +17 -14
- data/spec/lib/rambling/trie/configuration/properties_spec.rb +25 -7
- data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +42 -14
- data/spec/lib/rambling/trie/container_spec.rb +200 -325
- data/spec/lib/rambling/trie/enumerable_spec.rb +18 -10
- data/spec/lib/rambling/trie/inspectable_spec.rb +9 -3
- data/spec/lib/rambling/trie/nodes/node_spec.rb +1 -1
- data/spec/lib/rambling/trie/nodes/raw_spec.rb +26 -23
- data/spec/lib/rambling/trie/readers/plain_text_spec.rb +11 -1
- data/spec/lib/rambling/trie/readers/reader_spec.rb +14 -0
- data/spec/lib/rambling/trie/serializers/file_spec.rb +1 -3
- data/spec/lib/rambling/trie/serializers/marshal_spec.rb +1 -3
- data/spec/lib/rambling/trie/serializers/serializer_spec.rb +21 -0
- data/spec/lib/rambling/trie/serializers/yaml_spec.rb +1 -3
- data/spec/lib/rambling/trie/serializers/zip_spec.rb +24 -16
- data/spec/lib/rambling/trie/stringifyable_spec.rb +17 -13
- data/spec/lib/rambling/trie_spec.rb +106 -44
- data/spec/spec_helper.rb +15 -8
- data/spec/support/shared_examples/a_compressible_trie.rb +9 -3
- data/spec/support/shared_examples/a_container_partial_word.rb +17 -0
- data/spec/support/shared_examples/a_container_scan.rb +14 -0
- data/spec/support/shared_examples/a_container_word.rb +43 -0
- data/spec/support/shared_examples/a_container_words_within.rb +44 -0
- data/spec/support/shared_examples/a_serializable_trie.rb +4 -8
- data/spec/support/shared_examples/a_serializer.rb +36 -13
- data/spec/support/shared_examples/a_trie_data_structure.rb +24 -10
- data/spec/support/shared_examples/a_trie_node.rb +19 -12
- data/spec/support/shared_examples/a_trie_node_implementation.rb +40 -43
- metadata +19 -3
@@ -3,20 +3,22 @@
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
module Serializers
|
6
|
-
# Zip file serializer. Dumps/loads contents from zip files.
|
7
|
-
# detects if zip file contains
|
8
|
-
|
6
|
+
# Zip file serializer. Dumps/loads contents from +.zip+ files.
|
7
|
+
# Automatically detects if zip file contains a +.marshal+ or +.yml+ file,
|
8
|
+
# or any other registered +:format => serializer+ combo.
|
9
|
+
class Zip < Serializer
|
9
10
|
# Creates a new Zip serializer.
|
10
11
|
# @param [Configuration::Properties] properties the configuration
|
11
12
|
# properties set up so far.
|
12
13
|
def initialize properties
|
13
14
|
@properties = properties
|
15
|
+
super()
|
14
16
|
end
|
15
17
|
|
16
18
|
# Unzip contents from specified filepath and load in contents from
|
17
19
|
# unzipped files.
|
18
20
|
# @param [String] filepath the filepath to load contents from.
|
19
|
-
# @return [
|
21
|
+
# @return [TContents] all contents of the unzipped loaded file.
|
20
22
|
# @see https://github.com/rubyzip/rubyzip#reading-a-zip-file Zip
|
21
23
|
# reading a file
|
22
24
|
def load filepath
|
@@ -35,7 +37,7 @@ module Rambling
|
|
35
37
|
# Dumps contents and zips into a specified filepath.
|
36
38
|
# @param [String] contents the contents to dump.
|
37
39
|
# @param [String] filepath the filepath to dump the contents to.
|
38
|
-
# @return [
|
40
|
+
# @return [TContents] number of bytes written to disk.
|
39
41
|
# @see https://github.com/rubyzip/rubyzip#basic-zip-archive-creation
|
40
42
|
# Zip archive creation
|
41
43
|
def dump contents, filepath
|
@@ -50,6 +52,8 @@ module Rambling
|
|
50
52
|
|
51
53
|
zip.add filename, entry_path
|
52
54
|
end
|
55
|
+
|
56
|
+
::File.size filepath
|
53
57
|
end
|
54
58
|
|
55
59
|
private
|
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
|
-
# Provides the String representation behavior for the trie data structure.
|
5
|
+
# Provides the +String+ representation behavior for the trie data structure.
|
6
6
|
module Stringifyable
|
7
7
|
# String representation of the current node, if it is a terminal node.
|
8
8
|
# @return [String] the string representation of the current node.
|
data/lib/rambling/trie.rb
CHANGED
@@ -10,12 +10,12 @@ end
|
|
10
10
|
|
11
11
|
# General namespace for all Rambling gems.
|
12
12
|
module Rambling
|
13
|
-
# Entry point for rambling-trie API.
|
13
|
+
# Entry point for +rambling-trie+ API.
|
14
14
|
module Trie
|
15
15
|
class << self
|
16
|
-
# Creates a new Rambling::Trie
|
16
|
+
# Creates a new +Rambling::Trie+. Entry point for the +rambling-trie+ API.
|
17
17
|
# @param [String, nil] filepath the file to load the words from.
|
18
|
-
# @param [Reader, nil] reader the file parser to get each word.
|
18
|
+
# @param [Readers::Reader, nil] reader the file parser to get each word.
|
19
19
|
# @return [Container] the trie just created.
|
20
20
|
# @yield [Container] the trie just created.
|
21
21
|
# @see Rambling::Trie::Readers Readers.
|
@@ -36,7 +36,7 @@ module Rambling
|
|
36
36
|
|
37
37
|
# Loads an existing trie from disk into memory. By default, it will
|
38
38
|
# deduce the correct way to deserialize based on the file extension.
|
39
|
-
# Available formats are
|
39
|
+
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
|
40
40
|
# previous formats. You can also define your own.
|
41
41
|
# @param [String] filepath the file to load the words from.
|
42
42
|
# @param [Serializer, nil] serializer the object responsible of loading
|
@@ -46,7 +46,7 @@ module Rambling
|
|
46
46
|
# @see Rambling::Trie::Serializers Serializers.
|
47
47
|
# @note Use of
|
48
48
|
# {https://ruby-doc.org/core-2.7.0/Marshal.html#method-c-load
|
49
|
-
# Marshal.load} is generally discouraged. Only use the
|
49
|
+
# Marshal.load} is generally discouraged. Only use the +.marshal+
|
50
50
|
# format with trusted input.
|
51
51
|
def load filepath, serializer = nil
|
52
52
|
serializer ||= serializers.resolve filepath
|
@@ -58,19 +58,21 @@ module Rambling
|
|
58
58
|
|
59
59
|
# Dumps an existing trie from memory into disk. By default, it will
|
60
60
|
# deduce the correct way to serialize based on the file extension.
|
61
|
-
# Available formats are
|
61
|
+
# Available formats are +yml+, +marshal+, and +zip+ versions of all the
|
62
62
|
# previous formats. You can also define your own.
|
63
63
|
# @param [Container] trie the trie to dump into disk.
|
64
64
|
# @param [String] filepath the file to dump to serialized trie into.
|
65
|
-
# @param [Serializer, nil] serializer the object responsible
|
65
|
+
# @param [Serializers::Serializer, nil] serializer the object responsible
|
66
|
+
# for trie serialization.
|
67
|
+
# @return [void]
|
66
68
|
# serializing and dumping the trie into disk.
|
67
|
-
# @see
|
69
|
+
# @see Serializers Serializers.
|
68
70
|
def dump trie, filepath, serializer = nil
|
69
71
|
serializer ||= serializers.resolve filepath
|
70
72
|
serializer.dump trie.root, filepath
|
71
73
|
end
|
72
74
|
|
73
|
-
# Provides configuration properties for the Rambling::Trie gem.
|
75
|
+
# Provides configuration properties for the +Rambling::Trie+ gem.
|
74
76
|
# @return [Configuration::Properties] the configured properties of the
|
75
77
|
# gem.
|
76
78
|
# @yield [Configuration::Properties] the configured properties of the
|
data/rambling-trie.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
$LOAD_PATH.push File.expand_path('
|
3
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
4
4
|
require 'rambling/trie/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |gem|
|
@@ -15,6 +15,10 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.summary = 'A Ruby implementation of the trie data structure.'
|
16
16
|
gem.homepage = 'http://github.com/gonzedge/rambling-trie'
|
17
17
|
gem.date = Time.now.strftime '%Y-%m-%d'
|
18
|
+
gem.metadata = {
|
19
|
+
'changelog_uri' => 'https://github.com/gonzedge/rambling-trie/blob/master/CHANGELOG.md',
|
20
|
+
'documentation_uri' => 'https://www.rubydoc.info/gems/rambling-trie',
|
21
|
+
}
|
18
22
|
|
19
23
|
executables = `git ls-files -- bin/*`.split "\n"
|
20
24
|
files = `git ls-files -- {lib,*file,*.gemspec,LICENSE*,README*}`.split "\n"
|
@@ -8,30 +8,26 @@ describe Rambling::Trie do
|
|
8
8
|
|
9
9
|
context 'when providing words directly' do
|
10
10
|
it_behaves_like 'a compressible trie' do
|
11
|
-
let(:trie) {
|
11
|
+
let(:trie) { described_class.create }
|
12
12
|
let(:words) { %w(a couple of words for our full trie integration test) }
|
13
13
|
|
14
|
-
before
|
15
|
-
trie.concat words
|
16
|
-
end
|
14
|
+
before { trie.concat words }
|
17
15
|
end
|
18
16
|
end
|
19
17
|
|
20
18
|
context 'when provided with words with unicode characters' do
|
21
19
|
it_behaves_like 'a compressible trie' do
|
22
|
-
let(:trie) {
|
20
|
+
let(:trie) { described_class.create }
|
23
21
|
let(:words) do
|
24
22
|
%w(poquísimas palabras para nuestra prueba de integración completa 🙃)
|
25
23
|
end
|
26
24
|
|
27
|
-
before
|
28
|
-
trie.concat words
|
29
|
-
end
|
25
|
+
before { trie.concat words }
|
30
26
|
end
|
31
27
|
end
|
32
28
|
|
33
29
|
context 'when provided with a filepath' do
|
34
|
-
let(:trie) {
|
30
|
+
let(:trie) { described_class.create filepath }
|
35
31
|
let(:words) { File.readlines(filepath).map(&:chomp) }
|
36
32
|
|
37
33
|
context 'with english words' do
|
@@ -53,33 +49,34 @@ describe Rambling::Trie do
|
|
53
49
|
|
54
50
|
context 'when serialized with Ruby marshal format (default)' do
|
55
51
|
it_behaves_like 'a serializable trie' do
|
56
|
-
let(:trie_to_serialize) {
|
52
|
+
let(:trie_to_serialize) { described_class.create words_filepath }
|
57
53
|
let(:format) { :marshal }
|
58
54
|
end
|
59
55
|
end
|
60
56
|
|
61
57
|
context 'when serialized with YAML' do
|
62
58
|
it_behaves_like 'a serializable trie' do
|
63
|
-
let(:trie_to_serialize) {
|
59
|
+
let(:trie_to_serialize) { described_class.create words_filepath }
|
64
60
|
let(:format) { :yml }
|
65
61
|
end
|
66
62
|
end
|
67
63
|
|
68
64
|
context 'when serialized with zipped Ruby marshal format' do
|
65
|
+
let!(:original_on_exists_proc) { ::Zip.on_exists_proc }
|
66
|
+
let!(:original_continue_on_exists_proc) { ::Zip.continue_on_exists_proc }
|
67
|
+
|
69
68
|
before do
|
70
|
-
@original_on_exists_proc = ::Zip.on_exists_proc
|
71
|
-
@original_continue_on_exists_proc = ::Zip.continue_on_exists_proc
|
72
69
|
::Zip.on_exists_proc = true
|
73
70
|
::Zip.continue_on_exists_proc = true
|
74
71
|
end
|
75
72
|
|
76
73
|
after do
|
77
|
-
::Zip.on_exists_proc =
|
78
|
-
::Zip.continue_on_exists_proc =
|
74
|
+
::Zip.on_exists_proc = original_on_exists_proc
|
75
|
+
::Zip.continue_on_exists_proc = original_continue_on_exists_proc
|
79
76
|
end
|
80
77
|
|
81
78
|
it_behaves_like 'a serializable trie' do
|
82
|
-
let(:trie_to_serialize) {
|
79
|
+
let(:trie_to_serialize) { described_class.create words_filepath }
|
83
80
|
let(:format) { 'marshal.zip' }
|
84
81
|
end
|
85
82
|
end
|
@@ -4,93 +4,83 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
describe Rambling::Trie::Comparable do
|
6
6
|
describe '#==' do
|
7
|
-
let(:
|
8
|
-
let(:
|
7
|
+
let(:node_one) { Rambling::Trie::Nodes::Raw.new }
|
8
|
+
let(:node_two) { Rambling::Trie::Nodes::Raw.new }
|
9
9
|
|
10
10
|
context 'when the nodes do not have the same letter' do
|
11
11
|
before do
|
12
|
-
|
13
|
-
|
12
|
+
node_one.letter = :a
|
13
|
+
node_two.letter = :b
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'returns false' do
|
17
|
-
expect(
|
17
|
+
expect(node_one).not_to eq node_two
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
context 'when
|
21
|
+
context 'when nodes have same letter, not terminal and no children' do
|
22
22
|
before do
|
23
|
-
|
24
|
-
|
23
|
+
node_one.letter = :a
|
24
|
+
node_two.letter = :a
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'returns true' do
|
28
|
-
expect(
|
28
|
+
expect(node_one).to eq node_two
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
32
|
context 'when the nodes have the same letter and are terminal' do
|
33
33
|
before do
|
34
|
-
|
35
|
-
|
34
|
+
node_one.letter = :a
|
35
|
+
node_one.terminal!
|
36
36
|
|
37
|
-
|
38
|
-
|
37
|
+
node_two.letter = :a
|
38
|
+
node_two.terminal!
|
39
39
|
end
|
40
40
|
|
41
41
|
it 'returns true' do
|
42
|
-
expect(
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context 'when the nodes have the same letter and are not terminal' do
|
47
|
-
before do
|
48
|
-
node_1.letter = :a
|
49
|
-
node_2.letter = :a
|
50
|
-
end
|
51
|
-
|
52
|
-
it 'returns true' do
|
53
|
-
expect(node_1).to eq node_2
|
42
|
+
expect(node_one).to eq node_two
|
54
43
|
end
|
55
44
|
end
|
56
45
|
|
57
46
|
context 'when the nodes have the same letter but are not both terminal' do
|
58
47
|
before do
|
59
|
-
|
60
|
-
|
48
|
+
node_one.letter = :a
|
49
|
+
node_one.terminal!
|
61
50
|
|
62
|
-
|
51
|
+
node_two.letter = :a
|
63
52
|
end
|
53
|
+
|
64
54
|
it 'returns false' do
|
65
|
-
expect(
|
55
|
+
expect(node_one).not_to eq node_two
|
66
56
|
end
|
67
57
|
end
|
68
58
|
|
69
59
|
context 'when the nodes have the same letter and the same children' do
|
70
60
|
before do
|
71
|
-
|
72
|
-
add_words
|
61
|
+
node_one.letter = :t
|
62
|
+
add_words node_one, %w(hese hree hings)
|
73
63
|
|
74
|
-
|
75
|
-
add_words
|
64
|
+
node_two.letter = :t
|
65
|
+
add_words node_two, %w(hese hree hings)
|
76
66
|
end
|
77
67
|
|
78
68
|
it 'returns true' do
|
79
|
-
expect(
|
69
|
+
expect(node_one).to eq node_two
|
80
70
|
end
|
81
71
|
end
|
82
72
|
|
83
73
|
context 'when the nodes have the same letter but different children' do
|
84
74
|
before do
|
85
|
-
|
86
|
-
add_words
|
75
|
+
node_one.letter = :t
|
76
|
+
add_words node_one, %w(hese wo)
|
87
77
|
|
88
|
-
|
89
|
-
add_words
|
78
|
+
node_two.letter = :t
|
79
|
+
add_words node_two, %w(hese hree hings)
|
90
80
|
end
|
91
81
|
|
92
82
|
it 'returns false' do
|
93
|
-
expect(
|
83
|
+
expect(node_one).not_to eq node_two
|
94
84
|
end
|
95
85
|
end
|
96
86
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Rambling::Trie::Compressor do
|
6
|
-
let(:compressor) {
|
6
|
+
let(:compressor) { described_class.new }
|
7
7
|
|
8
8
|
describe '#compress' do
|
9
9
|
let(:node) { Rambling::Trie::Nodes::Raw.new }
|
@@ -16,9 +16,7 @@ describe Rambling::Trie::Compressor do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
context 'with at least one word' do
|
19
|
-
before
|
20
|
-
add_words node, %w(all the words)
|
21
|
-
end
|
19
|
+
before { add_words node, %w(all the words) }
|
22
20
|
|
23
21
|
it 'keeps the node letter nil' do
|
24
22
|
compressed = compressor.compress node
|
@@ -28,25 +26,25 @@ describe Rambling::Trie::Compressor do
|
|
28
26
|
end
|
29
27
|
|
30
28
|
context 'with a single word' do
|
31
|
-
before
|
32
|
-
add_word node, 'all'
|
33
|
-
end
|
29
|
+
before { add_word node, 'all' }
|
34
30
|
|
31
|
+
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
|
35
32
|
it 'compresses into a single node without children' do
|
36
33
|
compressed = compressor.compress node
|
34
|
+
compressed_node_a = compressed[:a]
|
37
35
|
|
38
|
-
expect(
|
39
|
-
expect(
|
40
|
-
expect(
|
41
|
-
expect(
|
36
|
+
expect(compressed_node_a.letter).to eq :all
|
37
|
+
expect(compressed_node_a.children.size).to eq 0
|
38
|
+
expect(compressed_node_a).to be_terminal
|
39
|
+
expect(compressed_node_a).to be_compressed
|
42
40
|
end
|
41
|
+
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
43
42
|
end
|
44
43
|
|
45
44
|
context 'with two words' do
|
46
|
-
before
|
47
|
-
add_words node, %w(all ask)
|
48
|
-
end
|
45
|
+
before { add_words node, %w(all ask) }
|
49
46
|
|
47
|
+
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
|
50
48
|
it 'compresses into corresponding three nodes' do
|
51
49
|
compressed = compressor.compress node
|
52
50
|
|
@@ -65,8 +63,10 @@ describe Rambling::Trie::Compressor do
|
|
65
63
|
expect(compressed[:a][:l]).to be_compressed
|
66
64
|
expect(compressed[:a][:s]).to be_compressed
|
67
65
|
end
|
66
|
+
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
68
67
|
end
|
69
68
|
|
69
|
+
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
|
70
70
|
it 'reassigns the parent nodes correctly' do
|
71
71
|
add_words node, %w(repay rest repaint)
|
72
72
|
compressed = compressor.compress node
|
@@ -91,7 +91,9 @@ describe Rambling::Trie::Compressor do
|
|
91
91
|
expect(compressed[:r][:p][:i].parent).to eq compressed[:r][:p]
|
92
92
|
expect(compressed[:r][:p][:i].children.size).to eq 0
|
93
93
|
end
|
94
|
+
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
94
95
|
|
96
|
+
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
|
95
97
|
it 'does not compress terminal nodes' do
|
96
98
|
add_words node, %w(you your yours)
|
97
99
|
compressed = compressor.compress node
|
@@ -104,5 +106,6 @@ describe Rambling::Trie::Compressor do
|
|
104
106
|
expect(compressed[:y][:r][:s].letter).to eq :s
|
105
107
|
expect(compressed[:y][:r][:s]).to be_compressed
|
106
108
|
end
|
109
|
+
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
|
107
110
|
end
|
108
111
|
end
|
@@ -3,13 +3,17 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Rambling::Trie::Configuration::Properties do
|
6
|
-
let(:properties) {
|
6
|
+
let(:properties) { described_class.new }
|
7
7
|
|
8
8
|
describe '.new' do
|
9
|
-
it 'configures the
|
9
|
+
it 'configures the serializer formats' do
|
10
10
|
serializers = properties.serializers
|
11
|
-
|
12
11
|
expect(serializers.formats).to match_array %i(marshal yaml yml zip)
|
12
|
+
end
|
13
|
+
|
14
|
+
# rubocop:disable RSpec/ExampleLength
|
15
|
+
it 'configures the serializer providers' do
|
16
|
+
serializers = properties.serializers
|
13
17
|
expect(serializers.providers.to_a).to match_array [
|
14
18
|
[:marshal, Rambling::Trie::Serializers::Marshal],
|
15
19
|
[:yaml, Rambling::Trie::Serializers::Yaml],
|
@@ -17,11 +21,15 @@ describe Rambling::Trie::Configuration::Properties do
|
|
17
21
|
[:zip, Rambling::Trie::Serializers::Zip],
|
18
22
|
]
|
19
23
|
end
|
24
|
+
# rubocop:enable RSpec/ExampleLength
|
20
25
|
|
21
|
-
it 'configures the
|
26
|
+
it 'configures the reader formats' do
|
22
27
|
readers = properties.readers
|
23
|
-
|
24
28
|
expect(readers.formats).to match_array %i(txt)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'configures the reader providers' do
|
32
|
+
readers = properties.readers
|
25
33
|
expect(readers.providers.to_a).to match_array [
|
26
34
|
[:txt, Rambling::Trie::Readers::PlainText],
|
27
35
|
]
|
@@ -44,14 +52,24 @@ describe Rambling::Trie::Configuration::Properties do
|
|
44
52
|
properties.readers.add :test, 'test'
|
45
53
|
end
|
46
54
|
|
47
|
-
|
55
|
+
# rubocop:disable RSpec/MultipleExpectations
|
56
|
+
it 'resets the serializers to initial values' do
|
48
57
|
expect(properties.serializers.formats).to include :test
|
49
|
-
expect(properties.readers.formats).to include :test
|
50
58
|
|
51
59
|
properties.reset
|
52
60
|
|
53
61
|
expect(properties.serializers.formats).not_to include :test
|
62
|
+
end
|
63
|
+
# rubocop:enable RSpec/MultipleExpectations
|
64
|
+
|
65
|
+
# rubocop:disable RSpec/MultipleExpectations
|
66
|
+
it 'resets the readers to initial values' do
|
67
|
+
expect(properties.readers.formats).to include :test
|
68
|
+
|
69
|
+
properties.reset
|
70
|
+
|
54
71
|
expect(properties.readers.formats).not_to include :test
|
55
72
|
end
|
73
|
+
# rubocop:enable RSpec/MultipleExpectations
|
56
74
|
end
|
57
75
|
end
|
@@ -8,11 +8,15 @@ describe Rambling::Trie::Configuration::ProviderCollection do
|
|
8
8
|
{ one: first_provider, two: second_provider }
|
9
9
|
end
|
10
10
|
|
11
|
-
let(:first_provider)
|
12
|
-
|
11
|
+
let(:first_provider) do
|
12
|
+
instance_double 'Rambling::Trie::Serializers::Marshal', :first_provider
|
13
|
+
end
|
14
|
+
let(:second_provider) do
|
15
|
+
instance_double 'Rambling::Trie::Serializers::Marshal', :second_provider
|
16
|
+
end
|
13
17
|
|
14
18
|
let(:provider_collection) do
|
15
|
-
|
19
|
+
described_class.new(
|
16
20
|
:provider,
|
17
21
|
configured_providers,
|
18
22
|
configured_default,
|
@@ -46,25 +50,31 @@ describe Rambling::Trie::Configuration::ProviderCollection do
|
|
46
50
|
let(:providers) { provider_collection.providers }
|
47
51
|
|
48
52
|
before do
|
49
|
-
allow(providers)
|
53
|
+
allow(providers).to receive_messages(
|
50
54
|
:[] => 'value',
|
51
55
|
keys: %i(a b),
|
52
56
|
)
|
53
57
|
end
|
54
58
|
|
59
|
+
# rubocop:disable RSpec/MultipleExpectations
|
55
60
|
it 'delegates `#[]` to providers' do
|
56
61
|
expect(provider_collection[:key]).to eq 'value'
|
57
62
|
expect(providers).to have_received(:[]).with :key
|
58
63
|
end
|
64
|
+
# rubocop:enable RSpec/MultipleExpectations
|
59
65
|
|
66
|
+
# rubocop:disable RSpec/MultipleExpectations
|
60
67
|
it 'aliases `#formats` to `providers#keys`' do
|
61
68
|
expect(provider_collection.formats).to eq %i(a b)
|
62
69
|
expect(providers).to have_received :keys
|
63
70
|
end
|
71
|
+
# rubocop:enable RSpec/MultipleExpectations
|
64
72
|
end
|
65
73
|
|
66
74
|
describe '#add' do
|
67
|
-
let(:provider)
|
75
|
+
let(:provider) do
|
76
|
+
instance_double 'Rambling::Trie::Serializers::Marshal', :provider
|
77
|
+
end
|
68
78
|
|
69
79
|
before do
|
70
80
|
provider_collection.add :three, provider
|
@@ -76,7 +86,9 @@ describe Rambling::Trie::Configuration::ProviderCollection do
|
|
76
86
|
end
|
77
87
|
|
78
88
|
describe '#default=' do
|
79
|
-
let(:other_provider)
|
89
|
+
let(:other_provider) do
|
90
|
+
instance_double 'Rambling::Trie::Serializers::Marshal', :other_provider
|
91
|
+
end
|
80
92
|
|
81
93
|
context 'when the given value is in the providers list' do
|
82
94
|
it 'changes the default provider' do
|
@@ -106,43 +118,59 @@ describe Rambling::Trie::Configuration::ProviderCollection do
|
|
106
118
|
expect(provider_collection.default).to be_nil
|
107
119
|
end
|
108
120
|
|
121
|
+
# rubocop:disable RSpec/MultipleExpectations
|
109
122
|
it 'raises an ArgumentError for any other provider' do
|
110
123
|
expect do
|
111
124
|
provider_collection.default = other_provider
|
112
125
|
end.to raise_error ArgumentError
|
113
126
|
expect(provider_collection.default).to be_nil
|
114
127
|
end
|
128
|
+
# rubocop:enable RSpec/MultipleExpectations
|
115
129
|
end
|
116
130
|
end
|
117
131
|
|
118
132
|
describe '#resolve' do
|
119
133
|
context 'when the file extension is one of the providers' do
|
120
|
-
|
121
|
-
|
122
|
-
|
134
|
+
[
|
135
|
+
['hola.one', :first_provider],
|
136
|
+
['hola.two', :second_provider],
|
137
|
+
].each do |test_params|
|
138
|
+
filepath, provider = test_params
|
139
|
+
|
140
|
+
it 'returns the corresponding provider' do
|
141
|
+
provider_instance = public_send provider
|
142
|
+
expect(provider_collection.resolve filepath).to eq provider_instance
|
143
|
+
end
|
123
144
|
end
|
124
145
|
end
|
125
146
|
|
126
147
|
context 'when the file extension is not one of the providers' do
|
127
|
-
|
128
|
-
|
129
|
-
|
148
|
+
%w(hola.unknown hola).each do |filepath|
|
149
|
+
it 'returns the default provider' do
|
150
|
+
expect(provider_collection.resolve filepath).to eq first_provider
|
151
|
+
end
|
130
152
|
end
|
131
153
|
end
|
132
154
|
end
|
133
155
|
|
134
156
|
describe '#reset' do
|
135
157
|
let(:configured_default) { second_provider }
|
136
|
-
let(:provider)
|
158
|
+
let(:provider) do
|
159
|
+
instance_double 'Rambling::Trie::Serializers::Marshal', :provider
|
160
|
+
end
|
137
161
|
|
138
162
|
before do
|
139
163
|
provider_collection.add :three, provider
|
140
164
|
provider_collection.default = provider
|
141
165
|
end
|
142
166
|
|
143
|
-
it 'resets to back to the initially configured values' do
|
167
|
+
it 'resets to back to the initially configured values (:three => nil)' do
|
144
168
|
provider_collection.reset
|
145
169
|
expect(provider_collection[:three]).to be_nil
|
170
|
+
end
|
171
|
+
|
172
|
+
it 'resets to back to the initially configured default' do
|
173
|
+
provider_collection.reset
|
146
174
|
expect(provider_collection.default).to eq second_provider
|
147
175
|
end
|
148
176
|
end
|