diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 1595bf4..00c2a8d 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -202,7 +202,7 @@ impl Cache { testcase: Option, ) -> Result<(hashmap<&'static str, String>, [String; 2]), Error> { trace!("pre run code..."); - use crate::helper::code_path; + use crate::helper::{code_path, test_cases_path}; use std::fs::File; use std::io::Read; @@ -218,6 +218,24 @@ impl Cache { let mut json: HashMap<&'static str, String> = HashMap::new(); let mut code: String = "".to_string(); + let maybe_file_testcases: Option = test_cases_path(&p) + .map(|filename| { + let mut tests = "".to_string(); + File::open(filename) + .and_then(|mut file_descriptor| file_descriptor.read_to_string(&mut tests)) + .map(|_| Some(tests)) + .unwrap_or(None) + }) + .unwrap_or(None); + + // Takes test cases using following priority + // 1. cli parameter + // 2. test cases from the file + // 3. sample test case from the task + let testcase = testcase + .or(maybe_file_testcases) + .unwrap_or(d.case); + File::open(code_path(&p, None)?)?.read_to_string(&mut code)?; json.insert("lang", conf.code.lang.to_string()); @@ -226,10 +244,7 @@ impl Cache { // pass manually data json.insert("name", p.name.to_string()); - match testcase { - Some(case) => json.insert("data_input", case), - _ => json.insert("data_input", d.case), - }; + json.insert("data_input", testcase); let url = match run { Run::Test => conf.sys.urls.get("test")?.replace("$slug", &p.slug), diff --git a/src/cache/models.rs b/src/cache/models.rs index ea333f3..e474a0f 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -118,6 +118,7 @@ pub struct Question { pub stats: Stats, pub defs: CodeDefintion, pub case: String, + pub all_cases: String, pub metadata: MetaData, pub test: bool, pub t_content: String, @@ -166,7 +167,7 @@ mod question { #[derive(Debug, Default, Serialize, Deserialize)] pub struct MetaData { pub name: Option, - pub params: Option, + pub params: Option>, pub r#return: Return, } @@ -378,7 +379,7 @@ impl std::fmt::Display for VerifyResult { // if anybody reach this, welcome to fix this! 13 | 14 => write!(f, "\n{}\n", &self.status.status_msg.yellow().bold(),)?, // Runtime error - 15 => write!(f, "\n{}\n", &self.status.status_msg.red().bold())?, + 15 => write!(f, "\n{}\n{}\n'", &self.status.status_msg.red().bold(), &self.status.runtime_error)?, // Compile Error 20 => write!( f, @@ -479,6 +480,8 @@ mod verify { pub status_memory: String, #[serde(default)] pub status_runtime: String, + #[serde(default)] + pub runtime_error: String } #[derive(Debug, Default, Deserialize)] diff --git a/src/cache/parser.rs b/src/cache/parser.rs index e61bd46..1b3f25c 100644 --- a/src/cache/parser.rs +++ b/src/cache/parser.rs @@ -43,6 +43,10 @@ pub fn desc(q: &mut Question, v: Value) -> Result<(), Error> { stats: serde_json::from_str(o.get("stats")?.as_str()?)?, defs: serde_json::from_str(o.get("codeDefinition")?.as_str()?)?, case: o.get("sampleTestCase")?.as_str()?.to_string(), + all_cases: o.get("exampleTestcases") + .unwrap_or(o.get("sampleTestCase")?) // soft fail to the sampleTestCase + .as_str()? + .to_string(), metadata: serde_json::from_str(o.get("metaData")?.as_str()?)?, test: o.get("enableRunCode")?.as_bool()?, t_content: o diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 82d9e6e..1edf7d6 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -64,6 +64,8 @@ impl Command for EditCommand { let lang = conf.code.lang; let path = crate::helper::code_path(&target, Some(lang.to_owned()))?; + let tests_path = crate::helper::test_cases_path(&target)?; + if !Path::new(&path).exists() { let mut qr = serde_json::from_str(&target.desc); if qr.is_err() { @@ -71,17 +73,24 @@ impl Command for EditCommand { } let question: Question = qr?; - let mut f = File::create(&path)?; + + let mut file_code = File::create(&path)?; + let mut file_tests = File::create(&tests_path)?; + let mut flag = false; for d in question.defs.0 { if d.value == lang { flag = true; - f.write_all(d.code.to_string().as_bytes())?; + file_code.write_all(d.code.to_string().as_bytes())?; + file_tests.write_all(question.all_cases.as_bytes())?; } } + // if language is not found in the list of supported languges clean up files if !flag { std::fs::remove_file(&path)?; + std::fs::remove_file(&tests_path)?; + return Err(crate::Error::FeatureError(format!( "This question doesn't support {}, please try another", &lang diff --git a/src/helper.rs b/src/helper.rs index 8fb096a..2ab56f7 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,6 +1,6 @@ //! A set of helper traits pub use self::digit::Digit; -pub use self::file::{code_path, load_script}; +pub use self::file::{code_path, load_script, test_cases_path}; pub use self::filter::{filter, squash}; pub use self::html::HTML; @@ -282,6 +282,23 @@ mod file { } use crate::cache::models::Problem; + + /// Generate test casese path by fid + pub fn test_cases_path(target: &Problem) -> Result { + let conf = crate::cfg::locate()?; + + let mut path = format!( + "{}/{}.tests.dat", + conf.storage.code()?, + conf.code.pick, + ); + + path = path.replace("${fid}", &target.fid.to_string()); + path = path.replace("${slug}", &target.slug.to_string()); + + Ok(path) + } + /// Generate code path by fid pub fn code_path(target: &Problem, l: Option) -> Result { let conf = crate::cfg::locate()?; diff --git a/src/plugins/leetcode.rs b/src/plugins/leetcode.rs index 480dbc9..c6d2d1a 100644 --- a/src/plugins/leetcode.rs +++ b/src/plugins/leetcode.rs @@ -135,6 +135,7 @@ impl LeetCode { " stats", " codeDefinition", " sampleTestCase", + " exampleTestcases", " enableRunCode", " metaData", " translatedContent",

AltStyle によって変換されたページ (->オリジナル) /