diff options
author | Nao Pross <np@0hm.ch> | 2021-03-10 01:13:14 +0100 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2021-03-10 01:13:14 +0100 |
commit | 822dcf858a254f05b35bd42afdf5fcd236354713 (patch) | |
tree | 0c9b61230529586e689290ebd4e818e59e8934d6 /src | |
parent | Create tests windows (diff) | |
download | testbench-ui-822dcf858a254f05b35bd42afdf5fcd236354713.tar.gz testbench-ui-822dcf858a254f05b35bd42afdf5fcd236354713.zip |
Make tests runnable
Diffstat (limited to 'src')
-rw-r--r-- | src/testbench.rs | 90 |
1 files changed, 82 insertions, 8 deletions
diff --git a/src/testbench.rs b/src/testbench.rs index ed0a2c1..9de4db1 100644 --- a/src/testbench.rs +++ b/src/testbench.rs @@ -1,8 +1,8 @@ +use std::ffi::OsStr; use std::fs; use std::io; use std::option::Option; use std::path::{Path, PathBuf}; -use std::ffi::OsStr; use imgui; use rlua::Lua; @@ -10,21 +10,59 @@ use rlua::Lua; #[derive(Default, PartialEq)] pub struct Test { pub path: PathBuf, + text: Option<String>, +} + +impl Test { + pub fn new(path: PathBuf) -> Test { + Test { + path: path, + text: None, + } + } + + pub fn read(&mut self) -> &str { + if self.text.is_none() { + self.text = Some(fs::read_to_string(&self.path).expect("Failed to read file")); + } + + return self.text.as_ref().unwrap(); + } } #[derive(Default)] pub struct Bench { lua: Lua, tests: Vec<Test>, + tests_path: PathBuf, selected_test: Option<usize>, + console: Vec<imgui::ImString>, } impl Bench { pub fn new() -> Bench { + /* Prepare lua environment */ + let lua = Lua::new(); + let lua_setup = lua.context(|context| -> rlua::Result<()> { + use rlua::{String, Variadic}; + + let ui_table = context.create_table()?; + let ui_print = context.create_function(|_, strings: Variadic<String>| Ok(()))?; + + ui_table.set("print", ui_print)?; + + Ok(()) + }); + + // TODO: handle errors + // if let Err(e) = lua_setup {} + let mut b = Bench { - lua: Lua::new(), + lua: lua, tests: Vec::new(), + tests_path: PathBuf::from("lua"), selected_test: None, + console: Vec::new(), }; // TODO: set graphically and use RV @@ -48,7 +86,7 @@ impl Bench { .into_iter() .filter(|f| f.is_file()) .filter(|f| f.extension().unwrap_or(OsStr::new("")) == "lua") - .map(|f| Test { path: f }) + .map(|f| Test::new(f)) .collect(); return Ok(()); @@ -84,17 +122,53 @@ impl Bench { ui.text_wrapped(&imstr); ui.same_line(0.); if ui.button(im_str!("Run"), [0., 0.]) { + self.run_test(index); } + ui.same_line(0.); - if ui.button(im_str!("Show"), [0., 0.]) {} + if ui.button(im_str!("Show"), [0., 0.]) { + self.show_test(index); + } } } + + ui.separator(); + ChildWindow::new("console") + .size([0., 0.]) + .scrollable(true) + .build(&ui, || { + for line in &self.console { + ui.text(line); + } + }); }); } - fn run_test(&self, index: usize) { - self.lua.context(|context| { - - }); + fn run_test(&mut self, index: usize) { + if let Some(test) = self.tests.get_mut(index) { + let text = test.read(); + let mut output = imgui::ImString::new(""); + + self.lua.context( + |context| match context.load(text).eval::<rlua::MultiValue>() { + Ok(values) => { + output.push_str( + &values + .iter() + .map(|value| format!("{:?}", value)) + .collect::<Vec<_>>() + .join("\t"), + ); + } + Err(e) => { + output.push_str(&e.to_string()); + } + }, + ); + + self.console.push(output); + } } + + fn show_test(&self, index: usize) {} } |