Skip to content

Commit

Permalink
remaining rubocops!
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzedge committed May 4, 2023
1 parent 976cdf7 commit 112c4cc
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 122 deletions.
22 changes: 13 additions & 9 deletions spec/lib/rambling/trie/enumerable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ module Trie
let(:node) { Rambling::Trie::Nodes::Raw.new }
let(:words) { %w(add some words and another word) }

before do
add_words node, words
end
before { add_words node, words }

describe '#each' do
it 'returns an enumerator' do
expect(node.each).to be_a Enumerator
end

it 'includes every word contained in the trie' do
node.each do |word|
expect(words).to include word
end

it 'has the same word count as the trie' do
expect(node.count).to eq words.count
end

it 'includes every word contained in the trie' do
node.each { |word| expect(words).to include word }
end
end

describe '#size' do
Expand All @@ -32,9 +30,15 @@ module Trie
end
end

it 'includes the core Enumerable module' do
it 'includes #all? from the core Enumerable module' do
expect(node.all? { |word| words.include? word }).to be true
end

it 'includes #any? from the core Enumerable module' do
expect(node.any? { |word| word.start_with? 's' }).to be true
end

it 'includes #to_a from the core Enumerable module' do
expect(node.to_a).to match_array words
end
end
Expand Down
12 changes: 9 additions & 3 deletions spec/lib/rambling/trie/inspectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,44 @@
let(:child) { node[:o] }
let(:terminal_node) { node[:o][:n][:l][:y] }

it 'returns a pretty printed version of the node' do
it 'returns a pretty printed version of the parent node' do
expect(node.inspect).to eq one_line <<~RAW
#<Rambling::Trie::Nodes::Raw letter: nil,
terminal: nil,
children: [:o, :t, :w]>
RAW
end

it 'returns a pretty printed version of the child node' do
expect(child.inspect).to eq one_line <<~CHILD
#<Rambling::Trie::Nodes::Raw letter: :o,
terminal: nil,
children: [:n]>
CHILD
end

it 'returns a pretty printed version of the terminal node' do
expect(terminal_node.inspect).to eq one_line <<~TERMINAL
#<Rambling::Trie::Nodes::Raw letter: :y,
terminal: true,
children: []>
TERMINAL
end

context 'for a compressed node' do
context 'with a compressed node' do
let(:compressor) { Rambling::Trie::Compressor.new }
let(:compressed) { compressor.compress node }
let(:compressed_child) { compressed[:o] }

it 'returns a pretty printed version of the compressed node' do
it 'returns a pretty printed version of the compressed parent node' do
expect(compressed.inspect).to eq one_line <<~COMPRESSED
#<Rambling::Trie::Nodes::Compressed letter: nil,
terminal: nil,
children: [:o, :t, :w]>
COMPRESSED
end

it 'returns a pretty printed version of the compressed child node' do
expect(compressed_child.inspect).to eq one_line <<~CHILD
#<Rambling::Trie::Nodes::Compressed letter: :only,
terminal: true,
Expand Down
43 changes: 23 additions & 20 deletions spec/lib/rambling/trie/nodes/raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,28 @@ def assign_letter letter

describe '#add' do
context 'when the node has no branches' do
before do
add_word node, 'abc'
end
before { add_word node, 'abc' }

it 'adds only one child' do
expect(node.children.size).to eq 1
end

# rubocop:disable RSpec/MultipleExpectations
it 'adds the full subtree' do
expect(node[:a]).not_to be_nil
expect(node[:a][:b]).not_to be_nil
expect(node[:a][:b][:c]).not_to be_nil
end
# rubocop:enable RSpec/MultipleExpectations

# rubocop:disable RSpec/MultipleExpectations
it 'marks only the last child as terminal' do
expect(node).not_to be_terminal
expect(node[:a]).not_to be_terminal
expect(node[:a][:b]).not_to be_terminal
expect(node[:a][:b][:c]).to be_terminal
end
# rubocop:enable RSpec/MultipleExpectations
end

