set.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var Similar = require('../src/similar');
  2. describe("simple keys", () => {
  3. var similar = new Similar(),
  4. entry = { key: 'stringkey', val: 'stringval' };
  5. similar.set(entry.key, entry.val);
  6. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  7. it("has proper size", () => { expect(similar.size).toEqual(1); });
  8. it("has proper key", () => { expect(similar.list[0].key).toEqual(entry.key); });
  9. it("has proper val", () => { expect(similar.list[0].val).toEqual(entry.val); });
  10. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(entry.key); expect(similar.lastItem.val).toEqual(entry.val); });
  11. it("resolves has() correctly", () => { expect(similar.has(entry.key)).toEqual(true); expect(similar.has(entry.val)).toEqual(false); });
  12. it("resolves get() correctly", () => { expect(similar.get(entry.key)).toEqual(entry.val); });
  13. });
  14. describe("NaN keys", () => {
  15. var similar = new Similar(),
  16. key = NaN,
  17. val1 = 'val1',
  18. val2 = 'val2';
  19. similar.set(key, val1);
  20. similar.set(key, val2);
  21. similar.set(key, val1);
  22. similar.set(key, val2);
  23. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  24. it("has proper size", () => { expect(similar.size).toEqual(1); });
  25. it("has proper key", () => { expect(similar.list[0].key).toEqual(key); });
  26. it("has proper val", () => { expect(similar.list[0].val).toEqual(val2); });
  27. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(key); expect(similar.lastItem.val).toEqual(val2); });
  28. it("resolves has() correctly", () => { expect(similar.has(key)).toEqual(true); expect(similar.has(val2)).toEqual(false); });
  29. it("resolves get() correctly", () => { expect(similar.get(key)).toEqual(val2); });
  30. if (!!Map) {
  31. var map = new Map();
  32. map.set(key, val1);
  33. map.set(key, val2);
  34. map.set(key, val1);
  35. map.set(key, val2);
  36. it("matches Map get()", () => { expect(similar.get(key)).toEqual(map.get(key)); });
  37. it("matches Map has()", () => { expect(similar.has(key)).toEqual(map.has(key)); });
  38. }
  39. });
  40. describe("complex keys", () => {
  41. var similar = new Similar(),
  42. entry = { key: { prop: ['a', 'b'] }, val: { prop: 'val1' } };
  43. similar.set(entry.key, entry.val);
  44. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  45. it("has proper size", () => { expect(similar.size).toEqual(1); });
  46. it("has proper key", () => { expect(similar.list[0].key).toEqual(entry.key); });
  47. it("has proper val", () => { expect(similar.list[0].val).toEqual(entry.val); });
  48. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(entry.key); expect(similar.lastItem.val).toEqual(entry.val); });
  49. it("resolves has() correctly", () => { expect(similar.has(entry.key)).toEqual(true); expect(similar.has(entry.val)).toEqual(false); });
  50. it("resolves get() correctly", () => { expect(similar.get(entry.key)).toEqual(entry.val); });
  51. if (!!Map) {
  52. var map = new Map();
  53. map.set(entry.key, entry.val);
  54. it("matches Map get()", () => { expect(similar.get(entry.key)).toEqual(map.get(entry.key)); });
  55. it("matches Map has()", () => { expect(similar.has(entry.key)).toEqual(map.has(entry.key)); });
  56. }
  57. });
  58. describe("undefined keys and values", () => {
  59. var similar = new Similar(),
  60. entry = { key: undefined, val: undefined };
  61. similar.set(entry.key, entry.val);
  62. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  63. it("has proper size", () => { expect(similar.size).toEqual(1); });
  64. it("has proper key", () => { expect(similar.list[0].key).toEqual(entry.key); });
  65. it("has proper val", () => { expect(similar.list[0].val).toEqual(entry.val); });
  66. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(entry.key); expect(similar.lastItem.val).toEqual(entry.val); });
  67. it("resolves has() correctly", () => { expect(similar.has(entry.key)).toEqual(true); expect(similar.has(null)).toEqual(false); });
  68. it("resolves get() correctly", () => { expect(similar.get(entry.key)).toEqual(entry.val); });
  69. if (!!Map) {
  70. var map = new Map();
  71. map.set(entry.key, entry.val);
  72. it("matches Map get()", () => { expect(similar.get(entry.key)).toEqual(map.get(entry.key)); });
  73. it("matches Map has()", () => { expect(similar.has(entry.key)).toEqual(map.has(entry.key)); });
  74. }
  75. });
  76. describe("same keys (replace)", () => {
  77. var similar = new Similar(),
  78. key = { key: { prop: ['a', 'b'] }},
  79. val1 = 'val1',
  80. val2 = 'val2';
  81. similar.set(key, val1);
  82. similar.set(key, val2);
  83. similar.set(key, val1);
  84. similar.set(key, val2);
  85. it("has proper length", () => { expect(similar.list.length).toEqual(1); });
  86. it("has proper size", () => { expect(similar.size).toEqual(1); });
  87. it("has proper key", () => { expect(similar.list[0].key).toEqual(key); });
  88. it("has proper val", () => { expect(similar.list[0].val).toEqual(val2); });
  89. it("has proper lastItem", () => { expect(similar.lastItem.key).toEqual(key); expect(similar.lastItem.val).toEqual(val2); });
  90. it("resolves has() correctly", () => { expect(similar.has(key)).toEqual(true); expect(similar.has(val2)).toEqual(false); });
  91. it("resolves get() correctly", () => { expect(similar.get(key)).toEqual(val2); });
  92. if (!!Map) {
  93. var map = new Map();
  94. map.set(key, val1);
  95. map.set(key, val2);
  96. map.set(key, val1);
  97. map.set(key, val2);
  98. it("matches Map get()", () => { expect(similar.get(key)).toEqual(map.get(key)); });
  99. it("matches Map has()", () => { expect(similar.has(key)).toEqual(map.has(key)); });
  100. }
  101. });