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
+
8
+ var safeBuffer = Buffer . from && Buffer . from . length > 1 ? Buffer . from : Buffer ;
5
9
6
10
function tag ( obj , value ) {
7
11
if ( hasSymbols && Symbol . toStringTags && Object . defineProperty ) {
@@ -220,11 +224,28 @@ test('Dates', function (t) {
220
224
st . end ( ) ;
221
225
} ) ;
222
226
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
+
223
245
t . end ( ) ;
224
246
} ) ;
225
247
226
248
test ( 'buffers' , { skip : typeof Buffer !== 'function' } , function ( t ) {
227
- var safeBuffer = Buffer . from && Buffer . from . length > 1 ? Buffer . from : Buffer ;
228
249
/* eslint no-buffer-constructor: 1, new-cap: 1 */
229
250
t . deepEqualTest (
230
251
safeBuffer ( 'xyz' ) ,
@@ -473,6 +494,18 @@ test('regexen', function (t) {
473
494
t . deepEqualTest ( / a b c / , / a b c / , 'two abc regexes' , true , true , false ) ;
474
495
t . deepEqualTest ( / x y z / , / x y z / , 'two xyz regexes' , true , true , false ) ;
475
496
497
+ t . test ( 'fake RegExp' , { skip : ! hasDunderProto } , function ( st ) {
498
+ var a = / a b c / 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
+
476
509
t . end ( ) ;
477
510
} ) ;
478
511
@@ -506,6 +539,23 @@ test('Errors', function (t) {
506
539
false
507
540
) ;
508
541
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
+
509
559
t . end ( ) ;
510
560
} ) ;
511
561
@@ -525,22 +575,23 @@ test('error = Object', function (t) {
525
575
t . end ( ) ;
526
576
} ) ;
527
577
528
- test ( '[[Prototypes]]' , { skip : ! Object . getPrototypeOf } , function ( t ) {
578
+ test ( '[[Prototypes]]' , function ( t ) {
529
579
function C ( ) { }
530
580
var instance = new C ( ) ;
531
581
delete instance . constructor ;
532
582
533
583
t . deepEqualTest ( { } , instance , 'two identical objects with different [[Prototypes]]' , true , false ) ;
534
584
535
- t . test ( 'Dates with different prototypes' , { skip : ! Object . setPrototypeOf } , function ( st ) {
585
+ t . test ( 'Dates with different prototypes' , { skip : ! hasDunderProto } , function ( st ) {
536
586
var d1 = new Date ( 0 ) ;
537
587
var d2 = new Date ( 0 ) ;
538
588
539
589
t . deepEqualTest ( d1 , d2 , 'two dates with the same timestamp' , true , true ) ;
540
590
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
544
595
st . ok ( d2 instanceof Date , 'd2 is still a Date instance after tweaking [[Prototype]]' ) ;
545
596
546
597
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
629
680
}
630
681
631
682
t . deepEqualTest ( a , [ 1 , 1 ] , 'fake and real array with same contents and [[Prototype]]' , false , false ) ;
683
+
684
+ var b = tag ( / a b c / , '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
+
632
694
t . end ( ) ;
633
695
} ) ;
634
696
@@ -665,3 +727,47 @@ test('circular references', function (t) {
665
727
666
728
t . end ( ) ;
667
729
} ) ;
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