2012-08-15 11:27:28 +08:00
|
|
|
var test = require('tap').test
|
|
|
|
var once = require('../once.js')
|
|
|
|
|
|
|
|
test('once', function (t) {
|
|
|
|
var f = 0
|
2014-11-05 07:08:12 +08:00
|
|
|
function fn (g) {
|
2012-08-15 11:27:28 +08:00
|
|
|
t.equal(f, 0)
|
|
|
|
f ++
|
|
|
|
return f + g + this
|
2014-11-05 07:08:12 +08:00
|
|
|
}
|
|
|
|
fn.ownProperty = {}
|
|
|
|
var foo = once(fn)
|
|
|
|
t.equal(fn.ownProperty, foo.ownProperty)
|
2013-10-25 00:21:59 +08:00
|
|
|
t.notOk(foo.called)
|
2012-08-15 11:27:28 +08:00
|
|
|
for (var i = 0; i < 1E3; i++) {
|
|
|
|
t.same(f, i === 0 ? 0 : 1)
|
|
|
|
var g = foo.call(1, 1)
|
2013-10-25 00:21:59 +08:00
|
|
|
t.ok(foo.called)
|
|
|
|
t.same(g, 3)
|
2012-08-15 11:27:28 +08:00
|
|
|
t.same(f, 1)
|
|
|
|
}
|
|
|
|
t.end()
|
|
|
|
})
|