Permalink
Browse files

Fixed a bug that caused an infinite loop if

multiple actions were added to a parser.

Added a tests/args/actions to test if parser
actions were executed, and in the correct order.

Updated tests/args to run tests/args/actions.
  • Loading branch information...
1 parent 8c8137b commit adbb52b6f9102f0bbe2a621de81d5930cb2b9b91 @abhinav abhinav committed Aug 7, 2010
Showing with 38 additions and 5 deletions.
  1. +3 −3 packages/narwhal-lib/lib/narwhal/args.js
  2. +3 −2 tests/args.js
  3. +32 −0 tests/args/actions.js
@@ -112,12 +112,12 @@ exports.Parser.prototype.act = function (args, options) {
exports.Parser.prototype.action = function (action) {
if (this._action) {
- action = (function (previous) {
+ action = (function (previous, next) {
return function () {
previous.apply(this, arguments);
- action.apply(this, arguments);
+ next.apply(this, arguments);
};
- })(action);
+ })(this._action, action);
}
this._action = action;
return this;
View
@@ -1,6 +1,6 @@
-var util = require('util');
-var args = require('args');
+var util = require('narwhal/util');
+var args = require('narwhal/args');
var assert = require('test/assert');
// subclass Parser to prevent it from assertively
@@ -26,6 +26,7 @@ exports.testOptions = require("./args/options");
exports.testDomain = require("./args/domain");
exports.testShifting = require("./args/shifting");
exports.testChoices = require("./args/choices");
+exports.testActions = require("./args/actions");
if (require.main == module.id)
require("os").exit(require("test/runner").run(exports));
View
@@ -0,0 +1,32 @@
+
+var assert = require("assert");
+var test = require("../args");
+
+exports['test parser with single action'] = function () {
+ var parser = new test.Parser();
+ var wasExecuted = false;
+
+ parser.action(function () { wasExecuted = true });
+ parser.act(['c'], {});
+
+ assert.ok(wasExecuted);
+};
+
+exports['test parser with multiple actions'] = function () {
+ var parser = new test.Parser();
+ var order = [];
+
+ // add actions which push 1 - 3 to order.
+ parser.action(function () { order.push(1) });
+ parser.action(function () { order.push(2) });
+ parser.action(function () { order.push(3) });
+
+ // when parsing, the numbers should be pushed in the correct order
+
+ parser.act(['c'], {});
+ assert.deepEqual(order, [1,2,3]);
+};
+
+if (require.main == module.id)
+ require("os").exit(require("test").run(exports));
+

0 comments on commit adbb52b

Please sign in to comment.