summaryrefslogtreecommitdiffstats
path: root/src/testbench.rs
blob: ed0a2c12ddb26644fd42890512da3987c2e6b1e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::fs;
use std::io;
use std::option::Option;
use std::path::{Path, PathBuf};
use std::ffi::OsStr;

use imgui;
use rlua::Lua;

#[derive(Default, PartialEq)]
pub struct Test {
    pub path: PathBuf,
}

#[derive(Default)]
pub struct Bench {
    lua: Lua,
    tests: Vec<Test>,
    selected_test: Option<usize>,
}

impl Bench {
    pub fn new() -> Bench {
        let mut b = Bench {
            lua: Lua::new(),
            tests: Vec::new(),
            selected_test: None,
        };

        // TODO: set graphically and use RV
        b.load_tests("lua");

        return b;
    }

    pub fn load_tests<P>(&mut self, path: P) -> io::Result<()>
    where
        P: AsRef<Path>,
    {
        let entries: Result<Vec<PathBuf>, io::Error> = fs::read_dir(path)?
            .into_iter()
            .map(|r| r.map(|f| f.path()))
            .collect();

        match entries {
            Ok(files) => {
                self.tests = files
                    .into_iter()
                    .filter(|f| f.is_file())
                    .filter(|f| f.extension().unwrap_or(OsStr::new("")) == "lua")
                    .map(|f| Test { path: f })
                    .collect();

                return Ok(());
            }
            Err(e) => return Err(e),
        }
    }

    pub fn draw(&mut self, _: &mut bool, ui: &mut imgui::Ui) {
        use imgui::*;

        let win = Window::new(im_str!("Testbench"))
            .size([400., 500.], Condition::Appearing)
            .position([20., 20.], Condition::Appearing);

        win.build(&ui, || {
            for (index, test) in self.tests.iter().enumerate() {
                if let Some(test_name) = test.path.to_str() {
                    let test_name: ImString = test_name.to_string().into();
                    let selected = matches!(self.selected_test, Some(i) if i == index);

                    if Selectable::new(&test_name).selected(selected).build(ui) {
                        self.selected_test = Some(index)
                    }
                }
            }

            ui.separator();
            if let Some(index) = self.selected_test {
                if let Some(test_name) = self.tests[index].path.to_str() {
                    let imstr: ImString =
                        format!("Selected Test: {}", test_name).to_string().into();
                    ui.text_wrapped(&imstr);
                    ui.same_line(0.);
                    if ui.button(im_str!("Run"), [0., 0.]) {
                    }
                    ui.same_line(0.);
                    if ui.button(im_str!("Show"), [0., 0.]) {}
                }
            }
        });
    }

    fn run_test(&self, index: usize) {
        self.lua.context(|context| {
            
        });
    }
}