2023-02-24 16:54:42 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
use nu_protocol::engine::Command;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub fn test_examples(cmd: impl Command + 'static) {
|
|
|
|
test_examples::test_examples(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_examples {
|
2023-02-27 22:58:56 +01:00
|
|
|
use crate::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-05-06 20:39:54 +02:00
|
|
|
use crate::{
|
2023-11-19 16:25:09 +01:00
|
|
|
Break, Collect, Def, Describe, Echo, ExportCommand, ExportDef, If, Let, Module, Mut, Use,
|
2023-05-06 20:39:54 +02:00
|
|
|
};
|
2023-02-24 16:54:42 +01:00
|
|
|
use nu_protocol::{
|
2023-02-27 22:58:56 +01:00
|
|
|
engine::{Command, EngineState, StateWorkingSet},
|
2023-05-06 20:39:54 +02:00
|
|
|
Type, Value,
|
2023-02-24 16:54:42 +01:00
|
|
|
};
|
revert: move to ahash (#9464)
This PR reverts https://github.com/nushell/nushell/pull/9391
We try not to revert PRs like this, though after discussion with the
Nushell team, we decided to revert this one.
The main reason is that Nushell, as a codebase, isn't ready for these
kinds of optimisations. It's in the part of the development cycle where
our main focus should be on improving the algorithms inside of Nushell
itself. Once we have matured our algorithms, then we can look for
opportunities to switch out technologies we're using for alternate
forms.
Much of Nushell still has lots of opportunities for tuning the codebase,
paying down technical debt, and making the codebase generally cleaner
and more robust. This should be the focus. Performance improvements
should flow out of that work.
Said another, optimisation that isn't part of tuning the codebase is
premature at this stage. We need to focus on doing the hard work of
making the engine, parser, etc better.
# User-Facing Changes
Reverts the HashMap -> ahash change.
cc @FilipAndersson245
2023-06-18 05:27:57 +02:00
|
|
|
use std::collections::HashSet;
|
2023-02-24 16:54:42 +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());
|
|
|
|
|
|
|
|
let cwd = std::env::current_dir().expect("Could not get current working directory.");
|
|
|
|
|
|
|
|
let mut witnessed_type_transformations = HashSet::<(Type, Type)>::new();
|
|
|
|
|
|
|
|
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(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
check_example_evaluates_to_expected_output(&example, cwd.as_path(), &mut engine_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2023-05-06 20:39:54 +02:00
|
|
|
let cwd = std::env::current_dir()
|
|
|
|
.expect("Could not get current working directory.")
|
|
|
|
.to_string_lossy()
|
|
|
|
.to_string();
|
|
|
|
engine_state.add_env_var("PWD".to_string(), Value::test_string(cwd));
|
2023-02-24 16:54:42 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
working_set.add_decl(Box::new(Break));
|
2023-05-06 20:39:54 +02:00
|
|
|
working_set.add_decl(Box::new(Collect));
|
|
|
|
working_set.add_decl(Box::new(Def));
|
2023-02-24 16:54:42 +01:00
|
|
|
working_set.add_decl(Box::new(Describe));
|
|
|
|
working_set.add_decl(Box::new(Echo));
|
2023-05-06 20:39:54 +02:00
|
|
|
working_set.add_decl(Box::new(ExportCommand));
|
|
|
|
working_set.add_decl(Box::new(ExportDef));
|
2023-02-24 16:54:42 +01:00
|
|
|
working_set.add_decl(Box::new(If));
|
|
|
|
working_set.add_decl(Box::new(Let));
|
2023-05-06 20:39:54 +02:00
|
|
|
working_set.add_decl(Box::new(Module));
|
2023-02-24 16:54:42 +01:00
|
|
|
working_set.add_decl(Box::new(Mut));
|
2023-05-06 20:39:54 +02:00
|
|
|
working_set.add_decl(Box::new(Use));
|
2023-02-24 16:54:42 +01:00
|
|
|
|
|
|
|
// Adding the command that is being tested to the working set
|
|
|
|
working_set.add_decl(cmd);
|
|
|
|
|
|
|
|
working_set.render()
|
|
|
|
};
|
|
|
|
|
2023-06-25 14:43:48 +02:00
|
|
|
engine_state.add_env_var("PWD".to_string(), Value::test_string("."));
|
|
|
|
|
2023-02-24 16:54:42 +01:00
|
|
|
engine_state
|
|
|
|
.merge_delta(delta)
|
|
|
|
.expect("Error merging delta");
|
|
|
|
engine_state
|
|
|
|
}
|
|
|
|
}
|