Skip to content

Commit 7a50b66

Browse files
committed
primitives - eval - test and & bn
1 parent 98bde7b commit 7a50b66

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

primitives/src/targeting/eval.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl Value {
4949
pub fn new_string(string: &str) -> Self {
5050
Self::String(string.to_string())
5151
}
52+
53+
pub fn new_number(number: impl Into<Number>) -> Self {
54+
Self::Number(number.into())
55+
}
5256
}
5357

5458
impl TryFrom<SerdeValue> for Value {
@@ -115,6 +119,10 @@ impl Function {
115119
pub fn new_get(key: &str) -> Self {
116120
Self::Get(key.to_string())
117121
}
122+
123+
pub fn new_bn(value: impl Into<Value>) -> Self {
124+
Self::Bn(value.into())
125+
}
118126
}
119127

120128
impl Value {
@@ -336,6 +344,30 @@ mod test {
336344
);
337345
}
338346

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+
339371
#[test]
340372
fn test_if_eval() {
341373
let input = Input::default();
@@ -354,6 +386,57 @@ mod test {
354386
let rule = Rule::Function(Function::new_if(Value::Bool(false), then));
355387

356388
assert_eq!(Ok(None), rule.eval(&input, &mut output));
389+
}
357390

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+
}
358441
}
359442
}

0 commit comments

Comments
 (0)