123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- suite('lunr.Query', function () {
- var allFields = ['title', 'body']
- suite('#term', function () {
- setup(function () {
- this.query = new lunr.Query (allFields)
- })
- suite('single string term', function () {
- setup(function () {
- this.query.term('foo')
- })
- test('adds a single clause', function () {
- assert.equal(this.query.clauses.length, 1)
- })
- test('clause has the correct term', function () {
- assert.equal(this.query.clauses[0].term, 'foo')
- })
- })
- suite('single token term', function () {
- setup(function () {
- this.query.term(new lunr.Token('foo'))
- })
- test('adds a single clause', function () {
- assert.equal(this.query.clauses.length, 1)
- })
- test('clause has the correct term', function () {
- assert.equal(this.query.clauses[0].term, 'foo')
- })
- })
- suite('multiple string terms', function () {
- setup(function () {
- this.query.term(['foo', 'bar'])
- })
- test('adds a single clause', function () {
- assert.equal(this.query.clauses.length, 2)
- })
- test('clause has the correct term', function () {
- var terms = this.query.clauses.map(function (c) { return c.term })
- assert.sameMembers(terms, ['foo', 'bar'])
- })
- })
- suite('multiple string terms with options', function () {
- setup(function () {
- this.query.term(['foo', 'bar'], { usePipeline: false })
- })
- test('clause has the correct term', function () {
- var terms = this.query.clauses.map(function (c) { return c.term })
- assert.sameMembers(terms, ['foo', 'bar'])
- })
- })
- suite('multiple token terms', function () {
- setup(function () {
- this.query.term(lunr.tokenizer('foo bar'))
- })
- test('adds a single clause', function () {
- assert.equal(this.query.clauses.length, 2)
- })
- test('clause has the correct term', function () {
- var terms = this.query.clauses.map(function (c) { return c.term })
- assert.sameMembers(terms, ['foo', 'bar'])
- })
- })
- })
- suite('#clause', function () {
- setup(function () {
- this.query = new lunr.Query (allFields)
- })
- suite('defaults', function () {
- setup(function () {
- this.query.clause({term: 'foo'})
- this.clause = this.query.clauses[0]
- })
- test('fields', function () {
- assert.sameMembers(this.clause.fields, allFields)
- })
- test('boost', function () {
- assert.equal(this.clause.boost, 1)
- })
- test('usePipeline', function () {
- assert.isTrue(this.clause.usePipeline)
- })
- })
- suite('specified', function () {
- setup(function () {
- this.query.clause({
- term: 'foo',
- boost: 10,
- fields: ['title'],
- usePipeline: false
- })
- this.clause = this.query.clauses[0]
- })
- test('fields', function () {
- assert.sameMembers(this.clause.fields, ['title'])
- })
- test('boost', function () {
- assert.equal(this.clause.boost, 10)
- })
- test('usePipeline', function () {
- assert.isFalse(this.clause.usePipeline)
- })
- })
- suite('wildcards', function () {
- suite('none', function () {
- setup(function () {
- this.query.clause({
- term: 'foo',
- wildcard: lunr.Query.wildcard.NONE
- })
- this.clause = this.query.clauses[0]
- })
- test('no wildcard', function () {
- assert.equal(this.clause.term, 'foo')
- })
- })
- suite('leading', function () {
- setup(function () {
- this.query.clause({
- term: 'foo',
- wildcard: lunr.Query.wildcard.LEADING
- })
- this.clause = this.query.clauses[0]
- })
- test('adds wildcard', function () {
- assert.equal(this.clause.term, '*foo')
- })
- })
- suite('trailing', function () {
- setup(function () {
- this.query.clause({
- term: 'foo',
- wildcard: lunr.Query.wildcard.TRAILING
- })
- this.clause = this.query.clauses[0]
- })
- test('adds wildcard', function () {
- assert.equal(this.clause.term, 'foo*')
- })
- })
- suite('leading and trailing', function () {
- setup(function () {
- this.query.clause({
- term: 'foo',
- wildcard: lunr.Query.wildcard.TRAILING | lunr.Query.wildcard.LEADING
- })
- this.clause = this.query.clauses[0]
- })
- test('adds wildcards', function () {
- assert.equal(this.clause.term, '*foo*')
- })
- })
- suite('existing', function () {
- setup(function () {
- this.query.clause({
- term: '*foo*',
- wildcard: lunr.Query.wildcard.TRAILING | lunr.Query.wildcard.LEADING
- })
- this.clause = this.query.clauses[0]
- })
- test('no additional wildcards', function () {
- assert.equal(this.clause.term, '*foo*')
- })
- })
- })
- })
- suite('#isNegated', function () {
- setup(function () {
- this.query = new lunr.Query (allFields)
- })
- suite('all prohibited', function () {
- setup(function () {
- this.query.term('foo', { presence: lunr.Query.presence.PROHIBITED })
- this.query.term('bar', { presence: lunr.Query.presence.PROHIBITED })
- })
- test('is negated', function () {
- assert.isTrue(this.query.isNegated())
- })
- })
- suite('some prohibited', function () {
- setup(function () {
- this.query.term('foo', { presence: lunr.Query.presence.PROHIBITED })
- this.query.term('bar', { presence: lunr.Query.presence.REQUIRED })
- })
- test('is negated', function () {
- assert.isFalse(this.query.isNegated())
- })
- })
- suite('none prohibited', function () {
- setup(function () {
- this.query.term('foo', { presence: lunr.Query.presence.OPTIONAL })
- this.query.term('bar', { presence: lunr.Query.presence.REQUIRED })
- })
- test('is negated', function () {
- assert.isFalse(this.query.isNegated())
- })
- })
- })
- })
|