Ajout exercices supplémentaires formation JS

This commit is contained in:
2015-04-01 22:05:05 +02:00
committed by Benjamin Bohard
parent ee0ae6d1be
commit b1cbcaefd3
3012 changed files with 363655 additions and 126 deletions

View File

@ -0,0 +1,4 @@
j = 0
j += i for i in [0..5]
exports.name = "mock_coffee_#{j}"

View File

@ -0,0 +1 @@
exports.name = 'mock_module3';

View File

@ -0,0 +1 @@
exports.name = 'mock_module4';

View File

@ -0,0 +1,4 @@
exports['example test'] = function (test) {
test.ok(true);
test.done();
};

View File

@ -0,0 +1 @@
exports.name = 'mock_module1';

View File

@ -0,0 +1 @@
exports.name = 'mock_module2';

View File

@ -0,0 +1,3 @@
function hello_world(arg) {
return "_" + arg + "_";
}

View File

@ -0,0 +1,3 @@
function get_a_variable() {
return typeof a_variable;
}

View File

@ -0,0 +1 @@
var t=t?t+1:1;

View File

@ -0,0 +1,239 @@
/*
* This module is not a plain nodeunit test suite, but instead uses the
* assert module to ensure a basic level of functionality is present,
* allowing the rest of the tests to be written using nodeunit itself.
*
* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var assert = require('assert'), // @REMOVE_LINE_FOR_BROWSER
async = require('../deps/async'), // @REMOVE_LINE_FOR_BROWSER
nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
// NOT A TEST - util function to make testing faster.
// retries the assertion until it passes or the timeout is reached,
// at which point it throws the assertion error
var waitFor = function (fn, timeout, callback, start) {
start = start || new Date().getTime();
callback = callback || function () {};
try {
fn();
callback();
}
catch (e) {
if (e instanceof assert.AssertionError) {
var now = new Date().getTime();
if (now - start >= timeout) {
throw e;
}
else {
async.nextTick(function () {
waitFor(fn, timeout, callback, start);
});
}
}
else {
throw e;
}
}
};
// TESTS:
// Are exported tests actually run? - store completed tests in this variable
// for checking later
var tests_called = {};
// most basic test that should run, the tests_called object is tested
// at the end of this module to ensure the tests were actually run by nodeunit
exports.testCalled = function (test) {
tests_called.testCalled = true;
test.done();
};
// generates test functions for nodeunit assertions
var makeTest = function (method, args_pass, args_fail) {
return function (test) {
var test1_called = false;
var test2_called = false;
// test pass
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_pass);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// test failure
nodeunit.runTest(
'testname',
function (test) {
test[method].apply(test, args_fail);
test.done();
},
{testDone: function (name, assertions) {
assert.equal(assertions.length, 1);
assert.equal(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// ensure tests were run
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
tests_called[method] = true;
}, 500, test.done);
};
};
// ensure basic assertions are working:
exports.testOk = makeTest('ok', [true], [false]);
exports.testEquals = makeTest('equals', [1, 1], [1, 2]);
exports.testSame = makeTest('same',
[{test: 'test'}, {test: 'test'}],
[{test: 'test'}, {monkey: 'penguin'}]
);
// from the assert module:
exports.testEqual = makeTest('equal', [1, 1], [1, 2]);
exports.testNotEqual = makeTest('notEqual', [1, 2], [1, 1]);
exports.testDeepEqual = makeTest('deepEqual',
[{one: 1}, {one: 1}], [{one: 1}, {two: 2}]
);
exports.testNotDeepEqual = makeTest('notDeepEqual',
[{one: 1}, {two: 2}], [{one: 1}, {one: 1}]
);
exports.testStrictEqual = makeTest('strictEqual', [1, 1], [1, true]);
exports.testNotStrictEqual = makeTest('notStrictEqual', [true, 1], [1, 1]);
exports.testThrows = makeTest('throws',
[function () {
throw new Error('test');
}],
[function () {
return;
}]
);
exports.testThrowsWithReGex = makeTest('throws',
[function () {
throw new Error('test');
}, /test/],
[function () {
throw new Error('test');
}, /fail/]
);
exports.testThrowsWithErrorValidation = makeTest('throws',
[function () {
throw new Error('test');
}, function(err) {
return true;
}],
[function () {
throw new Error('test');
}, function(err) {
return false;
}]
);
exports.testDoesNotThrows = makeTest('doesNotThrow',
[function () {
return;
}],
[function () {
throw new Error('test');
}]
);
exports.testIfError = makeTest('ifError', [false], [new Error('test')]);
exports.testExpect = function (test) {
var test1_called = false,
test2_called = false,
test3_called = false;
// correct number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 2);
test.equals(assertions.failures(), 0);
}},
function () {
test1_called = true;
}
);
// no tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 1);
test.equals(assertions.failures(), 1);
}},
function () {
test2_called = true;
}
);
// incorrect number of tests run
nodeunit.runTest(
'testname',
function (test) {
test.expect(2);
test.ok(true);
test.ok(true);
test.ok(true);
test.done();
},
{testDone: function (name, assertions) {
test.equals(assertions.length, 4);
test.equals(assertions.failures(), 1);
}},
function () {
test3_called = true;
}
);
// ensure callbacks fired
waitFor(function () {
assert.ok(test1_called);
assert.ok(test2_called);
assert.ok(test3_called);
tests_called.expect = true;
}, 1000, test.done);
};
// tests are async, so wait for them to be called
waitFor(function () {
assert.ok(tests_called.testCalled);
assert.ok(tests_called.ok);
assert.ok(tests_called.equals);
assert.ok(tests_called.same);
assert.ok(tests_called.expect);
}, 10000);

View File

@ -0,0 +1,76 @@
/*
* Test utils.betterErrors. utils.betterErrors should provide sensible error messages even when the error does not
* contain expected, actual or operator.
*/
var assert = require("../lib/assert");
var should = require("should");
var types = require("../lib/types");
var util = require('util');
var utils = require("../lib/utils");
function betterErrorStringFromError(error) {
var assertion = types.assertion({error: error});
var better = utils.betterErrors(assertion);
return better.error.stack.toString();
}
function performBasicChecks(betterErrorString) {
betterErrorString.should.include("AssertionError");
betterErrorString.should.include("test-bettererrors");
//betterErrorString.should.not.include("undefined");
}
/**
* Control test. Provide an AssertionError that contains actual, expected operator values.
* @param test the test object from nodeunit
*/
exports.testEqual = function (test) {
try {
assert.equal(true, false);
} catch (error) {
var betterErrorString = betterErrorStringFromError(error);
performBasicChecks(betterErrorString);
betterErrorString.should.include("true");
betterErrorString.should.include("false");
betterErrorString.should.include("==");
test.done();
}
};
/**
* Test an AssertionError that does not contain actual, expected or operator values.
* @param test the test object from nodeunit
*/
exports.testAssertThrows = function (test) {
try {
assert.throws(function () {
});
} catch (error) {
var betterErrorString = betterErrorStringFromError(error);
performBasicChecks(betterErrorString);
test.done();
}
};
/**
* Test with an error that is not an AssertionError.
*
* This function name MUST NOT include "AssertionError" because one of the
* tests it performs asserts that the returned error string does not contain
* the "AssertionError" term. If this function name does include that term, it
* will show up in the stack trace and the test will fail!
* @param test the test object from nodeunit
*/
exports.testErrorIsNotAssertion = function (test) {
try {
throw new Error("test error");
} catch (error) {
var betterErrorString = betterErrorStringFromError(error);
betterErrorString.should.not.include("AssertionError");
betterErrorString.should.include("Error");
betterErrorString.should.include("test error");
betterErrorString.should.include("test-bettererrors");
betterErrorString.should.not.include("undefined");
test.done();
}
};

