Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for weak.is*() #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions test/is.js
Original file line number Diff line number Diff line change
@@ -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));
});

});