context 'when a word is added more than once' do
Expand All @@ -59,19 +61,23 @@ def assign_letter letter
add_word node, 'ack'
end

# rubocop:disable RSpec/MultipleExpectations
it 'only counts it once' do
expect(node.children.size).to eq 1
expect(node[:a].children.size).to eq 1
expect(node[:a][:c].children.size).to eq 1
expect(node[:a][:c][:k].children.size).to eq 0
end
# rubocop:enable RSpec/MultipleExpectations

# rubocop:disable RSpec/MultipleExpectations
it 'does not change the terminal nodes in the tree' do
expect(node).not_to be_terminal
expect(node[:a]).not_to be_terminal
expect(node[:a][:c]).not_to be_terminal
expect(node[:a][:c][:k]).to be_terminal
end
# rubocop:enable RSpec/MultipleExpectations

it 'still returns the "added" node' do
child = add_word node, 'ack'
Expand All @@ -80,21 +86,20 @@ def assign_letter letter
end

context 'when the word does not exist in the tree but the letters do' do
before do
add_words node, %w(ack a)
end
before { add_words node, %w(ack a) }

it 'does not add another branch' do
expect(node.children.size).to eq 1
end

# rubocop:disable RSpec/MultipleExpectations
it 'marks the corresponding node as terminal' do
expect(node[:a]).to be_terminal

expect(node).not_to be_terminal
expect(node[:a]).to be_terminal
expect(node[:a][:c]).not_to be_terminal
expect(node[:a][:c][:k]).to be_terminal
end
# rubocop:enable RSpec/MultipleExpectations

it 'returns the added node' do
child = add_word node, 'a'
Expand All @@ -106,10 +111,8 @@ def assign_letter letter
let(:parent) { described_class.new }
let(:node) { described_class.new :a, parent }

context 'adding an empty string' do
before do
add_word node, ''
end
context 'when adding an empty string' do
before { add_word node, '' }

it 'does not alter the node letter' do
expect(node.letter).to eq :a
Expand All @@ -124,19 +127,19 @@ def assign_letter letter
end
end

context 'adding a one letter word' do
before do
add_word node, 'b'
end
context 'when adding a one letter word' do
before { add_word node, 'b' }

it 'does not alter the node letter' do
expect(node.letter).to eq :a
end

# rubocop:disable RSpec/MultipleExpectations
it 'adds a child with the expected letter' do
expect(node.children.size).to eq 1
expect(node.children.first.letter).to eq :b
end
# rubocop:enable RSpec/MultipleExpectations

it 'reports it has the expected letter a key' do
expect(node).to have_key(:b)
Expand All @@ -155,15 +158,14 @@ def assign_letter letter
end
end

context 'adding a large word' do
before do
add_word node, 'mplified'
end
context 'when adding a large word' do
before { add_word node, 'mplified' }

it 'marks the last letter as terminal' do
expect(node[:m][:p][:l][:i][:f][:i][:e][:d]).to be_terminal
end

# rubocop:disable RSpec/ExampleLength, RSpec/MultipleExpectations
it 'does not mark any other letter as terminal' do
expect(node[:m][:p][:l][:i][:f][:i][:e]).not_to be_terminal
expect(node[:m][:p][:l][:i][:f][:i]).not_to be_terminal
Expand All @@ -173,6 +175,7 @@ def assign_letter letter
expect(node[:m][:p]).not_to be_terminal
expect(node[:m]).not_to be_terminal
end
# rubocop:enable RSpec/ExampleLength, RSpec/MultipleExpectations
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion spec/lib/rambling/trie/readers/plain_text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
require 'spec_helper'

describe Rambling::Trie::Readers::PlainText do
subject(:reader) { described_class.new }

describe '#each_word' do
let(:filepath) { File.join(::SPEC_ROOT, 'assets', 'test_words.en_US.txt') }
let(:words) { File.readlines(filepath).map(&:chomp) }

