123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- module('lunr.Index')
- test("defining what fields to index", function () {
- var idx = new lunr.Index
- idx.field('foo')
- deepEqual(idx._fields[0], {name: 'foo', boost: 1})
- })
- test("giving a particular field a weighting", function () {
- var idx = new lunr.Index
- idx.field('foo', { boost: 10 })
- deepEqual(idx._fields[0], {name: 'foo', boost: 10})
- })
- test('default reference should be id', function () {
- var idx = new lunr.Index
- equal(idx._ref, 'id')
- })
- test("defining the reference field for the index", function () {
- var idx = new lunr.Index
- idx.ref('foo')
- deepEqual(idx._ref, 'foo')
- })
- test("default tokenizer should be the lunr.tokenizer", function () {
- var idx = new lunr.Index
- equal(idx.tokenizerFn, lunr.tokenizer)
- })
- test("using a custom tokenizer", function () {
- var idx = new lunr.Index,
- fn = function () {}
- lunr.tokenizer.registerFunction(fn, 'test')
- idx.tokenizer(fn)
- equal(idx.tokenizerFn, fn)
- })
- test('adding a document to the index', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'}
- idx.field('body')
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- ok(!!idx.documentStore.get(1))
- })
- test('adding a document with an empty field', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'test', title: ''}
- idx.field('title')
- idx.field('body')
- idx.add(doc)
- ok(!isNaN(idx.tokenStore.get('test')[1].tf))
- })
- test('ignore empty tokens', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'test ???'}
- idx.field('body')
- idx.pipeline.add(lunr.trimmer)
- idx.add(doc)
-
- var tokens = idx.documentStore.get(1).toArray()
- equal(tokens.length, 1)
- deepEqual(tokens, ['test']) // ??? should be ignored
- })
- test('triggering add events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'},
- callbackCalled = false,
- callbackArgs = []
- idx.on('add', function (doc, index) {
- callbackCalled = true
- callbackArgs = Array.prototype.slice.call(arguments)
- })
- idx.field('body')
- idx.add(doc)
- ok(callbackCalled)
- equal(callbackArgs.length, 2)
- deepEqual(callbackArgs[0], doc)
- deepEqual(callbackArgs[1], idx)
- })
- test('silencing add events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'},
- callbackCalled = false,
- callbackArgs = []
- idx.on('add', function (doc, index) {
- callbackCalled = true
- callbackArgs = Array.prototype.slice.call(arguments)
- })
- idx.field('body')
- idx.add(doc, false)
- ok(!callbackCalled)
- })
- test('removing a document from the index', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'}
- idx.field('body')
- equal(idx.documentStore.length, 0)
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- idx.remove(doc)
- equal(idx.documentStore.length, 0)
- })
- test('triggering remove events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'},
- callbackCalled = false,
- callbackArgs = []
- idx.on('remove', function (doc, index) {
- callbackCalled = true
- callbackArgs = Array.prototype.slice.call(arguments)
- })
- idx.field('body')
- idx.add(doc)
- idx.remove(doc)
- ok(callbackCalled)
- equal(callbackArgs.length, 2)
- deepEqual(callbackArgs[0], doc)
- deepEqual(callbackArgs[1], idx)
- })
- test('silencing remove events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'},
- callbackCalled = false,
- callbackArgs = []
- idx.on('remove', function (doc, index) {
- callbackCalled = true
- callbackArgs = Array.prototype.slice.call(arguments)
- })
- idx.field('body')
- idx.add(doc)
- idx.remove(doc, false)
- ok(!callbackCalled)
- })
- test('removing a non-existent document from the index', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'this is a test'},
- doc2 = {id: 2, body: 'i dont exist'},
- callbackCalled = false
- idx.on('remove', function (doc, index) {
- callbackCalled = true
- })
- idx.field('body')
- equal(idx.documentStore.length, 0)
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- idx.remove(doc2)
- equal(idx.documentStore.length, 1)
- ok(!callbackCalled)
- })
- test('updating a document', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'foo'}
- idx.field('body')
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- ok(idx.tokenStore.has('foo'))
- doc.body = 'bar'
- idx.update(doc)
- equal(idx.documentStore.length, 1)
- ok(idx.tokenStore.has('bar'))
- })
- test('emitting update events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'foo'},
- addCallbackCalled = false,
- removeCallbackCalled = false,
- updateCallbackCalled = false,
- callbackArgs = []
- idx.field('body')
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- ok(idx.tokenStore.has('foo'))
- idx.on('update', function (doc, index) {
- updateCallbackCalled = true
- callbackArgs = Array.prototype.slice.call(arguments)
- })
- idx.on('add', function () {
- addCallbackCalled = true
- })
- idx.on('remove', function () {
- removeCallbackCalled = true
- })
- doc.body = 'bar'
- idx.update(doc)
- ok(updateCallbackCalled)
- equal(callbackArgs.length, 2)
- deepEqual(callbackArgs[0], doc)
- deepEqual(callbackArgs[1], idx)
- ok(!addCallbackCalled)
- ok(!removeCallbackCalled)
- })
- test('silencing update events', function () {
- var idx = new lunr.Index,
- doc = {id: 1, body: 'foo'},
- callbackCalled = false
- idx.field('body')
- idx.add(doc)
- equal(idx.documentStore.length, 1)
- ok(idx.tokenStore.has('foo'))
- idx.on('update', function (doc, index) {
- callbackCalled = true
- })
- doc.body = 'bar'
- idx.update(doc, false)
- ok(!callbackCalled)
- })
- test('serialising', function () {
- var idx = new lunr.Index,
- mockDocumentStore = { toJSON: function () { return 'documentStore' }},
- mockTokenStore = { toJSON: function () { return 'tokenStore' }},
- mockCorpusTokens = { toJSON: function () { return 'corpusTokens' }},
- mockPipeline = { toJSON: function () { return 'pipeline' }}
- idx.documentStore = mockDocumentStore
- idx.tokenStore = mockTokenStore
- idx.corpusTokens = mockCorpusTokens
- idx.pipeline = mockPipeline
- idx.ref('id')
- idx.field('title', { boost: 10 })
- idx.field('body')
- deepEqual(idx.toJSON(), {
- version: '@VERSION', // this is what the lunr version is set to before being built
- fields: [
- { name: 'title', boost: 10 },
- { name: 'body', boost: 1 }
- ],
- ref: 'id',
- documentStore: 'documentStore',
- tokenStore: 'tokenStore',
- corpusTokens: 'corpusTokens',
- pipeline: 'pipeline',
- tokenizer: 'default'
- })
- })
- test('loading a serialised index', function () {
- var serialisedData = {
- version: '@VERSION', // this is what the lunr version is set to before being built
- fields: [
- { name: 'title', boost: 10 },
- { name: 'body', boost: 1 }
- ],
- ref: 'id',
- documentStore: { store: {}, length: 0 },
- tokenStore: { root: {}, length: 0 },
- corpusTokens: [],
- pipeline: ['stopWordFilter', 'stemmer'],
- tokenizer: 'default'
- }
- var idx = lunr.Index.load(serialisedData)
- deepEqual(idx._fields, serialisedData.fields)
- equal(idx._ref, 'id')
- })
- test('idf cache with reserved words', function () {
- var idx = new lunr.Index
- var troublesomeTokens = [
- 'constructor',
- '__proto__',
- 'hasOwnProperty',
- 'isPrototypeOf',
- 'propertyIsEnumerable',
- 'toLocaleString',
- 'toString',
- 'valueOf'
- ]
- troublesomeTokens.forEach(function (token) {
- equal(typeof(idx.idf(token)), 'number', 'Using token: ' + token)
- })
- })
- test('using a plugin', function () {
- var idx = new lunr.Index,
- ctx, args,
- plugin = function () {
- ctx = this
- args = Array.prototype.slice.call(arguments)
- this.pluginLoaded = true
- }
- idx.use(plugin, 'foo', 'bar')
- equal(ctx, idx)
- deepEqual(args, [idx, 'foo', 'bar'])
- ok(idx.pluginLoaded)
- })
|