1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Test command
use super::Command;
use async_trait::async_trait;
use clap::{App, ArgMatches};

/// Abstract Test Command
///
/// ```sh
/// leetcode-test
/// Edit question by id
///
/// USAGE:
///     leetcode test <id>
///
/// FLAGS:
///     -h, --help       Prints help information
///     -V, --version    Prints version information
///
/// ARGS:
///     <id>    question id
/// ```
pub struct TestCommand;

#[async_trait]
impl Command for TestCommand {
    /// `test` usage
    fn usage<'a, 'edit>() -> App<'a, 'edit> {
        use clap::{Arg, SubCommand};
        SubCommand::with_name("test")
            .about("Test question by id")
            .visible_alias("t")
            .arg(
                Arg::with_name("id")
                    .takes_value(true)
                    .required(true)
                    .help("question id"),
            )
            .arg(
                Arg::with_name("testcase")
                    .takes_value(true)
                    .required(false)
                    .help("custom testcase"),
            )
    }

    /// `test` handler
    async fn handler(m: &ArgMatches<'_>) -> Result<(), crate::Error> {
        use crate::cache::{Cache, Run};
        let id: i32 = m.value_of("id")?.parse()?;
        let testcase = m.value_of("testcase");
        let case_str: Option<String>;
        match testcase {
            Some(case) => case_str = Option::from(case.replace("\\n", "\n")),
            _ => case_str = None,
        }
        let cache = Cache::new()?;
        let res = cache.exec_problem(id, Run::Test, case_str).await?;

        println!("{}", res);
        Ok(())
    }
}