08-27-周三_17-09-29
This commit is contained in:
23
node_modules/weak/test/buffer.js
generated
vendored
Normal file
23
node_modules/weak/test/buffer.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
var assert = require('assert')
|
||||
var weak = require('../')
|
||||
|
||||
describe('weak()', function () {
|
||||
|
||||
afterEach(gc)
|
||||
|
||||
describe('Buffer', function () {
|
||||
|
||||
it('should invoke callback before destroying Buffer', function () {
|
||||
|
||||
var called = false
|
||||
weak(Buffer('test'), function (buf) {
|
||||
called = true
|
||||
})
|
||||
|
||||
assert(!called)
|
||||
gc()
|
||||
assert(called)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
102
node_modules/weak/test/callback.js
generated
vendored
Normal file
102
node_modules/weak/test/callback.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
var assert = require('assert')
|
||||
var weak = require('../')
|
||||
|
||||
describe('weak()', function () {
|
||||
|
||||
afterEach(gc)
|
||||
|
||||
describe('garbage collection callback', function () {
|
||||
|
||||
it('should accept a function as second argument', function () {
|
||||
var r = weak({}, function () {})
|
||||
assert.equal(1, weak.callbacks(r).length)
|
||||
})
|
||||
|
||||
it('should invoke the callback before the target is gc\'d', function () {
|
||||
var called = false
|
||||
weak({}, function () {
|
||||
called = true
|
||||
})
|
||||
assert(!called)
|
||||
gc()
|
||||
assert(called)
|
||||
})
|
||||
|
||||
it('should invoke *all* callbacks in the internal "callback" Array'
|
||||
, function () {
|
||||
var r = weak({})
|
||||
, called1 = false
|
||||
, called2 = false
|
||||
weak.addCallback(r, function () {
|
||||
called1 = true
|
||||
})
|
||||
weak.addCallback(r, function () {
|
||||
called2 = true
|
||||
})
|
||||
gc()
|
||||
assert(called1)
|
||||
assert(called2)
|
||||
})
|
||||
|
||||
it('should preempt code for GC callback but not nextTick callbacks'
|
||||
, function(done) {
|
||||
var calledGcCallback = false
|
||||
, calledTickCallback = false
|
||||
weak({}, function() {
|
||||
calledGcCallback = true
|
||||
})
|
||||
|
||||
process.nextTick(function() {
|
||||
calledTickCallback = true
|
||||
});
|
||||
|
||||
assert(!calledGcCallback)
|
||||
assert(!calledTickCallback)
|
||||
gc()
|
||||
assert(calledGcCallback)
|
||||
assert(!calledTickCallback)
|
||||
setTimeout(function() {
|
||||
assert(calledTickCallback);
|
||||
done();
|
||||
}, 0)
|
||||
})
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
describe('callbacks()', function () {
|
||||
|
||||
it('should return the Weakref\'s "callback" Array', function () {
|
||||
var r = weak({}, function() {})
|
||||
, callbacks = weak.callbacks(r)
|
||||
assert(Array.isArray(callbacks))
|
||||
assert.equal(1, callbacks.length)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('removeCallback()', function() {
|
||||
|
||||
it('removed callbacks should not be called', function() {
|
||||
var called = false
|
||||
, fn = function() { called = true }
|
||||
, r = weak({}, fn)
|
||||
weak.removeCallback(r, fn)
|
||||
gc()
|
||||
assert(!called)
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
describe('removeCallbacks()', function() {
|
||||
|
||||
it('removed callbacks should not be called', function() {
|
||||
var called = false
|
||||
, fn = function() { called = true }
|
||||
, r = weak({}, fn)
|
||||
weak.removeCallbacks(r)
|
||||
gc()
|
||||
assert(!called)
|
||||
})
|
||||
|
||||
})
|
23
node_modules/weak/test/create.js
generated
vendored
Normal file
23
node_modules/weak/test/create.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
var assert = require('assert')
|
||||
var weak = require('../')
|
||||
|
||||
describe('create()', function () {
|
||||
|
||||
afterEach(gc)
|
||||
|
||||
it('should throw on non-"object" values', function () {
|
||||
[ 0
|
||||
, 0.0
|
||||
, true
|
||||
, false
|
||||
, null
|
||||
, undefined
|
||||
, 'foo'
|
||||
].forEach(function (val) {
|
||||
assert.throws(function () {
|
||||
weak.create(val)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
})
|
32
node_modules/weak/test/exports.js
generated
vendored
Normal file
32
node_modules/weak/test/exports.js
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
var assert = require('assert');
|
||||
var weak = require('../')
|
||||
|
||||
function checkFunction (prop) {
|
||||
it('should have a function "' + prop + '"', function () {
|
||||
assert('function' == typeof weak[prop]);
|
||||
})
|
||||
}
|
||||
|
||||
describe('exports', function () {
|
||||
|
||||
afterEach(gc)
|
||||
|
||||
it('should be a function', function () {
|
||||
assert('function' == typeof weak);
|
||||
})
|
||||
|
||||
checkFunction('get')
|
||||
checkFunction('create')
|
||||
checkFunction('isWeakRef')
|
||||
checkFunction('isNearDeath')
|
||||
checkFunction('isDead')
|
||||
checkFunction('callbacks')
|
||||
checkFunction('addCallback')
|
||||
checkFunction('removeCallback')
|
||||
checkFunction('removeCallbacks')
|
||||
|
||||
it('should be a circular reference to "create"', function () {
|
||||
assert(weak === weak.create);
|
||||
})
|
||||
|
||||
})
|
78
node_modules/weak/test/weakref.js
generated
vendored
Normal file
78
node_modules/weak/test/weakref.js
generated
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
var assert = require('assert')
|
||||
var weak = require('../')
|
||||
|
||||
describe('Weakref', function () {
|
||||
|
||||
afterEach(gc)
|
||||
|
||||
it('weak() should return a `Weakref` instance', function () {
|
||||
var ref = weak({})
|
||||
assert(weak.isWeakRef(ref))
|
||||
})
|
||||
|
||||
it('should proxy named gets to the target', function () {
|
||||
var o = { foo: 'bar' }
|
||||
, r = weak(o)
|
||||
assert.equal(r.foo, 'bar')
|
||||
})
|
||||
|
||||
it('should proxy named sets to the target', function () {
|
||||
var o = {}
|
||||
, r = weak(o)
|
||||
r.foo = 'bar'
|
||||
assert.equal(r.foo, 'bar')
|
||||
})
|
||||
|
||||
it('should proxy named deletes to the target', function () {
|
||||
var o = { foo: 'bar' }
|
||||
, r = weak(o)
|
||||
delete r.foo
|
||||
assert(!r.foo)
|
||||
})
|
||||
|
||||
it('should proxy indexed gets to the target', function () {
|
||||
var a = [ 'foo' ]
|
||||
, r = weak(a)
|
||||
assert.equal(1, a.length)
|
||||
assert.equal(1, r.length)
|
||||
assert.equal('foo', r[0])
|
||||
})
|
||||
|
||||
it('should proxy indexed sets to the target', function () {
|
||||
var a = []
|
||||
, r = weak(a)
|
||||
assert.equal(0, a.length)
|
||||
assert.equal(0, r.length)
|
||||
r[0] = 'foo'
|
||||
assert.equal(1, a.length)
|
||||
assert.equal('foo', a[0])
|
||||
r.push('bar')
|
||||
assert.equal(2, a.length)
|
||||
assert.equal('bar', a[1])
|
||||
})
|
||||
|
||||
it('should proxy indexed deletes to the target', function () {
|
||||
var a = [ 'foo' ]
|
||||
, r = weak(a)
|
||||
delete r[0]
|
||||
assert.equal('undefined', typeof a[0])
|
||||
})
|
||||
|
||||
it('should proxy enumeration', function () {
|
||||
var o = { a: 'a', b: 'b', c: 'c', d: 'd' }
|
||||
, r = weak(o)
|
||||
assert.deepEqual(Object.keys(o), Object.keys(r))
|
||||
})
|
||||
|
||||
it('should act like an empty object after target is gc\'d'
|
||||
, function () {
|
||||
var o = { foo: 'bar' }
|
||||
, r = weak(o)
|
||||
o = null
|
||||
assert.equal('bar', r.foo)
|
||||
gc()
|
||||
assert(!r.foo)
|
||||
assert.equal(0,Object.keys(r).length)
|
||||
})
|
||||
|
||||
})
|
Reference in New Issue
Block a user