2024-01-01 21:11:32 +00:00
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2023-12-08 09:06:27 +00:00
import { AssertionError , assertObjectMatch , assertThrows } from "./mod.ts" ;
2023-07-13 07:04:30 +00:00
2023-12-08 09:06:27 +00:00
const sym = Symbol ( "foo" ) ;
const a = { foo : true , bar : false } ;
const b = { . . . a , baz : a } ;
const c = { . . . b , qux : b } ;
const d = { corge : c , grault : c } ;
const e = { foo : true } as { [ key : string ] : unknown } ;
e . bar = e ;
const f = { [ sym ] : true , bar : false } ;
interface r {
foo : boolean ;
bar : boolean ;
}
const g : r = { foo : true , bar : false } ;
const h = { foo : [ 1 , 2 , 3 ] , bar : true } ;
const i = { foo : [ a , e ] , bar : true } ;
const j = { foo : [ [ 1 , 2 , 3 ] ] , bar : true } ;
const k = { foo : [ [ 1 , [ 2 , [ 3 ] ] ] ] , bar : true } ;
const l = { foo : [ [ 1 , [ 2 , [ a , e , j , k ] ] ] ] , bar : true } ;
const m = { foo : /abc+/i , bar : [ /abc/g , /abc/m ] } ;
const n = {
foo : new Set ( [ "foo" , "bar" ] ) ,
bar : new Map ( [
[ "foo" , 1 ] ,
[ "bar" , 2 ] ,
] ) ,
baz : new Map ( [
[ "a" , a ] ,
[ "b" , b ] ,
] ) ,
} ;
2023-07-13 07:04:30 +00:00
2023-12-08 09:06:27 +00:00
Deno . test ( "assertObjectMatch() matches simple subset" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( a , {
foo : true ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with another subset" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( b , {
foo : true ,
baz : { bar : false } ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with multiple subsets" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( c , {
foo : true ,
baz : { bar : false } ,
qux : {
baz : { foo : true } ,
} ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with same object reference as subset" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( d , {
corge : {
foo : true ,
qux : { bar : false } ,
} ,
grault : {
bar : false ,
qux : { foo : true } ,
} ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with circular reference" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( e , {
foo : true ,
bar : {
bar : {
bar : {
foo : true ,
} ,
} ,
} ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with interface" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( g , { bar : false } ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with same symbol" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( f , {
[ sym ] : true ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with array inside" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( h , { foo : [ ] } ) ;
assertObjectMatch ( h , { foo : [ 1 , 2 ] } ) ;
assertObjectMatch ( h , { foo : [ 1 , 2 , 3 ] } ) ;
assertObjectMatch ( i , { foo : [ { bar : false } ] } ) ;
assertObjectMatch ( i , {
foo : [ { bar : false } , { bar : { bar : { bar : { foo : true } } } } ] ,
} ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with nested array inside" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( j , { foo : [ [ 1 , 2 , 3 ] ] } ) ;
assertObjectMatch ( k , { foo : [ [ 1 , [ 2 , [ 3 ] ] ] ] } ) ;
assertObjectMatch ( l , { foo : [ [ 1 , [ 2 , [ a , e , j , k ] ] ] ] } ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with regexp" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( m , { foo : /abc+/i } ) ;
assertObjectMatch ( m , { bar : [ /abc/g , /abc/m ] } ) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() matches subset with built-in data structures" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertObjectMatch ( n , { foo : new Set ( [ "foo" ] ) } ) ;
assertObjectMatch ( n , { bar : new Map ( [ [ "bar" , 2 ] ] ) } ) ;
assertObjectMatch ( n , { baz : new Map ( [ [ "b" , b ] ] ) } ) ;
assertObjectMatch ( n , { baz : new Map ( [ [ "b" , { foo : true } ] ] ) } ) ;
2023-12-08 09:06:27 +00:00
} ) ;
2023-07-13 07:04:30 +00:00
2023-12-08 09:06:27 +00:00
Deno . test ( "assertObjectMatch() throws when a key is missing from subset" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch (
{
foo : true ,
} ,
{
foo : true ,
bar : false ,
} ,
2023-12-08 09:06:27 +00:00
) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when simple subset mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( a , {
foo : false ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with another subset mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( b , {
foo : true ,
baz : { bar : true } ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with multiple subsets mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( c , {
foo : true ,
baz : { bar : false } ,
qux : {
baz : { foo : false } ,
} ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with same object reference as subset mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( d , {
corge : {
foo : true ,
qux : { bar : true } ,
} ,
grault : {
bar : false ,
qux : { foo : false } ,
} ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with circular reference mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( e , {
foo : true ,
bar : {
bar : {
bar : {
foo : false ,
} ,
} ,
} ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with symbol key mismatches other string key" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( f , {
2023-12-08 09:06:27 +00:00
[ sym ] : false ,
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when subset with array inside mismatches" , ( ) = > {
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( i , {
foo : [ 1 , 2 , 3 , 4 ] ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
assertThrows (
( ) = >
2023-07-13 07:04:30 +00:00
assertObjectMatch ( i , {
foo : [ { bar : true } , { foo : false } ] ,
2023-12-08 09:06:27 +00:00
} ) ,
AssertionError ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() matches when actual/expected value as instance of class mismatches" , ( ) = > {
class A {
a : number ;
constructor ( a : number ) {
this . a = a ;
2023-07-13 07:04:30 +00:00
}
}
2023-12-08 09:06:27 +00:00
assertObjectMatch ( { test : new A ( 1 ) } , { test : { a : 1 } } ) ;
assertObjectMatch ( { test : { a : 1 } } , { test : { a : 1 } } ) ;
assertObjectMatch ( { test : { a : 1 } } , { test : new A ( 1 ) } ) ;
assertObjectMatch ( { test : new A ( 1 ) } , { test : new A ( 1 ) } ) ;
} ) ;
Deno . test ( "assertObjectMatch() matches when actual/expected contains same instance of Map/TypedArray/etc" , ( ) = > {
const body = new Uint8Array ( [ 0 , 1 , 2 ] ) ;
assertObjectMatch ( { body , foo : "foo" } , { body } ) ;
} ) ;
Deno . test ( "assertObjectMatch() matches subsets of arrays" , ( ) = > {
assertObjectMatch (
{ positions : [ [ 1 , 2 , 3 , 4 ] ] } ,
{
positions : [ [ 1 , 2 , 3 ] ] ,
} ,
) ;
} ) ;
Deno . test ( "assertObjectMatch() throws when regexp mismatches" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertThrows ( ( ) = > assertObjectMatch ( m , { foo : /abc+/ } ) , AssertionError ) ;
assertThrows ( ( ) = > assertObjectMatch ( m , { foo : /abc*/i } ) , AssertionError ) ;
assertThrows (
( ) = > assertObjectMatch ( m , { bar : [ /abc/m , /abc/g ] } ) ,
AssertionError ,
) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() throws when built-in data structures mismatches" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertThrows (
( ) = > assertObjectMatch ( n , { foo : new Set ( [ "baz" ] ) } ) ,
AssertionError ,
) ;
assertThrows (
( ) = > assertObjectMatch ( n , { bar : new Map ( [ [ "bar" , 3 ] ] ) } ) ,
AssertionError ,
) ;
assertThrows (
( ) = > assertObjectMatch ( n , { baz : new Map ( [ [ "a" , { baz : true } ] ] ) } ) ,
AssertionError ,
) ;
2023-12-08 09:06:27 +00:00
} ) ;
Deno . test ( "assertObjectMatch() throws assertion error when in the first argument mismatches, rather than a TypeError: Invalid value used as weak map key" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertThrows (
( ) = > assertObjectMatch ( { foo : null } , { foo : { bar : 42 } } ) ,
AssertionError ,
) ;
assertObjectMatch ( { foo : null , bar : null } , { foo : null } ) ;
assertObjectMatch ( { foo : undefined , bar : null } , { foo : undefined } ) ;
assertThrows (
( ) = > assertObjectMatch ( { foo : undefined , bar : null } , { foo : null } ) ,
AssertionError ,
) ;
2023-12-08 09:06:27 +00:00
} ) ;
2024-05-07 00:08:16 +00:00
Deno . test ( "assertObjectMatch() throws readable type error for non mappable primitive types" , ( ) = > {
2023-07-13 07:04:30 +00:00
assertThrows (
// @ts-expect-error Argument of type 'null' is not assignable to parameter of type 'Record<PropertyKey, any>'
( ) = > assertObjectMatch ( null , { foo : 42 } ) ,
TypeError ,
"assertObjectMatch" ,
) ;
// @ts-expect-error Argument of type 'null' is not assignable to parameter of type 'Record<PropertyKey, any>'
assertThrows ( ( ) = > assertObjectMatch ( null , { foo : 42 } ) , TypeError , "null" ) ; // since typeof null is "object", want to make sure user knows the bad value is "null"
assertThrows (
// @ts-expect-error Argument of type 'undefined' is not assignable to parameter of type 'Record<PropertyKey, any>'
( ) = > assertObjectMatch ( undefined , { foo : 42 } ) ,
TypeError ,
"assertObjectMatch" ,
) ;
// @ts-expect-error Argument of type 'number' is not assignable to parameter of type 'Record<PropertyKey, any>'
assertThrows ( ( ) = > assertObjectMatch ( 21 , 42 ) , TypeError , "assertObjectMatch" ) ;
assertThrows (
// @ts-expect-error Argument of type 'string' is not assignable to parameter of type 'Record<PropertyKey, any>'
( ) = > assertObjectMatch ( "string" , "string" ) ,
TypeError ,
"assertObjectMatch" ,
) ;
} ) ;