Implement calculator_input in tests
use rpn_calculator::*;
-use CalculatorInput::*;
+
+fn calculator_input(s: &str) -> Vec<CalculatorInput> {
+ s.split_whitespace()
+ .map(|s| match s {
+ "+" => CalculatorInput::Add,
+ "-" => CalculatorInput::Subtract,
+ "*" => CalculatorInput::Multiply,
+ "/" => CalculatorInput::Divide,
+ n => CalculatorInput::Value(n.parse().unwrap()),
+ })
+ .collect()
+}
#[test]
fn test_empty_input_returns_none() {
- let input = vec![];
+ let input = calculator_input("");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn test_simple_value() {
- let input = vec![Value(10)];
+ let input = calculator_input("10");
assert_eq!(evaluate(&input), Some(10));
}
#[test]
#[ignore]
fn test_simple_addition() {
- let input = vec![Value(2), Value(2), Add];
+ let input = calculator_input("2 2 +");
assert_eq!(evaluate(&input), Some(4));
}
#[test]
#[ignore]
fn test_simple_subtraction() {
- let input = vec![Value(7), Value(11), Subtract];
+ let input = calculator_input("7 11 -");
assert_eq!(evaluate(&input), Some(-4));
}
#[test]
#[ignore]
fn test_simple_multiplication() {
- let input = vec![Value(6), Value(9), Multiply];
+ let input = calculator_input("6 9 *");
assert_eq!(evaluate(&input), Some(54));
}
#[test]
#[ignore]
fn test_simple_division() {
- let input = vec![Value(57), Value(19), Divide];
+ let input = calculator_input("57 19 /");
assert_eq!(evaluate(&input), Some(3));
}
#[test]
#[ignore]
fn test_complex_operation() {
- let input = vec![
- Value(4),
- Value(8),
- Add,
- Value(7),
- Value(5),
- Subtract,
- Divide,
- ];
+ let input = calculator_input("4 8 + 7 5 - /");
assert_eq!(evaluate(&input), Some(6));
}
#[test]
#[ignore]
fn test_too_few_operands_returns_none() {
- let input = vec![Value(2), Add];
+ let input = calculator_input("2 +");
assert_eq!(evaluate(&input), None);
}
#[test]
#[ignore]
fn test_too_many_operands_returns_none() {
- let input = vec![Value(2), Value(2)];
+ let input = calculator_input("2 2");
assert_eq!(evaluate(&input), None);
}
