You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.4 KiB

12 years ago
var fs = require('fs');
var util = require('util');
var assert = require('assert');
var support = {
random: {
10 years ago
string: function(str_len) {
str_len = str_len || 8;
var chars = "abcdefghiklmnopqrstuvwxyz";
var random_str = '';
for (var i = 0; i < str_len; i++) {
var rnum = Math.floor(Math.random() * chars.length);
random_str += chars.substring(rnum, rnum + 1);
}
return random_str;
},
10 years ago
number: function(max) {
max = max || 1000;
return Math.floor((Math.random() * max));
}
},
10 years ago
check_err: function(err) {
if (err) {
var msg;
if (err instanceof Error) {
msg = err;
} else if (err.msg) {
msg = err.msg;
} else {
msg = util.inspect(err);
}
var error = new Error(msg);
throw error;
}
},
10 years ago
assert_between: function(actual, lower, upper) {
assert.ok(actual >= lower, "Expected " + actual + " to be >= " + lower);
assert.ok(actual <= upper, "Expected " + actual + " to be <= " + upper);
},
10 years ago
assert_within: function(actual, expected, delta) {
var lower = expected - delta;
var upper = expected + delta;
this.assert_between(actual, lower, upper);
12 years ago
},
10 years ago
walk_dir: function(dir, validation_function, cb) {
12 years ago
if (arguments.length === 2) {
cb = validation_function;
validation_function = null;
}
var results = [];
10 years ago
fs.readdir(dir, function(err, list) {
12 years ago
if (err) { return cb(err); }
var pending = list.length;
if (!pending) { return cb(null, results); }
10 years ago
list.forEach(function(file) {
12 years ago
file = dir + '/' + file;
10 years ago
fs.stat(file, function(err, stat) {
12 years ago
if (stat && stat.isDirectory()) {
10 years ago
support.walk_dir(file, validation_function, function(err, res) {
12 years ago
results = results.concat(res);
if (!--pending) { cb(null, results); }
});
} else {
if (typeof validation_function === 'function') {
if (validation_function(file)) {
results.push(file);
}
} else {
results.push(file);
}
if (!--pending) { cb(null, results); }
}
});
});
});
},
10 years ago
test_set_get_del: function(cache, cb) {
12 years ago
var key = 'TEST' + support.random.string();
var val = support.random.string();
var ttl;
12 years ago
10 years ago
cache.set(key, val, ttl, function(err) {
12 years ago
if (err) { return cb(err); }
10 years ago
cache.get(key, function(err, result) {
12 years ago
if (err) { return cb(err); }
assert.equal(result, val);
10 years ago
cache.del(key, function(err) {
12 years ago
if (err) { return cb(err); }
10 years ago
cache.get(key, function(err, result) {
12 years ago
if (err) { return cb(err); }
assert.ok(!result);
cb();
});
});
});
});
}
};
module.exports = support;