spec/lib/rambling/trie/compressor_spec.rb in rambling-trie-1.0.3 vs spec/lib/rambling/trie/compressor_spec.rb in rambling-trie-2.0.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'spec_helper' describe Rambling::Trie::Compressor do let(:compressor) { Rambling::Trie::Compressor.new } @@ -8,11 +10,11 @@ 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) + expect(compressed.children_tree.keys).to eq %i(a f w h) end context 'with at least one word' do before do add_words node, %w(all the words) @@ -31,14 +33,14 @@ end 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 + expect(compressed[:a].letter).to eq :all + expect(compressed[:a].children.size).to eq 0 + expect(compressed[:a]).to be_terminal + expect(compressed[:a]).to be_compressed end end context 'with two words' do before do @@ -49,58 +51,58 @@ 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][:l].letter).to eq :ll + expect(compressed[:a][:s].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][:l].children.size).to eq 0 + expect(compressed[:a][:s].children.size).to eq 0 - expect(compressed[:a][:ll]).to be_terminal - expect(compressed[:a][:sk]).to be_terminal + expect(compressed[:a][:l]).to be_terminal + expect(compressed[:a][:s]).to be_terminal - expect(compressed[:a][:ll]).to be_compressed - expect(compressed[:a][:sk]).to be_compressed + expect(compressed[:a][:l]).to be_compressed + expect(compressed[:a][:s]).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[:r].letter).to eq :re + expect(compressed[:r].parent).to eq compressed + expect(compressed[:r].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[:r][:p].letter).to eq :pa + expect(compressed[:r][:p].parent).to eq compressed[:r] + expect(compressed[:r][:p].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[:r][:s].letter).to eq :st + expect(compressed[:r][:s].parent).to eq compressed[:r] + expect(compressed[:r][:s].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[:r][:p][:y].letter).to eq :y + expect(compressed[:r][:p][:y].parent).to eq compressed[:r][:p] + expect(compressed[:r][:p][: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 + expect(compressed[:r][:p][:i].letter).to eq :int + expect(compressed[:r][:p][:i].parent).to eq compressed[:r][:p] + expect(compressed[:r][:p][:i].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[:y].letter).to eq :you - expect(compressed[:you][:r].letter).to eq :r - expect(compressed[:you][:r]).to be_compressed + expect(compressed[:y][:r].letter).to eq :r + expect(compressed[:y][:r]).to be_compressed - expect(compressed[:you][:r][:s].letter).to eq :s - expect(compressed[:you][:r][:s]).to be_compressed + expect(compressed[:y][:r][:s].letter).to eq :s + expect(compressed[:y][:r][:s]).to be_compressed end end end