spec/integration/rambling/trie_spec.rb in rambling-trie-0.8.1 vs spec/integration/rambling/trie_spec.rb in rambling-trie-0.9.0

- old
+ new

@@ -1,20 +1,74 @@ require 'spec_helper' describe Rambling::Trie do - describe 'when a filepath is provided' do - let(:filepath) { File.join ::SPEC_ROOT, 'assets', 'test_words.txt' } - let(:words) { File.readlines(filepath).map &:chomp } - subject { Rambling::Trie.create filepath } + 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 { |word| expect(subject).to include word } + words.each do |word| + expect(trie).to include word + expect(trie.word? word).to be true + end end - describe 'and the trie is compressed' do - it 'still contains all the words from the file' do - subject.compress! - words.each { |word| expect(subject).to include word } + 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] } + let(:trie) { Rambling::Trie.create } + + before do + words.each do |word| + trie << word + trie.add word + end + end + end + end + + describe 'with words from a file' do + it_behaves_like 'a compressable trie' do + let(:filepath) { File.join ::SPEC_ROOT, 'assets', 'test_words.txt' } + let(:words) { File.readlines(filepath).map &:chomp } + let(:trie) { Rambling::Trie.create filepath } end end end