Sha256: 9574df94ed80ac44ffb3c70f5aeb77e51c6be950717ecb5bc393e07813a60d9e

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

require "csv"

shared_examples "Levenshtein Distance" do |options|
  options ||= {}

  describe ".distance" do
    context "when the two strings are equal" do
      it "is 0" do
        expect(described_class.distance("kitten", "kitten", options)).to eq 0
      end
    end

    context "when the first string is empty" do
      it "is the size of the second string" do
        expect(described_class.distance("","kitten", options)).to eq("kitten".size)
      end
    end

    context "when the second string is empty" do
      it "is the size of the first string" do
        expect(described_class.distance("kitten","", options)).to eq("kitten".size)
      end
    end

    context "when max_distance is passed as option" do
      context "and normal distance is greater than max_distance" do
        let(:max_distance) { 2 }

        it "is trimmed to max_distance" do
          expect(described_class.distance("kitten", "sitting",
            max_distance: max_distance)).to eq max_distance
        end
      end
    end

    CSV.foreach("spec/fixtures/levenshtein.csv") do |row|
      from, to, distance = row
      from = from.to_s.strip
      to   = to.to_s.strip

      it "calculates the distance from '#{from}' to '#{to}' correctly" do
        expect(described_class.distance(from, to, options)).to eq distance.to_i
      end
    end

    context "when insertion_cost is passed" do
      it "takes this cost into account" do
        expect(described_class.distance("kitten", "sitting", insertion_cost: 1)).not_to eq(
        described_class.distance("kitten", "sitting", insertion_cost: 2))
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
string_metric-0.1.1 spec/support/levenshtein.rb
string_metric-0.1.0 spec/support/levenshtein.rb