gaddag 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +1 -0
- data/gaddag.gemspec +31 -0
- data/lib/gaddag.rb +47 -0
- data/lib/gaddag/arc.rb +41 -0
- data/lib/gaddag/node.rb +115 -0
- data/lib/gaddag/path.rb +72 -0
- data/lib/gaddag/word.rb +39 -0
- data/spec/shared/unit/gaddag/arc_context.rb +6 -0
- data/spec/shared/unit/gaddag/node/create_arc_behaviour.rb +41 -0
- data/spec/shared/unit/gaddag/node/create_final_path_behaviour.rb +33 -0
- data/spec/shared/unit/gaddag/node/create_path_behaviour.rb +15 -0
- data/spec/shared/unit/gaddag/node/create_path_context.rb +6 -0
- data/spec/unit/gaddag/add_spec.rb +46 -0
- data/spec/unit/gaddag/arc/add_final_letter_spec.rb +24 -0
- data/spec/unit/gaddag/arc/final_paths_spec.rb +49 -0
- data/spec/unit/gaddag/arc/initialize_spec.rb +16 -0
- data/spec/unit/gaddag/find_spec.rb +66 -0
- data/spec/unit/gaddag/initialize_spec.rb +11 -0
- data/spec/unit/gaddag/node/arc_spec.rb +23 -0
- data/spec/unit/gaddag/node/create_arc_spec.rb +8 -0
- data/spec/unit/gaddag/node/create_final_arc_spec.rb +18 -0
- data/spec/unit/gaddag/node/create_final_path_spec.rb +43 -0
- data/spec/unit/gaddag/node/create_path_spec.rb +44 -0
- data/spec/unit/gaddag/node/final_path_spec.rb +30 -0
- data/spec/unit/gaddag/node/final_paths_spec.rb +48 -0
- data/spec/unit/gaddag/node/follow_arc_spec.rb +24 -0
- data/spec/unit/gaddag/node/follow_path_spec.rb +41 -0
- data/spec/unit/gaddag/node/path_spec.rb +30 -0
- data/spec/unit/gaddag/path/equal_value_spec.rb +17 -0
- data/spec/unit/gaddag/path/include_delimiter_spec.rb +15 -0
- data/spec/unit/gaddag/path/initialize_spec.rb +12 -0
- data/spec/unit/gaddag/path/reversed_prefix_letters_spec.rb +34 -0
- data/spec/unit/gaddag/path/start_with_spec.rb +39 -0
- data/spec/unit/gaddag/path/suffix_letters_spec.rb +34 -0
- data/spec/unit/gaddag/path/to_ary_spec.rb +15 -0
- data/spec/unit/gaddag/path/to_s_spec.rb +18 -0
- data/spec/unit/gaddag/path/to_word_spec.rb +37 -0
- data/spec/unit/gaddag/word/equal_value_spec.rb +17 -0
- data/spec/unit/gaddag/word/initialize_spec.rb +12 -0
- data/spec/unit/gaddag/word/to_delimited_paths_spec.rb +29 -0
- data/spec/unit/gaddag/word/to_s_spec.rb +18 -0
- metadata +252 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Node, '#final_path?' do
|
6
|
+
subject { GADDAG::Node.new }
|
7
|
+
let(:letters) { %w[A B C D] }
|
8
|
+
|
9
|
+
context 'when a final path exists for the given list of letters' do
|
10
|
+
before { subject.create_final_path(letters) }
|
11
|
+
|
12
|
+
it 'returns true' do
|
13
|
+
expect(subject.final_path?(letters)).to eq(true)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when a final path does not exist for the given list of letters' do
|
18
|
+
it 'returns false' do
|
19
|
+
expect(subject.final_path?(letters)).to eq(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when the list of letters is empty' do
|
23
|
+
let(:letters) { [] }
|
24
|
+
|
25
|
+
it 'returns false' do
|
26
|
+
expect(subject.final_path?(letters)).to eq(false)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Node, '#final_paths' do
|
6
|
+
subject { GADDAG::Node.new }
|
7
|
+
let(:final_paths) { subject.final_paths }
|
8
|
+
|
9
|
+
context 'when no paths have been created' do
|
10
|
+
it 'returns an empty list' do
|
11
|
+
expect(final_paths).to be_empty
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when some paths have been created' do
|
16
|
+
context 'when none of the paths are final' do
|
17
|
+
before { subject.create_path(%w[K A E R B]) }
|
18
|
+
before { subject.create_path(%w[A B C D]) }
|
19
|
+
|
20
|
+
it 'returns an empty list' do
|
21
|
+
expect(final_paths).to be_empty
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when some of the paths are final' do
|
26
|
+
before { subject.create_path(%w[K A E R B]) }
|
27
|
+
before { subject.create_final_path(%w[A B C D]) }
|
28
|
+
|
29
|
+
it 'returns the paths that are final' do
|
30
|
+
expect(final_paths).to eq([GADDAG::Path.new(%w[A B C D])])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when two final paths exist with the same prefix' do
|
35
|
+
before do
|
36
|
+
subject.create_final_path(%w[F I N D])
|
37
|
+
subject.create_final_path(%w[F I N D E R])
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'returns both paths' do
|
41
|
+
expect(final_paths).to match_array([
|
42
|
+
GADDAG::Path.new(%w[F I N D]),
|
43
|
+
GADDAG::Path.new(%w[F I N D E R]),
|
44
|
+
])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Node, '#follow_arc' do
|
6
|
+
subject { GADDAG::Node.new }
|
7
|
+
|
8
|
+
let(:letter) { 'L' }
|
9
|
+
let(:destination) { GADDAG::Node.new }
|
10
|
+
|
11
|
+
context 'when the arc for the given letter exists' do
|
12
|
+
before { subject.create_arc(letter, destination) }
|
13
|
+
|
14
|
+
it 'returns the destination node that the arc points to' do
|
15
|
+
expect(subject.follow_arc(letter)).to equal(destination)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when the arc for the given letter does not exist' do
|
20
|
+
it 'raises an error indicating that no such arc exists' do
|
21
|
+
expect { subject.follow_arc(letter) }.to raise_error(KeyError)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Node, '#follow_path' do
|
6
|
+
subject { GADDAG::Node.new }
|
7
|
+
|
8
|
+
let(:letter) { 'L' }
|
9
|
+
let(:destinations) { GADDAG::Node.new }
|
10
|
+
|
11
|
+
context 'when the path for the given letters exists' do
|
12
|
+
context 'when given an empty path of letters' do
|
13
|
+
let(:letters) { [] }
|
14
|
+
before { subject.create_path(letters) }
|
15
|
+
|
16
|
+
it 'returns itself' do
|
17
|
+
expect(subject.follow_path(letters)).to equal(subject)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when given a path of letters' do
|
22
|
+
let(:letters) { %w[A B C D] }
|
23
|
+
let(:destinations) { 4.times.map { GADDAG::Node.new } }
|
24
|
+
before { subject.create_path(letters, destinations) }
|
25
|
+
|
26
|
+
it 'returns the destination node that the path of letters leads to' do
|
27
|
+
expect(subject.follow_path(letters)).to equal(destinations.last)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the path for the given letters does not exist' do
|
33
|
+
let(:letters) { %w[A B C D] }
|
34
|
+
let(:other_letters) { %w[A X Y D] }
|
35
|
+
before { subject.create_path(letters) }
|
36
|
+
|
37
|
+
it 'raises an error indicating that no such path exists' do
|
38
|
+
expect { subject.follow_path(other_letters) }.to raise_error(KeyError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Node, '#path?' do
|
6
|
+
subject { GADDAG::Node.new }
|
7
|
+
let(:letters) { %w[A B C D] }
|
8
|
+
|
9
|
+
context 'when a path exists for the given list of letters' do
|
10
|
+
before { subject.create_path(letters) }
|
11
|
+
|
12
|
+
it 'returns true' do
|
13
|
+
expect(subject.path?(letters)).to eq(true)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'when a path does not exist for the given list of letters' do
|
18
|
+
it 'returns false' do
|
19
|
+
expect(subject.path?(letters)).to eq(false)
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when the list of letters is empty' do
|
23
|
+
let(:letters) { [] }
|
24
|
+
|
25
|
+
it 'returns true' do
|
26
|
+
expect(subject.path?(letters)).to eq(true)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#==' do
|
6
|
+
context 'when two paths have the same letters' do
|
7
|
+
let(:path) { GADDAG::Path.new(%w[B ♢ R E A K]) }
|
8
|
+
let(:other_path) { GADDAG::Path.new(%w[B ♢ R E A K]) }
|
9
|
+
specify { expect(path).to eq(other_path) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when two paths have different letters' do
|
13
|
+
let(:path) { GADDAG::Path.new(%w[B ♢ R E A K]) }
|
14
|
+
let(:other_path) { GADDAG::Path.new(%w[C ♢ A R E]) }
|
15
|
+
specify { expect(path).to_not eq(other_path) }
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#include_delimiter?' do
|
6
|
+
context 'when the path includes a delimiter' do
|
7
|
+
subject { GADDAG::Path.new(%w[R B] + [GADDAG::Path::DELIMITER] + %w[E A K]) }
|
8
|
+
specify { expect(subject.include_delimiter?).to be_true }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when the path does not include a delimiter' do
|
12
|
+
subject { GADDAG::Path.new(%w[K A E R B]) }
|
13
|
+
specify { expect(subject.include_delimiter?).to be_false }
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#reversed_prefix_letters' do
|
6
|
+
context 'when the path is empty' do
|
7
|
+
subject { GADDAG::Path.new([]) }
|
8
|
+
specify { expect(subject.reversed_prefix_letters).to be_empty }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when the path includes a delimiter' do
|
12
|
+
subject { GADDAG::Path.new(%w[R B] + [GADDAG::Path::DELIMITER] + %w[E A K]) }
|
13
|
+
|
14
|
+
it 'returns an ordered list of letters that occur before the delimiter' do
|
15
|
+
expect(subject.reversed_prefix_letters).to eq(%w[R B])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when the path ends with a delimiter' do
|
20
|
+
subject { GADDAG::Path.new(%w[K A E R B] + [GADDAG::Path::DELIMITER]) }
|
21
|
+
|
22
|
+
it 'returns an ordered list of all the letters within the path' do
|
23
|
+
expect(subject.reversed_prefix_letters).to eq(%w[K A E R B])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when the path does not include a delimiter' do
|
28
|
+
subject { GADDAG::Path.new(%w[K A E R B]) }
|
29
|
+
|
30
|
+
it 'returns an ordered list of all the letters within the path' do
|
31
|
+
expect(subject.reversed_prefix_letters).to eq(%w[K A E R B])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#start_with?' do
|
6
|
+
let(:letters) { %w[H E L] }
|
7
|
+
|
8
|
+
context 'when a path starts with the given letters' do
|
9
|
+
subject { GADDAG::Path.new(%w[H E L L O]) }
|
10
|
+
|
11
|
+
it 'returns true' do
|
12
|
+
expect(subject.start_with?(letters)).to eq(true)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when a path does not start with the given letters' do
|
17
|
+
subject { GADDAG::Path.new(%w[B R E A K]) }
|
18
|
+
|
19
|
+
it 'returns false' do
|
20
|
+
expect(subject.start_with?(letters)).to eq(false)
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when the list of letters consists of nil values' do
|
24
|
+
let(:letters) { [nil, nil, nil] }
|
25
|
+
|
26
|
+
it 'returns true' do
|
27
|
+
expect(subject.start_with?(letters)).to eq(true)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'when the list of letters is empty' do
|
32
|
+
let(:letters) { [] }
|
33
|
+
|
34
|
+
it 'returns true' do
|
35
|
+
expect(subject.start_with?(letters)).to eq(true)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#suffix_letters' do
|
6
|
+
context 'when the path is empty' do
|
7
|
+
subject { GADDAG::Path.new([]) }
|
8
|
+
specify { expect(subject.to_s).to be_empty }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when the path includes a delimiter' do
|
12
|
+
subject { GADDAG::Path.new(%w[R B] + [GADDAG::Path::DELIMITER] + %w[E A K]) }
|
13
|
+
|
14
|
+
it 'returns an ordered list of letters that occur after the delimiter' do
|
15
|
+
expect(subject.suffix_letters).to eq(%w[E A K])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when the path ends with a delimiter' do
|
20
|
+
subject { GADDAG::Path.new(%w[K A E R B] + [GADDAG::Path::DELIMITER]) }
|
21
|
+
|
22
|
+
it 'returns an empty list' do
|
23
|
+
expect(subject.suffix_letters).to be_empty
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'when the path does not include a delimiter' do
|
28
|
+
subject { GADDAG::Path.new(%w[K A E R B]) }
|
29
|
+
|
30
|
+
it 'returns an empty list' do
|
31
|
+
expect(subject.suffix_letters).to be_empty
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#to_ary' do
|
6
|
+
subject { GADDAG::Path.new(%w[B ♢ R E A K]) }
|
7
|
+
|
8
|
+
it 'is implemented' do
|
9
|
+
expect(subject).to respond_to(:to_ary)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'returns its letter list' do
|
13
|
+
expect(subject.to_ary).to eq(subject.letters)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#to_s' do
|
6
|
+
context 'when the path is empty' do
|
7
|
+
subject { GADDAG::Path.new([]) }
|
8
|
+
specify { expect(subject.to_s).to be_empty }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when the path is not empty' do
|
12
|
+
subject { GADDAG::Path.new(%w[B ♢ R E A K]) }
|
13
|
+
|
14
|
+
it 'concatenates the letters in this path, delimited by \' > \'' do
|
15
|
+
expect(subject.to_s).to eq('B > ♢ > R > E > A > K')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Path, '#to_word' do
|
6
|
+
context 'when the path is empty' do
|
7
|
+
subject { GADDAG::Path.new([]) }
|
8
|
+
|
9
|
+
it 'returns an empty word' do
|
10
|
+
expect(subject.to_word).to eq(GADDAG::Word.new([]))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when the path includes a delimiter' do
|
15
|
+
subject { GADDAG::Path.new(%w[R B] + [GADDAG::Path::DELIMITER] + %w[E A K]) }
|
16
|
+
|
17
|
+
it 'returns a word with the letters in the correct order' do
|
18
|
+
expect(subject.to_word).to eq(GADDAG::Word.new(%w[B R E A K]))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when the path ends with a delimiter' do
|
23
|
+
subject { GADDAG::Path.new(%w[K A E R B] + [GADDAG::Path::DELIMITER]) }
|
24
|
+
|
25
|
+
it 'returns a word with the letters in the correct order' do
|
26
|
+
expect(subject.to_word).to eq(GADDAG::Word.new(%w[B R E A K]))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when the path does not include a delimiter' do
|
31
|
+
subject { GADDAG::Path.new(%w[K A E R B]) }
|
32
|
+
|
33
|
+
it 'returns a word with the letters in the correct order' do
|
34
|
+
expect(subject.to_word).to eq(GADDAG::Word.new(%w[B R E A K]))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'gaddag'
|
4
|
+
|
5
|
+
describe GADDAG::Word, '#==' do
|
6
|
+
context 'when two words have the same letters' do
|
7
|
+
let(:word) { GADDAG::Word.new(%w[B R E A K]) }
|
8
|
+
let(:other_word) { GADDAG::Word.new(%w[B R E A K]) }
|
9
|
+
specify { expect(word).to eq(other_word) }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'when two words have different letters' do
|
13
|
+
let(:word) { GADDAG::Word.new(%w[B R E A K]) }
|
14
|
+
let(:other_word) { GADDAG::Word.new(%w[C A R E]) }
|
15
|
+
specify { expect(word).to_not eq(other_word) }
|
16
|
+
end
|
17
|
+
end
|