Hi
Perhaps I'm thinking about testing wrong here? But I'm trying to write a couple of tests on an Enum for my CLI. One is a valid action and one is invalid. I have a impl frm_Str()
method which uses a match to confirm that the passed in action is valid, if valid it returns the type of enum. If it fails it returns Err(format!("Unknown action: {}", e))
.
These are my tests:
#[cfg(test)]
mod test {
use super::*;
#[test]
fn valid_action() {
let action : ActionKind = "gray".parse().unwrap();
assert_eq!( action, ActionKind::Gray );
}
#[test]
fn invalid_action() {
let invalid : ActionKind = "invalid".parse().unwrap();
assert_eq!( invalid, Err );
}
}
The enum and impl is:
#[derive(Debug, PartialEq)]
pub enum ActionKind {
Gray,
Thumb,
Rotate,
Crop
}
impl FromStr for ActionKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"gray" => Ok(ActionKind::Gray),
"thumb" => Ok(ActionKind::Thumb),
"rotate" => Ok(ActionKind::Rotate),
"crop" => Ok(ActionKind::Crop),
e @ _ => Err(format!("Unknown action: {}", e)),
}
}
}
So, am I testing things that don't need to be tested here? and if I'm not, how should I write a test to confirm that the commands are handled properly? (i.e if it's valid it returns the Enum, if invalid it Err)