leetcode_cli/cmds/
test.rs1use super::Command;
3use crate::{Error, Result};
4use async_trait::async_trait;
5use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command as ClapCommand};
6
7pub struct TestCommand;
24
25#[async_trait]
26impl Command for TestCommand {
27 fn usage() -> ClapCommand {
29 ClapCommand::new("test")
30 .about("Test a question")
31 .visible_alias("t")
32 .arg(
33 Arg::new("id")
34 .num_args(1)
35 .value_parser(clap::value_parser!(i32))
36 .help("question id"),
37 )
38 .arg(
39 Arg::new("testcase")
40 .num_args(1)
41 .required(false)
42 .help("custom testcase"),
43 )
44 .arg(
45 Arg::new("daily")
46 .short('d')
47 .long("daily")
48 .help("Test today's daily challenge")
49 .action(ArgAction::SetTrue),
50 )
51 .group(
52 ArgGroup::new("question-id")
53 .args(["id", "daily"])
54 .multiple(false)
55 .required(true),
56 )
57 }
58
59 async fn handler(m: &ArgMatches) -> Result<()> {
61 use crate::cache::{Cache, Run};
62
63 let cache = Cache::new()?;
64
65 let daily = m.get_one::<bool>("daily").unwrap_or(&false);
66 let daily_id = if *daily {
67 Some(cache.get_daily_problem_id().await?)
68 } else {
69 None
70 };
71
72 let id = m
73 .get_one::<i32>("id")
74 .copied()
75 .or(daily_id)
76 .ok_or(Error::NoneError)?;
77
78 let testcase = m.get_one::<String>("testcase");
79 let case_str: Option<String> = match testcase {
80 Some(case) => Option::from(case.replace("\\n", "\n")),
81 _ => None,
82 };
83 let res = cache.exec_problem(id, Run::Test, case_str).await?;
84
85 println!("{}", res);
86 Ok(())
87 }
88}