2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2021-10-09 15:10:10 +02:00
|
|
|
use nu_engine::eval_block;
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2021-10-09 15:10:10 +02:00
|
|
|
use nu_parser::parse;
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2021-10-09 15:10:10 +02:00
|
|
|
use nu_protocol::{
|
2021-10-25 18:58:58 +02:00
|
|
|
engine::{Command, EngineState, Stack, StateWorkingSet},
|
2022-04-19 00:28:01 +02:00
|
|
|
PipelineData, Span, Value,
|
2021-10-09 15:10:10 +02:00
|
|
|
};
|
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2021-10-29 08:26:29 +02:00
|
|
|
use crate::To;
|
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2022-01-08 01:40:40 +01:00
|
|
|
use super::{
|
2022-08-18 22:24:39 +02:00
|
|
|
Ansi, Date, From, If, Into, LetEnv, Math, Path, Random, Split, SplitColumn, SplitRow, Str,
|
2022-09-11 10:48:27 +02:00
|
|
|
StrJoin, StrLength, StrReplace, Url, Wrap,
|
2022-01-08 01:40:40 +01:00
|
|
|
};
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2021-10-09 15:10:10 +02:00
|
|
|
pub fn test_examples(cmd: impl Command + 'static) {
|
2021-12-24 01:16:50 +01:00
|
|
|
use crate::BuildString;
|
|
|
|
|
2021-10-09 15:10:10 +02:00
|
|
|
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);
|
2021-11-09 02:59:44 +01:00
|
|
|
working_set.add_decl(Box::new(Str));
|
2022-09-11 10:48:27 +02:00
|
|
|
working_set.add_decl(Box::new(StrJoin));
|
2022-01-08 01:40:40 +01:00
|
|
|
working_set.add_decl(Box::new(StrLength));
|
2022-04-07 15:41:09 +02:00
|
|
|
working_set.add_decl(Box::new(StrReplace));
|
2021-12-24 01:16:50 +01:00
|
|
|
working_set.add_decl(Box::new(BuildString));
|
2021-10-09 15:10:10 +02:00
|
|
|
working_set.add_decl(Box::new(From));
|
2022-01-08 01:40:40 +01:00
|
|
|
working_set.add_decl(Box::new(If));
|
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));
|
2021-11-30 07:12:19 +01:00
|
|
|
working_set.add_decl(Box::new(Random));
|
2021-10-09 15:28:09 +02:00
|
|
|
working_set.add_decl(Box::new(Split));
|
2022-02-12 03:06:49 +01:00
|
|
|
working_set.add_decl(Box::new(SplitColumn));
|
|
|
|
working_set.add_decl(Box::new(SplitRow));
|
2021-10-26 01:56:22 +02:00
|
|
|
working_set.add_decl(Box::new(Math));
|
2021-12-13 02:47:14 +01:00
|
|
|
working_set.add_decl(Box::new(Path));
|
2021-10-31 07:54:51 +01:00
|
|
|
working_set.add_decl(Box::new(Date));
|
2021-12-10 01:09:30 +01:00
|
|
|
working_set.add_decl(Box::new(Url));
|
2021-12-17 21:32:03 +01:00
|
|
|
working_set.add_decl(Box::new(Ansi));
|
2022-01-22 00:50:26 +01:00
|
|
|
working_set.add_decl(Box::new(Wrap));
|
2022-08-18 22:24:39 +02:00
|
|
|
working_set.add_decl(Box::new(LetEnv));
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2021-11-03 19:46:33 +01: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()
|
|
|
|
};
|
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
2022-07-14 16:09:27 +02:00
|
|
|
|
|
|
|
engine_state
|
|
|
|
.merge_delta(delta)
|
|
|
|
.expect("Error merging 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();
|
|
|
|
|
2022-01-05 01:26:01 +01:00
|
|
|
let mut stack = Stack::new();
|
|
|
|
|
|
|
|
// Set up PWD
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
|
|
|
Value::String {
|
|
|
|
val: cwd.to_string_lossy().to_string(),
|
|
|
|
span: Span::test_data(),
|
|
|
|
},
|
|
|
|
);
|
2022-07-14 16:09:27 +02:00
|
|
|
|
|
|
|
engine_state
|
|
|
|
.merge_env(&mut stack, &cwd)
|
|
|
|
.expect("Error merging environment");
|
2022-01-05 01:26:01 +01:00
|
|
|
|
2021-10-09 15:10:10 +02:00
|
|
|
let (block, delta) = {
|
|
|
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
2022-03-18 20:03:57 +01:00
|
|
|
let (output, err) = parse(
|
|
|
|
&mut working_set,
|
|
|
|
None,
|
|
|
|
example.example.as_bytes(),
|
|
|
|
false,
|
|
|
|
&[],
|
|
|
|
);
|
2021-10-09 15:10:10 +02:00
|
|
|
|
|
|
|
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())
|
|
|
|
};
|
|
|
|
|
2022-07-14 16:09:27 +02:00
|
|
|
engine_state
|
|
|
|
.merge_delta(delta)
|
|
|
|
.expect("Error merging 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
|
|
|
|
2022-01-04 23:30:34 +01:00
|
|
|
// Set up PWD
|
|
|
|
stack.add_env_var(
|
|
|
|
"PWD".to_string(),
|
|
|
|
Value::String {
|
|
|
|
val: cwd.to_string_lossy().to_string(),
|
|
|
|
span: Span::test_data(),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-11-06 06:50:33 +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-06 06:50:33 +01:00
|
|
|
) {
|
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) => {
|
2021-12-19 08:46:13 +01:00
|
|
|
let result = result.into_value(Span::test_data());
|
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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|