'use strict' ; import { diffJson } from 'diff' ; import { inspect } from 'util' ; import { WatchableFunctionLogic } from '../logic/watchable-function-logic' ; import { convertStructToPojo , humanizeTimes , isStruct } from '../utils' ; import * as color from './color' ; function sinonFormat ( ... args ) { return ( inspect as any ) ( ... args ) ; } function colorSinonMatchText ( matcher , calledArg , calledArgMessage ) { var calledArgumentMessage = calledArgMessage ; if ( ! matcher . test ( calledArg ) ) { matcher . message = color . red ( matcher . message ) ; if ( calledArgumentMessage ) { calledArgumentMessage = color . green ( calledArgumentMessage ) ; } } return `${ calledArgumentMessage } ${ matcher . message }` ; } function colorDiffText ( diff ) { var objects = diff . map ( ( part ) => { var text = part . value ; if ( part . added ) { text = color . green ( text ) ; } else if ( part . removed ) { text = color . red ( text ) ; } if ( diff . length === 2 ) { text += ' ' ; } return text ; } ) ; return objects . join ( '' ) ; } function quoteStringValue ( value ) { if ( typeof value === 'string' ) { return JSON . stringify ( value ) ; } return value ; } export const formatters = { c : ( watchableContract ) => { return humanizeTimes ( watchableContract . getCallCount ( ) ) ; } , n : ( watchableContract ) => { return watchableContract . getName ( ) ; } , D : ( watchableContract , args ) => { var message = '' ; for ( var i = 0 , l = watchableContract . getCallCount ( ) ; i < l ; ++ i ) { if ( l > 1 ) { message += `\nCall ${ i + 1 }:` ; } var calledArgs = watchableContract . getCall ( i ) . args ; var expectedArgs = args . slice ( ) ; for ( var j = 0 ; j < calledArgs . length || j < expectedArgs . length ; ++ j ) { let parsedCalledArgs = calledArgs [ j ] ; let parsedExpectedArgs = expectedArgs [ j ] ; if ( parsedCalledArgs ) { if ( isStruct ( parsedCalledArgs ) ) { parsedCalledArgs = convertStructToPojo ( parsedCalledArgs ) ; } parsedCalledArgs = quoteStringValue ( parsedCalledArgs ) ; } if ( parsedExpectedArgs ) { parsedExpectedArgs = quoteStringValue ( parsedExpectedArgs ) ; } message += '\n' ; var calledArgMessage = j < calledArgs . length ? sinonFormat ( parsedCalledArgs ) : '' ; var expectedArgMessage = j < expectedArgs . length ? sinonFormat ( parsedExpectedArgs ) : '' ; var diff = diffJson ( calledArgMessage , expectedArgMessage ) ; message += colorDiffText ( diff ) ; } } return message ; } , C : ( watchableContract ) => { let calls = [ ] ; for ( var i = 0 , l = watchableContract . getCallCount ( ) ; i < l ; ++ i ) { var stringifiedCall = `    ` ; if ( /\n/ . test ( calls [ i - 1 ] ) ) { stringifiedCall = `\n${ stringifiedCall }` ; } calls . push ( stringifiedCall ) ; } return calls . length > 0 ? `\n${ calls . join ( '\n' ) }` : '' ; } , t : ( watchableContract ) => { let objects = [ ] ; for ( var i = 0 , l = watchableContract . getCallCount ( ) ; i < l ; ++ i ) { } return objects . join ( ', ' ) ; } , '*' : ( watchableContract , args ) => { return args . map ( ( arg ) => sinonFormat ( arg ) ) . join ( ', ' ) ; } , } ;