summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Array/group.js
blob: 9e89d1889d6221801d456dee3c74293ab270e817 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function isNeg(x) {
  if (Object.is(x, -0) || x < 0) {
    return true;
  }
  return false;
}

{
  const a1 = [-Infinity, -2, -1, -0, 0, 1, 2, Infinity];
  const expectedObj = { neg: [-Infinity, -2, -1, -0], pos: [0, 1, 2, Infinity] };
  Object.setPrototypeOf(expectedObj, null);

  const groupedArray = Object.groupBy(a1, x => isNeg(x) ? 'neg' : 'pos');
  const mappedArray = Map.groupBy(a1, x => isNeg(x) ? 'neg' : 'pos');

  assertEq(Object.getPrototypeOf(groupedArray), null)
  assertDeepEq(groupedArray, expectedObj);
  assertDeepEq(mappedArray.get("neg"), expectedObj["neg"]);
  assertDeepEq(mappedArray.get("pos"), expectedObj["pos"]);


  const expectedObj2 = {"undefined": [1,2,3]}
  Object.setPrototypeOf(expectedObj2, null);
  assertDeepEq(Object.groupBy([1,2,3], () => {}), expectedObj2);
  assertDeepEq(Object.groupBy([], () => {}), Object.create(null));
  assertDeepEq((Map.groupBy([1,2,3], () => {})).get(undefined), [1,2,3]);
  assertEq((Map.groupBy([1,2,3], () => {})).size, 1);

  const negMappedArray = Map.groupBy(a1, x => isNeg(x) ? -0 : 0);
  assertDeepEq(negMappedArray.get(0), a1);
  assertDeepEq(negMappedArray.size, 1);

  assertThrowsInstanceOf(() => Object.groupBy([], undefined), TypeError);
  assertThrowsInstanceOf(() => Object.groupBy([], null), TypeError);
  assertThrowsInstanceOf(() => Object.groupBy([], 0), TypeError);
  assertThrowsInstanceOf(() => Object.groupBy([], ""), TypeError);
  assertThrowsInstanceOf(() => Map.groupBy([], undefined), TypeError);
  assertThrowsInstanceOf(() => Map.groupBy([], null), TypeError);
  assertThrowsInstanceOf(() => Map.groupBy([], 0), TypeError);
  assertThrowsInstanceOf(() => Map.groupBy([], ""), TypeError);
}

const array = [ 'test' ];
Object.defineProperty(Map.prototype, 4, {
  get() {
    throw new Error('monkey-patched Map get call');
  },
  set(v) {
    throw new Error('monkey-patched Map set call');
  },
  has(v) {
    throw new Error('monkey-patched Map has call');
  }
});

const map1 = Map.groupBy(array, key => key.length);

assertEq('test', map1.get(4)[0])

Object.defineProperty(Array.prototype, '4', {
  set(v) {
    throw new Error('user observable array set');
  },
  get() {
    throw new Error('user observable array get');
  }
});

const map2 = Map.groupBy(array, key => key.length);
const arr = Object.groupBy(array, key => key.length);

assertEq('test', map2.get(4)[0])
assertEq('test', arr[4][0])

Object.defineProperty(Object.prototype, "foo", {
  get() { throw new Error("user observable object get"); },
  set(v) { throw new Error("user observable object set"); }
});
Object.groupBy([1, 2, 3], () => 'foo');

// Ensure property key is correctly accessed
count = 0;
p = Object.groupBy([1], () => ({ toString() { count++; return 10 } }));
assertEq(count, 1);
Map.groupBy([1], () => ({ toString() { count++; return 10 } }));
assertEq(count, 1);

reportCompare(0, 0);