2021-11-29 07:50:57 +01:00
|
|
|
use nu_engine::eval_block;
|
|
|
|
use nu_parser::parse;
|
|
|
|
use nu_protocol::{
|
|
|
|
engine::{Command, EngineState, Stack, StateWorkingSet},
|
|
|
|
PipelineData, Span, Value, CONFIG_VARIABLE_ID,
|
|
|
|
};
|
|
|
|
|
2021-12-21 19:32:09 +01:00
|
|
|
use super::eager::ToDataFrame;
|
2021-12-06 05:09:49 +01:00
|
|
|
use crate::Let;
|
2021-11-29 07:50:57 +01:00
|
|
|
|
2021-12-18 18:45:09 +01:00
|
|
|
pub fn test_dataframe(cmds: Vec<Box<dyn Command + 'static>>) {
|
|
|
|
if cmds.is_empty() {
|
|
|
|
panic!("Empty commands vector")
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first element in the cmds vector must be the one tested
|
|
|
|
let examples = cmds[0].examples();
|
2021-11-29 07:50:57 +01:00
|
|
|
let mut engine_state = Box::new(EngineState::new());
|
|
|
|
|
|
|
|
let delta = {
|
|
|
|
// Base functions that are needed for testing
|
|
|
|
// Try to keep this working set small to keep tests running as fast as possible
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
2021-12-06 05:09:49 +01:00
|
|
|
working_set.add_decl(Box::new(Let));
|
2021-11-29 07:50:57 +01:00
|
|
|
working_set.add_decl(Box::new(ToDataFrame));
|
|
|
|
|
|
|
|
// Adding the command that is being tested to the working set
|
2021-12-18 18:45:09 +01:00
|
|
|
for cmd in cmds {
|
|
|
|
working_set.add_decl(cmd);
|
|
|
|
}
|
2021-11-29 07:50:57 +01:00
|
|
|
|
|
|
|
working_set.render()
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
let _ = engine_state.merge_delta(delta, None, &cwd);
|
2021-11-29 07:50:57 +01:00
|
|
|
|
|
|
|
for example in examples {
|
|
|
|
// Skip tests that don't have results to compare to
|
|
|
|
if example.result.is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
|
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
|
|
|
let (output, err) = parse(&mut working_set, None, example.example.as_bytes(), false);
|
|
|
|
|
|
|
|
if let Some(err) = err {
|
|
|
|
panic!("test parse error in `{}`: {:?}", example.example, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
(output, working_set.render())
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let _ = engine_state.merge_delta(delta, None, &cwd);
|
2021-11-29 07:50:57 +01:00
|
|
|
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
|
|
|
// Set up our initial config to start from
|
|
|
|
stack.vars.insert(
|
|
|
|
CONFIG_VARIABLE_ID,
|
|
|
|
Value::Record {
|
|
|
|
cols: vec![],
|
|
|
|
vals: vec![],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-11-29 07:50:57 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
match eval_block(
|
|
|
|
&engine_state,
|
|
|
|
&mut stack,
|
|
|
|
&block,
|
2021-12-19 08:46:13 +01:00
|
|
|
PipelineData::new(Span::test_data()),
|
2022-02-21 23:22:21 +01:00
|
|
|
true,
|
|
|
|
true,
|
2021-11-29 07:50:57 +01:00
|
|
|
) {
|
|
|
|
Err(err) => panic!("test eval error in `{}`: {:?}", example.example, err),
|
|
|
|
Ok(result) => {
|
2021-12-19 08:46:13 +01:00
|
|
|
let result = result.into_value(Span::test_data());
|
2021-11-29 07:50:57 +01:00
|
|
|
println!("input: {}", example.example);
|
|
|
|
println!("result: {:?}", result);
|
|
|
|
println!("done: {:?}", start.elapsed());
|
|
|
|
|
|
|
|
// Note. Value implements PartialEq for Bool, Int, Float, String and Block
|
|
|
|
// If the command you are testing requires to compare another case, then
|
|
|
|
// you need to define its equality in the Value struct
|
|
|
|
if let Some(expected) = example.result {
|
|
|
|
if result != expected {
|
|
|
|
panic!(
|
|
|
|
"the example result is different to expected value: {:?} != {:?}",
|
|
|
|
result, expected
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|