Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[test] added test script

  • Loading branch information...
commit c1f59bc9630f935848df96ae3bc9f8360ff4b985 1 parent ab62f9d
@dscape authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules/
View
3  samples/declaring_in_a_library.js → samples/declaring_in_a_library.test.js
@@ -1,4 +1,5 @@
+/* [ 2, 4, 6 ] */
var map = require('./hof').map;
map(function duplicate(x) { return x*2; }, [1,2,3], [],
- function (x) { console.log(x); });
+ function (x) { console.error(x); });
View
0  samples/errors.js → samples/error.js
File renamed without changes
View
23 samples/hof.js
@@ -1,20 +1,21 @@
var map = require('../pattern')
, mapa = require('../pattern')
- , _, f, ac
+ , zip_with = require('../pattern')
+ , _, f, ac, l, l1, l2, cb
;
// high order functions
-map(f, [], ac, _,
+map(f, [], ac, cb,
function map_done(f, l, ac, cb) { return cb(ac); });
-map(f, _, ac, _,
+map(f, l, ac, cb,
function map_catch_all(f, l, ac, cb) {
ac.push(f(l.shift())); // head
map(f, l, ac, cb); // l is now tail
});
-mapa(f, [], ac, _,
+mapa(f, [], ac, cb,
function map_done(f, l, ac, cb) { return cb(ac); });
-mapa(f, _, ac, _,
+mapa(f, l, ac, cb,
function map_catch_all(f, l, ac, cb) {
f(l.shift(), function(x) {
ac.push(x);
@@ -22,4 +23,14 @@ mapa(f, _, ac, _,
});
});
-module.exports = { map: map, map_async: mapa };
+// filter
+// foldl
+
+zip_with(f, [], l2, ac, cb, function emptyl1(f, l1, l2, ac, cb) { cb(ac); });
+zip_with(f, l1, [], ac, cb, function emptyl2(f, l1, l2, ac, cb) { cb(ac); });
+zip_with(f, l1, l2, ac, cb, function all(f, l1, l2, ac, cb) {
+ ac.push(f(l1.shift, l2.shift));
+ zip_with(f, l1, l2, ac, cb);
+});
+
+module.exports = { map: map, map_async: mapa, zip_with: zip_with };
View
12 samples/map.js
@@ -1,12 +0,0 @@
-var map = require('../pattern')
- , _, f, ac
- ;
-
-map(f, [], ac, function done(_, _, ac) { return console.log(ac); });
-map(f, _, ac, function all(f, l, ac) {
- ac.push(f(l.shift())); // head
- map(f, l, ac); // l is now tail
-});
-
-map(function duplicate(x) { console.log(x*2); return x*2; }, [1,2,3], []);
-map(function plusone(x) { return x+1; }, [1,2,3], []);
View
12 samples/map.test.js
@@ -0,0 +1,12 @@
+/* [ 2, 3, 4 ] */
+var map = require('../pattern')
+ , _, f, ac
+ ;
+
+map(f, [], ac, function done(_, _, ac) { return console.error(ac); });
+map(f, _, ac, function all(f, l, ac) {
+ ac.push(f(l.shift())); // head
+ map(f, l, ac); // l is now tail
+});
+
+map(function plusone(x) { return x+1; }, [1,2,3], []);
View
3  samples/map_async.js → samples/map_async.test.js
@@ -1,7 +1,8 @@
+/* [ 2, 4, 6 ] */
var map_async = require('./hof').map_async;
function duplicate(x, cb) {
setTimeout(function() { cb(x*2); }, Math.ceil(Math.random() * 100));
}
-map_async(duplicate, [1,2,3], [], function(ac) { console.log(ac); });
+map_async(duplicate, [1,2,3], [], function(ac) { console.error(ac); });
View
5 samples/nodetuts.js → samples/nodetuts.test.js
@@ -1,9 +1,10 @@
+/* done */
var insert_all = require('../pattern'), _;
// pretending we are doing an async call
function insert_element(data, callback) {
setTimeout(function() { callback(data); },
- Math.ceil(Math.random() * 1000));
+ Math.ceil(Math.random() * 100));
}
insert_all([], _, function stop(l,cb) { cb(); });
@@ -15,4 +16,4 @@ insert_all(_, _, function catchall(l, cb) {
});
insert_all([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
- function done() { console.log('done'); });
+ function done() { console.error('done'); });
View
5 samples/readme.js → samples/readme.test.js
@@ -1,10 +1,11 @@
+/* CAT */
var insert_all = require('../pattern'), _;
function insert_element(data, callback) {
- setTimeout(function() { callback(data); }, Math.ceil(Math.random() * 1000));
+ setTimeout(function() { callback(data); }, Math.ceil(Math.random() * 10));
}
-insert_all([], function () { console.log('done'); });
+insert_all([], function () { console.error('CAT'); });
insert_all(_, function (l) {
insert_element(l.shift(), function (elem) {
console.log('', elem);
View
6 samples/zip_with.js
@@ -0,0 +1,6 @@
+var zip = require('./hof').zip_with;
+
+zip(function concat(a,b) { return a + ':' + b; },
+ [1,3,5], [2,4,6,8], [], function (ac) {
+ console.log(ac);
+ });
View
40 test/index.js
@@ -0,0 +1,40 @@
+var fs = require('fs')
+ , assert = require('assert')
+ , path = __dirname + '/../samples/'
+ , files = fs.readdirSync(path)
+ , spawn = require('child_process').spawn
+ , colors
+ , return_code = 0
+ , i = 0
+ ;
+
+try { colors = require('colors'); } catch (e) {}
+function c(msg,color) { return colors ? msg[color] : msg; }
+
+var tests = files.filter(function (f) { return f.indexOf('.test.') !== -1; });
+var expected = tests.length;
+
+tests.forEach(
+ function (f) {
+ var contents = fs.readFileSync(path + f, 'utf-8')
+ , output = /\/\*\s*(.*?)\s*\*\//.exec(contents)[1]
+ , node = spawn('node', [path + f])
+ , buffer = ''
+ ;
+ node.stderr.on('data', function(data) { buffer += data; });
+ node.on('exit', function() {
+ i++;
+ buffer = buffer.replace(/^\s+|\s+$/g,"");
+ var ok = buffer === output;
+ console.log(c(ok ? '' : '', ok ? 'green' : 'red'),f);
+ if(!ok) {
+ return_code=1;
+ console.log(c('', 'cyan'), buffer);
+ console.log(c(' δ', 'magenta'), output);
+ }
+ if(i===expected) {
+ process.exit(return_code);
+ }
+ });
+ }
+);
Please sign in to comment.
Something went wrong with that request. Please try again.