From 693a31e91c2e29e627b21a9092f298761b869f25 Mon Sep 17 00:00:00 2001 From: James Pudson <600720+nepsilon@users.noreply.github.com> Date: Fri, 27 Aug 2021 11:14:28 +0800 Subject: [PATCH] Add more tests to fix alwaysArray bug. --- test/test.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/test/test.js b/test/test.js index 11a9085..eb1e43a 100644 --- a/test/test.js +++ b/test/test.js @@ -58,6 +58,62 @@ describe('Search query syntax parser', function () { parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); }); + it('should return a tokenized string for excluded terms', function () { + var searchQuery = "fancy pyjama wear"; + var options = {tokenize: true, offsets: false, alwaysArray: true}; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('text', ['fancy', 'pyjama', 'wear']); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + + it('should return a array for only one term when alwaysArray is true', function () { + var searchQuery = "fancy"; + var options = {tokenize: true, offsets: false, alwaysArray: true}; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('text', ['fancy']); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + + it('should return a array for only one excluded term when alwaysArray is true', function () { + var searchQuery = "-fancy"; + var options = {tokenize: true, offsets: false, alwaysArray: true}; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('exclude', ['fancy']); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + + it('should return a string for only one excluded term when alwaysArray is false', function () { + var searchQuery = "fancy"; + var options = {tokenize: true, offsets: false, alwaysArray: false}; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('text', 'fancy'); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + it('should return a tokenized string with negation of unquoted terms', function () { var searchQuery = "fancy -pyjama -wear"; var options = { tokenize: true };