1+ import test from 'ava'
2+ import { inspect } from 'util'
3+
4+ import { Arg } from '../src'
5+ import { RecordedArguments } from '../src/RecordedArguments'
6+
7+ const testObject = { 'foo' : 'bar' }
8+ const testArray = [ 'a' , 1 , true ]
9+
10+ // #90: Infinite recursion in deepEqual https://github.com/ffMathy/FluffySpoon.JavaScript.Testing.Faking/blob/master/spec/issues/90.test.ts
11+ const parent = { } as any
12+ parent . child = parent
13+ const root = { } as any
14+ root . path = { to : { nested : root } }
15+
16+ const testFunc = ( ) => { }
17+ const testSymbol = Symbol ( )
18+
19+ test ( 'records values and classifies them correctly' , t => {
20+ const emptyArguments = RecordedArguments . from ( [ ] )
21+ t . deepEqual ( emptyArguments . value , [ ] )
22+ t . is ( emptyArguments . argumentsClass , 'plain' )
23+ t . is ( emptyArguments . hasNoArguments , false )
24+
25+ const primitivesOnlyArguments = RecordedArguments . from ( [ 1 , 'Substitute' , false , testSymbol , undefined , null , testFunc , { } ] )
26+ t . deepEqual ( primitivesOnlyArguments . value , [ 1 , 'Substitute' , false , testSymbol , undefined , null , testFunc , { } ] )
27+ t . is ( primitivesOnlyArguments . argumentsClass , 'plain' )
28+ t . is ( primitivesOnlyArguments . hasNoArguments , false )
29+
30+ const anyArg = Arg . any ( 'any' )
31+ const withSingleArgumentArguments = RecordedArguments . from ( [ 1 , 'Substitute' , false , testSymbol , undefined , null , testFunc , { } , anyArg ] )
32+ t . deepEqual ( withSingleArgumentArguments . value , [ 1 , 'Substitute' , false , testSymbol , undefined , null , testFunc , { } , anyArg ] )
33+ t . is ( withSingleArgumentArguments . argumentsClass , 'with-predicate' )
34+ t . is ( withSingleArgumentArguments . hasNoArguments , false )
35+
36+ const allArg = Arg . all ( )
37+ const allArgumentArguments = RecordedArguments . from ( [ allArg ] )
38+ t . deepEqual ( allArgumentArguments . value , [ allArg ] )
39+ t . is ( allArgumentArguments . argumentsClass , 'wildcard' )
40+ t . is ( allArgumentArguments . hasNoArguments , false )
41+ } )
42+
43+ test ( 'creates a valid instance for no arguments' , t => {
44+ const args = RecordedArguments . none ( )
45+
46+ t . is ( args . value , undefined )
47+ t . is ( args . argumentsClass , undefined )
48+ t . is ( args . hasNoArguments , true )
49+ } )
50+
51+ test ( 'sorts correctly objects with RecordedArguments' , t => {
52+ const plain1 = RecordedArguments . from ( [ ] )
53+ const plain2 = RecordedArguments . from ( [ 1 , 2 ] )
54+ const withPredicate1 = RecordedArguments . from ( [ 1 , Arg . any ( ) ] )
55+ const withPredicate2 = RecordedArguments . from ( [ Arg . any ( ) ] )
56+ const wildcard1 = RecordedArguments . from ( [ Arg . all ( ) ] )
57+ const wildcard2 = RecordedArguments . from ( [ Arg . all ( ) ] )
58+
59+ const wrapper = ( recordedArguments : RecordedArguments [ ] ) => recordedArguments . map ( args => ( { recordedArguments : args } ) )
60+ const sortedArgs1 = RecordedArguments . sort ( wrapper ( [ wildcard1 , wildcard2 , withPredicate1 , withPredicate2 , plain1 , plain2 ] ) )
61+ const sortedArgs2 = RecordedArguments . sort ( wrapper ( [ wildcard1 , withPredicate1 , plain1 , withPredicate2 , wildcard2 , plain2 ] ) )
62+
63+ t . deepEqual ( sortedArgs1 , wrapper ( [ plain1 , plain2 , withPredicate1 , withPredicate2 , wildcard1 , wildcard2 ] ) )
64+ t . deepEqual ( sortedArgs2 , wrapper ( [ plain1 , plain2 , withPredicate1 , withPredicate2 , wildcard1 , wildcard2 ] ) )
65+ } )
66+
67+ test ( 'matches correctly with another RecordedArguments instance when none arguments are recorded' , t => {
68+ const args = RecordedArguments . none ( )
69+
70+ t . true ( args . match ( args ) )
71+ t . true ( args . match ( RecordedArguments . none ( ) ) )
72+
73+ t . false ( args . match ( RecordedArguments . from ( [ ] ) ) )
74+ t . false ( RecordedArguments . from ( [ ] ) . match ( args ) )
75+ t . false ( args . match ( RecordedArguments . from ( [ undefined ] ) ) )
76+ } )
77+
78+ test ( 'matches correctly with another RecordedArguments instance when primitive arguments are recorded' , t => {
79+ // single
80+ t . true ( RecordedArguments . from ( [ ] ) . match ( RecordedArguments . from ( [ ] ) ) )
81+ t . true ( RecordedArguments . from ( [ 'Substitute' ] ) . match ( RecordedArguments . from ( [ 'Substitute' ] ) ) )
82+ t . true ( RecordedArguments . from ( [ 0 ] ) . match ( RecordedArguments . from ( [ 0 ] ) ) )
83+ t . true ( RecordedArguments . from ( [ true ] ) . match ( RecordedArguments . from ( [ true ] ) ) )
84+ t . true ( RecordedArguments . from ( [ false ] ) . match ( RecordedArguments . from ( [ false ] ) ) )
85+ t . true ( RecordedArguments . from ( [ undefined ] ) . match ( RecordedArguments . from ( [ undefined ] ) ) )
86+ t . true ( RecordedArguments . from ( [ null ] ) . match ( RecordedArguments . from ( [ null ] ) ) )
87+ t . true ( RecordedArguments . from ( [ Symbol . for ( 'test' ) ] ) . match ( RecordedArguments . from ( [ Symbol . for ( 'test' ) ] ) ) )
88+
89+ t . false ( RecordedArguments . from ( [ 'a' ] ) . match ( RecordedArguments . from ( [ 'b' ] ) ) )
90+ t . false ( RecordedArguments . from ( [ 1 ] ) . match ( RecordedArguments . from ( [ 2 ] ) ) )
91+ t . false ( RecordedArguments . from ( [ true ] ) . match ( RecordedArguments . from ( [ false ] ) ) )
92+ t . false ( RecordedArguments . from ( [ undefined ] ) . match ( RecordedArguments . from ( [ null ] ) ) )
93+ t . false ( RecordedArguments . from ( [ '1' ] ) . match ( RecordedArguments . from ( [ 1 ] ) ) )
94+
95+ // multi
96+ t . true ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) . match ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) ) )
97+
98+ t . false ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) . match ( RecordedArguments . from ( [ 3 , 2 , 1 ] ) ) )
99+ t . false ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) . match ( RecordedArguments . from ( [ 1 , 2 , 3 , 4 ] ) ) )
100+ t . false ( RecordedArguments . from ( [ 1 , 2 , 3 , 4 ] ) . match ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) ) )
101+ } )
102+
103+ test ( 'matches correctly with another RecordedArguments instance when object arguments are recorded' , t => {
104+ // same reference
105+ t . true ( RecordedArguments . from ( [ testObject ] ) . match ( RecordedArguments . from ( [ testObject ] ) ) )
106+ t . true ( RecordedArguments . from ( [ testArray ] ) . match ( RecordedArguments . from ( [ testArray ] ) ) )
107+ t . true ( RecordedArguments . from ( [ testFunc ] ) . match ( RecordedArguments . from ( [ testFunc ] ) ) )
108+ t . true ( RecordedArguments . from ( [ parent ] ) . match ( RecordedArguments . from ( [ parent ] ) ) )
109+ t . true ( RecordedArguments . from ( [ root ] ) . match ( RecordedArguments . from ( [ root ] ) ) )
110+
111+ // deep equal
112+ const objectWithSelfReference = { a : 1 , b : 2 } as any
113+ objectWithSelfReference . c = objectWithSelfReference
114+ const anotherObjectWithSelfReference = { a : 1 , b : 2 } as any
115+ anotherObjectWithSelfReference . c = anotherObjectWithSelfReference
116+
117+ t . true ( RecordedArguments . from ( [ { a : 1 } ] ) . match ( RecordedArguments . from ( [ { a : 1 } ] ) ) )
118+ t . true ( RecordedArguments . from ( [ [ ] ] ) . match ( RecordedArguments . from ( [ [ ] ] ) ) )
119+ t . true ( RecordedArguments . from ( [ [ 1 , 'a' ] ] ) . match ( RecordedArguments . from ( [ [ 1 , 'a' ] ] ) ) )
120+ t . true ( RecordedArguments . from ( [ objectWithSelfReference ] ) . match ( RecordedArguments . from ( [ anotherObjectWithSelfReference ] ) ) )
121+ } )
122+
123+ test ( 'matches correctly with another RecordedArguments instance when using a wildcard argument' , t => {
124+ t . true ( RecordedArguments . from ( [ Arg . all ( ) ] ) . match ( RecordedArguments . from ( [ 1 , 2 , 3 ] ) ) )
125+ t . true ( RecordedArguments . from ( [ 'Substitute' , 'JS' ] ) . match ( RecordedArguments . from ( [ Arg . all ( ) ] ) ) )
126+ } )
127+
128+ test ( 'matches correctly with another RecordedArguments instance when using predicate arguments' , t => {
129+ t . true ( RecordedArguments . from ( [ Arg . any ( ) , Arg . any ( 'number' ) , Arg . is ( ( x : number ) => x === 3 ) , 4 ] ) . match ( RecordedArguments . from ( [ 1 , 2 , 3 , 4 ] ) ) )
130+ t . true ( RecordedArguments . from ( [ 'Substitute' , 'JS' ] ) . match ( RecordedArguments . from ( [ Arg . is ( x => typeof x === 'string' ) , Arg . any ( 'string' ) ] ) ) )
131+ } )
132+
133+ test ( 'generates custom text representation' , t => {
134+ t . is ( inspect ( RecordedArguments . none ( ) ) , '' )
135+ t . is ( inspect ( RecordedArguments . from ( [ ] ) ) , '()' )
136+ t . is ( inspect ( RecordedArguments . from ( [ undefined ] ) ) , 'undefined' )
137+ t . is ( inspect ( RecordedArguments . from ( [ undefined , 1 ] ) ) , '(undefined, 1)' )
138+ } )
0 commit comments