From 6ec79a31c3c19512c026f1aefd6d876437c12652 Mon Sep 17 00:00:00 2001 From: Matthijs van Duin Date: Wed, 28 Jun 2017 11:56:40 +0200 Subject: [PATCH] add tests for weak.is*() --- test/is.js | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 test/is.js diff --git a/test/is.js b/test/is.js new file mode 100644 index 0000000..bc51d51 --- /dev/null +++ b/test/is.js @@ -0,0 +1,72 @@ +var assert = require('assert'); +var weak = require('../'); + +describe('weak.isWeakRef()', function () { + + afterEach(gc); + + it('should be true for a `WeakRef` instance', function () { + var r = weak({}); + assert(weak.isWeakRef(r)); + }); + + it('should be false for a normal object', function () { + var obj = {}; + assert(!weak.isWeakRef(obj)); + }); + +}); + +describe('weak.isDead()', function () { + + afterEach(gc); + + it('should be false before the target is gc\'d', function () { + var obj = {}; + var r = weak(obj); + assert(!weak.isDead(r)); + }); + + it('should be false inside garbage collection callback', function () { + var result; + var r = weak({}, function () { + result = !weak.isDead(r); + }); + gc(); + assert(result); + }); + + it('should be true after the target is gc\'d', function () { + var r = weak({}); + gc(); + assert(weak.isDead(r)); + }); + +}); + +describe('weak.isNearDeath()', function () { + + afterEach(gc); + + it('should be false before the target is gc\'d', function () { + var obj = {}; + var r = weak(obj); + assert(!weak.isNearDeath(r)); + }); + + it('should be true inside garbage collection callback', function () { + var result; + var r = weak({}, function () { + result = weak.isNearDeath(r); + }); + gc(); + assert(result); + }); + + it('should be false after the target is gc\'d', function () { + var r = weak({}); + gc(); + assert(!weak.isNearDeath(r)); + }); + +});