View File

@ -0,0 +1,16 @@
var exec = require('child_process').exec,
path = require('path');
var bin = path.resolve(__dirname, '../bin/nodeunit');
var testfile_fullpath = path.resolve(__dirname, './fixtures/example_test.js');
exports['run test suite using absolute path'] = function (test) {
exec(bin + ' ' + testfile_fullpath, function (err, stdout, stderr) {
if (err) {
return test.done(err);
}
test.ok(/example test/.test(stdout));
test.ok(/1 assertion/.test(stdout));
test.done();
});
};

View File

@ -0,0 +1,114 @@
var nodeunit = require('../lib/nodeunit');
exports.testFailingLog = function (test) {
test.expect(3);
// this is meant to bubble to the top, and will be ignored for the purposes
// of testing:
var ignored_error = new Error('ignore this callback error');
var err_handler = function (err) {
if (err && err.message !== ignored_error.message) {
throw err;
}
};
process.addListener('uncaughtException', err_handler);
// A failing callback should not affect the test outcome
var testfn = function (test) {
test.ok(true, 'test.ok');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(true, 'log called');
throw ignored_error;
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
process.removeListener('uncaughtException', err_handler);
}
}, test.done);
};
exports.testFailingTestDone = function (test) {
test.expect(2);
var ignored_error = new Error('ignore this callback error');
var err_handler = function (err) {
if (err && err.message !== ignored_error.message) {
throw err;
}
};
process.addListener('uncaughtException', err_handler);
// A failing callback should not affect the test outcome
var testfn = function (test) {
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(false, 'log should not be called');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'total');
process.nextTick(function () {
process.removeListener('uncaughtException', err_handler);
test.done();
});
throw ignored_error;
}
}, function () {});
};
exports.testAssertionObj = function (test) {
test.expect(4);
var testfn = function (test) {
test.ok(true, 'ok true');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.ok(assertion.passed() === true, 'assertion.passed');
test.ok(assertion.failed() === false, 'assertion.failed');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};
exports.testLogOptional = function (test) {
test.expect(2);
var testfn = function (test) {
test.ok(true, 'ok true');
test.done();
};
nodeunit.runTest('testname', testfn, {
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};
exports.testExpectWithFailure = function (test) {
test.expect(3);
var testfn = function (test) {
test.expect(1);
test.ok(false, 'test.ok');
test.done();
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.equals(assertion.method, 'ok', 'assertion.method');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 1, 'failures');
test.equals(assertions.length, 1, 'total');
}
}, test.done);
};

