UNPKG

autosuggestion

Version:

  Generates suggestions for text completion.  

130 lines 7.14 kB
import { Dictionary } from './dictionary'; import { Suggestion } from './suggestion'; describe('LookupNode', function () { describe('.matchPattern(...)', function () { it('returns no matches, given tokens which do not match in any context', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['small', 'slug'])).toEqual([]); }); it('returns no matches, given tokens which only partially match a complete word in a context (input is substring of context word)', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['bi', 'slug'])).toEqual([]); }); it('returns no matches, given tokens which match words within a pattern but not the entire pattern of a sub-context', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big', 'slug'])).toEqual([]); }); it('returns no matches, given tokens which complete word in a context partially matches an input word (input is superstring of context word)', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['bigger', 'slug'])).toEqual([]); }); it('returns a match with no remainder, given exactly matching input tokens', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; var matches = lookup.matchPattern(['big', 'bug']); expect(matches.length).toEqual(1); expect(matches[0].remainder.length).toEqual(0); }); it('returns a match with no remainder, given tokens which partially match a sub-context (the matched node is within the sub-context)', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); var bTrie = dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big'])).toEqual([ { nodes: [bTrie.next.word['b'].next.char['i'].next.char['g'], lookup], remainder: [] } ]); }); it('returns the lookup node as a match when a match has no remainder and is a complete match in the sub-context', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big', 'bug'])).toEqual([{ nodes: [lookup], remainder: [] }]); }); it('returns a match with a remainder, given tokens which match a sub-context but no other words in the current context (the match node is the lookup)', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug']]); trie.add({ 'b': 'B' }); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big', 'bug', 'here'])).toEqual([ { nodes: [lookup], remainder: ['here'] } ]); }); it('returns a match on the current lookup node with no remainder, given a pattern which matches the first sub-contextual lookup', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug'], ['bib']]); trie.add([{ 'b': 'B' }, { 'b': 'B' }]); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big', 'bug'])).toEqual([ { nodes: [lookup], remainder: [] } ]); }); it('returns a match (a node from a nested context) with no remainder, given a pattern which matches a second sequential sub-contextual lookup', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); var bTrie = dictionary.define('B', [['big', 'bug'], ['bib']]); trie.add([{ 'b': 'B' }, { 'bb': 'B' }]); var lookup = trie.next.lookup['b']; expect(lookup.matchPattern(['big', 'bug', 'b'])).toEqual([ { nodes: [bTrie.next.word['b'], lookup.next.lookup['bb']], remainder: [] } ]); }); it('returns a match on the current lookup node with no remainder, given a pattern which matches multiple consecutive sub-contextual lookups', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); dictionary.define('B', [['big', 'bug'], ['bib']]); trie.add([{ 'b': 'B' }, { 'bb': 'B' }]); var firstLookup = trie.next.lookup['b']; var lastLookup = trie.next.lookup['b'].next.lookup['bb']; expect(firstLookup.matchPattern(['big', 'bug', 'big', 'bug'])).toEqual([ { nodes: [lastLookup], remainder: [] } ]); }); }); describe('.completePattern(...)', function () { it('returns a single suggestion, given a terminal leaf lookup node with one word', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); var bTrie = dictionary.define('B', [['bub']]); trie.add([{ 'familiars': 'B' }]); var expectations = [ new Suggestion([]) ]; expect(trie.next.lookup['familiars'].completePattern([])).toEqual(expectations); }); it('returns a single suggestion, given a nonterminal lookup node with a following lookup', function () { var dictionary = new Dictionary(); var trie = dictionary.define('A'); var bTrie = dictionary.define('B', [['bub']]); trie.add([{ 'familiars': 'B' }, { 'unfamiliars': 'B' }]); var expectations = [ new Suggestion([{ 'unfamiliars': [bTrie] }]) ]; expect(trie.next.lookup['familiars'].completePattern([])).toEqual(expectations); }); }); }); //# sourceMappingURL=lookup.test.js.map