[Groonga-commit] groonga/gcs [master] BqTranslator: thorw error on garbage query is found after valid query

Back to archive index

null+****@clear***** null+****@clear*****
2012年 7月 27日 (金) 18:52:23 JST


Kouhei Sutou	2012-07-27 18:52:23 +0900 (Fri, 27 Jul 2012)

  New Revision: 3e348c5efb165ec351800b878b6d399a0f3fab40
  https://github.com/groonga/gcs/commit/3e348c5efb165ec351800b878b6d399a0f3fab40

  Log:
    BqTranslator: thorw error on garbage query is found after valid query

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

  Modified: lib/bq-translator.js (+13 -2)
===================================================================
--- lib/bq-translator.js    2012-07-27 18:26:09 +0900 (306656e)
+++ lib/bq-translator.js    2012-07-27 18:52:23 +0900 (835e9ac)
@@ -20,6 +20,17 @@
 function BooleanQueryTranslator() {
 }
 
+function throwTranslateError(query, context, detail) {
+  var message = "";
+  message += "<";
+  message += query.substring(0, context.offset);
+  message += "|" + query[context.offset] + "|";
+  message += query.substring(context.offset + 1);
+  message += ">";
+  message += ": " + detail;
+  throw new Error(message);
+};
+
 BooleanQueryTranslator.prototype = {
   translate: function(query, defaultField) {
     var context = {
@@ -33,8 +44,8 @@ BooleanQueryTranslator.prototype = {
       expression = this.translateExpression(query, context);
     }
     if (context.offset != query.length) {
-      // TODO: report error: garbages are exists after expression
-      return "";
+      throwTranslateError(query, context,
+                          "garbages exist after valid boolean query");
     }
     return expression;
   },

  Modified: test/bq-translator.test.js (+21 -0)
===================================================================
--- test/bq-translator.test.js    2012-07-27 18:26:09 +0900 (e6e16c3)
+++ test/bq-translator.test.js    2012-07-27 18:52:23 +0900 (0f1f44a)
@@ -13,6 +13,22 @@ function testQuery(label, expected, query) {
   });
 }
 
+function testQueryError(label, detail, context, query) {
+  test('error: query: ' + label + ': ' + '<' + query + '>', function() {
+    var translator = new BooleanQueryTranslator();
+    var actualError;
+    assert.throw(function() {
+      try {
+        translator.translate(query, "field");
+      } catch (error) {
+        actualError = error;
+        throw error;
+      }
+    });
+    assert.equal(actualError.message, "<" + context + ">" + ": " + detail);
+  });
+}
+
 function testGroup(label, expectedScriptGrnExpr, expectedOffset, group) {
   test('gorup: ' + label + ': ' +
        '<' + group + '> -> <' + expectedScriptGrnExpr + '>', function() {
@@ -66,6 +82,11 @@ suite('BoolanQueryTranslator', function() {
             '(field @ "keyword1" && field @ "keyword2" && type @ "ModelName")',
             "(and 'keyword1 keyword2' type:'ModelName')");
 
+  testQueryError("garbage",
+                 "garbages exist after valid boolean query",
+                 "(and 'keyword' type:'ModelName')| |garbage1 garbage2",
+                 "(and 'keyword' type:'ModelName') garbage1 garbage2");
+
   testGroup("field",
             "field1 @ \"keyword1\"",
             "(field field1 'keyword1')".length,
-------------- next part --------------
HTML$B$NE:IU%U%!%$%k$rJ]4I$7$^$7$?(B...
Download 



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