View File

@ -0,0 +1,55 @@
var nodeunit = require('../lib/nodeunit');
var httputil = require('../lib/utils').httputil;
exports.testHttpUtilBasics = function (test) {
test.expect(6);
httputil(function (req, resp) {
test.equal(req.method, 'PUT');
test.equal(req.url, '/newpair');
test.equal(req.headers.foo, 'bar');
resp.writeHead(500, {'content-type': 'text/plain'});
resp.end('failed');
}, function (server, client) {
client.fetch('PUT', '/newpair', {'foo': 'bar'}, function (resp) {
test.equal(resp.statusCode, 500);
test.equal(resp.headers['content-type'], 'text/plain');
test.equal(resp.body, 'failed');
server.close();
test.done();
});
});
};
exports.testHttpUtilJsonHandling = function (test) {
test.expect(9);
httputil(function (req, resp) {
test.equal(req.method, 'GET');
test.equal(req.url, '/');
test.equal(req.headers.foo, 'bar');
var testdata = {foo1: 'bar', foo2: 'baz'};
resp.writeHead(200, {'content-type': 'application/json'});
resp.end(JSON.stringify(testdata));
}, function (server, client) {
client.fetch('GET', '/', {'foo': 'bar'}, function (resp) {
test.equal(resp.statusCode, 200);
test.equal(resp.headers['content-type'], 'application/json');
test.ok(resp.bodyAsObject);
test.equal(typeof resp.bodyAsObject, 'object');
test.equal(resp.bodyAsObject.foo1, 'bar');
test.equal(resp.bodyAsObject.foo2, 'baz');
server.close();
test.done();
});
});
};

View File

