Skip to content

Commit

Permalink
Add tests for all arbitrary-value changes:
Browse files Browse the repository at this point in the history
- `Nodes::Node#value`
- `Container#add` and `Container#concat`; updated the `add_word` spec helpers.
- `Compressor#compress`
  • Loading branch information
gonzedge committed Dec 8, 2024
1 parent 6bb93dc commit 063fd26
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
23 changes: 23 additions & 0 deletions spec/lib/rambling/trie/compressor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,28 @@
expect(compressed[:y][:r][:s]).to be_compressed
end
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations

context 'with arbitrary values' do
# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
it 'preserves the arbitrary values in compressed terminal nodes' do
add_words node, %w(some wordy word), [1, 2, 3]
compressed = compressor.compress node

expect(compressed[:s].value).to eq 1
expect(compressed[:w].value).to eq 3
expect(compressed[:w][:y].value).to eq 2
end

it 'preserves nil values in compressed non-terminal nodes' do
add_words node, %w(a word other words), [4, 3, 2, 1]
compressed = compressor.compress node

expect(compressed[:a].value).to eq 4
expect(compressed[:o].value).to eq 2
expect(compressed[:w].value).to eq 3
expect(compressed[:w][:s].value).to eq 1
end
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/rambling/trie/container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
expect(root.children.size).to eq 0
expect(root.to_a).to be_empty
end

it 'stores given arbitrary value in terminal node' do
add_word container, 'hi', 5
expect(root[:h][:i].value).to eq 5
end
# rubocop:enable RSpec/MultipleExpectations
end

Expand All @@ -58,6 +63,12 @@
expect(nodes.first.letter).to eq :o
expect(nodes.last.letter).to eq :w
end

it 'stores arbitrary values in the terminal nodes' do
container.concat %w(other words), [10, 20]
expect(root[:o][:t][:h][:e][:r].value).to eq 10
expect(root[:w][:o][:r][:d][:s].value).to eq 20
end
# rubocop:enable RSpec/MultipleExpectations
end

Expand Down
11 changes: 6 additions & 5 deletions spec/support/helpers/add_word.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
module Support
module Helpers
module AddWord
def add_words node, words
words.each { |word| add_word node, word }
def add_words node, words, values = nil
values ||= []
words.zip(values).each { |word, value| add_word node, word, value }
end

def add_word node, word
def add_word node, word, value = nil
case node
when Rambling::Trie::Container
node.add word
node.add word, value
else
node.add word.chars.reverse.map(&:to_sym)
node.add word.chars.reverse.map(&:to_sym), value
end
end
end
Expand Down
19 changes: 12 additions & 7 deletions spec/support/shared_examples/a_compressible_trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,31 @@
context 'with an uncompressed trie' do
it_behaves_like 'a trie data structure'

it 'does not alter the input' do
it 'does not alter the input word' do
word = 'string'
add_word trie, word

expect(word).to eq 'string'
end

it 'does not alert the input' do
value = 'string'
add_word trie, 'word', value

expect(value).to eq 'string'
end

it 'is marked as not compressed' do
expect(trie).not_to be_compressed
end
end

context 'with an compressed trie' do
context 'with a compressed trie' do
let!(:original_root) { trie.root }
let!(:original_keys) { original_root.children_tree.keys }
let!(:original_values) { original_root.children_tree.values }

before do
trie.compress!
end
before { trie.compress! }

it_behaves_like 'a trie data structure'

Expand All @@ -35,11 +40,11 @@
expect(original_root.children_tree.keys).to eq original_keys
end

it 'leaves the original trie keys intact' do
it 'leaves the original trie children tree keys intact' do
expect(trie.children_tree.keys).to eq original_keys
end

it 'leaves the original trie values intact' do
it 'changes the original trie children tree values' do
expect(trie.children_tree.values).not_to eq original_values
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/shared_examples/a_serializable_trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end
end

context 'with an compressed trie' do
context 'with a compressed trie' do
let(:trie) { Rambling::Trie.load filepath }

before { Rambling::Trie.dump trie_to_serialize.compress!, filepath }
Expand Down
11 changes: 11 additions & 0 deletions spec/support/shared_examples/a_trie_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
expect(node).not_to be_terminal
end

it 'has no value' do
expect(node.value).to be_nil
end

it 'returns empty string as its word' do
expect(node.as_word).to be_empty
end
Expand Down Expand Up @@ -71,6 +75,13 @@
end
end

describe '#value=' do
it 'assigns the given value to the node' do
node.value = 42
expect(node.value).to eq 42
end
end

describe 'delegates and aliases' do
let :children_tree do
instance_double(
Expand Down

0 comments on commit 063fd26

Please sign in to comment.