|
4 | 4 | require "edits/levenshtein_shared" |
5 | 5 |
|
6 | 6 | RSpec.describe Edits::RestrictedEdit do |
| 7 | + cases = [ |
| 8 | + # simple transpositions |
| 9 | + ["a cat", "an act", 2], |
| 10 | + ["abc", "acb", 1], |
| 11 | + ["abc", "bac", 1], |
| 12 | + ["abcdef", "abcdfe", 1], |
| 13 | + ["abcdefghij", "acbdegfhji", 3], |
| 14 | + ["acre", "acer", 1], |
| 15 | + ["art", "ran", 2], |
| 16 | + ["caned", "acned", 1], |
| 17 | + ["iota", "atom", 3], |
| 18 | + ["minion", "noir", 4], |
| 19 | + |
| 20 | + # complex transpositions |
| 21 | + ["a cat", "a tc", 3], |
| 22 | + ["a cat", "an abct", 4], |
| 23 | + ["acer", "earn", 4], |
| 24 | + ["craned", "read", 4], |
| 25 | + ["information", "informant", 4], |
| 26 | + ["raced", "dear", 5], |
| 27 | + ["roam", "art", 4], |
| 28 | + ["tram", "rota", 4] |
| 29 | + ] |
| 30 | + |
7 | 31 | describe ".distance" do |
8 | 32 | subject { described_class.distance a, b } |
9 | 33 |
|
10 | 34 | include_examples "levenshtein" |
11 | 35 |
|
12 | | - [ |
13 | | - # swaps |
14 | | - ["a cat", "an act", 2], |
15 | | - ["abc", "acb", 1], |
16 | | - ["abc", "bac", 1], |
17 | | - ["abcdef", "abcdfe", 1], |
18 | | - ["abcdefghij", "acbdegfhji", 3], |
19 | | - ["acre", "acer", 1], |
20 | | - ["art", "ran", 2], |
21 | | - ["caned", "acned", 1], |
22 | | - ["iota", "atom", 3], |
23 | | - ["minion", "noir", 4], |
24 | | - |
25 | | - # complex transpositions |
26 | | - ["a cat", "a tc", 3], |
27 | | - ["a cat", "an abct", 4], |
28 | | - ["acer", "earn", 4], |
29 | | - ["craned", "read", 4], |
30 | | - ["information", "informant", 4], |
31 | | - ["raced", "dear", 5], |
32 | | - ["roam", "art", 4], |
33 | | - ["tram", "rota", 4] |
34 | | - ].each do |(a, b, distance)| |
| 36 | + cases.each do |(a, b, distance)| |
35 | 37 | context "with '#{a}', '#{b}'" do |
36 | 38 | let(:a) { a } |
37 | 39 | let(:b) { b } |
|
40 | 42 | end |
41 | 43 | end |
42 | 44 | end |
| 45 | + |
| 46 | + describe ".distance_with_max" do |
| 47 | + subject { described_class.distance_with_max a, b, max } |
| 48 | + |
| 49 | + context "when max is 100" do |
| 50 | + let(:max) { 100 } |
| 51 | + |
| 52 | + include_examples "levenshtein" |
| 53 | + |
| 54 | + cases.each do |(a, b, distance)| |
| 55 | + context "with '#{a}', '#{b}'" do |
| 56 | + let(:a) { a } |
| 57 | + let(:b) { b } |
| 58 | + |
| 59 | + it { is_expected.to eq distance } |
| 60 | + end |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + context "when max is 4" do |
| 65 | + let(:max) { 4 } |
| 66 | + |
| 67 | + cases.each do |(a, b, distance)| |
| 68 | + context "with '#{a}', '#{b}'" do |
| 69 | + let(:a) { a } |
| 70 | + let(:b) { b } |
| 71 | + |
| 72 | + it { is_expected.to eq(distance > max ? max : distance) } |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + context "with '', 'abcdfe'" do |
| 77 | + let(:a) { "" } |
| 78 | + let(:b) { "abcdfe" } |
| 79 | + |
| 80 | + it { is_expected.to eq max } |
| 81 | + end |
| 82 | + |
| 83 | + context "with 'abcdfe', ''" do |
| 84 | + let(:a) { "abcdfe" } |
| 85 | + let(:b) { "" } |
| 86 | + |
| 87 | + it { is_expected.to eq max } |
| 88 | + end |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + describe ".most_similar" do |
| 93 | + let(:prototype) { "atom" } |
| 94 | + |
| 95 | + subject { described_class.most_similar prototype, words } |
| 96 | + |
| 97 | + context "with empty array" do |
| 98 | + let(:words) { [] } |
| 99 | + |
| 100 | + it { is_expected.to be_nil } |
| 101 | + end |
| 102 | + |
| 103 | + context "when a single word has the lowest distance" do |
| 104 | + let(:words) { %w[light at atlas beer iota train] } |
| 105 | + |
| 106 | + it "returns the word with lowest distance from prototype" do |
| 107 | + expect(subject).to eq "at" |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + context "when two words share the lowest distance" do |
| 112 | + let(:words) { %w[light beer iota train] } |
| 113 | + |
| 114 | + it "returns the first with lowest distance from prototype" do |
| 115 | + expect(subject).to eq "iota" |
| 116 | + end |
| 117 | + end |
| 118 | + end |
43 | 119 | end |
0 commit comments