@@ -49,6 +49,10 @@ impl Value {
49
49
pub fn new_string ( string : & str ) -> Self {
50
50
Self :: String ( string. to_string ( ) )
51
51
}
52
+
53
+ pub fn new_number ( number : impl Into < Number > ) -> Self {
54
+ Self :: Number ( number. into ( ) )
55
+ }
52
56
}
53
57
54
58
impl TryFrom < SerdeValue > for Value {
@@ -115,6 +119,10 @@ impl Function {
115
119
pub fn new_get ( key : & str ) -> Self {
116
120
Self :: Get ( key. to_string ( ) )
117
121
}
122
+
123
+ pub fn new_bn ( value : impl Into < Value > ) -> Self {
124
+ Self :: Bn ( value. into ( ) )
125
+ }
118
126
}
119
127
120
128
impl Value {
@@ -336,6 +344,30 @@ mod test {
336
344
) ;
337
345
}
338
346
347
+ #[ test]
348
+ fn test_and_eval ( ) {
349
+ let input = Input :: default ( ) ;
350
+ let mut output = Output {
351
+ show : true ,
352
+ boost : 1.0 ,
353
+ price : Default :: default ( ) ,
354
+ } ;
355
+
356
+ let cases = [
357
+ ( true , true , true ) ,
358
+ ( false , false , false ) ,
359
+ ( false , true , false ) ,
360
+ ( true , false , false ) ,
361
+ ] ;
362
+
363
+ for ( lhs, rhs, expected) in cases. iter ( ) {
364
+ let rule = Rule :: Function ( Function :: new_and ( Value :: Bool ( * lhs) , Value :: Bool ( * rhs) ) ) ;
365
+ let expected = Some ( Value :: Bool ( * expected) ) ;
366
+
367
+ assert_eq ! ( Ok ( expected) , rule. eval( & input, & mut output) ) ;
368
+ }
369
+ }
370
+
339
371
#[ test]
340
372
fn test_if_eval ( ) {
341
373
let input = Input :: default ( ) ;
@@ -354,6 +386,57 @@ mod test {
354
386
let rule = Rule :: Function ( Function :: new_if ( Value :: Bool ( false ) , then) ) ;
355
387
356
388
assert_eq ! ( Ok ( None ) , rule. eval( & input, & mut output) ) ;
389
+ }
357
390
391
+ #[ test]
392
+ fn test_bn_eval_from_actual_number_value_string_bignum_or_number ( ) {
393
+ let input = Input :: default ( ) ;
394
+ let mut output = Output {
395
+ show : true ,
396
+ boost : 1.0 ,
397
+ price : Default :: default ( ) ,
398
+ } ;
399
+
400
+ let cases = vec ! [
401
+ ( Value :: new_string( "1000" ) , Value :: BigNum ( 1000 . into( ) ) ) ,
402
+ ( Value :: new_number( 5000 ) , Value :: BigNum ( 5000 . into( ) ) ) ,
403
+ ( Value :: BigNum ( 2 . into( ) ) , Value :: BigNum ( 2 . into( ) ) ) ,
404
+ // rounded floats should work!
405
+ (
406
+ Value :: Number ( Number :: from_f64( 2.0 ) . expect( "should create float number" ) ) ,
407
+ Value :: BigNum ( 2 . into( ) ) ,
408
+ ) ,
409
+ ] ;
410
+
411
+ for ( from, expected) in cases. into_iter ( ) {
412
+ let rule = Rule :: Function ( Function :: new_bn ( from) ) ;
413
+
414
+ assert_eq ! ( Ok ( Some ( expected) ) , rule. eval( & input, & mut output) ) ;
415
+ }
416
+ }
417
+
418
+ #[ test]
419
+ fn test_bn_eval_from_actual_incorrect_value ( ) {
420
+ let input = Input :: default ( ) ;
421
+ let mut output = Output {
422
+ show : true ,
423
+ boost : 1.0 ,
424
+ price : Default :: default ( ) ,
425
+ } ;
426
+
427
+ let error_cases = vec ! [
428
+ Value :: new_string( "text" ) ,
429
+ // BigNums can only be possitive
430
+ Value :: new_number( -100 ) ,
431
+ Value :: Bool ( true ) ,
432
+ Value :: Array ( vec![ Value :: Bool ( false ) ] ) ,
433
+ Value :: Number ( Number :: from_f64( 2.5 ) . expect( "should create float number" ) ) ,
434
+ ] ;
435
+
436
+ for error_case in error_cases. into_iter ( ) {
437
+ let rule = Rule :: Function ( Function :: new_bn ( error_case) ) ;
438
+
439
+ assert_eq ! ( Err ( Error :: TypeError ) , rule. eval( & input, & mut output) ) ;
440
+ }
358
441
}
359
442
}
0 commit comments