Skip to content

Allow for custom testcases with the leetcode test command, and some minor edits #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/target
/Cargo.lock
**/*.rs.bk
.DS_Store
.idea
15 changes: 10 additions & 5 deletions src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl Cache {
&self,
run: Run,
rfid: i32,
testcase: Option<String>
) -> Result<(HashMap<&'static str, String>, [String; 2]), Error> {
trace!("pre run code...");
use crate::helper::code_path;
Expand All @@ -207,7 +208,11 @@ impl Cache {

// pass manually data
json.insert("name", p.name.to_string());
json.insert("data_input", d.case);
match testcase {
Some(case) => json.insert("data_input", case),
_ => json.insert("data_input", d.case)

};

let url = match run {
Run::Test => conf.sys.urls.get("test")?.replace("$slug", &p.slug),
Expand Down Expand Up @@ -251,10 +256,10 @@ impl Cache {
Ok(res)
}

/// Exec problem fliter —— Test or Submit
pub fn exec_problem(&self, rfid: i32, run: Run) -> Result<VerifyResult, Error> {
trace!("Exec problem fliter —— Test or Submit");
let pre = self.pre_run_code(run.clone(), rfid)?;
/// Exec problem filter —— Test or Submit
pub fn exec_problem(&self, rfid: i32, run: Run, testcase: Option<String>) -> Result<VerifyResult, Error> {
trace!("Exec problem filter —— Test or Submit");
let pre = self.pre_run_code(run.clone(), rfid, testcase)?;
let json = pre.0;

let run_res: RunCode = self
Expand Down
6 changes: 3 additions & 3 deletions src/cache/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl std::fmt::Display for VerifyResult {
" Runtime: ".dimmed(),
&self.status.status_runtime.dimmed(),
"\n Your input: ",
&self.data_input.replace("\n", ", "),
&self.data_input.replace("\n", ""),
"\n Output: ",
ca,
"\n Expected: ",
Expand Down Expand Up @@ -331,7 +331,7 @@ impl std::fmt::Display for VerifyResult {
" Runtime: ".dimmed(),
&self.status.status_runtime.dimmed(),
"\n Your input: ",
&self.data_input.replace("\n", ", "),
&self.data_input.replace("\n", ""),
"\n Output: ",
ca,
"\n Expected: ",
Expand Down Expand Up @@ -387,7 +387,7 @@ impl std::fmt::Display for VerifyResult {
&self.status.status_msg.red().bold(),
&self.error.full_compile_error
),
_ => write!(f, "{}", "\nUnKnow Error...\n".red().bold()),
_ => write!(f, "{}", "\nUnknown Error...\n".red().bold()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Command for ExecCommand {

let id: i32 = m.value_of("id")?.parse()?;
let cache = Cache::new()?;
let res = cache.exec_problem(id, Run::Submit)?;
let res = cache.exec_problem(id, Run::Submit, None)?;

println!("{}", res);
Ok(())
Expand Down
16 changes: 14 additions & 2 deletions src/cmds/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,34 @@ impl Command for TestCommand {
fn usage<'a, 'edit>() -> App<'a, 'edit> {
use clap::{Arg, SubCommand};
SubCommand::with_name("test")
.about("Edit question by id")
.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
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").to_string()),
_ => case_str = None,
}
let cache = Cache::new()?;
let res = cache.exec_problem(id, Run::Test)?;
let res = cache.exec_problem(id, Run::Test, case_str)?;

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