Created
December 11, 2011 11:50
-
-
Save kazupon/1460168 to your computer and use it in GitHub Desktop.
blog "http://d.hatena.ne.jp/kazu_pon/20111212" sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
assert.greater(5, 4); | |
assert.lesser(4, 5); | |
assert.isDefined(null); | |
assert.isDefined(1); | |
assert.isDefined({}); | |
assert.isZero(0); | |
assert.isNotZero(1); | |
assert.inDelta(42, 40, 5); | |
assert.inDelta(42, 40, 2); | |
assert.inDelta(42, 42, 0); | |
assert.inDelta(3.1, 3.0, 0.2); | |
assert.includes([0, 4, 33], 4); | |
assert.includes('hello world', 'hello'); | |
assert.includes({ foo: 4 }, 'foo'); | |
assert.deepInclude([0, 4, 33], 4); | |
assert.deepInclude([{ foo: 4 }, { bar: 33 }], { foo: 4 }); | |
assert.deepInclude('hello world', 'hello'); | |
assert.deepInclude({ foo: 4 }, 'foo'); | |
assert.matches('hello world', /^[a-z]+ [a-z]+$/); | |
assert.lengthOf([0, 1], 2); | |
assert.isNotEmpty({ hoge: 4 }); | |
assert.isNotEmpty('hoge'); | |
assert.isNotEmpty([0, 1, 2]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var EventEmitter = require('events').EventEmitter; | |
var fs = require('fs'); | |
vows.describe('async').addBatch({ | |
'this.callback : ': { | |
'hoge.txt': { | |
topic: function () { | |
fs.stat('./hoge.txt', this.callback); | |
}, | |
'can be accessed': function (err, stats) { | |
assert.isNull(err); | |
assert.isObject(stats); | |
}, | |
'is not empty': function (err, stats) { | |
assert.isNotZero(stats.size); | |
} | |
} | |
}, | |
'EventEmitter : ': { | |
'hoge.txt': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
fs.stat('./hoge.txt', function (err, stats) { | |
if (err) { | |
promise.emit('error', err); | |
} else { | |
promise.emit('success', stats); | |
} | |
}); | |
return promise; | |
}, | |
'can be accessed': function (err, stats) { | |
assert.isNull(err); | |
assert.isObject(stats); | |
}, | |
'is not empty': function (err, stats) { | |
assert.isNotZero(stats.size); | |
} | |
} | |
} | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var suite = vows.describe('Array'); | |
suite.addBatch({ // batch | |
'An array with 3 elements': { // context | |
topic: [1, 2, 3], // topic | |
'has a length of 3': function (topic) { // vow | |
assert.equal(topic.length, 3); | |
} | |
}, | |
'An array': { // context | |
'with zero elements': { // sub-context | |
topic: [], // topic | |
'has a length of 0': function (topic) { // vow | |
assert.equal(topic.length, 0); | |
}, | |
'returns *undefined*, when `pop()`ed': function (topic) { // vow | |
assert.isUndefined(topic.pop()); | |
} | |
} | |
} | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hello |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var EventEmitter = require('events').EventEmitter; | |
var createTimer = require('./timer').createTimer; | |
vows.describe('sub-events-no-use').addBatch({ | |
'calling `run` with 5 sec': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = createTimer(); | |
this.timer = timer; | |
timer.run(5, function () { | |
promise.emit('success', true); | |
}); | |
return promise; | |
}, | |
'on `three` event': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = this.timer; | |
timer.on('3', function (ret) { | |
promise.emit('success', ret); | |
}); | |
return promise; | |
}, | |
'will emit `3` value': function (topic) { | |
assert.equal(topic, 3); | |
}, | |
'on `two` event': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = this.timer; | |
timer.on('2', function (ret) { | |
promise.emit('success', ret); | |
}); | |
return promise; | |
}, | |
'will emit `2` value': function (topic) { | |
assert.equal(topic, 2); | |
}, | |
'on `one` event': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = this.timer; | |
timer.on('1', function (ret) { | |
promise.emit('success', ret); | |
}); | |
return promise; | |
}, | |
'will emit `1` value': function (topic) { | |
assert.equal(topic, 1); | |
} | |
} | |
} | |
} | |
} | |
}).export(module); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'calling `run` with 5 sec': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = createTimer(); | |
timer.on('3', function (ret) { | |
promise.emit('three', ret); | |
}); | |
timer.on('2', function (ret) { | |
promise.emit('two', ret); | |
}); | |
timer.on('1', function (ret) { | |
promise.emit('one', ret); | |
}); | |
timer.run(5, function () { | |
promise.emit('success', timer); | |
}); | |
return promise; | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: { | |
'three': { | |
'will emit `3` value': function (ret) { | |
assert.equal(ret, 3); | |
} | |
}, | |
'two': { | |
'will emit `2` value': function (ret) { | |
assert.equal(ret, 2); | |
} | |
}, | |
'one': { | |
'will emit `1` value': function (ret) { | |
assert.equal(ret, 1); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var EventEmitter = require('events').EventEmitter; | |
var createTimer = require('./timer').createTimer; | |
vows.describe('sub-events').addBatch({ | |
'calling `run` with 5 sec': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = createTimer(); | |
timer.on('3', function (ret) { | |
promise.emit('three', ret); | |
}); | |
timer.on('2', function (ret) { | |
promise.emit('two', ret); | |
}); | |
timer.on('1', function (ret) { | |
promise.emit('one', ret); | |
}); | |
timer.run(5, function () { | |
promise.emit('success', timer); | |
}); | |
return promise; | |
}, | |
on: { | |
'three': { | |
'will emit `3` value': function (ret) { | |
assert.equal(ret, 3); | |
} | |
}, | |
'two': { | |
'will emit `2` value': function (ret) { | |
assert.equal(ret, 2); | |
} | |
}, | |
'one': { | |
'will emit `1` value': function (ret) { | |
assert.equal(ret, 1); | |
} | |
} | |
} | |
} | |
}).export(module); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var EventEmitter = require('events').EventEmitter; | |
var createTimer = require('./timer').createTimer; | |
vows.describe('sub-events-order').addBatch({ | |
'calling `run` with 5 sec': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = createTimer(); | |
timer.on('3', function (ret) { | |
promise.emit('three', ret); | |
}); | |
timer.on('2', function (ret) { | |
promise.emit('two', ret); | |
}); | |
timer.on('1', function (ret) { | |
promise.emit('one', ret); | |
}); | |
timer.run(5, function () { | |
promise.emit('success', timer); | |
}); | |
return promise; | |
}, | |
on: { | |
'three': { | |
'will emit `3` value': function (ret) { | |
assert.equal(ret, 3); | |
}, | |
on: { | |
'two': { | |
'will emit `2` value': function (ret) { | |
assert.equal(ret, 2); | |
}, | |
on: { | |
'one': { | |
'will emit `1` value': function (ret) { | |
assert.equal(ret, 1); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}).export(module); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var EventEmitter = require('events').EventEmitter; | |
var createTimer = require('./timer').createTimer; | |
vows.describe('sub-events-order').addBatch({ | |
'calling `run` with 5 sec': { | |
topic: function () { | |
var promise = new EventEmitter(); | |
var timer = createTimer(); | |
timer.on('3', function (ret) { | |
promise.emit('three', ret); | |
}); | |
timer.on('2', function (ret) { | |
promise.emit('two', ret); | |
}); | |
timer.on('1', function (ret) { | |
promise.emit('one', ret); | |
}); | |
timer.run(5, function () { | |
promise.emit('success', timer); | |
}); | |
return promise; | |
}, | |
on: { | |
'three': { | |
'will emit `3` value': function (ret) { | |
assert.equal(ret, 3); | |
}, | |
on: { | |
'one': { | |
'will emit `1` value': function (ret) { | |
assert.equal(ret, 1); | |
}, | |
on: { | |
'two': { | |
'will emit `2` value': function (ret) { | |
assert.equal(ret, 2); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}).export(module); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
vows.describe('teardown').addBatch({ | |
'context': { | |
topic: function () { | |
// do something ... | |
return { flag: true }; | |
}, | |
'vow1': function (topic) { | |
assert.isTrue(topic.flag); | |
}, | |
'vow2': function (topic) { | |
assert.isTrue(topic.flag); | |
}, | |
teardown: function (topic) { | |
console.log('context teardown !!'); | |
topic.flag = false; | |
} | |
}, | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
vows.describe('teardown').addBatch({ | |
'context': { | |
topic: function () { | |
// do something ... | |
return { flag: true }; | |
}, | |
'vow1': function (topic) { | |
assert.isTrue(topic.flag); | |
}, | |
'vow2': function (topic) { | |
assert.isTrue(topic.flag); | |
}, | |
teardown: function (topic) { | |
console.log('context teardown !!'); | |
topic.flag = false; | |
}, | |
'subcontext': { | |
'nested vow': function (topic) { | |
assert.isTrue(topic.flag); | |
}, | |
teardown: function (topic) { | |
console.log('subcontext teardown !!'); | |
} | |
}, | |
}, | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var flag = false; | |
vows.describe('teardown').addBatch({ | |
'context1': { | |
topic: function () { | |
// do something ... | |
flag = true; | |
return true; | |
}, | |
'vow1': function () { | |
assert.isTrue(flag); | |
}, | |
teardown: function (topic) { | |
var asyncDoSomething = function () { | |
setTimeout(function () { | |
console.log('teardown !!'); | |
flag = false; | |
}, 10); | |
}; | |
asyncDoSomething(); | |
} | |
} | |
}).addBatch({ | |
'context2': { | |
'vow2': function () { | |
assert.isFalse(flag); | |
} | |
} | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var vows = require('vows'); | |
var assert = require('assert'); | |
var flag = false; | |
vows.describe('teardown').addBatch({ | |
'context1': { | |
topic: function () { | |
// do something ... | |
flag = true; | |
return true; | |
}, | |
'vow1': function () { | |
assert.isTrue(flag); | |
}, | |
teardown: function (topic) { | |
var asyncDoSomething = function (cb) { | |
setTimeout(function () { | |
console.log('teardown !!'); | |
flag = false; | |
cb(); | |
}, 10); | |
}; | |
asyncDoSomething(this.callback); | |
} | |
} | |
}).addBatch({ | |
'context2': { | |
'vow2': function () { | |
assert.isFalse(flag); | |
} | |
} | |
}).export(module); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventEmitter = require('events').EventEmitter; | |
var createTimer = function () { | |
var timer = Object.create(EventEmitter.prototype, { | |
run: { | |
value: function (sec, cb) { | |
var self = this; | |
var count = sec; | |
var id = setInterval(function () { | |
if (count === 0) { | |
clearInterval(id); | |
return; | |
} | |
if (count <= 3) { | |
self.emit(count.toString(), count); | |
} | |
count--; | |
}, 1000); | |
cb && cb(); | |
} | |
} | |
}); | |
return timer; | |
}; | |
exports.createTimer = createTimer; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment