Permalink
Browse files

Added a parameter parser to support changes in node 0.3.0.

  • Loading branch information...
cskr committed Oct 31, 2010
1 parent e32c4ac commit 00da2b381eccd10da3e2fd9709abf8555e7e663d
View
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2010 Chandra Sekar S
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+function ParamParser() {
+ this.params = {};
+}
+
+ParamParser.prototype.addParam = function(name, value) {
+ if(name.indexOf('.') < 0) {
+ includeValue(this.params, name, value);
+ } else {
+ includeObject(this.params, name, value);
+ }
+};
+
+ParamParser.prototype.getParams = function() {
+ return this.params;
+};
+
+exports.ParamParser = ParamParser;
+
+function includeObject(parent, name, value) {
+ var objName = name.substring(0, name.indexOf('.'));
+ if(parent[objName] === undefined) {
+ parent[objName] = {};
+ }
+ var fieldName = name.substring(name.indexOf('.') + 1);
+ if(fieldName.indexOf('.') < 0) {
+ includeValue(parent[objName], fieldName, value);
+ } else {
+ includeObject(parent[objName], fieldName, value);
+ }
+}
+
+function includeValue(paramObj, name, value) {
+ if(paramObj[name] === undefined) {
+ paramObj[name] = value;
+ } else {
+ if(Array.isArray(paramObj[name])) {
+ paramObj[name].push(value);
+ } else {
+ paramObj[name] = [paramObj[name], value];
+ }
+ }
+}
File renamed without changes.
File renamed without changes.
@@ -1,7 +1,7 @@
-var RequestContext = require('../grasshopper/lib/renderer').RequestContext,
+var RequestContext = require('../../grasshopper/lib/renderer').RequestContext,
assert = require('assert'),
- mocks = require('./mocks'),
- base64 = require('../grasshopper/lib/base64');
+ mocks = require('../common/mocks'),
+ base64 = require('../../grasshopper/lib/base64');
var suite = {name: 'Authentication Tests'};
exports.suite = suite;
@@ -44,4 +44,4 @@ suite.tests = {
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,7 +1,7 @@
-var dispatcher = require('../grasshopper/lib/dispatcher'),
+var dispatcher = require('../../grasshopper/lib/dispatcher'),
http = require('http'),
assert = require('assert'),
- mocks = require('./mocks');
+ mocks = require('../common/mocks');
var suite = {name: 'Dispatcher Tests'};
exports.suite = suite;
@@ -95,4 +95,4 @@ function invoke(method, path, headers, body, route, controller) {
}
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,5 +1,5 @@
var assert = require('assert'),
- ghp = require('../grasshopper/lib/ghp');
+ ghp = require('../../grasshopper/lib/ghp');
var suite = {name: 'GHP Tests'};
exports.suite = suite;
@@ -19,60 +19,60 @@ MockResponse.prototype.end = function() {
suite.tests = {
'Fill simple template.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/simple.txt', response, {name: 'Chandru'}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/simple.txt', response, {name: 'Chandru'}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, 'Hello, Chandru!\n');
assert.ok(response.ended);
next();
},
'Fill template with include.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/simple_with_include.txt', response, {}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/simple_with_include.txt', response, {}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, 'Hello, Chandru\n!\n');
assert.ok(response.ended);
next();
},
'Fill template with include and updation function.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/include_with_updater.txt', response, {}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/include_with_updater.txt', response, {}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, 'Hello, Chandru!\n\n');
assert.ok(response.ended);
next();
},
'Template with newline in code.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/multiline_with_newline.txt', response, {items: ['A', 'B', 'C']}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/multiline_with_newline.txt', response, {items: ['A', 'B', 'C']}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, '<li>\nA</li>\n<li>B</li>\n<li>C\n</li>\n');
assert.ok(response.ended);
next();
},
'Template with escaped HTML.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/escaped_html.txt', response, {}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/escaped_html.txt', response, {}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, '&lt;b&gt;ABC&lt;/b&gt;\n');
assert.ok(response.ended);
next();
},
'Multi-line with single quote.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/with_quote.txt', response, {}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/with_quote.txt', response, {}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, 'Quoted: \'line-1\'\n\'line-2\'\n');
assert.ok(response.ended);
next();
},
'Template with undefinde value.': function(next) {
var response = new MockResponse();
- ghp.fill('./fixtures/ghp/template_with_undefined.txt', response, {}, 'utf8', './fixtures/ghp', 'txt');
+ ghp.fill('../fixtures/ghp/template_with_undefined.txt', response, {}, 'utf8', '../fixtures/ghp', 'txt');
assert.equal(response.out, '\n');
assert.ok(response.ended);
next();
}
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,5 +1,5 @@
-var gzip = require('../grasshopper/lib/gzip'),
- mocks = require('./mocks'),
+var gzip = require('../../grasshopper/lib/gzip'),
+ mocks = require('../common/mocks'),
assert = require('assert');
var suite = {name: 'Gzip Tests'};
@@ -37,4 +37,4 @@ suite.tests = {
}
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,5 +1,5 @@
-var gh = require('../grasshopper'),
- helpers = require('../grasshopper/lib/helpers'),
+var gh = require('../../grasshopper'),
+ helpers = require('../../grasshopper/lib/helpers'),
assert = require('assert');
var suite = {name: 'Helpers Tests'};
@@ -61,4 +61,4 @@ suite.tests = {
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,4 +1,4 @@
-var i18n = require('../grasshopper/lib/i18n'),
+var i18n = require('../../grasshopper/lib/i18n'),
assert = require('assert');
var suite = {name: 'i18n Tests'};
@@ -52,4 +52,4 @@ suite.tests = {
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
@@ -1,4 +1,4 @@
-var gh = require('../grasshopper'),
+var gh = require('../../grasshopper'),
assert = require('assert');
var suite = {name: 'Model Tests'};
@@ -94,4 +94,4 @@ suite.tests = {
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
View
@@ -0,0 +1,66 @@
+var assert = require('assert'),
+ ParamParser = require('../../grasshopper/lib/params').ParamParser;
+
+var suite = {name: 'Params Tests'};
+exports.suite = suite;
+
+suite.tests = {
+ 'Simple parameters.': function(next) {
+ var parser = new ParamParser();
+ parser.addParam('name', 'Product_A');
+ parser.addParam('price', '200');
+ assert.deepEqual(parser.getParams(), {
+ name: 'Product_A',
+ price: '200'
+ });
+ next();
+ },
+
+ 'Parameters with repeating keys.': function(next) {
+ var parser = new ParamParser();
+ parser.addParam('names', 'Product_A');
+ parser.addParam('names', 'Product_B');
+ assert.deepEqual(parser.getParams(), {
+ names: ['Product_A', 'Product_B']
+ });
+ next();
+ },
+
+ 'Object Parameters.': function(next) {
+ var parser = new ParamParser();
+ parser.addParam('product.names', 'Product_A');
+ parser.addParam('product.names', 'Product_B');
+ parser.addParam('product.price', '200');
+ assert.deepEqual(parser.getParams(), {
+ product: {
+ names: ['Product_A', 'Product_B'],
+ price: '200'
+ }
+ });
+ next();
+ },
+
+ 'Object Parameters with nested objects.': function(next) {
+ var parser = new ParamParser();
+ parser.addParam('product.names', 'Product_A');
+ parser.addParam('product.names', 'Product_B');
+ parser.addParam('product.price', '200');
+ parser.addParam('product.category.names', 'Category_A');
+ parser.addParam('product.category.names', 'Category_B');
+ parser.addParam('product.category.stock', '1000');
+ assert.deepEqual(parser.getParams(), {
+ product: {
+ names: ['Product_A', 'Product_B'],
+ price: '200',
+ category: {
+ names: ['Category_A', 'Category_B'],
+ stock: '1000'
+ }
+ }
+ });
+ next();
+ }
+};
+
+if(process.argv[1] == __filename)
+ require('../common/ghunit').test(suite);
@@ -1,4 +1,4 @@
-var routes = require('../grasshopper/lib/routes'),
+var routes = require('../../grasshopper/lib/routes'),
assert = require('assert');
var suite = {name: 'Routing Tests'};
@@ -63,4 +63,4 @@ suite.tests = {
};
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
View
@@ -0,0 +1,14 @@
+var suites = [
+ require('./auth-test').suite,
+ require('./dispatcher-test').suite,
+ require('./ghp-test').suite,
+ require('./gzip-test').suite,
+ require('./helpers-test').suite,
+ require('./i18n-test').suite,
+ require('./model-test').suite,
+ require('./routes-test').suite,
+ require('./wrapper-test').suite,
+ require('./params-test').suite
+];
+
+require('../common/ghunit').test.apply(null, suites);
@@ -1,5 +1,5 @@
-var wrapper = require('../grasshopper/lib/wrapper'),
- mocks = require('./mocks'),
+var wrapper = require('../../grasshopper/lib/wrapper'),
+ mocks = require('../common/mocks'),
assert = require('assert');
var suite = {name: 'Wrapper Tests'};
@@ -74,4 +74,4 @@ suite.tests = {
}
if(process.argv[1] == __filename)
- require('./ghunit').test(suite);
+ require('../common/ghunit').test(suite);
View
@@ -1,15 +0,0 @@
-var ghunit = require('./ghunit');
-
-var suites = [
- require('./auth-test.js').suite,
- require('./dispatcher-test.js').suite,
- require('./ghp-test.js').suite,
- require('./gzip-test.js').suite,
- require('./helpers-test.js').suite,
- require('./i18n-test.js').suite,
- require('./model-test.js').suite,
- require('./routes-test.js').suite,
- require('./wrapper-test.js').suite
-];
-
-ghunit.test.apply(null, suites);

0 comments on commit 00da2b3

Please sign in to comment.