-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77a2ea3
commit 7e41a8c
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/*jslint vars: true, white: true, plusplus: true, todo: true */ | ||
"use strict"; | ||
|
||
var actionRE = /(?:^|\s)([\-+])([\-a-z]+)(?:{([^}]+)})?(?=\s|$)/g; | ||
var emptyActionsRE = /^\s*\{\s*\}\s*$/; | ||
|
||
function parseActions(actionsClause) { | ||
var actions = []; | ||
var remainder = actionsClause.replace(actionRE, function() { | ||
actions.push(Array.prototype.slice.call(arguments, 1)); | ||
return ""; | ||
}); | ||
if (!emptyActionsRE.test(remainder)) { | ||
throw new Error("Malformed actions clause:\n\n" + actionsClause); | ||
} | ||
return actions; | ||
} | ||
|
||
exports.parseActions = parseActions; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var aParser = require("./actions"); | ||
|
||
exports["test parse valid"] = function(assert) { | ||
var actions = aParser.parseActions([ | ||
"{", | ||
"+block{nothing}", | ||
"-handle-as-image", | ||
"}" | ||
].join("\n")); | ||
assert.ok(actions, "Actions were parsed"); | ||
assert.strictEqual(actions.length, 2, "Correct number of actions"); | ||
}; | ||
|
||
exports["test parse invalid"] = function(assert) { | ||
assert.throws(function() { | ||
aParser.parseActions("{ foo +block{nothing} -handle-as-image }"); | ||
}, "Extra garbage inside braces"); | ||
assert.throws(function() { | ||
aParser.parseActions("foo{ +block{nothing} -handle-as-image }"); | ||
}, "Extra garbage outside braces"); | ||
assert.throws(function() { | ||
aParser.parseActions("{ +block{nothing}} -handle-as-image }"); | ||
}, "Extra braces"); | ||
assert.throws(function() { | ||
aParser.parseActions("{} +block{nothing} -handle-as-image {}"); | ||
}, "More extra braces"); | ||
}; | ||
|
||
require("sdk/test").run(exports); | ||
|