basic.js 1.8 KB

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