Files: 32aeaca5a546e28da16423548ecc95a2efd17e77 / tests / index.js
1286 bytesRaw
1 | const tape = require('tape') |
2 | const ReferanceMap = require('../index.js') |
3 | |
4 | tape('referance mapping', t => { |
5 | t.plan(11) |
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 | referanceMap.delete(ref1) |
25 | try { |
26 | referanceMap.get(ref1) |
27 | } catch (e) { |
28 | t.true(true, 'should delete refances') |
29 | } |
30 | |
31 | t.equals(referanceMap.has(ref1), false, 'should detect if it does not has the ref') |
32 | |
33 | referanceMap.clear() |
34 | try { |
35 | referanceMap.get(ref2) |
36 | } catch (e) { |
37 | t.true(true, 'should clear refances') |
38 | } |
39 | |
40 | const aMap = new Map() |
41 | const ref3 = referanceMap.add(aMap) |
42 | t.equals(referanceMap.get(ref3, Map), aMap) |
43 | try { |
44 | referanceMap.get(ref3, Set) |
45 | } catch (e) { |
46 | t.true(true, 'should throw if wrong type') |
47 | } |
48 | }) |
49 |
Built with git-ssb-web