Skip to content

Commit

Permalink
[Tests] add coverage from tc39/test262#3464
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 4, 2022
1 parent 749d46b commit 2172830
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@
],
}],
},

"overrides": [
{
"files": "test/**",
"rules": {
"max-lines-per-function": "off",
},
},
]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"eslint": "=8.8.0",
"evalmd": "^0.0.19",
"functions-have-names": "^1.2.2",
"has": "^1.0.3",
"has-strict-mode": "^1.0.1",
"nyc": "^10.3.2",
"safe-publish-latest": "^2.0.0",
Expand Down
119 changes: 119 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var has = require('has');

module.exports = function (toSorted, t) {
var nums = [2, 1, 3];
var result = toSorted(nums);
Expand Down Expand Up @@ -28,4 +30,121 @@ module.exports = function (toSorted, t) {
['a', 'c', halfPoo, endPoo],
'code point is sorted as expected'
);

var arrayLikeLengthValueOf = {
length: {
valueOf: function () { return 2; }
},
0: 4,
1: 0,
2: 1
};
t.deepEqual(toSorted(arrayLikeLengthValueOf), [0, 4]);

t.test('not positive integer lengths', function (st) {
st.deepEqual(toSorted({ length: -2 }), []);
st.deepEqual(toSorted({ length: 'dog' }), []);
st.deepEqual(toSorted({ length: NaN }), []);

st.end();
});

t.test('getters', { skip: !Object.defineProperty }, function (st) {
var getCalls = [];

var arrayLike = {
0: 2,
1: 1,
2: 3,
length: 3
};
Object.defineProperty(arrayLike, '0', {
get: function () {
getCalls.push(0);
return 2;
}
});
Object.defineProperty(arrayLike, '1', {
get: function () {
getCalls.push(1);
return 1;
}
});
Object.defineProperty(arrayLike, '2', {
get: function () {
getCalls.push(2);
return 3;
}
});

var up = {};
st['throws'](
function () {
toSorted(arrayLike, function () {
throw up;
});
},
up
);
st.deepEqual(getCalls, [0, 1, 2]);

var arr1 = [5, 0, 3];
Object.defineProperty(arr1, '0', {
get: function () {
arr1.push(1);
return 5;
}
});
st.deepEqual(toSorted(arr1), [0, 3, 5]);

var arr = [5, 1, 4, 6, 3];
Array.prototype[3] = 2; // eslint-disable-line no-extend-native
st.teardown(function () {
delete Array.prototype[3];
});

Object.defineProperty(arr, '2', {
get: function () {
arr.length = 1;
return 4;
}
});

st.deepEqual(toSorted(arr), [1, 2, 4, 5, undefined]);

st.end();
});

t.test('too-large lengths', function (st) {
var arrayLike = {
0: 0,
4294967295: 4294967295,
4294967296: 4294967296,
length: Math.pow(2, 32)
};

st['throws'](
function () { toSorted(arrayLike); },
RangeError
);

st.end();
});

t.deepEqual(toSorted(true), [], 'true yields empty array');
t.deepEqual(toSorted(false), [], 'false yields empty array');

t.test('holes', function (st) {
var arr = [3, /* hole */, 4, /* hole */, 1]; // eslint-disable-line no-sparse-arrays
Array.prototype[3] = 2; // eslint-disable-line no-extend-native
st.teardown(function () {
delete Array.prototype[3];
});

var sorted = toSorted(arr);
st.deepEqual(sorted, [1, 2, 3, 4, undefined]);
st.ok(has(sorted, 4));

st.end();
});
};

0 comments on commit 2172830

Please sign in to comment.