Skip to content

Commit

Permalink
documentation, test.stream
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Nov 25, 2012
1 parent 40aafce commit 7e1d1f0
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 8 deletions.
2 changes: 1 addition & 1 deletion example/timing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var test = require('../');

test('a simple test', function (t) {
test('timing test', function (t) {
t.plan(2);

t.equal(typeof Date.now, 'function');
Expand Down
35 changes: 35 additions & 0 deletions example/too_many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var falafel = require('falafel');
var test = require('../');

test('array', function (t) {
t.plan(3);

var src = '(' + function () {
var xs = [ 1, 2, [ 3, 4 ] ];
var ys = [ 5, 6 ];
g([ xs, ys ]);
} + ')()';

var output = falafel(src, function (node) {
if (node.type === 'ArrayExpression') {
node.update('fn(' + node.source() + ')');
}
});

var arrays = [
[ 3, 4 ],
[ 1, 2, [ 3, 4 ] ],
[ 5, 6 ],
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ],
];

Function(['fn','g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
},
function (xs) {
t.same(xs, [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]);
}
);
});
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createHarness () {
var began = false;
var out = new Render();

return function (name, conf, cb) {
var test = function (name, conf, cb) {
if (typeof conf === 'function') {
cb = conf;
conf = {};
Expand Down Expand Up @@ -59,5 +59,6 @@ function createHarness () {
}
};

return out;
test.stream = out;
return test;
}
4 changes: 2 additions & 2 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ function encodeResult (res, count) {
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';

var ex = json.stringify(res.expected);
var ac = json.stringify(res.actual);
var ex = json.stringify(res.expected) || '';
var ac = json.stringify(res.actual) || '';

if (Math.max(ex.length, ac.length) > 65) {
output += inner + 'expected:\n' + inner + ' ' + ex + '\n';
Expand Down
4 changes: 2 additions & 2 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Test.prototype.equal
= Test.prototype.strictEqual
= Test.prototype.strictEquals
= function (a, b, msg, extra) {
this._assert(a == b, {
this._assert(a === b, {
message : defined(msg, 'should be equal'),
operator : 'equal',
actual : a,
Expand All @@ -175,7 +175,7 @@ Test.prototype.notEqual
= Test.prototype.doesNotEqual
= Test.prototype.isInequal
= function (a, b, msg, extra) {
this._assert(a != b, {
this._assert(a !== b, {
message : defined(msg, 'should not be equal'),
operator : 'notEqual',
actual : a,
Expand Down
156 changes: 155 additions & 1 deletion readme.markdown
Original file line number Diff line number Diff line change
@@ -1,4 +1,158 @@
# tape

pretty test harness with tap output
tap-producing test harness that works in node and browsers

# example

``` js
var test = require('tape');

test('timing test', function (t) {
t.plan(2);

t.equal(typeof Date.now, 'function');
var start = Date.now();

setTimeout(function () {
t.equal(Date.now() - start, 100);
}, 100);
});
```

```
$ node example/timing.js
TAP version 13
# timing test
ok 1 should be equal
not ok 2 should be equal
---
operator: equal
expected: 100
actual: 107
...
1..2
# tests 2
# pass 1
# fail 1
```

# methods

The assertion methods in tape are heavily influenced or copied from the methods
in [node-tap](https://github.com/isaacs/node-tap).

```
var test = require('tape')
```

## test(name, cb)

Create a new test with an optional `name` string. `cb(t)` fires with the new
test object `t` once all preceeding tests have finished. Tests execute serially.

If you forget to `t.plan()` out how many assertions you are going to run and you
don't call `t.end()` explicitly, your test will hang.

## t.plan(n)

Declare that `n` assertions should be run. `t.end()` will be called
automatically after the `n`th assertion. If there are any more assertions after
the `n`th, or after `t.end()` is called, they will generate errors.

## t.end()

Declare the end of a test explicitly.

## t.fail(msg)

Generate a failing assertion with a message `msg`.

## t.pass(msg)

Generate a passing assertion with a message `msg`.

## t.skip(msg)

Generate an assertion that will be skipped over.

## t.ok(value, msg)

Assert that `value` is truthy with an optional description message `msg`.

Aliases: `t.true()`, `t.assert()`

## t.notOk(value, msg)

Assert that `value` is falsy with an optional description message `msg`.

Aliases: `t.false()`, `t.notok()`

## t.error(err, msg)

Assert that `err` is falsy. If `err` is non-falsy, use its `err.message` as the
description message.

Aliases: `t.ifError()`, `t.ifErr()`, `t.iferror()`

## t.equal(a, b, msg)

Assert that `a === b` with an optional description `msg`.

Aliases: `t.equals()`, `t.isEqual()`, `t.is()`, `t.strictEqual()`,
`t.strictEquals()`

## t.notEqual(a, b, msg)

Assert that `a !== b` with an optional description `msg`.

Aliases: `t.notEquals()`, `t.notStrictEqual()`, `t.notStrictEquals()`,
`t.isNotEqual()`, `t.isNot()`, `t.not()`, `t.doesNotEqual()`, `t.isInequal()`

## t.deepEqual(a, b, msg)

Assert that `a` and `b` have the same structure and nested values using
[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
with an optional description `msg`.

Aliases: `t.deepEquals()`, `t.isEquivalent()`, `t.looseEqual()`,
`t.looseEquals()`, `t.same()`

## t.notDeepEqual(a, b, msg)

Assert that `a` and `b` do not have the same structure and nested values using
[node's deepEqual() algorithm](https://github.com/substack/node-deep-equal)
with an optional description `msg`.

Aliases: `t.notEquivalent()`, `t.notDeeply()`, `t.notSame()`,
`t.isNotDeepEqual()`, `t.isNotDeeply()`, `t.isNotEquivalent()`,
`t.isInequivalent()`

## t.throws(fn, expected, msg)

Assert that the function call `fn()` throws an exception.

## t.doesNotThrow(fn, expected, msg)

Assert that the function call `fn()` does not throw an exception.

## var htest = test.createHarness()

Create a new test harness instance, which is a function like `test()`, but with
a new pending stack and test state.

By default the TAP output goes to `process.stdout` or `console.log()` if the
environment doesn't have `process.stdout`. You can pipe the output to someplace
else if you `test.stream.pipe()` to a destination stream on the first tick.

# install

With [npm](https://npmjs.org) do:

```
npm install tape
```

# license

MIT

0 comments on commit 7e1d1f0

Please sign in to comment.