| 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 |
|
| 15 |
goog.provide('goog.ui.ac.ArrayMatcherTest'); |
| 16 |
goog.setTestOnly('goog.ui.ac.ArrayMatcherTest'); |
| 17 |
|
| 18 |
goog.require('goog.testing.jsunit'); |
| 19 |
goog.require('goog.ui.ac.ArrayMatcher'); |
| 20 |
|
| 21 |
var ArrayMatcher = goog.ui.ac.ArrayMatcher; |
| 22 |
|
| 23 |
function testRequestingRows() { |
| 24 |
var items = ['a', 'Ab', 'abc', 'ba', 'ca']; |
| 25 |
var am = new ArrayMatcher(items, true); |
| 26 |
|
| 27 |
var res; |
| 28 |
function matcher(token, matches) { |
| 29 |
assertEquals('a', token); |
| 30 |
res = matches; |
| 31 |
assertEquals('Should have three matches', 3, matches.length); |
| 32 |
assertEquals('a', matches[0]); |
| 33 |
assertEquals('Ab', matches[1]); |
| 34 |
assertEquals('abc', matches[2]); |
| 35 |
} |
| 36 |
|
| 37 |
am.requestMatchingRows('a', 10, matcher); |
| 38 |
var res2 = goog.ui.ac.ArrayMatcher.getMatchesForRows('a', 10, items); |
| 39 |
assertArrayEquals(res, res2); |
| 40 |
} |
| 41 |
|
| 42 |
function testRequestingRowsMaxMatches() { |
| 43 |
var items = ['a', 'Ab', 'abc', 'ba', 'ca']; |
| 44 |
var am = new ArrayMatcher(items, true); |
| 45 |
|
| 46 |
function matcher(token, matches) { |
| 47 |
assertEquals('a', token); |
| 48 |
assertEquals('Should have two matches', 2, matches.length); |
| 49 |
assertEquals('a', matches[0]); |
| 50 |
assertEquals('Ab', matches[1]); |
| 51 |
} |
| 52 |
|
| 53 |
am.requestMatchingRows('a', 2, matcher); |
| 54 |
} |
| 55 |
|
| 56 |
function testRequestingRowsSimilarMatches() { |
| 57 |
// No prefix matches so use similar |
| 58 |
var items = ['b', 'c', 'ba', 'ca']; |
| 59 |
var am = new ArrayMatcher(items, false); |
| 60 |
|
| 61 |
function matcher(token, matches) { |
| 62 |
assertEquals('a', token); |
| 63 |
assertEquals('Should have two matches', 2, matches.length); |
| 64 |
assertEquals('ba', matches[0]); |
| 65 |
assertEquals('ca', matches[1]); |
| 66 |
} |
| 67 |
|
| 68 |
am.requestMatchingRows('a', 10, matcher); |
| 69 |
} |
| 70 |
|
| 71 |
function testRequestingRowsSimilarMatchesMaxMatches() { |
| 72 |
// No prefix matches so use similar |
| 73 |
var items = ['b', 'c', 'ba', 'ca']; |
| 74 |
var am = new ArrayMatcher(items, false); |
| 75 |
|
| 76 |
function matcher(token, matches) { |
| 77 |
assertEquals('a', token); |
| 78 |
assertEquals('Should have one match', 1, matches.length); |
| 79 |
assertEquals('ba', matches[0]); |
| 80 |
} |
| 81 |
|
| 82 |
am.requestMatchingRows('a', 1, matcher); |
| 83 |
} |
| 84 |
|
| 85 |
function testGetPrefixMatches() { |
| 86 |
var items = ['a', 'b', 'c']; |
| 87 |
var am = new ArrayMatcher(items, true); |
| 88 |
|
| 89 |
var res = am.getPrefixMatches('a', 10); |
| 90 |
assertEquals('Should have one match', 1, res.length); |
| 91 |
assertEquals('Should return \'a\'', 'a', res[0]); |
| 92 |
var res2 = goog.ui.ac.ArrayMatcher.getPrefixMatchesForRows('a', 10, items); |
| 93 |
assertArrayEquals(res, res2); |
| 94 |
} |
| 95 |
|
| 96 |
function testGetPrefixMatchesMaxMatches() { |
| 97 |
var items = ['a', 'Ab', 'abc', 'ba', 'ca']; |
| 98 |
var am = new ArrayMatcher(items, true); |
| 99 |
|
| 100 |
var res = am.getPrefixMatches('a', 2); |
| 101 |
assertEquals('Should have two matches', 2, res.length); |
| 102 |
assertEquals('a', res[0]); |
| 103 |
} |
| 104 |
|
| 105 |
function testGetPrefixMatchesEmptyToken() { |
| 106 |
var items = ['a', 'b', 'c']; |
| 107 |
var am = new ArrayMatcher(items, true); |
| 108 |
|
| 109 |
var res = am.getPrefixMatches('', 10); |
| 110 |
assertEquals('Should have no matches', 0, res.length); |
| 111 |
} |
| 112 |
|
| 113 |
function testGetSimilarRowsSimple() { |
| 114 |
var items = ['xa', 'xb', 'xc']; |
| 115 |
var am = new ArrayMatcher(items, true); |
| 116 |
|
| 117 |
var res = am.getSimilarRows('a', 10); |
| 118 |
assertEquals('Should have one match', 1, res.length); |
| 119 |
assertEquals('xa', res[0]); |
| 120 |
var res2 = goog.ui.ac.ArrayMatcher.getSimilarMatchesForRows('a', 10, items); |
| 121 |
assertArrayEquals(res, res2); |
| 122 |
} |
| 123 |
|
| 124 |
function testGetSimilarRowsMaxMatches() { |
| 125 |
var items = ['xa', 'xAa', 'xaAa']; |
| 126 |
var am = new ArrayMatcher(items, true); |
| 127 |
|
| 128 |
var res = am.getSimilarRows('a', 2); |
| 129 |
assertEquals('Should have two matches', 2, res.length); |
| 130 |
assertEquals('xa', res[0]); |
| 131 |
assertEquals('xAa', res[1]); |
| 132 |
} |
| 133 |
|
| 134 |
function testGetSimilarRowsTermDistance() { |
| 135 |
var items = ['surgeon', 'pleasantly', 'closely', 'ba']; |
| 136 |
var am = new ArrayMatcher(items, true); |
| 137 |
|
| 138 |
var res = am.getSimilarRows('urgently', 4); |
| 139 |
assertEquals('Should have one match', 1, res.length); |
| 140 |
assertEquals('surgeon', res[0]); |
| 141 |
|
| 142 |
var res2 = ArrayMatcher.getSimilarMatchesForRows('urgently', 4, items); |
| 143 |
assertArrayEquals(res, res2); |
| 144 |
} |
| 145 |
|
| 146 |
function testGetSimilarRowsContainedTerms() { |
| 147 |
var items = ['application', 'apple', 'happy']; |
| 148 |
var am = new ArrayMatcher(items, true); |
| 149 |
var res = am.getSimilarRows('app', 4); |
| 150 |
assertEquals('Should have three matches', 3, res.length); |
| 151 |
assertEquals('application', res[0]); |
| 152 |
assertEquals('apple', res[1]); |
| 153 |
assertEquals('happy', res[2]); |
| 154 |
|
| 155 |
var res2 = ArrayMatcher.getSimilarMatchesForRows('app', 4, items); |
| 156 |
assertArrayEquals(res, res2); |
| 157 |
} |