@ -0,0 +1,231 @@
var assert = require('assert'),
fs = require('fs'),
path = require('path'),
nodeunit = require('../lib/nodeunit');
var setup = function (fn) {
return function (test) {
process.chdir(__dirname);
var env = {
mock_module1: require(__dirname + '/fixtures/mock_module1'),
mock_module2: require(__dirname + '/fixtures/mock_module2'),
mock_module3: require(__dirname + '/fixtures/dir/mock_module3'),
mock_module4: require(__dirname + '/fixtures/dir/mock_module4')
};
fn.call(env, test);
};
};
exports.testRunFiles = setup(function (test) {
test.expect(28);
var runModule_copy = nodeunit.runModule;
var runModule_calls = [];
var modules = [];
var opts = {
moduleStart: function () {
return 'moduleStart';
},
testDone: function () {
return 'testDone';
},
testReady: function () {
return 'testReady';
},
testStart: function () {
return 'testStart';
},
log: function () {
return 'log';
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 4, 'length');
test.ok(typeof assertions.duration === "number");
var called_with = function (name) {
return runModule_calls.some(function (m) {
return m.name === name;
});
};
test.ok(called_with('mock_module1'), 'mock_module1 ran');
test.ok(called_with('mock_module2'), 'mock_module2 ran');
test.ok(called_with('mock_module3'), 'mock_module3 ran');
test.ok(called_with('mock_module4'), 'mock_module4 ran');
test.equals(runModule_calls.length, 4);
nodeunit.runModule = runModule_copy;
test.done();
}
};
nodeunit.runModule = function (name, mod, options, callback) {
test.equals(options.testDone, opts.testDone);
test.equals(options.testReady, opts.testReady);
test.equals(options.testStart, opts.testStart);
test.equals(options.log, opts.log);
test.ok(typeof name === "string");
runModule_calls.push(mod);
var m = [{failed: function () {
return false;
}}];
modules.push(m);
callback(null, m);
};
nodeunit.runFiles(
[__dirname + '/fixtures/mock_module1.js',
__dirname + '/fixtures/mock_module2.js',
__dirname + '/fixtures/dir'],
opts
);
});
exports.testRunFilesEmpty = function (test) {
test.expect(3);
nodeunit.runFiles([], {
moduleStart: function () {
test.ok(false, 'should not be called');
},
testDone: function () {
test.ok(false, 'should not be called');
},
testReady: function () {
test.ok(false, 'should not be called');
},
testStart: function () {
test.ok(false, 'should not be called');
},
log: function () {
test.ok(false, 'should not be called');
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'length');
test.ok(typeof assertions.duration === "number");
test.done();
}
});
};
exports.testEmptyDir = function (test) {
var dir2 = __dirname + '/fixtures/dir2';
// git doesn't like empty directories, so we have to create one
path.exists(dir2, function (exists) {
if (!exists) {
fs.mkdirSync(dir2, 0777);
}
// runFiles on empty directory:
nodeunit.runFiles([dir2], {
moduleStart: function () {
test.ok(false, 'should not be called');
},
testDone: function () {
test.ok(false, 'should not be called');
},
testReady: function () {
test.ok(false, 'should not be called');
},
testStart: function () {
test.ok(false, 'should not be called');
},
log: function () {
test.ok(false, 'should not be called');
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 0, 'length');
test.ok(typeof assertions.duration === "number");
test.done();
}
});
});
};
var CoffeeScript;
try {
CoffeeScript = require('coffee-script');
if (CoffeeScript.register != null) {
CoffeeScript.register();
}
} catch (e) {
}
if (CoffeeScript) {
exports.testCoffeeScript = function (test) {
process.chdir(__dirname);
var env = {
mock_coffee_module: require(__dirname +
'/fixtures/coffee/mock_coffee_module')
};
test.expect(10);
var runModule_copy = nodeunit.runModule;
var runModule_calls = [];
var modules = [];
var opts = {
moduleStart: function () {
return 'moduleStart';
},
testDone: function () {
return 'testDone';
},
testReady: function () {
return 'testReady';
},
testStart: function () {
return 'testStart';
},
log: function () {
return 'log';
},
done: function (assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 1, 'length');
test.ok(typeof assertions.duration === "number");
var called_with = function (name) {
return runModule_calls.some(function (m) {
return m.name === name;
});
};
test.ok(
called_with('mock_coffee_15'),
'mock_coffee_module ran'
);
test.equals(runModule_calls.length, 1);
nodeunit.runModule = runModule_copy;
test.done();
}
};
nodeunit.runModule = function (name, mod, options, callback) {
test.equals(options.testDone, opts.testDone);
test.equals(options.testReady, opts.testReady);
test.equals(options.testStart, opts.testStart);
test.equals(options.log, opts.log);
test.ok(typeof name === "string");
runModule_calls.push(mod);
var m = [{failed: function () {
return false;
}}];
modules.push(m);
callback(null, m);
};
nodeunit.runFiles(
[__dirname + '/fixtures/coffee/mock_coffee_module.coffee'],
opts
);
};
}

