Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mod captcha {
- use std::fmt;
- pub enum Mode {
- NumFirst,
- TextFirst,
- }
- pub enum Operator {
- Plus,
- Minus,
- Multiply,
- }
- impl fmt::Display for Operator {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- Operator::Plus => write!(f, "+"),
- Operator::Minus => write!(f, "-"),
- Operator::Multiply => write!(f, "*"),
- }
- }
- }
- type Result = std::result::Result<String, Error>;
- struct Error;
- trait Operand {
- fn text(&self) -> Result;
- }
- pub struct NumberOperand(i32);
- impl fmt::Display for NumberOperand {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.text() {
- Ok(text) => write!(f, "{}", text),
- Err(Error) => panic!("invalid number"),
- }
- }
- }
- impl Operand for NumberOperand {
- fn text(&self) -> Result {
- Ok(self.0.to_string())
- }
- }
- pub struct TextOperand(i32);
- impl fmt::Display for TextOperand {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self.text() {
- Ok(text) => write!(f, "{}", text),
- Err(Error) => panic!("invalid number"),
- }
- }
- }
- impl Operand for TextOperand {
- fn text(&self) -> Result {
- match self.0 {
- 1 => Ok(String::from("One")),
- 2 => Ok(String::from("Two")),
- 3 => Ok(String::from("Three")),
- 4 => Ok(String::from("Four")),
- 5 => Ok(String::from("Five")),
- 6 => Ok(String::from("Six")),
- 7 => Ok(String::from("Seven")),
- 8 => Ok(String::from("Eight")),
- 9 => Ok(String::from("Nine")),
- 0 => Ok(String::from("Zero")),
- _ => Err(Error),
- }
- }
- }
- pub fn captcha(mode: Mode, left: i32, operator: Operator, right: i32) -> String {
- match mode {
- Mode::NumFirst => format!(
- "{} {} {}",
- NumberOperand(left),
- operator,
- TextOperand(right),
- ),
- Mode::TextFirst => format!(
- "{} {} {}",
- TextOperand(left),
- operator,
- NumberOperand(right),
- ),
- }
- }
- }
- mod tests {
- use super::captcha::{captcha, Mode, Operator};
- struct TestCase {
- mode: Mode,
- left: i32,
- right: i32,
- operator: Operator,
- expected: String,
- }
- #[test]
- fn test_captcha() {
- let tcs = vec![
- // test operand
- TestCase {
- mode: Mode::NumFirst,
- left: 1,
- operator: Operator::Plus,
- right: 1,
- expected: String::from("1 + One"),
- },
- TestCase {
- mode: Mode::NumFirst,
- left: 2,
- operator: Operator::Plus,
- right: 1,
- expected: String::from("2 + One"),
- },
- // end test operand
- // test operator
- TestCase {
- mode: Mode::NumFirst,
- left: 2,
- operator: Operator::Plus,
- right: 2,
- expected: String::from("2 + Two"),
- },
- TestCase {
- mode: Mode::NumFirst,
- left: 2,
- operator: Operator::Minus,
- right: 2,
- expected: String::from("2 - Two"),
- },
- TestCase {
- mode: Mode::NumFirst,
- left: 2,
- operator: Operator::Multiply,
- right: 2,
- expected: String::from("2 * Two"),
- },
- // end test operator.
- // test mode
- TestCase {
- mode: Mode::TextFirst,
- left: 2,
- operator: Operator::Minus,
- right: 2,
- expected: String::from("Two - 2"),
- },
- // end test mode
- ];
- for tc in tcs {
- assert_eq!(
- captcha(tc.mode, tc.left, tc.operator, tc.right),
- tc.expected
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement