• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var t = require('tap')
2
3process.env.TEST_PSEUDOMAP = 'true'
4
5var PM = require('../')
6runTests(PM)
7
8// if possible, verify that Map also behaves the same way
9if (typeof Map === 'function')
10  runTests(Map)
11
12
13function runTests (Map) {
14  t.throws(Map)
15
16  var m = new Map()
17
18  t.equal(m.size, 0)
19
20  m.set(1, '1 string')
21  t.equal(m.get(1), '1 string')
22  t.equal(m.size, 1)
23  m.size = 1000
24  t.equal(m.size, 1)
25  m.size = 0
26  t.equal(m.size, 1)
27
28  m = new Map([[1, 'number 1'], ['1', 'string 1']])
29  t.equal(m.get(1), 'number 1')
30  t.equal(m.get('1'), 'string 1')
31  t.equal(m.size, 2)
32
33  m = new Map(m)
34  t.equal(m.get(1), 'number 1')
35  t.equal(m.get('1'), 'string 1')
36  t.equal(m.size, 2)
37
38  var akey = {}
39  var bkey = {}
40  m.set(akey, { some: 'data' })
41  m.set(bkey, { some: 'other data' })
42  t.same(m.get(akey), { some: 'data' })
43  t.same(m.get(bkey), { some: 'other data' })
44  t.equal(m.size, 4)
45
46  var x = /x/
47  var y = /x/
48  m.set(x, 'x regex')
49  m.set(y, 'y regex')
50  t.equal(m.get(x), 'x regex')
51  m.set(x, 'x again')
52  t.equal(m.get(x), 'x again')
53  t.equal(m.size, 6)
54
55  m.set(NaN, 'not a number')
56  t.equal(m.get(NaN), 'not a number')
57  m.set(NaN, 'it is a ' + typeof NaN)
58  t.equal(m.get(NaN), 'it is a number')
59  m.set('NaN', 'stringie nan')
60  t.equal(m.get(NaN), 'it is a number')
61  t.equal(m.get('NaN'), 'stringie nan')
62  t.equal(m.size, 8)
63
64  m.delete(NaN)
65  t.equal(m.get(NaN), undefined)
66  t.equal(m.size, 7)
67
68  var expect = [
69    { value: 'number 1', key: 1 },
70    { value: 'string 1', key: '1' },
71    { value: { some: 'data' }, key: {} },
72    { value: { some: 'other data' }, key: {} },
73    { value: 'x again', key: /x/ },
74    { value: 'y regex', key: /x/ },
75    { value: 'stringie nan', key: 'NaN' }
76  ]
77  var actual = []
78
79  m.forEach(function (value, key) {
80    actual.push({ value: value, key: key })
81  })
82  t.same(actual, expect)
83
84  m.clear()
85  t.equal(m.size, 0)
86}
87