Skip to content

Commit 8e9f7a3

Browse files
committed
[Tests] add passing tests from node core
See nodejs/node#30764
1 parent fd59bbd commit 8e9f7a3

File tree

2 files changed

+114
-6
lines changed

2 files changed

+114
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
"auto-changelog": "^1.16.2",
4848
"eslint": "^6.7.2",
4949
"has-symbols": "^1.0.1",
50+
"has-typed-arrays": "^1.0.0",
5051
"object.assign": "^4.1.0",
52+
"object.getownpropertydescriptors": "^2.0.3",
5153
"safe-publish-latest": "^1.1.4",
5254
"tape": "^4.11.0"
5355
},

test/cmp.js

Lines changed: 112 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
var test = require('tape');
22
require('./_tape');
33
var assign = require('object.assign');
4+
var gOPDs = require('object.getownpropertydescriptors');
45
var hasSymbols = require('has-symbols')();
6+
var hasTypedArrays = require('has-typed-arrays')();
7+
8+
var safeBuffer = Buffer.from && Buffer.from.length > 1 ? Buffer.from : Buffer;
59

610
function tag(obj, value) {
711
if (hasSymbols && Symbol.toStringTags && Object.defineProperty) {
@@ -220,11 +224,28 @@ test('Dates', function (t) {
220224
st.end();
221225
});
222226

227+
t.test('fake Date', { skip: !hasDunderProto }, function (st) {
228+
var a = new Date(2000);
229+
var b = tag(Object.create(
230+
a.__proto__, // eslint-disable-line no-proto
231+
gOPDs(a)
232+
), 'Date');
233+
234+
st.deepEqualTest(
235+
a,
236+
b,
237+
'Date, and fake Date',
238+
false,
239+
false
240+
);
241+
242+
st.end();
243+
});
244+
223245
t.end();
224246
});
225247

226248
test('buffers', { skip: typeof Buffer !== 'function' }, function (t) {
227-
var safeBuffer = Buffer.from && Buffer.from.length > 1 ? Buffer.from : Buffer;
228249
/* eslint no-buffer-constructor: 1, new-cap: 1 */
229250
t.deepEqualTest(
230251
safeBuffer('xyz'),
@@ -473,6 +494,18 @@ test('regexen', function (t) {
473494
t.deepEqualTest(/abc/, /abc/, 'two abc regexes', true, true, false);
474495
t.deepEqualTest(/xyz/, /xyz/, 'two xyz regexes', true, true, false);
475496

497+
t.test('fake RegExp', { skip: !hasDunderProto }, function (st) {
498+
var a = /abc/g;
499+
var b = tag(Object.create(
500+
a.__proto__, // eslint-disable-line no-proto
501+
gOPDs(a)
502+
), 'RegExp');
503+
504+
st.deepEqualTest(a, b, 'regex and fake regex', false, false);
505+
506+
st.end();
507+
});
508+
476509
t.end();
477510
});
478511

@@ -506,6 +539,23 @@ test('Errors', function (t) {
506539
false
507540
);
508541

542+
t.test('fake error', { skip: !hasDunderProto }, function (st) {
543+
var a = tag({
544+
__proto__: null
545+
}, 'Error');
546+
var b = new RangeError('abc');
547+
b.__proto__ = null; // eslint-disable-line no-proto
548+
549+
st.deepEqualTest(
550+
a,
551+
b,
552+
'null object faking as an Error, RangeError with null proto',
553+
false,
554+
false
555+
);
556+
st.end();
557+
});
558+
509559
t.end();
510560
});
511561

@@ -525,22 +575,23 @@ test('error = Object', function (t) {
525575
t.end();
526576
});
527577

528-
test('[[Prototypes]]', { skip: !Object.getPrototypeOf }, function (t) {
578+
test('[[Prototypes]]', function (t) {
529579
function C() {}
530580
var instance = new C();
531581
delete instance.constructor;
532582

533583
t.deepEqualTest({}, instance, 'two identical objects with different [[Prototypes]]', true, false);
534584

535-
t.test('Dates with different prototypes', { skip: !Object.setPrototypeOf }, function (st) {
585+
t.test('Dates with different prototypes', { skip: !hasDunderProto }, function (st) {
536586
var d1 = new Date(0);
537587
var d2 = new Date(0);
538588

539589
t.deepEqualTest(d1, d2, 'two dates with the same timestamp', true, true);
540590

541-
var newProto = {};
542-
Object.setPrototypeOf(newProto, Date.prototype);
543-
Object.setPrototypeOf(d2, newProto);
591+
var newProto = {
592+
__proto__: Date.prototype
593+
};
594+
d2.__proto__ = newProto; // eslint-disable-line no-proto
544595
st.ok(d2 instanceof Date, 'd2 is still a Date instance after tweaking [[Prototype]]');
545596

546597
t.deepEqualTest(d1, d2, 'two dates with the same timestamp and different [[Prototype]]', true, false);
@@ -629,6 +680,17 @@ test('fake arrays: extra keys will be tested', { skip: !hasDunderProto || isAsse
629680
}
630681

631682
t.deepEqualTest(a, [1, 1], 'fake and real array with same contents and [[Prototype]]', false, false);
683+
684+
var b = tag(/abc/, 'Array');
685+
b.__proto__ = Array.prototype; // eslint-disable-line no-proto
686+
b.length = 3;
687+
if (Object.defineProperty) {
688+
Object.defineProperty(b, 'length', {
689+
enumerable: false
690+
});
691+
}
692+
t.deepEqualTest(b, ['a', 'b', 'c'], 'regex faking as array, and array', false, false);
693+
632694
t.end();
633695
});
634696

@@ -665,3 +727,47 @@ test('circular references', function (t) {
665727

666728
t.end();
667729
});
730+
731+
test('TypedArrays', { skip: !hasTypedArrays }, function (t) {
732+
t.test('Buffer faked as Uint8Array', { skip: typeof Buffer !== 'function' || !Object.create || !hasDunderProto }, function (st) {
733+
var a = safeBuffer('test');
734+
var b = tag(Object.create(
735+
a.__proto__, // eslint-disable-line no-proto
736+
assign(gOPDs(a), {
737+
length: {
738+
enumerable: false,
739+
value: 4
740+
}
741+
})
742+
), 'Uint8Array');
743+
744+
st.deepEqualTest(
745+
a,
746+
b,
747+
'Buffer and Uint8Array',
748+
Buffer.isBuffer(b), // node 0.7-0.12 can not detect the difference, except to crash node uncatchably
749+
Buffer.isBuffer(b)
750+
);
751+
752+
st.end();
753+
});
754+
755+
t.test('one TypedArray faking as another', { skip: !hasDunderProto }, function (st) {
756+
/* globals Uint8Array, Int8Array */
757+
var a = new Uint8Array(10);
758+
var b = tag(new Int8Array(10), 'Uint8Array');
759+
b.__proto__ = Uint8Array.prototype; // eslint-disable-line no-proto
760+
761+
st.deepEqualTest(
762+
a,
763+
b,
764+
'Uint8Array, and Int8Array pretending to be a Uint8Array',
765+
false,
766+
false
767+
);
768+
769+
st.end();
770+
});
771+
772+
t.end();
773+
});

0 commit comments

Comments
 (0)