diff options
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/src/lambda.rs | 26 | ||||
| -rw-r--r-- | rust/tests/interpret_test.rs | 2 |
2 files changed, 19 insertions, 9 deletions
diff --git a/rust/src/lambda.rs b/rust/src/lambda.rs index 3c1c8b6..7601640 100644 --- a/rust/src/lambda.rs +++ b/rust/src/lambda.rs @@ -13,32 +13,42 @@ pub fn run(arg: &str) -> String { #[derive(Debug, PartialEq)] pub enum Value { Num(i32), + Bool(bool), } impl Display for Value { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Value::Num(i) => write!(f, "{}", i), + Value::Bool(b) => write!(f, "{}", b), } } } fn interpret(arg: &str) -> Value { - Value::Num(arg.parse().unwrap()) + arg.parse::<i32>() + .map(Value::Num) + .or(arg.parse::<bool>().map(Value::Bool)) + .unwrap() } #[cfg(test)] mod tests { use super::interpret; - use super::Value::Num; - // Bring the macros and other important things into scope. + use super::Value::*; use proptest::prelude::*; proptest! { - #[test] - fn interpret_integer_as_number(i in -1000i32..1000) { - let result = interpret(&i.to_string()); - assert_eq!(Num(i), result); - } + #[test] + fn interpret_integer_as_number(i in -1000i32..1000) { + let result = interpret(&i.to_string()); + assert_eq!(Num(i), result); + } + + } + #[test] + fn interpret_truth_values_as_booleans() { + assert_eq!(Bool(true), interpret("true")); + assert_eq!(Bool(false), interpret("false")); } } diff --git a/rust/tests/interpret_test.rs b/rust/tests/interpret_test.rs index f0b5201..5c3108a 100644 --- a/rust/tests/interpret_test.rs +++ b/rust/tests/interpret_test.rs @@ -3,5 +3,5 @@ use lambda::run; #[test] fn interpreter_can_read_and_interpret_file() { let result = run("sample/test.txt"); - assert_eq!("1", result); + assert_eq!("12", result); } |
