Skip to content

Basic macro support. #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 16, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var TestCollection = require('./test-collection');
function noop() {}

var chainableMethods = {
spread: true,
defaults: {
type: 'test',
serial: false,
Expand Down Expand Up @@ -55,10 +54,19 @@ function Runner(options) {
util.inherits(Runner, EventEmitter);
module.exports = Runner;

optionChain(chainableMethods, function (opts, title, fn) {
if (typeof title === 'function') {
fn = title;
optionChain(chainableMethods, function (opts, args) {
var title;
var fn;
var macroArgIndex;

if (typeof args[0] === 'string') {
title = args[0];
fn = args[1];
macroArgIndex = 2;
} else {
fn = args[0];
title = null;
macroArgIndex = 1;
}

if (opts.todo) {
Expand All @@ -83,6 +91,20 @@ optionChain(chainableMethods, function (opts, title, fn) {
opts.exclusive = title !== null && matcher([title], this._match).length === 1;
}

if (args.length > macroArgIndex) {
args = args.slice(macroArgIndex);

if (!title && fn.title) {
title = fn.title(args);
}

var _fn = fn;

fn = function (t) {
return _fn.apply(this, [t, args]);
};
}

this.tests.add({
metadata: opts,
fn: fn,
Expand Down
27 changes: 27 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,33 @@ test.only.serial(...);

This means you can temporarily add `.skip` or `.only` at the end of a test or hook definition without having to make any other changes.

### Test macros

Additional arguments after the test function will be passed as an array to the test function. This is useful for creating reusable test macros.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it rather be "creating reusable tests"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That may be a cleaner way of saying it, but I'm trying to introduce the term. Maybe "reusable macros", but I think it's good as is.


```js
function macro(t, [input, expected]) {
t.is(eval(input), expected);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation should be a tab 😉

}

test('2 + 2 === 4', macro, '2 + 2', 4);
test('2 * 3 === 6', macro, '2 * 3', 6);
```

You can build the test title programatically by attaching a `title` function to the macro:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

programmatically


```js
function macro(t, [input, expected]) {
t.is(eval(input), expected);
}

macro.title = ([input, expected]) => `${input} === ${expected}`;

test(macro, '2 + 2', 4);
test(macro, '2 * 3', 6);
```


### Custom assertions

You can use any assertion library instead of or in addition to the built-in one, provided it throws exceptions when the assertion fails.
Expand Down
53 changes: 53 additions & 0 deletions test/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,56 @@ test('options.match overrides .only', function (t) {
t.end();
});
});

test('additional args will be passed as an array', function (t) {
t.plan(3);

var runner = new Runner();

runner.test('test1', function (avaT, args) {
t.deepEqual(args, ['foo', 'bar']);
}, 'foo', 'bar');

runner.run({}).then(function (stats) {
t.is(stats.passCount, 1);
t.is(stats.testCount, 1);
t.end();
});
});

test('macro functions can be named by attaching a custom function', function (t) {
t.plan(8);

var expectedTitles = [
'titleA',
'overridden',
'titleC'
];

var expectedArgs = [
['A'],
['B'],
['C']
];

function macroFn(avaT, args) {
t.is(avaT.title, expectedTitles.shift());
t.deepEqual(args, expectedArgs.shift());
}

macroFn.title = function (args) {
return 'title' + args[0];
};

var runner = new Runner();

runner.test(macroFn, 'A');
runner.test('overridden', macroFn, 'B');
runner.test(macroFn, 'C');

runner.run({}).then(function (stats) {
t.is(stats.passCount, 3);
t.is(stats.testCount, 3);
t.end();
});
});