View File

@ -0,0 +1,222 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testRunModule = function (test) {
test.expect(59);
var call_order = [];
var testmodule = {
test1: function (test) {
call_order.push('test1');
test.ok(true, 'ok true');
test.done();
},
test2: function (test) {
call_order.push('test2');
test.ok(false, 'ok false');
test.ok(false, 'ok false');
test.done();
},
test3: function (test) {
call_order.push('test3');
test.done();
}
};
nodeunit.runModule('testmodule', testmodule, {
log: function (assertion) {
call_order.push('log');
},
testStart: function (name) {
call_order.push('testStart');
test.ok(
name.toString() === 'test1' ||
name.toString() === 'test2' ||
name.toString() === 'test3',
'testStart called with test name '
);
},
testReady: function (tst) {
call_order.push('testReady');
test.ok(tst.done, 'testReady called with non-test object');
test.ok(tst.ok, 'testReady called with non-test object');
test.ok(tst.same, 'testReady called with non-test object');
test.ok(tst.expect, 'testReady called with non-test object');
test.ok(tst._assertion_list, 'testReady called with non-test object');
test.ok(tst.AssertionError, 'testReady called with non-test object');
test.ok(tst.fail, 'testReady called with non-test object');
test.ok(tst.equal, 'testReady called with non-test object');
test.ok(tst.notEqual, 'testReady called with non-test object');
test.ok(tst.deepEqual, 'testReady called with non-test object');
test.ok(tst.notDeepEqual, 'testReady called with non-test object');
test.ok(tst.strictEqual, 'testReady called with non-test object');
test.ok(tst.notStrictEqual, 'testReady called with non-test object');
test.ok(tst.throws, 'testReady called with non-test object');
test.ok(tst.doesNotThrow, 'testReady called with non-test object');
test.ok(tst.ifError, 'testReady called with non-test object');
},
testDone: function (name, assertions) {
call_order.push('testDone');
test.ok(
name.toString() === 'test1' ||
name.toString() === 'test2' ||
name.toString() === 'test3',
'testDone called with test name'
);
},
moduleDone: function (name, assertions) {
call_order.push('moduleDone');
test.equals(assertions.length, 3);
test.equals(assertions.failures(), 2);
test.equals(name, 'testmodule');
test.ok(typeof assertions.duration === "number");
test.same(call_order, [
'testStart', 'testReady', 'test1', 'log', 'testDone',
'testStart', 'testReady', 'test2', 'log', 'log', 'testDone',
'testStart', 'testReady', 'test3', 'testDone',
'moduleDone'
]);
}
}, test.done);
};
exports.testRunModuleTestSpec = function (test) {
test.expect(22);
var call_order = [];
var testmodule = {
test1: function (test) {
test.ok(true, 'ok true');
test.done();
},
test2: function (test) {
call_order.push('test2');
test.ok(false, 'ok false');
test.ok(false, 'ok false');
test.done();
},
test3: function (test) {
test.done();
}
};
nodeunit.runModule('testmodule', testmodule, {
testspec: "test2",
log: function (assertion) {
call_order.push('log');
},
testStart: function (name) {
call_order.push('testStart');
test.equals(
name,'test2',
'testStart called with test name '
);
},
testReady: function (tst) {
call_order.push('testReady');
test.ok(tst.done, 'testReady called with non-test object');
test.ok(tst.ok, 'testReady called with non-test object');
test.ok(tst.same, 'testReady called with non-test object');
test.ok(tst.expect, 'testReady called with non-test object');
test.ok(tst._assertion_list, 'testReady called with non-test object');
test.ok(tst.AssertionError, 'testReady called with non-test object');
test.ok(tst.fail, 'testReady called with non-test object');
test.ok(tst.equal, 'testReady called with non-test object');
test.ok(tst.notEqual, 'testReady called with non-test object');
test.ok(tst.deepEqual, 'testReady called with non-test object');
test.ok(tst.notDeepEqual, 'testReady called with non-test object');
test.ok(tst.strictEqual, 'testReady called with non-test object');
test.ok(tst.notStrictEqual, 'testReady called with non-test object');
test.ok(tst.throws, 'testReady called with non-test object');
test.ok(tst.doesNotThrow, 'testReady called with non-test object');
test.ok(tst.ifError, 'testReady called with non-test object');
},
testDone: function (name, assertions) {
call_order.push('testDone');
test.equal(
name, 'test2',
'testDone called with test name'
);
},
moduleDone: function (name, assertions) {
call_order.push('moduleDone');
test.equals(assertions.length, 2);
test.equals(name, 'testmodule');
test.ok(typeof assertions.duration === "number");
test.same(call_order, [
'testStart', 'testReady', 'test2', 'log', 'log', 'testDone',
'moduleDone'
]);
}
}, test.done);
};
exports.testRunModuleEmpty = function (test) {
nodeunit.runModule('module with no exports', {}, {
log: function (assertion) {
test.ok(false, 'log should not be called');
},
testStart: function (name) {
test.ok(false, 'testStart should not be called');
},
testReady: function (tst) {
test.ok(false, 'testReady should not be called');
},
testDone: function (name, assertions) {
test.ok(false, 'testDone should not be called');
},
moduleDone: function (name, assertions) {
test.equals(assertions.length, 0);
test.equals(assertions.failures(), 0);
test.equals(name, 'module with no exports');
test.ok(typeof assertions.duration === "number");
}
}, test.done);
};
exports.testNestedTests = function (test) {
var call_order = [];
var m = {
test1: function (test) {
test.done();
},
suite: {
t1: function (test) {
test.done();
},
t2: function (test) {
test.done();
},
another_suite: {
t3: function (test) {
test.done();
}
}
}
};
nodeunit.runModule('modulename', m, {
testStart: function (name) {
call_order.push(['testStart'].concat(name));
},
testReady: function (tst) {
call_order.push(['testReady']);
},
testDone: function (name, assertions) {
call_order.push(['testDone'].concat(name));
}
}, function () {
test.same(call_order, [
['testStart', 'test1'], ['testReady'], ['testDone', 'test1'],
['testStart', 'suite', 't1'], ['testReady'], ['testDone', 'suite', 't1'],
['testStart', 'suite', 't2'], ['testReady'], ['testDone', 'suite', 't2'],
['testStart', 'suite', 'another_suite', 't3'],
['testReady'],
['testDone', 'suite', 'another_suite', 't3']
]);
test.done();
});
};

