@@ -9,12 +9,29 @@ export class WalletValueSteps {
99
1010 wallet : Wallet = new Wallet ( )
1111 answers : number [ ] = [ ]
12+ dates : string [ ] = [ ]
13+ referenceRatesAtDate : Map < string , Map < string , Map < string , number > > > = new Map ( )
14+ referenceRateWalletValue : number = 0
1215
1316 @given ( 'I have a wallet' )
1417 public setNewWallet ( ) {
1518 this . wallet = new Wallet ( )
1619 }
1720
21+ @given ( 'the rates at the time of {string} are' )
22+ public setRatesToMap ( date : string , setRatesForDate : { rawTable : Array < Array < any > > } ) {
23+ let referenceRates = new Map < string , Map < string , number > > ( )
24+ setRatesForDate . rawTable . forEach ( ( rate : Array < string > ) => {
25+ if ( referenceRates . has ( rate [ 0 ] ) ) {
26+ referenceRates . get ( rate [ 0 ] ) ?. set ( rate [ 1 ] , parseFloat ( rate [ 2 ] ) )
27+ } else {
28+ referenceRates . set ( rate [ 0 ] , new Map ( ) )
29+ referenceRates . get ( rate [ 0 ] ) ?. set ( rate [ 1 ] , parseFloat ( rate [ 2 ] ) )
30+ }
31+ } ) ;
32+ this . referenceRatesAtDate . set ( date , referenceRates )
33+ }
34+
1835 @given ( 'the following stocks are in the wallet' )
1936 public setStocks ( stocks : any ) {
2037 for ( let row of stocks . rawTable ) {
@@ -29,32 +46,52 @@ export class WalletValueSteps {
2946
3047 @when ( 'I ask to compute it\'s value in {string} from the rates of {string}' )
3148 public async computeRatesOneDate ( currency : Currencies , date : string ) {
49+ this . dates = [ ]
50+ this . dates . push ( date )
51+ this . answers = [ ]
3252 this . answers . push ( await Wallet . computeValue ( this . wallet , currency , date ) )
53+ this . referenceRateWalletValue = this . computeWalletValueLocal ( currency , date )
3354 }
3455
3556 @when ( 'I ask to compute it\'s value in {string} from the rates of {string} and then from the rates of {string}' )
3657 async computeRatesTwoDates ( currency : Currencies , date1 : string , date2 : string ) {
58+ this . dates = [ ]
59+ this . dates . push ( date1 )
60+ this . dates . push ( date2 )
3761 this . answers = [ ]
3862 this . answers . push ( await Wallet . computeValue ( this . wallet , currency , date1 ) )
3963 this . answers . push ( await Wallet . computeValue ( this . wallet , currency , date2 ) )
4064 }
4165
4266 @then ( 'the wallet value in the given currency should be {string}' )
4367 public compareAnswer ( expectedAnswer : string ) {
44- assert . strictEqual ( this . answers . shift ( ) , parseFloat ( expectedAnswer ) ) ;
45- this . answers = [ ]
68+ const actual = this . answers . shift ( ) || 0
69+ const expected = parseFloat ( expectedAnswer )
70+
71+ //test actual with expected answer
72+ assert . strictEqual ( actual , expected ) ;
73+ //test actual with computesd value with rates defiend locally in scébario
74+ assert . strictEqual ( actual , this . referenceRateWalletValue ) ;
4675 }
4776
4877 @then ( 'the two values calculated should be different' )
4978 compareTwoDifferentAnswers ( ) {
5079 assert . notStrictEqual ( this . answers [ 0 ] , this . answers [ 1 ] )
51- this . answers = [ ]
5280 }
5381
5482 @then ( 'the two values calculated should be identical' )
5583 public compareTwoSameAnswers ( ) {
5684 assert . strictEqual ( this . answers . shift ( ) , this . answers . shift ( ) )
57- this . answers = [ ]
85+ }
86+
87+ public computeWalletValueLocal ( baseCurrency : Currencies , date : string ) : number {
88+ let result = 0
89+ for ( let stock of this . wallet . stocks ) {
90+ let multiplier = this . referenceRatesAtDate ?. get ( date ) ?. get ( stock . currency ) ?. get ( baseCurrency ) ? this . referenceRatesAtDate ?. get ( date ) ?. get ( stock . currency ) ?. get ( baseCurrency ) : 0
91+ if ( multiplier )
92+ result += parseFloat ( ( multiplier * stock . count ) . toFixed ( 2 ) )
93+ }
94+ return parseFloat ( result . toFixed ( 2 ) )
5895 }
5996
6097}
0 commit comments