[79a0317] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var test = require('tape');
|
---|
| 4 |
|
---|
| 5 | var getSideChannelWeakMap = require('../');
|
---|
| 6 |
|
---|
| 7 | test('getSideChannelMap', { skip: typeof WeakMap !== 'function' && typeof Map !== 'function' }, function (t) {
|
---|
| 8 | var getSideChannel = getSideChannelWeakMap || function () {
|
---|
| 9 | throw new EvalError('should never happen');
|
---|
| 10 | };
|
---|
| 11 |
|
---|
| 12 | t.test('export', function (st) {
|
---|
| 13 | st.equal(typeof getSideChannel, 'function', 'is a function');
|
---|
| 14 |
|
---|
| 15 | st.equal(getSideChannel.length, 0, 'takes no arguments');
|
---|
| 16 |
|
---|
| 17 | var channel = getSideChannel();
|
---|
| 18 | st.ok(channel, 'is truthy');
|
---|
| 19 | st.equal(typeof channel, 'object', 'is an object');
|
---|
| 20 | st.end();
|
---|
| 21 | });
|
---|
| 22 |
|
---|
| 23 | t.test('assert', function (st) {
|
---|
| 24 | var channel = getSideChannel();
|
---|
| 25 | st['throws'](
|
---|
| 26 | function () { channel.assert({}); },
|
---|
| 27 | TypeError,
|
---|
| 28 | 'nonexistent value throws'
|
---|
| 29 | );
|
---|
| 30 |
|
---|
| 31 | var o = {};
|
---|
| 32 | channel.set(o, 'data');
|
---|
| 33 | st.doesNotThrow(function () { channel.assert(o); }, 'existent value noops');
|
---|
| 34 |
|
---|
| 35 | st.end();
|
---|
| 36 | });
|
---|
| 37 |
|
---|
| 38 | t.test('has', function (st) {
|
---|
| 39 | var channel = getSideChannel();
|
---|
| 40 | /** @type {unknown[]} */ var o = [];
|
---|
| 41 |
|
---|
| 42 | st.equal(channel.has(o), false, 'nonexistent value yields false');
|
---|
| 43 |
|
---|
| 44 | channel.set(o, 'foo');
|
---|
| 45 | st.equal(channel.has(o), true, 'existent value yields true');
|
---|
| 46 |
|
---|
| 47 | st.equal(channel.has('abc'), false, 'non object value non existent yields false');
|
---|
| 48 |
|
---|
| 49 | channel.set('abc', 'foo');
|
---|
| 50 | st.equal(channel.has('abc'), true, 'non object value that exists yields true');
|
---|
| 51 |
|
---|
| 52 | st.end();
|
---|
| 53 | });
|
---|
| 54 |
|
---|
| 55 | t.test('get', function (st) {
|
---|
| 56 | var channel = getSideChannel();
|
---|
| 57 | var o = {};
|
---|
| 58 | st.equal(channel.get(o), undefined, 'nonexistent value yields undefined');
|
---|
| 59 |
|
---|
| 60 | var data = {};
|
---|
| 61 | channel.set(o, data);
|
---|
| 62 | st.equal(channel.get(o), data, '"get" yields data set by "set"');
|
---|
| 63 |
|
---|
| 64 | st.end();
|
---|
| 65 | });
|
---|
| 66 |
|
---|
| 67 | t.test('set', function (st) {
|
---|
| 68 | var channel = getSideChannel();
|
---|
| 69 | var o = function () {};
|
---|
| 70 | st.equal(channel.get(o), undefined, 'value not set');
|
---|
| 71 |
|
---|
| 72 | channel.set(o, 42);
|
---|
| 73 | st.equal(channel.get(o), 42, 'value was set');
|
---|
| 74 |
|
---|
| 75 | channel.set(o, Infinity);
|
---|
| 76 | st.equal(channel.get(o), Infinity, 'value was set again');
|
---|
| 77 |
|
---|
| 78 | var o2 = {};
|
---|
| 79 | channel.set(o2, 17);
|
---|
| 80 | st.equal(channel.get(o), Infinity, 'o is not modified');
|
---|
| 81 | st.equal(channel.get(o2), 17, 'o2 is set');
|
---|
| 82 |
|
---|
| 83 | channel.set(o, 14);
|
---|
| 84 | st.equal(channel.get(o), 14, 'o is modified');
|
---|
| 85 | st.equal(channel.get(o2), 17, 'o2 is not modified');
|
---|
| 86 |
|
---|
| 87 | st.end();
|
---|
| 88 | });
|
---|
| 89 |
|
---|
| 90 | t.test('delete', function (st) {
|
---|
| 91 | var channel = getSideChannel();
|
---|
| 92 | var o = {};
|
---|
| 93 | st.equal(channel['delete']({}), false, 'nonexistent value yields false');
|
---|
| 94 |
|
---|
| 95 | channel.set(o, 42);
|
---|
| 96 | st.equal(channel.has(o), true, 'value is set');
|
---|
| 97 |
|
---|
| 98 | st.equal(channel['delete']({}), false, 'nonexistent value still yields false');
|
---|
| 99 |
|
---|
| 100 | st.equal(channel['delete'](o), true, 'deleted value yields true');
|
---|
| 101 |
|
---|
| 102 | st.equal(channel.has(o), false, 'value is no longer set');
|
---|
| 103 |
|
---|
| 104 | st.end();
|
---|
| 105 | });
|
---|
| 106 |
|
---|
| 107 | t.end();
|
---|
| 108 | });
|
---|
| 109 |
|
---|
| 110 | test('getSideChannelMap, no WeakMaps and/or Maps', { skip: typeof WeakMap === 'function' || typeof Map === 'function' }, function (t) {
|
---|
| 111 | t.equal(getSideChannelWeakMap, false, 'is false');
|
---|
| 112 |
|
---|
| 113 | t.end();
|
---|
| 114 | });
|
---|