View File

@ -0,0 +1,46 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testArgs = function (test) {
test.ok(test.expect instanceof Function, 'test.expect');
test.ok(test.done instanceof Function, 'test.done');
test.ok(test.ok instanceof Function, 'test.ok');
test.ok(test.same instanceof Function, 'test.same');
test.ok(test.equals instanceof Function, 'test.equals');
test.done();
};
exports.testDoneCallback = function (test) {
test.expect(4);
nodeunit.runTest('testname', exports.testArgs, {
testDone: function (name, assertions) {
test.equals(assertions.failures(), 0, 'failures');
test.equals(assertions.length, 5, 'length');
test.ok(typeof assertions.duration === "number");
test.equals(name, 'testname');
}
}, test.done);
};
exports.testThrowError = function (test) {
test.expect(3);
var err = new Error('test');
var testfn = function (test) {
throw err;
};
nodeunit.runTest('testname', testfn, {
log: function (assertion) {
test.same(assertion.error, err, 'assertion.error');
},
testDone: function (name, assertions) {
test.equals(assertions.failures(), 1);
test.equals(assertions.length, 1);
}
}, test.done);
};

View File

@ -0,0 +1,31 @@
var nodeunit = require('../lib/nodeunit');
var sandbox = require('../lib/utils').sandbox;
var testCase = nodeunit.testCase;
exports.testSimpleSandbox = function (test) {
var raw_jscode1 = sandbox(__dirname + '/fixtures/raw_jscode1.js');
test.equal(raw_jscode1.hello_world('foo'), '_foo_', 'evaluation ok');
test.done();
};
exports.testSandboxContext = function (test) {
var a_variable = 42; // should not be visible in the sandbox
var raw_jscode2 = sandbox(__dirname + '/fixtures/raw_jscode2.js');
a_variable = 42; // again for the win
test.equal(
raw_jscode2.get_a_variable(),
'undefined',
'the variable should not be defined'
);
test.done();
};
exports.testSandboxMultiple = function (test) {
var raw_jscode3 = sandbox([
__dirname + '/fixtures/raw_jscode3.js',
__dirname + '/fixtures/raw_jscode3.js',
__dirname + '/fixtures/raw_jscode3.js'
]);
test.equal(raw_jscode3.t, 3, 'two files loaded');
test.done();
};

