arraymatcher_test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.ui.ac.ArrayMatcherTest');
  15. goog.setTestOnly('goog.ui.ac.ArrayMatcherTest');
  16. goog.require('goog.testing.jsunit');
  17. goog.require('goog.ui.ac.ArrayMatcher');
  18. var ArrayMatcher = goog.ui.ac.ArrayMatcher;
  19. function testRequestingRows() {
  20. var items = ['a', 'Ab', 'abc', 'ba', 'ca'];
  21. var am = new ArrayMatcher(items, true);
  22. var res;
  23. function matcher(token, matches) {
  24. assertEquals('a', token);
  25. res = matches;
  26. assertEquals('Should have three matches', 3, matches.length);
  27. assertEquals('a', matches[0]);
  28. assertEquals('Ab', matches[1]);
  29. assertEquals('abc', matches[2]);
  30. }
  31. am.requestMatchingRows('a', 10, matcher);
  32. var res2 = goog.ui.ac.ArrayMatcher.getMatchesForRows('a', 10, items);
  33. assertArrayEquals(res, res2);
  34. }
  35. function testRequestingRowsMaxMatches() {
  36. var items = ['a', 'Ab', 'abc', 'ba', 'ca'];
  37. var am = new ArrayMatcher(items, true);
  38. function matcher(token, matches) {
  39. assertEquals('a', token);
  40. assertEquals('Should have two matches', 2, matches.length);
  41. assertEquals('a', matches[0]);
  42. assertEquals('Ab', matches[1]);
  43. }
  44. am.requestMatchingRows('a', 2, matcher);
  45. }
  46. function testRequestingRowsSimilarMatches() {
  47. // No prefix matches so use similar
  48. var items = ['b', 'c', 'ba', 'ca'];
  49. var am = new ArrayMatcher(items, false);
  50. function matcher(token, matches) {
  51. assertEquals('a', token);
  52. assertEquals('Should have two matches', 2, matches.length);
  53. assertEquals('ba', matches[0]);
  54. assertEquals('ca', matches[1]);
  55. }
  56. am.requestMatchingRows('a', 10, matcher);
  57. }
  58. function testRequestingRowsSimilarMatchesMaxMatches() {
  59. // No prefix matches so use similar
  60. var items = ['b', 'c', 'ba', 'ca'];
  61. var am = new ArrayMatcher(items, false);
  62. function matcher(token, matches) {
  63. assertEquals('a', token);
  64. assertEquals('Should have one match', 1, matches.length);
  65. assertEquals('ba', matches[0]);
  66. }
  67. am.requestMatchingRows('a', 1, matcher);
  68. }
  69. function testGetPrefixMatches() {
  70. var items = ['a', 'b', 'c'];
  71. var am = new ArrayMatcher(items, true);
  72. var res = am.getPrefixMatches('a', 10);
  73. assertEquals('Should have one match', 1, res.length);
  74. assertEquals('Should return \'a\'', 'a', res[0]);
  75. var res2 = goog.ui.ac.ArrayMatcher.getPrefixMatchesForRows('a', 10, items);
  76. assertArrayEquals(res, res2);
  77. }
  78. function testGetPrefixMatchesMaxMatches() {
  79. var items = ['a', 'Ab', 'abc', 'ba', 'ca'];
  80. var am = new ArrayMatcher(items, true);
  81. var res = am.getPrefixMatches('a', 2);
  82. assertEquals('Should have two matches', 2, res.length);
  83. assertEquals('a', res[0]);
  84. }
  85. function testGetPrefixMatchesEmptyToken() {
  86. var items = ['a', 'b', 'c'];
  87. var am = new ArrayMatcher(items, true);
  88. var res = am.getPrefixMatches('', 10);
  89. assertEquals('Should have no matches', 0, res.length);
  90. }
  91. function testGetSimilarRowsSimple() {
  92. var items = ['xa', 'xb', 'xc'];
  93. var am = new ArrayMatcher(items, true);
  94. var res = am.getSimilarRows('a', 10);
  95. assertEquals('Should have one match', 1, res.length);
  96. assertEquals('xa', res[0]);
  97. var res2 = goog.ui.ac.ArrayMatcher.getSimilarMatchesForRows('a', 10, items);
  98. assertArrayEquals(res, res2);
  99. }
  100. function testGetSimilarRowsMaxMatches() {
  101. var items = ['xa', 'xAa', 'xaAa'];
  102. var am = new ArrayMatcher(items, true);
  103. var res = am.getSimilarRows('a', 2);
  104. assertEquals('Should have two matches', 2, res.length);
  105. assertEquals('xa', res[0]);
  106. assertEquals('xAa', res[1]);
  107. }
  108. function testGetSimilarRowsTermDistance() {
  109. var items = ['surgeon', 'pleasantly', 'closely', 'ba'];
  110. var am = new ArrayMatcher(items, true);
  111. var res = am.getSimilarRows('urgently', 4);
  112. assertEquals('Should have one match', 1, res.length);
  113. assertEquals('surgeon', res[0]);
  114. var res2 = ArrayMatcher.getSimilarMatchesForRows('urgently', 4, items);
  115. assertArrayEquals(res, res2);
  116. }
  117. function testGetSimilarRowsContainedTerms() {
  118. var items = ['application', 'apple', 'happy'];
  119. var am = new ArrayMatcher(items, true);
  120. var res = am.getSimilarRows('app', 4);
  121. assertEquals('Should have three matches', 3, res.length);
  122. assertEquals('application', res[0]);
  123. assertEquals('apple', res[1]);
  124. assertEquals('happy', res[2]);
  125. var res2 = ArrayMatcher.getSimilarMatchesForRows('app', 4, items);
  126. assertArrayEquals(res, res2);
  127. }