spec/lib/rambling/trie/compressed_node_spec.rb in rambling-trie-0.9.3 vs spec/lib/rambling/trie/compressed_node_spec.rb in rambling-trie-1.0.0

- old
+ new

@@ -150,6 +150,50 @@ expect(node.scan %w(c b a d)).to be_a Rambling::Trie::MissingNode end end end end + + describe '#match_prefix' do + let(:raw_node) { Rambling::Trie::RawNode.new } + let(:compressor) { Rambling::Trie::Compressor.new } + let(:node) { compressor.compress raw_node } + + before do + raw_node.letter = :i + raw_node.add 'gnite' + raw_node.add 'mport' + raw_node.add 'mportant' + raw_node.add 'mportantly' + end + + context 'when the node is terminal' do + before do + raw_node.terminal! + end + + it 'adds itself to the words' do + expect(node.match_prefix %w(g n i t e)).to include 'i' + end + end + + context 'when the node is not terminal' do + it 'does not add itself to the words' do + expect(node.match_prefix %w(g n i t e)).not_to include 'i' + end + end + + context 'when the first few chars match a terminal node' do + it 'adds those terminal nodes to the words' do + words = node.match_prefix(%w(m p o r t a n t l y)).to_a + expect(words).to include 'import', 'important', 'importantly' + end + end + + context 'when the first few chars do not match a terminal node' do + it 'does not add any other words found' do + words = node.match_prefix(%w(m p m p o r t a n t l y)).to_a + expect(words).not_to include 'import', 'important', 'importantly' + end + end + end end