| 1 | var levenshtien = require("./../index");
|
|---|
| 2 |
|
|---|
| 3 | var assert = require("assert");
|
|---|
| 4 |
|
|---|
| 5 | describe("Damerau - Levenshtein", function() {
|
|---|
| 6 | describe("Equality", function() {
|
|---|
| 7 | it("returns 0 steps for equal strings", function() {
|
|---|
| 8 | assert.deepEqual(levenshtien("test", "test"), {
|
|---|
| 9 | steps: 0,
|
|---|
| 10 | relative: 0,
|
|---|
| 11 | similarity: 1
|
|---|
| 12 | });
|
|---|
| 13 | });
|
|---|
| 14 | });
|
|---|
| 15 |
|
|---|
| 16 | describe("Additions", function() {
|
|---|
| 17 | it("returns 1 step when appending one char", function() {
|
|---|
| 18 | assert.deepEqual(levenshtien("test", "tests"), {
|
|---|
| 19 | steps: 1,
|
|---|
| 20 | relative: 1 / 5,
|
|---|
| 21 | similarity: 1 - 1 / 5
|
|---|
| 22 | });
|
|---|
| 23 | });
|
|---|
| 24 |
|
|---|
| 25 | it("returns 1 step when prepending one char", function() {
|
|---|
| 26 | assert.deepEqual(levenshtien("test", "stest"), {
|
|---|
| 27 | steps: 1,
|
|---|
| 28 | relative: 1 / 5,
|
|---|
| 29 | similarity: 1 - 1 / 5
|
|---|
| 30 | });
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | it("returns 2 steps when appending two char", function() {
|
|---|
| 34 | assert.deepEqual(levenshtien("test", "mytest"), {
|
|---|
| 35 | steps: 2,
|
|---|
| 36 | relative: 2 / 6,
|
|---|
| 37 | similarity: 1 - 2 / 6
|
|---|
| 38 | });
|
|---|
| 39 | });
|
|---|
| 40 |
|
|---|
| 41 | it("returns 7 steps when appending seven char", function() {
|
|---|
| 42 | assert.deepEqual(levenshtien("test", "mycrazytest"), {
|
|---|
| 43 | steps: 7,
|
|---|
| 44 | relative: 7 / 11,
|
|---|
| 45 | similarity: 1 - 7 / 11
|
|---|
| 46 | });
|
|---|
| 47 | });
|
|---|
| 48 |
|
|---|
| 49 | it("returns 9 steps when prepend two chars and append seven chars", function() {
|
|---|
| 50 | assert.deepEqual(levenshtien("test", "mytestiscrazy"), {
|
|---|
| 51 | steps: 9,
|
|---|
| 52 | relative: 9 / 13,
|
|---|
| 53 | similarity: 1 - 9 / 13
|
|---|
| 54 | });
|
|---|
| 55 | });
|
|---|
| 56 | });
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | describe("Addition of repeated chars", function() {
|
|---|
| 60 | it("returns 1 step when repeating a character", function() {
|
|---|
| 61 | assert.deepEqual(levenshtien("test", "teest"), {
|
|---|
| 62 | steps: 1,
|
|---|
| 63 | relative: 1 / 5,
|
|---|
| 64 | similarity: 1 - 1 / 5
|
|---|
| 65 | });
|
|---|
| 66 | });
|
|---|
| 67 |
|
|---|
| 68 | it("returns 2 step when repeating a character twice", function() {
|
|---|
| 69 | assert.deepEqual(levenshtien("test", "teeest"), {
|
|---|
| 70 | steps: 2,
|
|---|
| 71 | relative: 2 / 6,
|
|---|
| 72 | similarity: 1 - 2 / 6
|
|---|
| 73 | });
|
|---|
| 74 | });
|
|---|
| 75 | });
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | describe("#Deletion", function() {
|
|---|
| 79 | it("returns 1 step when removing one char", function() {
|
|---|
| 80 | assert.deepEqual(levenshtien("test", "tst"), {
|
|---|
| 81 | steps: 1,
|
|---|
| 82 | relative: 1 / 4,
|
|---|
| 83 | similarity: 1 - 1 / 4
|
|---|
| 84 | });
|
|---|
| 85 | });
|
|---|
| 86 | });
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | describe("Transposition", function() {
|
|---|
| 90 | it("returns 1 step when transposing one char", function() {
|
|---|
| 91 | assert.deepEqual(levenshtien("test", "tset"), {
|
|---|
| 92 | steps: 1,
|
|---|
| 93 | relative: 1 / 4,
|
|---|
| 94 | similarity: 1 - 1 / 4
|
|---|
| 95 | });
|
|---|
| 96 | });
|
|---|
| 97 | });
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 | describe("Addition with transposition", function() {
|
|---|
| 101 | it("returns 2 step when transposing one char and append another", function() {
|
|---|
| 102 | assert.deepEqual(levenshtien("test", "tsets"), {
|
|---|
| 103 | steps: 2,
|
|---|
| 104 | relative: 2 / 5,
|
|---|
| 105 | similarity: 1 - 2 / 5
|
|---|
| 106 | });
|
|---|
| 107 | });
|
|---|
| 108 | it("returns 2 step when transposing a char and repeating it", function() {
|
|---|
| 109 | assert.deepEqual(levenshtien("test", "tsset"), {
|
|---|
| 110 | steps: 2,
|
|---|
| 111 | relative: 2 / 5,
|
|---|
| 112 | similarity: 1 - 2 / 5
|
|---|
| 113 | });
|
|---|
| 114 | });
|
|---|
| 115 | });
|
|---|
| 116 |
|
|---|
| 117 | describe("Transposition of multiple chars", function() {
|
|---|
| 118 | it("returns 1 step when transposing two neighbouring characters", function() {
|
|---|
| 119 | assert.deepEqual(levenshtien("banana", "banaan"), {
|
|---|
| 120 | steps: 1,
|
|---|
| 121 | relative: 1 / 6,
|
|---|
| 122 | similarity: 1 - 1 / 6
|
|---|
| 123 | });
|
|---|
| 124 | });
|
|---|
| 125 |
|
|---|
| 126 | it("returns 2 step when transposing two neighbouring characters by two places", function() {
|
|---|
| 127 | assert.deepEqual(levenshtien("banana", "nabana"), {
|
|---|
| 128 | steps: 2,
|
|---|
| 129 | relative: 2 / 6,
|
|---|
| 130 | similarity: 1 - 2 / 6
|
|---|
| 131 | });
|
|---|
| 132 | });
|
|---|
| 133 |
|
|---|
| 134 | it("returns 2 step when transposing two pairs of characters", function() {
|
|---|
| 135 | assert.deepEqual(levenshtien("banana", "abnaan"), {
|
|---|
| 136 | steps: 2,
|
|---|
| 137 | relative: 2 / 6,
|
|---|
| 138 | similarity: 1 - 2 / 6
|
|---|
| 139 | });
|
|---|
| 140 | });
|
|---|
| 141 | });
|
|---|
| 142 |
|
|---|
| 143 | describe("Empty strings", function() {
|
|---|
| 144 | it("returns 0 step and 0 relative when both are empty", function() {
|
|---|
| 145 | assert.deepEqual(levenshtien("", ""), {
|
|---|
| 146 | steps: 0,
|
|---|
| 147 | relative: 0,
|
|---|
| 148 | similarity: 1
|
|---|
| 149 | });
|
|---|
| 150 | });
|
|---|
| 151 |
|
|---|
| 152 | it("returns steps equal to first string lenght when second string is empty", function() {
|
|---|
| 153 | assert.deepEqual(levenshtien("test", ""), {
|
|---|
| 154 | steps: 4,
|
|---|
| 155 | relative: 4 / 4,
|
|---|
| 156 | similarity: 0
|
|---|
| 157 | });
|
|---|
| 158 | });
|
|---|
| 159 |
|
|---|
| 160 | it("returns steps equal to second string lenght when first string is empty", function() {
|
|---|
| 161 | assert.deepEqual(levenshtien("", "test"), {
|
|---|
| 162 | steps: 4,
|
|---|
| 163 | relative: 1,
|
|---|
| 164 | similarity: 0
|
|---|
| 165 | });
|
|---|
| 166 | });
|
|---|
| 167 | });
|
|---|
| 168 | });
|
|---|