1- import { Argument , AllArguments } from './Arguments' ;
2- import * as util from 'util' ;
3-
4- export type Call = any [ ] // list of args
1+ import { inspect } from 'util'
2+ import { RecordedArguments } from './RecordedArguments'
53
64export enum PropertyType {
7- method = 'method' ,
8- property = 'property'
9- }
10-
11- export enum SubstituteMethod {
12- received = 'received' ,
13- didNotReceive = 'didNotReceive' ,
14- mimicks = 'mimicks' ,
15- throws = 'throws' ,
16- returns = 'returns' ,
17- resolves = 'resolves' ,
18- rejects = 'rejects'
19- }
20-
21- export function isSubstituteMethod ( property : PropertyKey ) : property is SubstituteMethod {
22- return property === SubstituteMethod . returns ||
23- property === SubstituteMethod . mimicks ||
24- property === SubstituteMethod . throws ||
25- property === SubstituteMethod . resolves ||
26- property === SubstituteMethod . rejects ;
27- }
28-
29- const seenObject = Symbol ( ) ;
30-
31- export function stringifyArguments ( args : any [ ] ) {
32- args = args . map ( x => util . inspect ( x ) ) ;
33- return args && args . length > 0 ? 'arguments [' + args . join ( ', ' ) + ']' : 'no arguments' ;
34- } ;
35-
36- export function areArgumentArraysEqual ( a : any [ ] , b : any [ ] ) {
37- if ( a . find ( x => x instanceof AllArguments ) || b . find ( b => b instanceof AllArguments ) ) {
38- return true ;
39- }
40-
41- for ( let i = 0 ; i < Math . max ( b . length , a . length ) ; i ++ ) {
42- if ( ! areArgumentsEqual ( b [ i ] , a [ i ] ) ) {
43- return false ;
44- }
45- }
46-
47- return true ;
5+ method = 'method' ,
6+ property = 'property'
487}
498
50- export function stringifyCalls ( calls : Call [ ] ) {
51-
52- if ( calls . length === 0 )
53- return ' (no calls)' ;
54-
55- let output = '' ;
56- for ( let call of calls ) {
57- output += '\n-> call with ' + ( call . length ? stringifyArguments ( call ) : '(no arguments)' )
58- }
59-
60- return output ;
61- } ;
62-
63- export function areArgumentsEqual ( a : any , b : any ) {
64-
65- if ( a instanceof Argument && b instanceof Argument )
66- return false ;
9+ export type AssertionMethod = 'received' | 'didNotReceive'
6710
68- if ( a instanceof AllArguments || b instanceof AllArguments )
69- return true ;
11+ export const isAssertionMethod = ( property : PropertyKey ) : property is AssertionMethod =>
12+ property === 'received' || property === 'didNotReceive'
7013
71- if ( a instanceof Argument )
72- return a . matches ( b ) ;
14+ export type SubstitutionMethod = 'mimicks' | 'throws' | 'returns' | 'resolves' | 'rejects'
7315
74- if ( b instanceof Argument )
75- return b . matches ( a ) ;
16+ export const isSubstitutionMethod = ( property : PropertyKey ) : property is SubstitutionMethod =>
17+ property === 'mimicks' || property === 'returns' || property === 'throws' || property === 'resolves' || property === 'rejects'
7618
77- return deepEqual ( a , b ) ;
78- } ;
19+ export const stringifyArguments = ( args : RecordedArguments ) => textModifier . faint (
20+ args . hasNoArguments
21+ ? 'no arguments'
22+ : `arguments [${ args . value . map ( x => inspect ( x , { colors : true } ) ) . join ( ', ' ) } ]`
23+ )
7924
80- function deepEqual ( realA : any , realB : any , objectReferences : object [ ] = [ ] ) : boolean {
81- const a = objectReferences . includes ( realA ) ? seenObject : realA ;
82- const b = objectReferences . includes ( realB ) ? seenObject : realB ;
83- const newObjectReferences = updateObjectReferences ( objectReferences , a , b ) ;
25+ export const stringifyCalls = ( calls : RecordedArguments [ ] ) => {
26+ if ( calls . length === 0 ) return ' (no calls)'
8427
85- if ( nonNullObject ( a ) && nonNullObject ( b ) ) {
86- if ( a . constructor !== b . constructor ) return false ;
87- const objectAKeys = Object . keys ( a ) ;
88- if ( objectAKeys . length !== Object . keys ( b ) . length ) return false ;
89- for ( const key of objectAKeys ) {
90- if ( ! deepEqual ( a [ key ] , b [ key ] , newObjectReferences ) ) return false ;
91- }
92- return true ;
93- }
94- return a === b ;
28+ const key = '\n-> call with '
29+ const callsDetails = calls . map ( stringifyArguments )
30+ return `${ key } ${ callsDetails . join ( key ) } `
9531}
9632
97- function updateObjectReferences ( objectReferences : Array < object > , a : any , b : any ) {
98- const tempObjectReferences = [ ...objectReferences , nonNullObject ( a ) && ! objectReferences . includes ( a ) ? a : void 0 ] ;
99- return [ ...tempObjectReferences , nonNullObject ( b ) && ! tempObjectReferences . includes ( b ) ? b : void 0 ] ;
33+ const baseTextModifier = ( str : string , modifierStart : number , modifierEnd : number ) => `\x1b[${ modifierStart } m${ str } \x1b[${ modifierEnd } m`
34+ export const textModifier = {
35+ bold : ( str : string ) => baseTextModifier ( str , 1 , 22 ) ,
36+ faint : ( str : string ) => baseTextModifier ( str , 2 , 22 ) ,
37+ italic : ( str : string ) => baseTextModifier ( str , 3 , 23 )
10038}
10139
102- function nonNullObject ( value : any ) : value is { [ key : string ] : any } {
103- return typeof value === 'object' && value !== null ;
104- }
40+ export const plurify = ( str : string , count : number ) => `${ str } ${ count === 1 ? '' : 's' } `
0 commit comments