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

- old
+ new

@@ -345,64 +345,45 @@ end end end end - describe '#as_word' do - let(:node) { Rambling::Trie::RawNode.new } - - context 'for an empty node' do - before do - node.add '' - end - - it 'returns nil' do - expect(node.as_word).to be_empty - end + describe '#match_prefix' do + before do + node.letter = :i + node.add 'gnite' + node.add 'mport' + node.add 'mportant' + node.add 'mportantly' end - context 'for one letter' do + context 'when the node is terminal' do before do - node.letter = :a - node.add '' + node.terminal! end - it 'returns the expected one letter word' do - expect(node.as_word).to eq 'a' + it 'adds itself to the words' do + expect(node.match_prefix %w(g n i t e)).to include 'i' end end - context 'for a small word' do - before do - node.letter = :a - node.add 'll' + 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 - - it 'returns the expected small word' do - expect(node[:l][:l].as_word).to eq 'all' - end - - it 'raises an error for a non terminal node' do - expect { node[:l].as_word }.to raise_error Rambling::Trie::InvalidOperation - end end - context 'for a long word' do - before do - node.letter = :b - node.add 'eautiful' + 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 - - it 'returns the expected long word' do - expect(node[:e][:a][:u][:t][:i][:f][:u][:l].as_word).to eq 'beautiful' - end end - context 'for a node with nil letter' do - let(:node) { Rambling::Trie::RawNode.new nil } - - it 'returns nil' do - expect(node.as_word).to be_empty + 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