Sha256: 35950a8a7618e4da35d2f8ab7e703ff259cf61c6327b0206f281f1b504b87b29

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require 'spec_helper'

describe Rambling::Trie::Stringifyable do
  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
    end

    context 'for one letter' do
      before do
        node.letter = :a
        node.add ''
      end

      it 'returns the expected one letter word' do
        expect(node.as_word).to eq 'a'
      end
    end

    context 'for a small word' do
      before do
        node.letter = :a
        node.add 'll'
      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'
      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
      end
    end

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

      before do
        node.letter = :a
        node.add 'm'
        node.add 'dd'
      end

      it 'returns the words for the terminal nodes' do
        expect(compressed_node[:m].as_word).to eq 'am'
        expect(compressed_node[:dd].as_word).to eq 'add'
      end

      it 'raise an error for non terminal nodes' do
        expect { compressed_node.as_word }.to raise_error Rambling::Trie::InvalidOperation
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rambling-trie-1.0.2 spec/lib/rambling/trie/stringifyable_spec.rb
rambling-trie-1.0.1 spec/lib/rambling/trie/stringifyable_spec.rb
rambling-trie-1.0.0 spec/lib/rambling/trie/stringifyable_spec.rb