Skip to content

Commit

Permalink
chore(andax/fns/io): unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madonuko authored Sep 21, 2023
1 parent ee8a958 commit 9adcc56
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 37 deletions.
29 changes: 29 additions & 0 deletions andax/src/fns/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,32 @@ pub mod ar {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
fn shells() -> Result<(), Box<EvalAltResult>> {
let (en, sc) = crate::run::gen_en();
en.run(r#"
let a = sh("echo hai > test");
let b = sh(["echo", "hai"]);
let c = sh(["rm", "-rf", "test"]);
let d = sh("ls -al", "/");
let pwd = sh("pwd").sh_stdout();
let e = sh(["grep", "hai", "test"], pwd);
if a.sh_stderr() != "" {
throw "error!?";
}
if b.sh_stdout() != "hai\n" {
throw "bad echo?";
}
if c.sh_rc() != 0 {
throw "cannot rm?";
}
if d.sh_stdout().is_empty() {
throw "why is out empty?";
}
"#)?;
Ok(())
}
}
71 changes: 34 additions & 37 deletions andax/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn module_resolver() -> ModuleResolversCollection {

resolv
}
fn gen_en() -> (Engine, Scope<'static>) {
pub(crate) fn gen_en() -> (Engine, Scope<'static>) {
let mut sc = Scope::new();
sc.push("USER_AGENT", f::tsunagu::USER_AGENT);
sc.push("IS_WIN32", cfg!(windows));
Expand Down Expand Up @@ -127,44 +127,41 @@ pub fn _tb(proj: &str, scr: &Path, nanitozo: TbErr, pos: Position, rhai_fn: &str
}};
}
let f = die!(f, "{proj}: Cannot open `{scr}`: {}");
for (n, sl) in std::io::BufReader::new(f).lines().enumerate() {
if n != line - 1 {
continue;
let sl = std::io::BufReader::new(f).lines().nth(line - 1);
let sl = die!(sl, "{proj}: Non-existence exception at {scr}:{line}:{col}");
// replace tabs to avoid wrong position when print
let sl = die!(sl, "{proj}: Cannot read line: {}").replace('\t', " ");
let m = WORD_REGEX.find_at(sl.as_str(), col - 1).map_or(1, |x| {
let r = x.range();
if r.start == col - 1 {
r.len()
} else {
1
}
// replace tabs to avoid wrong position when print
let sl = die!(sl, "{proj}: Cannot read line: {}").replace('\t', " ");
let m = WORD_REGEX.find_at(sl.as_str(), col - 1).map_or(1, |x| {
let r = x.range();
if r.start == col - 1 {
r.len()
} else {
1
}
});
let ln = line.to_string().len();
let lns = " ".repeat(ln);
let l = "─".repeat(ln);
let r = "─".repeat(sl.len() + 2);
let mut code = format!(
"─{l}─┬{r}\n {lns} │ {scr}:{line}:{col}\n─{l}─┼{r}\n {line} │ {sl}\n {lns} │ {}{}",
" ".repeat(col - 1),
"🭶".repeat(m)
);
if !rhai_fn.is_empty() {
code += &format!("\n {lns} └─═ When invoking: {rhai_fn}()");
}
if !fn_src.is_empty() {
code += &format!("\n {lns} └─═ Function source: {fn_src}");
}
code += &format!("\n {lns} └─═ {nanitozo}");
code += &hint(&sl, &lns, &nanitozo, rhai_fn).unwrap_or_default();
let c = code.matches('└').count();
if c > 0 {
code = code.replacen('└', "├", c - 1);
}
return error!("Script Exception —— {proj}\n{code}");
});
let ln = line.to_string().len();
let lns = " ".repeat(ln);
let l = "─".repeat(ln);
let r = "─".repeat(sl.len() + 2);
let mut code = format!(
"─{l}─┬{r}\n {lns} │ {scr}:{line}:{col}\n─{l}─┼{r}\n {line} │ {sl}\n {lns} │ {}{}",
" ".repeat(col - 1),
"🭶".repeat(m)
);
if !rhai_fn.is_empty() {
code += &format!("\n {lns} └─═ When invoking: {rhai_fn}()");
}
if !fn_src.is_empty() {
code += &format!("\n {lns} └─═ Function source: {fn_src}");
}
code += &format!("\n {lns} └─═ {nanitozo}");
code += &hint(&sl, &lns, &nanitozo, rhai_fn).unwrap_or_default();
// slow but works!
let c = code.matches('└').count();
if c > 0 {
code = code.replacen('└', "├", c - 1);
}
error!("{proj}: Non-existence exception at {scr}:{line}:{col}");
return error!("Script Exception —— {proj}\n{code}");
}
error!("{proj}: {scr:?} (no position data)\n{nanitozo}");
}
Expand Down

0 comments on commit 9adcc56

Please sign in to comment.