View File

@ -0,0 +1,257 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
var testCase = nodeunit.testCase;
exports.testTestCase = function (test) {
test.expect(7);
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
test.equals(this.one, undefined);
this.one = 1;
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
test.ok(true, 'tearDown called');
callback();
},
test1: function (t) {
call_order.push('test1');
test.equals(this.one, 1);
this.one = 2;
t.done();
},
test2: function (t) {
call_order.push('test2');
test.equals(this.one, 1);
t.done();
}
});
nodeunit.runSuite(null, s, {}, function () {
test.same(call_order, [
'setUp', 'test1', 'tearDown',
'setUp', 'test2', 'tearDown'
]);
test.done();
});
};
exports.tearDownAfterError = function (test) {
test.expect(1);
var s = testCase({
tearDown: function (callback) {
test.ok(true, 'tearDown called');
callback();
},
test: function (t) {
throw new Error('some error');
}
});
nodeunit.runSuite(null, s, {}, function () {
test.done();
});
};
exports.catchSetUpError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
setUp: function (callback) {
throw test_error;
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.setUpErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
setUp: function (callback) {
callback(test_error);
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.catchTearDownError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
tearDown: function (callback) {
throw test_error;
},
test: function (t) {
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.tearDownErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = testCase({
tearDown: function (callback) {
callback(test_error);
},
test: function (t) {
t.done();
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.testErrorAndtearDownError = function (test) {
test.expect(3);
var error1 = new Error('test error one');
var error2 = new Error('test error two');
var s = testCase({
tearDown: function (callback) {
callback(error2);
},
test: function (t) {
t.done(error1);
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 2);
test.equal(assertions[0].error, error1);
test.equal(assertions[1].error, error2);
test.done();
});
};
exports.testCaseGroups = function (test) {
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.test2',
'tearDown'
]);
test.done();
});
};
exports.nestedTestCases = function (test) {
var call_order = [];
var s = testCase({
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: testCase({
setUp: function (callback) {
call_order.push('group1.setUp');
callback();
},
tearDown: function (callback) {
call_order.push('group1.tearDown');
callback();
},
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
})
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.setUp',
'group1.test2',
'group1.tearDown',
'tearDown'
]);
test.done();
});
};
exports.deepNestedTestCases = function (test) {
var val = 'foo';
var s = testCase({
setUp: function (callback) {
val = 'bar';
callback();
},
group1: testCase({
test: testCase({
test2: function (test) {
test.equal(val, 'bar');
test.done();
}
})
})
});
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.ok(!assertions[0].failed());
test.equal(assertions.length, 1);
test.done();
});
};

View File

