Debian Codegraph @

side-channel/test/index.js

raw
1 'use strict';
2
3 var test = require('tape');
4
5 var getSideChannel = require('../');
6
7 test('export', function (t) {
8 t.equal(typeof getSideChannel, 'function', 'is a function');
9 t.equal(getSideChannel.length, 0, 'takes no arguments');
10
11 var channel = getSideChannel();
12 t.ok(channel, 'is truthy');
13 t.equal(typeof channel, 'object', 'is an object');
14
15 t.end();
16 });
17
18 test('assert', function (t) {
19 var channel = getSideChannel();
20 t['throws'](
21 function () { channel.assert({}); },
22 TypeError,
23 'nonexistent value throws'
24 );
25
26 var o = {};
27 channel.set(o, 'data');
28 t.doesNotThrow(function () { channel.assert(o); }, 'existent value noops');
29
30 t.end();
31 });
32
33 test('has', function (t) {
34 var channel = getSideChannel();
35 var o = [];
36
37 t.equal(channel.has(o), false, 'nonexistent value yields false');
38
39 channel.set(o, 'foo');
40 t.equal(channel.has(o), true, 'existent value yields true');
41
42 t.end();
43 });
44
45 test('get', function (t) {
46 var channel = getSideChannel();
47 var o = {};
48 t.equal(channel.get(o), undefined, 'nonexistent value yields undefined');
49
50 var data = {};
51 channel.set(o, data);
52 t.equal(channel.get(o), data, '"get" yields data set by "set"');
53
54 t.end();
55 });
56
57 test('set', function (t) {
58 var channel = getSideChannel();
59 var o = function () {};
60 t.equal(channel.get(o), undefined, 'value not set');
61
62 channel.set(o, 42);
63 t.equal(channel.get(o), 42, 'value was set');
64
65 channel.set(o, Infinity);
66 t.equal(channel.get(o), Infinity, 'value was set again');
67
68 var o2 = {};
69 channel.set(o2, 17);
70 t.equal(channel.get(o), Infinity, 'o is not modified');
71 t.equal(channel.get(o2), 17, 'o2 is set');
72
73 channel.set(o, 14);
74 t.equal(channel.get(o), 14, 'o is modified');
75 t.equal(channel.get(o2), 17, 'o2 is not modified');
76
77 t.end();
78 });