1
1
var test = require ( 'tape' ) ;
2
2
require ( './_tape' ) ;
3
3
var assign = require ( 'object.assign' ) ;
4
+ var gOPDs = require ( 'object.getownpropertydescriptors' ) ;
4
5
var hasSymbols = require ( 'has-symbols' ) ( ) ;
6
+ var hasTypedArrays = require ( 'has-typed-arrays' ) ( ) ;
7
+ var semver = require ( 'semver' ) ;
8
+
9
+ var safeBuffer = typeof Buffer === 'function' ? Buffer . from && Buffer . from . length > 1 ? Buffer . from : Buffer : null ;
10
+
11
+ var isNode = typeof process === 'object' && typeof process . version === 'string' ;
5
12
6
13
function tag ( obj , value ) {
7
14
if ( hasSymbols && Symbol . toStringTags && Object . defineProperty ) {
@@ -220,11 +227,28 @@ test('Dates', function (t) {
220
227
st . end ( ) ;
221
228
} ) ;
222
229
230
+ t . test ( 'fake Date' , { skip : ! hasDunderProto } , function ( st ) {
231
+ var a = new Date ( 2000 ) ;
232
+ var b = tag ( Object . create (
233
+ a . __proto__ , // eslint-disable-line no-proto
234
+ gOPDs ( a )
235
+ ) , 'Date' ) ;
236
+
237
+ st . deepEqualTest (
238
+ a ,
239
+ b ,
240
+ 'Date, and fake Date' ,
241
+ false ,
242
+ false
243
+ ) ;
244
+
245
+ st . end ( ) ;
246
+ } ) ;
247
+
223
248
t . end ( ) ;
224
249
} ) ;
225
250
226
251
test ( 'buffers' , { skip : typeof Buffer !== 'function' } , function ( t ) {
227
- var safeBuffer = Buffer . from && Buffer . from . length > 1 ? Buffer . from : Buffer ;
228
252
/* eslint no-buffer-constructor: 1, new-cap: 1 */
229
253
t . deepEqualTest (
230
254
safeBuffer ( 'xyz' ) ,
@@ -473,6 +497,18 @@ test('regexen', function (t) {
473
497
t . deepEqualTest ( / a b c / , / a b c / , 'two abc regexes' , true , true , false ) ;
474
498
t . deepEqualTest ( / x y z / , / x y z / , 'two xyz regexes' , true , true , false ) ;
475
499
500
+ t . test ( 'fake RegExp' , { skip : ! hasDunderProto } , function ( st ) {
501
+ var a = / a b c / g;
502
+ var b = tag ( Object . create (
503
+ a . __proto__ , // eslint-disable-line no-proto
504
+ gOPDs ( a )
505
+ ) , 'RegExp' ) ;
506
+
507
+ st . deepEqualTest ( a , b , 'regex and fake regex' , false , false ) ;
508
+
509
+ st . end ( ) ;
510
+ } ) ;
511
+
476
512
t . end ( ) ;
477
513
} ) ;
478
514
@@ -506,6 +542,23 @@ test('Errors', function (t) {
506
542
false
507
543
) ;
508
544
545
+ t . test ( 'fake error' , { skip : ! hasDunderProto } , function ( st ) {
546
+ var a = tag ( {
547
+ __proto__ : null
548
+ } , 'Error' ) ;
549
+ var b = new RangeError ( 'abc' ) ;
550
+ b . __proto__ = null ; // eslint-disable-line no-proto
551
+
552
+ st . deepEqualTest (
553
+ a ,
554
+ b ,
555
+ 'null object faking as an Error, RangeError with null proto' ,
556
+ false ,
557
+ false
558
+ ) ;
559
+ st . end ( ) ;
560
+ } ) ;
561
+
509
562
t . end ( ) ;
510
563
} ) ;
511
564
@@ -525,22 +578,23 @@ test('error = Object', function (t) {
525
578
t . end ( ) ;
526
579
} ) ;
527
580
528
- test ( '[[Prototypes]]' , { skip : ! Object . getPrototypeOf } , function ( t ) {
581
+ test ( '[[Prototypes]]' , function ( t ) {
529
582
function C ( ) { }
530
583
var instance = new C ( ) ;
531
584
delete instance . constructor ;
532
585
533
586
t . deepEqualTest ( { } , instance , 'two identical objects with different [[Prototypes]]' , true , false ) ;
534
587
535
- t . test ( 'Dates with different prototypes' , { skip : ! Object . setPrototypeOf } , function ( st ) {
588
+ t . test ( 'Dates with different prototypes' , { skip : ! hasDunderProto } , function ( st ) {
536
589
var d1 = new Date ( 0 ) ;
537
590
var d2 = new Date ( 0 ) ;
538
591
539
592
t . deepEqualTest ( d1 , d2 , 'two dates with the same timestamp' , true , true ) ;
540
593
541
- var newProto = { } ;
542
- Object . setPrototypeOf ( newProto , Date . prototype ) ;
543
- Object . setPrototypeOf ( d2 , newProto ) ;
594
+ var newProto = {
595
+ __proto__ : Date . prototype
596
+ } ;
597
+ d2 . __proto__ = newProto ; // eslint-disable-line no-proto
544
598
st . ok ( d2 instanceof Date , 'd2 is still a Date instance after tweaking [[Prototype]]' ) ;
545
599
546
600
t . deepEqualTest ( d1 , d2 , 'two dates with the same timestamp and different [[Prototype]]' , true , false ) ;
@@ -613,8 +667,8 @@ test('getters', { skip: !Object.defineProperty }, function (t) {
613
667
t . end ( ) ;
614
668
} ) ;
615
669
616
- var isAssertAndNode1321 = process . env . ASSERT && process . version . replace ( / ^ v / g , '' ) . replace ( / [ ^ . ] + (?: . ) ? / g , function ( x , i ) { return x * Math . pow ( 10 , i ) ; } ) <= '1320000' ;
617
- test ( 'fake arrays: extra keys will be tested' , { skip : ! hasDunderProto || isAssertAndNode1321 } , function ( t ) {
670
+ var isAssertAndNode1321 = isNode && process . env . ASSERT && semver . satisfies ( process . version , '>= 13.2.0' ) ;
671
+ test ( 'fake arrays: extra keys will be tested' , { skip : ! hasDunderProto || ! isAssertAndNode1321 } , function ( t ) {
618
672
var a = tag ( {
619
673
__proto__ : Array . prototype ,
620
674
0 : 1 ,
@@ -629,6 +683,17 @@ test('fake arrays: extra keys will be tested', { skip: !hasDunderProto || isAsse
629
683
}
630
684
631
685
t . deepEqualTest ( a , [ 1 , 1 ] , 'fake and real array with same contents and [[Prototype]]' , false , false ) ;
686
+
687
+ var b = tag ( / a b c / , 'Array' ) ;
688
+ b . __proto__ = Array . prototype ; // eslint-disable-line no-proto
689
+ b . length = 3 ;
690
+ if ( Object . defineProperty ) {
691
+ Object . defineProperty ( b , 'length' , {
692
+ enumerable : false
693
+ } ) ;
694
+ }
695
+ t . deepEqualTest ( b , [ 'a' , 'b' , 'c' ] , 'regex faking as array, and array' , false , false ) ;
696
+
632
697
t . end ( ) ;
633
698
} ) ;
634
699
@@ -665,3 +730,50 @@ test('circular references', function (t) {
665
730
666
731
t . end ( ) ;
667
732
} ) ;
733
+
734
+ // io.js v2 is the only version where `console.log(b)` below is catchable
735
+ var isNodeWhereBufferBreaks = isNode && semver . satisfies ( process . version , '< 3' ) ;
736
+
737
+ test ( 'TypedArrays' , { skip : ! hasTypedArrays } , function ( t ) {
738
+ t . test ( 'Buffer faked as Uint8Array' , { skip : typeof Buffer !== 'function' || ! Object . create || ! hasDunderProto } , function ( st ) {
739
+ var a = safeBuffer ( 'test' ) ;
740
+ var b = tag ( Object . create (
741
+ a . __proto__ , // eslint-disable-line no-proto
742
+ assign ( gOPDs ( a ) , {
743
+ length : {
744
+ enumerable : false ,
745
+ value : 4
746
+ }
747
+ } )
748
+ ) , 'Uint8Array' ) ;
749
+
750
+ st . deepEqualTest (
751
+ a ,
752
+ b ,
753
+ 'Buffer and Uint8Array' ,
754
+ isNode0x ,
755
+ isNode0x
756
+ ) ;
757
+
758
+ st . end ( ) ;
759
+ } ) ;
760
+
761
+ t . test ( 'one TypedArray faking as another' , { skip : ! hasDunderProto } , function ( st ) {
762
+ /* globals Uint8Array, Int8Array */
763
+ var a = new Uint8Array ( 10 ) ;
764
+ var b = tag ( new Int8Array ( 10 ) , 'Uint8Array' ) ;
765
+ b . __proto__ = Uint8Array . prototype ; // eslint-disable-line no-proto
766
+
767
+ st . deepEqualTest (
768
+ a ,
769
+ b ,
770
+ 'Uint8Array, and Int8Array pretending to be a Uint8Array' ,
771
+ false ,
772
+ false
773
+ ) ;
774
+
775
+ st . end ( ) ;
776
+ } ) ;
777
+
778
+ t . end ( ) ;
779
+ } ) ;
0 commit comments