Permalink
Browse files

added some testing

  • Loading branch information...
1 parent c315c64 commit 1c27e36623d9f47ce29897325bfe3979068d7225 @kalisjoshua committed Sep 12, 2012
Showing with 109 additions and 7 deletions.
  1. +2 −3 README.md
  2. +3 −1 package.json
  3. +27 −3 sherpa.js
  4. +77 −0 test/expresso.js
View
5 README.md
@@ -8,16 +8,15 @@ I have started using this in conjunction with Grunt.js so I don't have to grunt
## Example (don't forget chmod +x <filename>)
- #!/usr/bin/env node
/*jshint laxcomma:true strict:false*/
/*globals require*/
var exec = require('child_process').exec
, sherpa = require("sherpa")
function notify (filename, command) {
- puts(null, "\nChange made to: " + filename);
+ sherpa.puts(null, "\nChange made to: " + filename);
- command && exec(command, puts);
+ command && exec(command, sherpa.puts);
}
sherpa("src", 300, function (filename, curr, prev) {
View
4 package.json
@@ -8,7 +8,9 @@
"url": "https://github.com/kalisjoshua/sherpa.git"
},
"main": "sherpa",
- "dependencies": {},
+ "dependencies": {
+ "expresso": ">=0.9.2"
+ },
"devDependencies": {},
"optionalDependencies": {},
"engines": {
View
30 sherpa.js
@@ -1,7 +1,19 @@
/*jshint laxcomma:true strict:false*/
/*globals console module require*/
var fs = require("fs")
- , path = require("path");
+ , path = require("path")
+ , util = require("util");
+
+function valid_args (args) {
+ return args.length > 1 &&
+ args[0] != null && args[1] != null &&
+ typeof [].pop.call(args) === "function";
+}
+
+function valid_root (val) {
+ return val !== "" &&
+ {}.toString.call(val) === "[object String]";
+}
function walk (start, callback) {
fs.lstat(start, function (err, stat) {
@@ -31,11 +43,11 @@ function walk (start, callback) {
}
module.exports = function (root, interval, callback) {
- if (arguments.length < 2) {
+ if (!valid_root(root) || !valid_args(arguments)) {
throw("Invalid arguments");
}
- if (typeof callback === "undefined" && typeof interval === "function") {
+ if (!callback && !!interval) {
callback = interval;
interval = undefined;
}
@@ -55,3 +67,15 @@ module.exports = function (root, interval, callback) {
});
};
+
+module.exports.puts = function puts(error, stdout, stderr) { util.puts(stdout); };
+
+module.exports.valid = {
+ args: function () {
+ return valid_args.apply(null, [].slice.call(arguments, 0));
+ }
+
+ ,root: function (val) {
+ return valid_root(val);
+ }
+};
View
77 test/expresso.js
@@ -0,0 +1,77 @@
+/*jshint strict:false*/
+/*globals module require*/
+var assert = require("assert")
+ , sherpa = require("../sherpa");
+
+/*
+ assert(value, message)
+ assert.deepEqual(actual, expected, [message])
+ assert.doesNotThrow(block, [error], [message])
+ assert.equal(actual, expected, [message])
+ assert.fail(actual, expected, message, operator)
+ assert.ifError(value)
+ assert.notDeepEqual(actual, expected, [message])
+ assert.notEqual(actual, expected, [message])
+ assert.notStrictEqual(actual, expected, [message])
+ assert.ok(value, [message])
+ assert.strictEqual(actual, expected, [message])
+ assert.throws(block, [error], [message])
+*/
+
+var invalids = [
+ null
+ , 0
+ , 1
+ , {}
+ , []
+ , /abc/
+ ];
+
+module.exports = {
+ "sherpa.valid.args() should be thorough": function () {
+
+ assert(true !== sherpa.valid.args([]), "not enough arguments passed in");
+ assert(false === sherpa.valid.args([]), "not enough arguments passed in");
+
+ assert(sherpa.valid.args([".", function () {}]), "simplest minimal valid arguments");
+ assert(sherpa.valid.args([".", 1, function () {}]), "simplest maximal valid arguments");
+
+ assert(!sherpa.valid.args([".", ""]), "last argument needs to be a callback function");
+
+ invalids
+ // add in a unction as an invalid value for the first argument
+ .concat([""])
+ .forEach(function (val) {
+ assert(!sherpa.valid.args(".", val), "invalid values for callback should produce an error");
+ });
+ }
+
+ ,"sherpa.valid.root() should validate only strings": function () {
+ invalids
+ // add in a unction as an invalid value for the first argument
+ .concat([function () {}])
+ .forEach(function (val) {
+ assert(!sherpa.valid.root(val), "invalid values for root should produce an error");
+ });
+ }
+
+ ,"sherpa called with no args fails": function () {
+ assert.throws(sherpa, "no arguments is going to be a problem");
+
+ assert.throws(function () { sherpa(); }, "no arguments is going to be a problem");
+ }
+
+ ,"sherpa called with only valid first arg fails": function () {
+ var err_msg = "sherpa must be passed a valid callback function";
+
+ assert.throws(function () { sherpa("."); }, err_msg);
+
+ assert.throws(function () { sherpa(".", null); }, err_msg);
+
+ assert.throws(function () { sherpa(".", undefined); }, err_msg);
+ }
+
+ ,"sherpa must have a path to watch": function () {
+ assert.throws(function () { sherpa("", function () {}); }, "root must not be empty");
+ }
+};

0 comments on commit 1c27e36

Please sign in to comment.