123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- suite('lunr.Builder', function () {
- suite('#add', function () {
- setup(function () {
- this.builder = new lunr.Builder
- })
- test('field contains terms that clash with object prototype', function () {
- this.builder.field('title')
- this.builder.add({ id: 'id', title: 'constructor'})
- assert.deepProperty(this.builder.invertedIndex, 'constructor.title.id')
- assert.deepEqual(this.builder.invertedIndex.constructor.title.id, {})
- assert.equal(this.builder.fieldTermFrequencies['title/id'].constructor, 1)
- })
- test('field name clashes with object prototype', function () {
- this.builder.field('constructor')
- this.builder.add({ id: 'id', constructor: 'constructor'})
- assert.deepProperty(this.builder.invertedIndex, 'constructor.constructor.id')
- assert.deepEqual(this.builder.invertedIndex.constructor.constructor.id, {})
- })
- test('document ref clashes with object prototype', function () {
- this.builder.field('title')
- this.builder.add({ id: 'constructor', title: 'word'})
- assert.deepProperty(this.builder.invertedIndex, 'word.title.constructor')
- assert.deepEqual(this.builder.invertedIndex.word.title.constructor, {})
- })
- test('token metadata clashes with object prototype', function () {
- var pipelineFunction = function (t) {
- t.metadata['constructor'] = 'foo'
- return t
- }
- lunr.Pipeline.registerFunction(pipelineFunction, 'test')
- this.builder.pipeline.add(pipelineFunction)
- // the registeredFunctions object is global, this is to prevent
- // polluting any other tests.
- delete lunr.Pipeline.registeredFunctions.test
- this.builder.metadataWhitelist.push('constructor')
- this.builder.field('title')
- this.builder.add({ id: 'id', title: 'word'})
- assert.deepProperty(this.builder.invertedIndex, 'word.title.id.constructor')
- assert.deepEqual(this.builder.invertedIndex.word.title.id.constructor, ['foo'])
- })
- test('extracting nested properties from a document', function () {
- var extractor = function (d) { return d.person.name }
- this.builder.field('name', {
- extractor: extractor
- })
- this.builder.add({
- id: 'id',
- person: {
- name: 'bob'
- }
- })
- assert.deepProperty(this.builder.invertedIndex, 'bob.name.id')
- })
- })
- suite('#field', function () {
- test('defining fields to index', function () {
- var builder = new lunr.Builder
- builder.field('foo')
- assert.property(builder._fields, 'foo')
- })
- test('field with illegal characters', function () {
- var builder = new lunr.Builder
- assert.throws(function () {
- builder.field('foo/bar')
- })
- })
- })
- suite('#ref', function () {
- test('default reference', function () {
- var builder = new lunr.Builder
- assert.equal('id', builder._ref)
- })
- test('defining a reference field', function () {
- var builder = new lunr.Builder
- builder.ref('foo')
- assert.equal('foo', builder._ref)
- })
- })
- suite('#b', function () {
- test('default value', function () {
- var builder = new lunr.Builder
- assert.equal(0.75, builder._b)
- })
- test('values less than zero', function () {
- var builder = new lunr.Builder
- builder.b(-1)
- assert.equal(0, builder._b)
- })
- test('values higher than one', function () {
- var builder = new lunr.Builder
- builder.b(1.5)
- assert.equal(1, builder._b)
- })
- test('value within range', function () {
- var builder = new lunr.Builder
- builder.b(0.5)
- assert.equal(0.5, builder._b)
- })
- })
- suite('#k1', function () {
- test('default value', function () {
- var builder = new lunr.Builder
- assert.equal(1.2, builder._k1)
- })
- test('values less than zero', function () {
- var builder = new lunr.Builder
- builder.k1(1.6)
- assert.equal(1.6, builder._k1)
- })
- })
- suite('#use', function () {
- setup(function () {
- this.builder = new lunr.Builder
- })
- test('calls plugin function', function () {
- var wasCalled = false,
- plugin = function () { wasCalled = true }
- this.builder.use(plugin)
- assert.isTrue(wasCalled)
- })
- test('sets context to the builder instance', function () {
- var context = null,
- plugin = function () { context = this }
- this.builder.use(plugin)
- assert.equal(context, this.builder)
- })
- test('passes builder as first argument', function () {
- var arg = null,
- plugin = function (a) { arg = a }
- this.builder.use(plugin)
- assert.equal(arg, this.builder)
- })
- test('forwards arguments to the plugin', function () {
- var args = null,
- plugin = function () { args = [].slice.call(arguments) }
- this.builder.use(plugin, 1, 2, 3)
- assert.deepEqual(args, [this.builder, 1, 2, 3])
- })
- })
- suite('#build', function () {
- setup(function () {
- var builder = new lunr.Builder,
- doc = { id: 'id', title: 'test', body: 'missing' }
- builder.ref('id')
- builder.field('title')
- builder.add(doc)
- builder.build()
- this.builder = builder
- })
- test('adds tokens to invertedIndex', function () {
- assert.deepProperty(this.builder.invertedIndex, 'test.title.id')
- })
- test('builds a vector space of the document fields', function () {
- assert.property(this.builder.fieldVectors, 'title/id')
- assert.instanceOf(this.builder.fieldVectors['title/id'], lunr.Vector)
- })
- test('skips fields not defined for indexing', function () {
- assert.notProperty(this.builder.invertedIndex, 'missing')
- })
- test('builds a token set for the corpus', function () {
- var needle = lunr.TokenSet.fromString('test')
- assert.include(this.builder.tokenSet.intersect(needle).toArray(), 'test')
- })
- test('calculates document count', function () {
- assert.equal(1, this.builder.documentCount)
- })
- test('calculates average field length', function () {
- assert.equal(1, this.builder.averageFieldLength['title'])
- })
- test('index returned', function () {
- var builder = new lunr.Builder,
- doc = { id: 'id', title: 'test', body: 'missing' }
- builder.ref('id')
- builder.field('title')
- builder.add(doc)
- assert.instanceOf(builder.build(), lunr.Index)
- })
- })
- })
|