diff --git a/examples/spec/finalizationregistry.js b/examples/spec/finalizationregistry.js new file mode 100644 index 0000000..e29094f --- /dev/null +++ b/examples/spec/finalizationregistry.js @@ -0,0 +1,28 @@ +import { test, summary } from './helpers.js'; + +console.log('FinalizationRegistry Tests\n'); + +let callbackCalled = false; +const fr = new FinalizationRegistry(function(heldValue) { + callbackCalled = true; +}); + +test('finalizationregistry instanceof', fr instanceof FinalizationRegistry, true); +test('finalizationregistry prototype', Object.getPrototypeOf(fr) === FinalizationRegistry.prototype, true); +test('finalizationregistry has register', typeof fr.register, 'function'); +test('finalizationregistry has unregister', typeof fr.unregister, 'function'); + +const target = {}; +const token = {}; +fr.register(target, 'held value', token); + +test('finalizationregistry register returns undefined', fr.register({}, 'test'), undefined); + +const unregisterResult = fr.unregister(token); +test('finalizationregistry unregister returns boolean', typeof unregisterResult, 'boolean'); +test('finalizationregistry unregister found token', unregisterResult, true); + +const notFoundResult = fr.unregister({}); +test('finalizationregistry unregister not found', notFoundResult, false); + +summary(); diff --git a/examples/spec/weakref.js b/examples/spec/weakref.js new file mode 100644 index 0000000..a8aaede --- /dev/null +++ b/examples/spec/weakref.js @@ -0,0 +1,21 @@ +import { test, summary } from './helpers.js'; + +console.log('WeakRef Tests\n'); + +const obj1 = { name: 'test' }; +const ref1 = new WeakRef(obj1); + +test('weakref deref returns target', ref1.deref() === obj1, true); +test('weakref deref value', ref1.deref().name, 'test'); + +const obj2 = { value: 42 }; +const ref2 = new WeakRef(obj2); +test('weakref deref number value', ref2.deref().value, 42); + +const obj3 = {}; +const ref3 = new WeakRef(obj3); +test('weakref has deref method', typeof ref3.deref, 'function'); +test('weakref instanceof', ref3 instanceof WeakRef, true); +test('weakref prototype', Object.getPrototypeOf(ref3) === WeakRef.prototype, true); + +summary();