@ -0,0 +1,256 @@
/* THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
* You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
* Only code on that line will be removed, its mostly to avoid requiring code
* that is node specific
*/
var nodeunit = require('../lib/nodeunit'); // @REMOVE_LINE_FOR_BROWSER
exports.testTestCase = function (test) {
test.expect(7);
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
test.equals(this.one, undefined, 'in setUp, this.one not set');
this.one = 1;
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
test.ok(true, 'tearDown called');
callback();
},
test1: function (t) {
call_order.push('test1');
test.equals(this.one, 1, 'in test1, this.one is 1');
this.one = 2;
t.done();
},
test2: function (t) {
call_order.push('test2');
test.equals(this.one, 1, 'in test2, this.one is still 1');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function () {
test.same(call_order, [
'setUp', 'test1', 'tearDown',
'setUp', 'test2', 'tearDown'
]);
test.done();
});
};
exports.tearDownAfterError = function (test) {
test.expect(1);
var s = {
tearDown: function (callback) {
test.ok(true, 'tearDown called');
callback();
},
test: function (t) {
throw new Error('some error');
}
};
nodeunit.runSuite(null, s, {}, function () {
test.done();
});
};
exports.catchSetUpError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
setUp: function (callback) {
throw test_error;
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.setUpErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
setUp: function (callback) {
callback(test_error);
},
test: function (t) {
test.ok(false, 'test function should not be called');
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.catchTearDownError = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
tearDown: function (callback) {
throw test_error;
},
test: function (t) {
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.tearDownErrorCallback = function (test) {
test.expect(2);
var test_error = new Error('test error');
var s = {
tearDown: function (callback) {
callback(test_error);
},
test: function (t) {
t.done();
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 1);
test.equal(assertions[0].error, test_error);
test.done();
});
};
exports.testErrorAndtearDownError = function (test) {
test.expect(3);
var error1 = new Error('test error one');
var error2 = new Error('test error two');
var s = {
tearDown: function (callback) {
callback(error2);
},
test: function (t) {
t.done(error1);
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.equal(assertions.length, 2);
test.equal(assertions[0].error, error1);
test.equal(assertions[1].error, error2);
test.done();
});
};
exports.testCaseGroups = function (test) {
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.test2',
'tearDown'
]);
test.done();
});
};
exports.nestedTestCases = function (test) {
var call_order = [];
var s = {
setUp: function (callback) {
call_order.push('setUp');
callback();
},
tearDown: function (callback) {
call_order.push('tearDown');
callback();
},
test1: function (test) {
call_order.push('test1');
test.done();
},
group1: {
setUp: function (callback) {
call_order.push('group1.setUp');
callback();
},
tearDown: function (callback) {
call_order.push('group1.tearDown');
callback();
},
test2: function (test) {
call_order.push('group1.test2');
test.done();
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.same(call_order, [
'setUp',
'test1',
'tearDown',
'setUp',
'group1.setUp',
'group1.test2',
'group1.tearDown',
'tearDown'
]);
test.done();
});
};
exports.deepNestedTestCases = function (test) {
var val = 'foo';
var s = {
setUp: function (callback) {
val = 'bar';
callback();
},
group1: {
test: {
test2: function (test) {
test.equal(val, 'bar');
test.done();
}
}
}
};
nodeunit.runSuite(null, s, {}, function (err, assertions) {
test.ok(!assertions[0].failed());
test.equal(assertions.length, 1);
test.done();
});
};

View File

@ -0,0 +1,28 @@
<html>
<head>
<title>Nodeunit Test Suite</title>
<!--
Note this file is only used for 'make browser', when it is copied to
dist/browser/test.html for running the browser test suite
-->
<link rel="stylesheet" href="nodeunit.css" type="text/css" media="screen" />
<script src="nodeunit.js"></script>
<script src="test-base.js"></script>
<script src="test-runmodule.js"></script>
<script src="test-runtest.js"></script>
<script src="test-testcase.js"></script>
<script src="test-testcase-legacy.js"></script>
</head>
<body>
<h1 id="nodeunit-header">Nodeunit Test Suite</h1>
<script>
nodeunit.run({
'test-base': test_base,
'test-runmodule': test_runmodule,
'test-runtest': test_runtest,
'test-testcase': test_testcase,
'test-testcase-legacy': test_testcase_legacy
});
</script>
</body>
</html>