test.js 4.4 KB

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