patternjavascriptMinor
Unit-testing function
Viewed 0 times
functiontestingunit
Problem
I've got this javascript function which was written a bit ad-hoc and I'm not really sure how to go about refactoring and improving it.
It's basically an implementation of draft Unit-Testing/1.1 specification.
```
// Runs the object as a test. The async paramater is an optional function
// To be passed in if the test is to be run async.
// notRoot is an internal paramater
var run = function(object, async, notRoot) {
// if its the root call then cache the async func for us in _pushTests
if (!notRoot) {
func = async;
}
// tests to run
var tests = [];
// Assert constructor to be used is either on the object or from assert
var Assert = object.Assert || assert.Assert;
var failures = 0;
// Push the tests to the tests array
_pushTests(object, tests);
var len = tests.length;
// function that runs the test
var testRunner;
// If async Do I have to document it? It's full of the hacks.
if (async) {
// results object stores the calls to pass/fail/error
var results = {};
results.passes = [];
results.fails = [];
results.errors = [];
// tests passed in the test object
var testsPassed = 0;
testRunner = function(val, key) {
// Local assert object
var _assert = new Assert;
// Forward the mute property to the assert
if (object.mute === true) {
_assert.mute = true;
}
// cache pass, fail & error
var _pass = _assert.pass;
var _fail = _assert.fail;
var _error = _assert.error;
// Wrap pass. Push the pass massge to the results object
_assert.pass = function(message) {
_pass.apply(_assert, arguments);
results.passes.push(message);
// If an assert passed after done then throw an error in
// assert.error
if (doneCalled) {
It's basically an implementation of draft Unit-Testing/1.1 specification.
```
// Runs the object as a test. The async paramater is an optional function
// To be passed in if the test is to be run async.
// notRoot is an internal paramater
var run = function(object, async, notRoot) {
// if its the root call then cache the async func for us in _pushTests
if (!notRoot) {
func = async;
}
// tests to run
var tests = [];
// Assert constructor to be used is either on the object or from assert
var Assert = object.Assert || assert.Assert;
var failures = 0;
// Push the tests to the tests array
_pushTests(object, tests);
var len = tests.length;
// function that runs the test
var testRunner;
// If async Do I have to document it? It's full of the hacks.
if (async) {
// results object stores the calls to pass/fail/error
var results = {};
results.passes = [];
results.fails = [];
results.errors = [];
// tests passed in the test object
var testsPassed = 0;
testRunner = function(val, key) {
// Local assert object
var _assert = new Assert;
// Forward the mute property to the assert
if (object.mute === true) {
_assert.mute = true;
}
// cache pass, fail & error
var _pass = _assert.pass;
var _fail = _assert.fail;
var _error = _assert.error;
// Wrap pass. Push the pass massge to the results object
_assert.pass = function(message) {
_pass.apply(_assert, arguments);
results.passes.push(message);
// If an assert passed after done then throw an error in
// assert.error
if (doneCalled) {
Solution
I would start out by cleaning the code along these lines:
i.e:
followed by the the functions them self
Try to build up your code as a series of progressions, each building logically on the former without needing to many commentary's along the way, in a similar manner as a well written textbook.
- Refactor variable names and functions so as to limit the use comments (code doesn't' lie, comments sometimes do)
- Use better descriptive names (fx key/val relates to test's in your code but has me thinking of object literals).
- Split the function in smaller functions so they are easier to test and comprehend by them self:
i.e:
testRunner = (async) ? asyncTestRunner : functionTestRunner;followed by the the functions them self
//
function asyncTestRunner(descriptiveParameterNames) {
:
}
//
function functionTestRunner(anotherDescriptiveParameter) {
:
}Try to build up your code as a series of progressions, each building logically on the former without needing to many commentary's along the way, in a similar manner as a well written textbook.
Code Snippets
testRunner = (async) ? asyncTestRunner : functionTestRunner;//
function asyncTestRunner(descriptiveParameterNames) {
:
}
//
function functionTestRunner(anotherDescriptiveParameter) {
:
}Context
StackExchange Code Review Q#856, answer score: 6
Revisions (0)
No revisions yet.