2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2022-11-09 22:55:05 +01:00
|
|
|
use nu_protocol::engine::Command;
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2022-11-09 22:55:05 +01:00
|
|
|
pub fn test_examples(cmd: impl Command + 'static) {
|
|
|
|
test_examples::test_examples(cmd);
|
|
|
|
}
|
2021-10-29 08:26:29 +02:00
|
|
|
|
2021-12-19 08:46:13 +01:00
|
|
|
#[cfg(test)]
|
2022-11-09 22:55:05 +01:00
|
|
|
mod test_examples {
|
|
|
|
use super::super::{
|
2023-02-24 16:54:42 +01:00
|
|
|
Ansi, Date, Enumerate, Flatten, From, Get, Into, IntoString, LetEnv, Math, MathEuler,
|
|
|
|
MathPi, MathRound, ParEach, Path, Random, Sort, SortBy, Split, SplitColumn, SplitRow, Str,
|
|
|
|
StrJoin, StrLength, StrReplace, Update, Url, Values, Wrap,
|
2022-11-09 22:55:05 +01:00
|
|
|
};
|
2023-02-24 16:54:42 +01:00
|
|
|
use crate::{Each, To};
|
2023-02-27 22:58:56 +01:00
|
|
|
use nu_cmd_lang::example_support::{
|
|
|
|
check_all_signature_input_output_types_entries_have_examples,
|
|
|
|
check_example_evaluates_to_expected_output,
|
|
|
|
check_example_input_and_output_types_match_command_signature,
|
|
|
|
};
|
2023-02-24 16:54:42 +01:00
|
|
|
use nu_cmd_lang::{Break, Echo, If, Let, Mut};
|
2022-11-09 22:55:05 +01:00
|
|
|
use nu_protocol::{
|
2023-02-27 22:58:56 +01:00
|
|
|
engine::{Command, EngineState, StateWorkingSet},
|
|
|
|
Type,
|
2022-11-09 22:55:05 +01:00
|
|
|
};
|
2023-01-29 23:31:35 +01:00
|
|
|
use std::collections::HashSet;
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
pub fn test_examples(cmd: impl Command + 'static) {
|
|
|
|
let examples = cmd.examples();
|
|
|
|
let signature = cmd.signature();
|
|
|
|
let mut engine_state = make_engine_state(cmd.clone_box());
|
2021-12-24 01:16:50 +01:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
let mut witnessed_type_transformations = HashSet::<(Type, Type)>::new();
|
2021-10-09 15:10:10 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
for example in examples {
|
|
|
|
if example.result.is_none() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
witnessed_type_transformations.extend(
|
|
|
|
check_example_input_and_output_types_match_command_signature(
|
|
|
|
&example,
|
|
|
|
&cwd,
|
|
|
|
&mut make_engine_state(cmd.clone_box()),
|
|
|
|
&signature.input_output_types,
|
|
|
|
signature.operates_on_cell_paths(),
|
|
|
|
signature.vectorizes_over_list,
|
|
|
|
),
|
|
|
|
);
|
2023-01-29 23:31:35 +01:00
|
|
|
check_example_evaluates_to_expected_output(&example, cwd.as_path(), &mut engine_state);
|
2022-11-09 22:55:05 +01:00
|
|
|
}
|
2022-07-14 16:09:27 +02:00
|
|
|
|
2022-11-09 22:55:05 +01:00
|
|
|
check_all_signature_input_output_types_entries_have_examples(
|
|
|
|
signature,
|
|
|
|
witnessed_type_transformations,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_engine_state(cmd: Box<dyn Command>) -> Box<EngineState> {
|
|
|
|
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
|
2022-12-07 19:31:57 +01:00
|
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Ansi));
|
|
|
|
working_set.add_decl(Box::new(Break));
|
|
|
|
working_set.add_decl(Box::new(Date));
|
2023-01-04 06:08:05 +01:00
|
|
|
working_set.add_decl(Box::new(Each));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Echo));
|
|
|
|
working_set.add_decl(Box::new(Enumerate));
|
|
|
|
working_set.add_decl(Box::new(Flatten));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(From));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Get));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(If));
|
|
|
|
working_set.add_decl(Box::new(Into));
|
2022-12-23 19:49:19 +01:00
|
|
|
working_set.add_decl(Box::new(IntoString));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Let));
|
|
|
|
working_set.add_decl(Box::new(LetEnv));
|
|
|
|
working_set.add_decl(Box::new(Math));
|
|
|
|
working_set.add_decl(Box::new(MathEuler));
|
|
|
|
working_set.add_decl(Box::new(MathPi));
|
|
|
|
working_set.add_decl(Box::new(MathRound));
|
|
|
|
working_set.add_decl(Box::new(Mut));
|
|
|
|
working_set.add_decl(Box::new(Path));
|
|
|
|
working_set.add_decl(Box::new(ParEach));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(Random));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Sort));
|
|
|
|
working_set.add_decl(Box::new(SortBy));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(Split));
|
|
|
|
working_set.add_decl(Box::new(SplitColumn));
|
|
|
|
working_set.add_decl(Box::new(SplitRow));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Str));
|
|
|
|
working_set.add_decl(Box::new(StrJoin));
|
|
|
|
working_set.add_decl(Box::new(StrLength));
|
|
|
|
working_set.add_decl(Box::new(StrReplace));
|
|
|
|
working_set.add_decl(Box::new(To));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(Url));
|
2023-02-02 23:59:58 +01:00
|
|
|
working_set.add_decl(Box::new(Update));
|
2022-12-23 19:49:19 +01:00
|
|
|
working_set.add_decl(Box::new(Values));
|
2022-11-09 22:55:05 +01:00
|
|
|
working_set.add_decl(Box::new(Wrap));
|
|
|
|
// Adding the command that is being tested to the working set
|
|
|
|
working_set.add_decl(cmd);
|
|
|
|
|
|
|
|
working_set.render()
|
|
|
|
};
|
|
|
|
|
|
|
|
engine_state
|
|
|
|
.merge_delta(delta)
|
|
|
|
.expect("Error merging delta");
|
|
|
|
engine_state
|
|
|
|
}
|
2021-10-09 15:10:10 +02:00
|
|
|
}
|