spec/lib/rambling/trie/compressor_spec.rb in rambling-trie-1.0.2 vs spec/lib/rambling/trie/compressor_spec.rb in rambling-trie-1.0.3
- old
+ new
@@ -2,30 +2,105 @@
describe Rambling::Trie::Compressor do
let(:compressor) { Rambling::Trie::Compressor.new }
describe '#compress' do
- let(:words) { %w(a few words hello hell) }
- let(:root) do
- Rambling::Trie::RawNode.new
+ let(:node) { Rambling::Trie::Nodes::Raw.new }
+
+ it 'compresses the node' do
+ add_words node, %w(a few words hello hell)
+ compressed = compressor.compress node
+
+ expect(compressed.children_tree.keys).to eq %i(a few words hell)
end
- before do
- words.each { |w| root.add w.clone }
+ context 'with at least one word' do
+ before do
+ add_words node, %w(all the words)
+ end
+
+ it 'keeps the node letter nil' do
+ compressed = compressor.compress node
+
+ expect(compressed.letter).to be_nil
+ end
end
- it 'generates a new root with the words from the passed root' do
- new_root = compressor.compress root
+ context 'with a single word' do
+ before do
+ add_word node, 'all'
+ end
- expect(words).not_to be_empty
- words.each do |word|
- expect(new_root).to include word
+ it 'compresses into a single node without children' do
+ compressed = compressor.compress node
+
+ expect(compressed[:all].letter).to eq :all
+ expect(compressed[:all].children.size).to eq 0
+ expect(compressed[:all]).to be_terminal
+ expect(compressed[:all]).to be_compressed
end
end
- it 'compresses the new root' do
- new_root = compressor.compress root
+ context 'with two words' do
+ before do
+ add_words node, %w(all ask)
+ end
- expect(new_root.children_tree.keys).to eq %i(a few words hell)
+ it 'compresses into corresponding three nodes' do
+ compressed = compressor.compress node
+
+ expect(compressed[:a].letter).to eq :a
+ expect(compressed[:a].children.size).to eq 2
+
+ expect(compressed[:a][:ll].letter).to eq :ll
+ expect(compressed[:a][:sk].letter).to eq :sk
+
+ expect(compressed[:a][:ll].children.size).to eq 0
+ expect(compressed[:a][:sk].children.size).to eq 0
+
+ expect(compressed[:a][:ll]).to be_terminal
+ expect(compressed[:a][:sk]).to be_terminal
+
+ expect(compressed[:a][:ll]).to be_compressed
+ expect(compressed[:a][:sk]).to be_compressed
+ end
+ end
+
+ it 'reassigns the parent nodes correctly' do
+ add_words node, %w(repay rest repaint)
+ compressed = compressor.compress node
+
+ expect(compressed[:re].letter).to eq :re
+ expect(compressed[:re].parent).to eq compressed
+ expect(compressed[:re].children.size).to eq 2
+
+ expect(compressed[:re][:pa].letter).to eq :pa
+ expect(compressed[:re][:pa].parent).to eq compressed[:re]
+ expect(compressed[:re][:pa].children.size).to eq 2
+
+ expect(compressed[:re][:st].letter).to eq :st
+ expect(compressed[:re][:st].parent).to eq compressed[:re]
+ expect(compressed[:re][:st].children.size).to eq 0
+
+ expect(compressed[:re][:pa][:y].letter).to eq :y
+ expect(compressed[:re][:pa][:y].parent).to eq compressed[:re][:pa]
+ expect(compressed[:re][:pa][:y].children.size).to eq 0
+
+ expect(compressed[:re][:pa][:int].letter).to eq :int
+ expect(compressed[:re][:pa][:int].parent).to eq compressed[:re][:pa]
+ expect(compressed[:re][:pa][:int].children.size).to eq 0
+ end
+
+ it 'does not compress terminal nodes' do
+ add_words node, %w(you your yours)
+ compressed = compressor.compress node
+
+ expect(compressed[:you].letter).to eq :you
+
+ expect(compressed[:you][:r].letter).to eq :r
+ expect(compressed[:you][:r]).to be_compressed
+
+ expect(compressed[:you][:r][:s].letter).to eq :s
+ expect(compressed[:you][:r][:s]).to be_compressed
end
end
end