Files: 159e255779ce023db47837d0b076a8b3e1ad29dc / tests / index.js
1630 bytesRaw
1 | const tape = require('tape') |
2 | const ReferanceMap = require('../index.js') |
3 | |
4 | tape('referance mapping', t => { |
5 | t.plan(12) |
6 | const referanceMap = new ReferanceMap() |
7 | const obj1 = {} |
8 | const obj2 = {} |
9 | const ref1 = referanceMap.add(obj1) |
10 | const ref2 = referanceMap.add(obj2) |
11 | t.equals(ref1, 0, 'should produce correct refs') |
12 | t.equals(ref2, 1, 'should produce correct refs') |
13 | |
14 | t.equals(referanceMap.has(ref1), true, 'should detect if it has the ref') |
15 | |
16 | const foundObj1 = referanceMap.get(ref1) |
17 | const foundObj2 = referanceMap.get(ref2) |
18 | |
19 | t.equals(foundObj1, obj1, 'should get the correct object') |
20 | t.equals(foundObj2, obj2, 'should get the correct object') |
21 | |
22 | t.equals(referanceMap.size, 2, 'should return the correct size') |
23 | |
24 | const dupRef = referanceMap.add(obj2) |
25 | |
26 | t.equals(dupRef, ref2, 'detect duplicate refs') |
27 | |
28 | referanceMap.delete(ref1) |
29 | try { |
30 | referanceMap.get(ref1) |
31 | } catch (e) { |
32 | t.true(true, 'should delete refances') |
33 | } |
34 | |
35 | t.equals(referanceMap.has(ref1), false, 'should detect if it does not has the ref') |
36 | |
37 | referanceMap.clear() |
38 | try { |
39 | referanceMap.get(ref2) |
40 | } catch (e) { |
41 | t.true(true, 'should clear refances') |
42 | } |
43 | |
44 | const aMap = new Map() |
45 | const ref3 = referanceMap.add(aMap, 'set') |
46 | t.equals(referanceMap.get(ref3, 'set'), aMap) |
47 | try { |
48 | referanceMap.get(ref3, 'lol') |
49 | } catch (e) { |
50 | t.true(true, 'should throw if wrong type') |
51 | } |
52 | }) |
53 | |
54 | tape('failures', t => { |
55 | const referanceMap = new ReferanceMap() |
56 | const buf = Buffer.from('hello') |
57 | referanceMap.add(buf) |
58 | referanceMap.clear() |
59 | const ref = referanceMap.add(buf) |
60 | t.deepEquals(referanceMap.get(ref), buf) |
61 | t.end() |
62 | }) |
63 |
Built with git-ssb-web