diff --git a/spec/integration/rambling/trie_spec.rb b/spec/integration/rambling/trie_spec.rb index 875234b6..a3fdbdff 100644 --- a/spec/integration/rambling/trie_spec.rb +++ b/spec/integration/rambling/trie_spec.rb @@ -1,55 +1,6 @@ require 'spec_helper' describe Rambling::Trie do - shared_examples_for 'a compressable trie' do - context 'and the trie is not compressed' do - it_behaves_like 'a trie data structure' - - it 'does not alter the input' do - word = 'string' - trie.add word - - expect(word).to eq 'string' - end - - it 'is marked as not compressed' do - expect(trie).not_to be_compressed - end - end - - context 'and the trie is compressed' do - before { trie.compress! } - - it_behaves_like 'a trie data structure' - - it 'is marked as compressed' do - expect(trie).to be_compressed - end - end - end - - shared_examples_for 'a trie data structure' do - it 'contains all the words from the file' do - words.each do |word| - expect(trie).to include word - expect(trie.word? word).to be true - end - end - - it 'matches the start of all the words from the file' do - words.each do |word| - expect(trie.match? word).to be true - expect(trie.match? word[0..-2]).to be true - expect(trie.partial_word? word).to be true - expect(trie.partial_word? word[0..-2]).to be true - end - end - - it 'allows iterating over all the words' do - expect(trie.to_a.sort).to eq words.sort - end - end - describe 'with words provided directly' do it_behaves_like 'a compressable trie' do let(:words) { %w[a couple of words for our full trie integration test] } @@ -80,33 +31,6 @@ end end - shared_examples_for 'a serializable trie' do - context 'and the trie is not compressed' do - before do - Rambling::Trie.dump trie_to_serialize, trie_filepath, serializer - end - - it_behaves_like 'a compressable trie' do - let(:trie) { loaded_trie } - end - end - - context 'and the trie is compressed' do - let(:trie) { loaded_trie } - - before do - FileUtils.rm_f trie_filepath - Rambling::Trie.dump trie_to_serialize.compress!, trie_filepath, serializer - end - - it_behaves_like 'a trie data structure' - - it 'is marked as compressed' do - expect(trie).to be_compressed - end - end - end - describe 'saving and loading full trie from a file' do let(:words_filepath) { File.join ::SPEC_ROOT, 'assets', 'test_words.en_US.txt' } let(:words) { File.readlines(words_filepath).map &:chomp! } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 236b1660..79ee2f2e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -28,4 +28,7 @@ end require 'support/config' +require 'support/shared_examples/a_compressable_trie' +require 'support/shared_examples/a_serializable_trie' require 'support/shared_examples/a_serializer' +require 'support/shared_examples/a_trie_data_structure' diff --git a/spec/support/shared_examples/a_compressable_trie.rb b/spec/support/shared_examples/a_compressable_trie.rb new file mode 100644 index 00000000..8e37739f --- /dev/null +++ b/spec/support/shared_examples/a_compressable_trie.rb @@ -0,0 +1,26 @@ +shared_examples_for 'a compressable trie' do + context 'and the trie is not compressed' do + it_behaves_like 'a trie data structure' + + it 'does not alter the input' do + word = 'string' + trie.add word + + expect(word).to eq 'string' + end + + it 'is marked as not compressed' do + expect(trie).not_to be_compressed + end + end + + context 'and the trie is compressed' do + before { trie.compress! } + + it_behaves_like 'a trie data structure' + + it 'is marked as compressed' do + expect(trie).to be_compressed + end + end +end diff --git a/spec/support/shared_examples/a_serializable_trie.rb b/spec/support/shared_examples/a_serializable_trie.rb new file mode 100644 index 00000000..50d0bf59 --- /dev/null +++ b/spec/support/shared_examples/a_serializable_trie.rb @@ -0,0 +1,26 @@ +shared_examples_for 'a serializable trie' do + context 'and the trie is not compressed' do + before do + Rambling::Trie.dump trie_to_serialize, trie_filepath, serializer + end + + it_behaves_like 'a compressable trie' do + let(:trie) { loaded_trie } + end + end + + context 'and the trie is compressed' do + let(:trie) { loaded_trie } + + before do + FileUtils.rm_f trie_filepath + Rambling::Trie.dump trie_to_serialize.compress!, trie_filepath, serializer + end + + it_behaves_like 'a trie data structure' + + it 'is marked as compressed' do + expect(trie).to be_compressed + end + end +end diff --git a/spec/support/shared_examples/a_trie_data_structure.rb b/spec/support/shared_examples/a_trie_data_structure.rb new file mode 100644 index 00000000..5e81f0c7 --- /dev/null +++ b/spec/support/shared_examples/a_trie_data_structure.rb @@ -0,0 +1,21 @@ +shared_examples_for 'a trie data structure' do + it 'contains all the words from the file' do + words.each do |word| + expect(trie).to include word + expect(trie.word? word).to be true + end + end + + it 'matches the start of all the words from the file' do + words.each do |word| + expect(trie.match? word).to be true + expect(trie.match? word[0..-2]).to be true + expect(trie.partial_word? word).to be true + expect(trie.partial_word? word[0..-2]).to be true + end + end + + it 'allows iterating over all the words' do + expect(trie.to_a.sort).to eq words.sort + end +end