nushell/crates/nu-command/src/example_test.rs

102 lines
3.4 KiB
Rust
Raw Normal View History

2021-10-09 15:10:10 +02:00
use nu_engine::eval_block;
use nu_parser::parse;
use nu_protocol::{
2021-10-25 18:58:58 +02:00
engine::{Command, EngineState, Stack, StateWorkingSet},
PipelineData, Span, Value, CONFIG_VARIABLE_ID,
2021-10-09 15:10:10 +02:00
};
2021-10-29 08:26:29 +02:00
use crate::To;
use super::{Date, From, Into, Math, Path, Random, Split, Str, StrCollect, Url};
2021-10-09 15:10:10 +02:00
pub fn test_examples(cmd: impl Command + 'static) {
let examples = cmd.examples();
2021-10-25 06:01:02 +02:00
let mut engine_state = Box::new(EngineState::new());
2021-10-09 15:10:10 +02:00
let delta = {
// Base functions that are needed for testing
2021-10-09 15:28:09 +02:00
// Try to keep this working set small to keep tests running as fast as possible
2021-10-09 15:10:10 +02:00
let mut working_set = StateWorkingSet::new(&*engine_state);
working_set.add_decl(Box::new(Str));
2021-10-09 15:10:10 +02:00
working_set.add_decl(Box::new(From));
2021-10-29 08:26:29 +02:00
working_set.add_decl(Box::new(To));
2021-10-11 03:56:19 +02:00
working_set.add_decl(Box::new(Into));
working_set.add_decl(Box::new(Random));
2021-10-09 15:28:09 +02:00
working_set.add_decl(Box::new(Split));
working_set.add_decl(Box::new(Math));
working_set.add_decl(Box::new(Path));
2021-10-31 07:54:51 +01:00
working_set.add_decl(Box::new(Date));
working_set.add_decl(Box::new(Url));
working_set.add_decl(Box::new(StrCollect));
2021-10-09 15:10:10 +02:00
use super::Echo;
working_set.add_decl(Box::new(Echo));
2021-10-09 15:10:10 +02:00
// Adding the command that is being tested to the working set
working_set.add_decl(Box::new(cmd));
working_set.render()
};
let _ = engine_state.merge_delta(delta);
2021-10-09 15:10:10 +02:00
for example in examples {
2021-10-11 03:56:19 +02:00
// Skip tests that don't have results to compare to
if example.result.is_none() {
continue;
}
2021-10-09 15:10:10 +02:00
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 {
2021-10-11 03:56:19 +02:00
panic!("test parse error in `{}`: {:?}", example.example, err)
2021-10-09 15:10:10 +02:00
}
(output, working_set.render())
};
let _ = engine_state.merge_delta(delta);
2021-10-09 15:10:10 +02:00
2021-10-25 08:31:39 +02:00
let mut stack = Stack::new();
2021-10-09 15:10:10 +02:00
// Set up our initial config to start from
stack.vars.insert(
CONFIG_VARIABLE_ID,
Value::Record {
cols: vec![],
vals: vec![],
span: Span::unknown(),
},
);
match eval_block(
&engine_state,
&mut stack,
&block,
PipelineData::new(Span::unknown()),
) {
2021-10-11 03:56:19 +02:00
Err(err) => panic!("test eval error in `{}`: {:?}", example.example, err),
2021-10-09 15:10:10 +02:00
Ok(result) => {
let result = result.into_value(Span::unknown());
2021-10-09 15:10:10 +02: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
)
}
}
}
}
}
}