it 'yields every word yielded by the file' do
yielded = []
subject.each_word(filepath) { |word| yielded << word }
reader.each_word(filepath) { |word| yielded << word }
expect(yielded).to eq words
end
end
Expand Down
30 changes: 17 additions & 13 deletions spec/lib/rambling/trie/stringifyable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
describe '#as_word' do
let(:node) { Rambling::Trie::Nodes::Raw.new }

context 'for an empty node' do
before do
add_word node, ''
end
context 'with an empty node' do
before { add_word node, '' }

it 'returns nil' do
expect(node.as_word).to be_empty
end
end

context 'for one letter' do
context 'with one letter' do
before do
node.letter = :a
add_word node, ''
Expand All @@ -27,7 +25,7 @@
end
end

context 'for a small word' do
context 'with a small word' do
before do
node.letter = :a
add_word node, 'll'
Expand All @@ -43,7 +41,7 @@
end
end

context 'for a long word' do
context 'with a long word' do
before do
node.letter = :b
add_word node, 'eautiful'
Expand All @@ -54,15 +52,15 @@
end
end

context 'for a node with nil letter' do
context 'with a node with nil letter' do
let(:node) { Rambling::Trie::Nodes::Raw.new nil }

it 'returns nil' do
expect(node.as_word).to be_empty
end
end

context 'for a compressed node' do
context 'with a compressed node' do
let(:compressor) { Rambling::Trie::Compressor.new }
let(:compressed_node) { compressor.compress node }

Expand All @@ -71,12 +69,18 @@
add_words node, %w(m dd)
end

it 'returns the words for the terminal nodes' do
expect(compressed_node[:m].as_word).to eq 'am'
expect(compressed_node[:d].as_word).to eq 'add'
[
[:m, 'am'],
[:d, 'add'],
].each do |test_params|
key, expected = test_params

it "returns the words for terminal nodes (#{key} => #{expected})" do
expect(compressed_node[key].as_word).to eq expected
end
end

it 'raise an error for non terminal nodes' do
it 'raises an error for non terminal nodes' do
expect { compressed_node.as_word }
.to raise_error Rambling::Trie::InvalidOperation
end
Expand Down
12 changes: 9 additions & 3 deletions spec/support/shared_examples/a_compressible_trie.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

shared_examples_for 'a compressible trie' do
context 'and the trie is not compressed' do
context 'with an uncompressed trie' do
it_behaves_like 'a trie data structure'

it 'does not alter the input' do
Expand All @@ -16,7 +16,7 @@
end
end

context 'and the trie is compressed' do
context 'with an compressed trie' do
let!(:original_root) { trie.root }
let!(:original_keys) { original_root.children_tree.keys }
let!(:original_values) { original_root.children_tree.values }
Expand All @@ -31,9 +31,15 @@
expect(trie).to be_compressed
end

it 'leaves the original root intact' do
it 'leaves the original root keys intact' do
expect(original_root.children_tree.keys).to eq original_keys
end

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

it 'leaves the original trie values intact' do
expect(trie.children_tree.values).not_to eq original_values
end
end
Expand Down
12 changes: 4 additions & 8 deletions spec/support/shared_examples/a_serializable_trie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,18 @@
let(:tmp_path) { File.join ::SPEC_ROOT, 'tmp' }
let(:filepath) { File.join tmp_path, "trie-root.#{format}" }

context 'and the trie is not compressed' do
before do
Rambling::Trie.dump trie_to_serialize, filepath
end
context 'with an uncompressed trie' do
before { Rambling::Trie.dump trie_to_serialize, filepath }

it_behaves_like 'a compressible trie' do
let(:trie) { Rambling::Trie.load filepath }
end
end

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

before do
Rambling::Trie.dump trie_to_serialize.compress!, filepath
end
before { Rambling::Trie.dump trie_to_serialize.compress!, filepath }

it_behaves_like 'a trie data structure'

Expand Down
Loading

0 comments on commit 112c4cc

Please sign in to comment.