@@ -3,17 +3,24 @@ import { Context } from "src/Context";
33import { areArgumentArraysEqual , Call , Type } from "../Utilities" ;
44import { GetPropertyState } from "./GetPropertyState" ;
55
6- const Nothing = Symbol ( )
7-
86interface ReturnMock {
97 args : Call
108 returnValues : any [ ] | Symbol // why symbol, what
119 returnIndex : 0
1210}
11+ interface MimickMock {
12+ args : Call
13+ mimickFunction : Function
14+ }
15+ interface ThrowMock {
16+ args : Call
17+ throwFunction : any
18+ }
1319
1420export class FunctionState implements ContextState {
1521 private returns : ReturnMock [ ] ;
16- private mimicks : Function | null ;
22+ private mimicks : MimickMock [ ] ;
23+ private throws : ThrowMock [ ] ;
1724
1825 private _calls : Call [ ] ; // list of lists of arguments this was called with
1926 private _lastArgs ?: Call // bit of a hack
@@ -32,8 +39,9 @@ export class FunctionState implements ContextState {
3239
3340 constructor ( private _getPropertyState : GetPropertyState ) {
3441 this . returns = [ ] ;
35- this . mimicks = null ;
42+ this . mimicks = [ ] ;
3643 this . _calls = [ ] ;
44+ this . throws = [ ] ;
3745 }
3846
3947 private getCallCount ( args : Call ) : number {
@@ -51,15 +59,22 @@ export class FunctionState implements ContextState {
5159 this . property ,
5260 args ) ;
5361
54- if ( ! hasExpectations ) {
62+ if ( ! hasExpectations ) {
5563 this . _calls . push ( args )
5664 }
5765
5866 if ( ! hasExpectations ) {
59- if ( this . mimicks )
60- return this . mimicks . apply ( this . mimicks , args ) ;
67+ if ( this . mimicks . length > 0 ) {
68+ const mimicks = this . mimicks . find ( mimick => areArgumentArraysEqual ( mimick . args , args ) )
69+ if ( mimicks !== void 0 ) return mimicks . mimickFunction . apply ( mimicks . mimickFunction , args ) ;
70+ }
71+
72+ if ( this . throws . length > 0 ) {
73+ const possibleThrow = this . throws . find ( throws => areArgumentArraysEqual ( throws . args , args ) )
74+ if ( possibleThrow !== void 0 ) throw possibleThrow . throwFunction ;
75+ }
6176
62- if ( ! this . returns . length )
77+ if ( ! this . returns . length )
6378 return context . proxy ;
6479 const returns = this . returns . find ( r => areArgumentArraysEqual ( r . args , args ) )
6580
@@ -85,16 +100,36 @@ export class FunctionState implements ContextState {
85100 if ( property === 'then' )
86101 return void 0 ;
87102
88- if ( property === 'mimicks' ) {
103+ if ( property === 'mimicks' ) {
89104 return ( input : Function ) => {
90- this . mimicks = input ;
105+ if ( ! this . _lastArgs ) {
106+ throw new Error ( 'Eh, there\'s a bug, no args recorded for this mimicks :/' )
107+ }
108+ this . mimicks . push ( {
109+ args : this . _lastArgs ,
110+ mimickFunction : input
111+ } )
91112 this . _calls . pop ( )
92113
93114 context . state = context . initialState ;
94115 }
95116 }
96117
97- if ( property === 'returns' ) {
118+ if ( property === 'throws' ) {
119+ return ( input : Error | Function ) => {
120+ if ( ! this . _lastArgs ) {
121+ throw new Error ( 'Eh, there\'s a bug, no args recorded for this throw :/' )
122+ }
123+ this . throws . push ( {
124+ args : this . _lastArgs ,
125+ throwFunction : input
126+ } ) ;
127+ this . _calls . pop ( ) ;
128+ context . state = context . initialState ;
129+ }
130+ }
131+
132+ if ( property === 'returns' ) {
98133 return ( ...returns : any [ ] ) => {
99134 if ( ! this . _lastArgs ) {
100135 throw new Error ( 'Eh, there\'s a bug, no args recorded for this return :/' )
@@ -106,7 +141,7 @@ export class FunctionState implements ContextState {
106141 } )
107142 this . _calls . pop ( )
108143
109- if ( this . callCount === 0 ) {
144+ if ( this . callCount === 0 ) {
110145 // var indexOfSelf = this
111146 // ._getPropertyState
112147 // .recordedFunctionStates
0 commit comments