1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
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.nestedProperty(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.nestedProperty(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.nestedProperty(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.nestedProperty(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.nestedProperty(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.nestedProperty(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)
})
})
})
|