From ef1f56c42c41a2dd4eefa319bd52df7290f68974 Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Fri, 16 Jan 2015 11:03:07 +0100 Subject: [PATCH] added tests --- .../modules/org/arangodb/aql/functions.js | 20 +++++++++++-- js/common/tests/shell-aqlfunctions.js | 30 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/js/common/modules/org/arangodb/aql/functions.js b/js/common/modules/org/arangodb/aql/functions.js index 6fd0a7e40c..60df07eed8 100644 --- a/js/common/modules/org/arangodb/aql/functions.js +++ b/js/common/modules/org/arangodb/aql/functions.js @@ -47,6 +47,8 @@ var ArangoError = arangodb.ArangoError; //////////////////////////////////////////////////////////////////////////////// var getStorage = function () { + "use strict"; + var functions = db._collection("_aqlfunctions"); if (functions === null) { @@ -65,6 +67,8 @@ var getStorage = function () { //////////////////////////////////////////////////////////////////////////////// var getFiltered = function (group) { + "use strict"; + var result = [ ]; if (group !== null && group !== undefined && group.length > 0) { @@ -92,6 +96,8 @@ var getFiltered = function (group) { //////////////////////////////////////////////////////////////////////////////// var validateName = function (name) { + "use strict"; + if (typeof name !== 'string' || ! name.match(/^[a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)+$/) || name.substr(0, 1) === "_") { @@ -108,12 +114,14 @@ var validateName = function (name) { //////////////////////////////////////////////////////////////////////////////// var stringifyFunction = function (code, name) { + "use strict"; + if (typeof code === 'function') { - code = String(code); + code = String(code) + "\n"; } if (typeof code === 'string') { - code = "(" + code + ")"; + code = "(\n" + code + "\n)"; if (! internal.parse) { // no parsing possible. assume always valid @@ -164,6 +172,8 @@ var stringifyFunction = function (code, name) { //////////////////////////////////////////////////////////////////////////////// var unregisterFunction = function (name) { + "use strict"; + var func = null; validateName(name); @@ -210,6 +220,8 @@ var unregisterFunction = function (name) { //////////////////////////////////////////////////////////////////////////////// var unregisterFunctionsGroup = function (group) { + "use strict"; + if (group.length === 0) { var err = new ArangoError(); err.errorNum = arangodb.errors.ERROR_BAD_PARAMETER.code; @@ -364,10 +376,12 @@ var registerFunction = function (name, code, isDeterministic) { //////////////////////////////////////////////////////////////////////////////// var toArrayFunctions = function (group) { + "use strict"; + var result = [ ]; getFiltered(group).forEach(function (f) { - result.push({ name: f.name, code: f.code.substr(1, f.code.length - 2) }); + result.push({ name: f.name, code: f.code.substr(1, f.code.length - 2).trim() }); }); return result; diff --git a/js/common/tests/shell-aqlfunctions.js b/js/common/tests/shell-aqlfunctions.js index c4804e0f65..95603f8e9a 100644 --- a/js/common/tests/shell-aqlfunctions.js +++ b/js/common/tests/shell-aqlfunctions.js @@ -493,6 +493,36 @@ function AqlFunctionsSuite () { assertEqual([ [ true, false, null, 1, 2, -4, [ 5.5, { a: 1, "b": "def" } ] ] ], actual); }, +//////////////////////////////////////////////////////////////////////////////// +/// @brief register a function and run a query +//////////////////////////////////////////////////////////////////////////////// + + testQueryStringWithComments : function () { + unregister("UnitTests::tryme"); + aqlfunctions.register("UnitTests::tryme", function (what) { return true; }, true); + + var actual = db._createStatement({ query: "// foo\n /* bar\nbaz\n*/ RETURN UnitTests::tryme(4) // some comment" }).execute().toArray(); + assertEqual([ true ], actual); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief register a function and run a query +//////////////////////////////////////////////////////////////////////////////// + + testQueryFuncWithComments : function () { + unregister("UnitTests::tryme"); + aqlfunctions.register("UnitTests::tryme", function (what) { + // foo + /* bar + baz + */ + return [ true, false, null, 1, 2, -4, [ 5.5, { a: 1, "b": "def" } ] ] // some comment + }, true); + + var actual = db._createStatement({ query: "RETURN UnitTests::tryme()" }).execute().toArray(); + assertEqual([ [ true, false, null, 1, 2, -4, [ 5.5, { a: 1, "b": "def" } ] ] ], actual); + }, + //////////////////////////////////////////////////////////////////////////////// /// @brief test for specific problem with docs returned by user functions ////////////////////////////////////////////////////////////////////////////////