Navigation Menu

Skip to content

Commit

Permalink
q: support phrase start character check
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 3, 2012
1 parent 61c70fe commit 2dd4492
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/q-translator.js
Expand Up @@ -36,8 +36,9 @@ QueryTranslator.prototype = {
},
translatePhraseTerm: function() {
if (this.query[this.offset] != '"') {
this.thorwTranslateError("phrase must start with <\">");
this.throwTranslateError("phrase must start with <\">");
}

this.offset++;
var phrase = "";
for (; this.offset < this.query.length; this.offset++) {
Expand All @@ -54,7 +55,7 @@ QueryTranslator.prototype = {
}
phrase += character;
}
this.thorwTranslateError("phrase is unterminated");
this.throwTranslateError("phrase is unterminated");
},
translateTerm: function() {
this.skipSpaces(this.query, this);
Expand Down
22 changes: 22 additions & 0 deletions test/q-translator.test.js
Expand Up @@ -41,6 +41,24 @@ function testPhraseTerm(label, phraseTerm,
});
}

function testPhraseTermError(label, phraseTerm, context, detail) {
test('error: phrase term: ' + label + ': ' +
'<' + phraseTerm + '>', function() {
var translator = new QueryTranslator(phraseTerm);
translator.defaultField = "field";
var actualError;
assert.throw(function() {
try {
translator.translatePhraseTerm();
} catch (error) {
actualError = error;
throw error;
}
});
assert.equal(actualError.message, "<" + context + ">" + ": " + detail);
});
}

function testTerm(label, term, expectedOffset, expectedBooleanQuery) {
test('term: ' + label + ': ' +
'<' + term + '> -> <' + expectedBooleanQuery + '>', function() {
Expand Down Expand Up @@ -76,6 +94,10 @@ suite('QueryTranslator', function() {
'"star \\" wars" luke',
'"star \\" wars"'.length,
"'\"star \\\" wars\"'");
testPhraseTermError("not start with <\">",
'star wars"',
'|s|tar wars"',
"phrase must start with <\">");

testTerm("a term",
" star wars",
Expand Down

0 comments on commit 2dd4492

Please sign in to comment.