[Groonga-commit] groonga/gcs [master] bq: throw exception for missing group start "("

Back to archive index

null+****@clear***** null+****@clear*****
2012年 8月 6日 (月) 13:04:25 JST


Kouhei Sutou	2012-08-06 13:04:25 +0900 (Mon, 06 Aug 2012)

  New Revision: ec2cebb0a921fb4ae793fc2ac13f4e87b209d1bf
  https://github.com/groonga/gcs/commit/ec2cebb0a921fb4ae793fc2ac13f4e87b209d1bf

  Log:
    bq: throw exception for missing group start "("

  Modified files:
    lib/bq-translator.js
    test/bq-translator.test.js

  Modified: lib/bq-translator.js (+7 -3)
===================================================================
--- lib/bq-translator.js    2012-08-06 11:52:22 +0900 (d39f765)
+++ lib/bq-translator.js    2012-08-06 13:04:25 +0900 (0319443)
@@ -55,8 +55,7 @@ BooleanQueryTranslator.prototype = {
   },
   translateGroup: function() {
     if (this.query[this.offset] != "(") {
-      // TODO: report error
-      return "";
+      this.throwTranslateError("not started with <(>");
     }
 
     this.offset++;
@@ -261,7 +260,12 @@ BooleanQueryTranslator.prototype = {
     return "";
   },
   translateExpressionValueStringKeyword: function(field, value) {
-    return field + " @ " + "\"" + value + "\"";
+    var operator = "@";
+    if (value[value.length - 1] == "*") {
+      operator = "@^";
+      value = value.substring(0, value.length - 1);
+    }
+    return field + " " + operator + " " + "\"" + value + "\"";
   },
   translateExpressionValuePhrase: function(field) {
     if (!(this.query[this.offset] == "'" &&

  Modified: test/bq-translator.test.js (+26 -0)
===================================================================
--- test/bq-translator.test.js    2012-08-06 11:52:22 +0900 (24dbe2c)
+++ test/bq-translator.test.js    2012-08-06 13:04:25 +0900 (139712e)
@@ -49,6 +49,23 @@ function testGroup(label, group, expectedOffset, expectedScriptGrnExpr) {
   });
 }
 
+function testGroupError(label, group, context, detail) {
+  test('error: group: ' + label + ': ' + '<' + group + '>', function() {
+    var translator = new BooleanQueryTranslator(group);
+    translator.defaultField = "field";
+    var actualError;
+    assert.throw(function() {
+      try {
+        translator.translateGroup();
+      } catch (error) {
+        actualError = error;
+        throw error;
+      }
+    });
+    assert.equal(actualError.message, "<" + context + ">" + ": " + detail);
+  });
+}
+
 function testExpression(label, expression,
                         expectedOffset, expectedScriptGrnExpr) {
   test('expression: ' + label + ': ' +
@@ -105,6 +122,11 @@ suite('BoolanQueryTranslator', function() {
             "(and (or f1:'k1' f2:'k2') f3:'k3')".length,
             "((f1 @ \"k1\" || f2 @ \"k2\") && f3 @ \"k3\")");
 
+  testGroupError("missing open parentheis",
+                 "and f1:'k1' f2:'k2')",
+                 "|a|nd f1:'k1' f2:'k2')",
+                 "not started with <(>");
+
   testExpression("value only: stirng: and: space",
                  "'keyword1 keyword2' 'other keyword'",
                  "'keyword1 keyword2'".length,
@@ -117,6 +139,10 @@ suite('BoolanQueryTranslator', function() {
                  "'keyword1|keyword2' 'other keyword'",
                  "'keyword1|keyword2'".length,
                  "field @ \"keyword1\" || field @ \"keyword2\"");
+  testExpression("value only: stirng: prefix search",
+                 "'keyword*' 'other keyword'",
+                 "'keyword*'".length,
+                 "field @^ \"keyword\"");
   testExpression("value only: stirng: phrase",
                  "'\"keyword1 keyword2\"' 'other keyword'",
                  "'\"keyword1 keyword2\"'".length,
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
다운로드 



Groonga-commit メーリングリストの案内
Back to archive index