mirror of
https://github.com/nushell/nushell.git
synced 2025-05-22 19:00:47 +02:00
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect -A clippy::result_large_err` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass - `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
104 lines
3.1 KiB
Rust
104 lines
3.1 KiB
Rust
use nu_engine::eval_block;
|
|
use nu_parser::parse;
|
|
use nu_protocol::{
|
|
engine::{Command, EngineState, Stack, StateWorkingSet},
|
|
Example, PipelineData, Span,
|
|
};
|
|
|
|
use super::eager::ToDataFrame;
|
|
use super::expressions::ExprCol;
|
|
use super::lazy::{LazyCollect, ToLazyFrame};
|
|
use nu_cmd_lang::Let;
|
|
|
|
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();
|
|
let mut engine_state = build_test_engine_state(cmds.clone());
|
|
|
|
for example in examples {
|
|
test_dataframe_example(&mut engine_state, &example);
|
|
}
|
|
}
|
|
|
|
pub fn build_test_engine_state(cmds: Vec<Box<dyn Command + 'static>>) -> 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
|
|
let mut working_set = StateWorkingSet::new(&engine_state);
|
|
working_set.add_decl(Box::new(Let));
|
|
working_set.add_decl(Box::new(ToDataFrame));
|
|
working_set.add_decl(Box::new(ToLazyFrame));
|
|
working_set.add_decl(Box::new(LazyCollect));
|
|
working_set.add_decl(Box::new(ExprCol));
|
|
|
|
// Adding the command that is being tested to the working set
|
|
for cmd in cmds.clone() {
|
|
working_set.add_decl(cmd);
|
|
}
|
|
|
|
working_set.render()
|
|
};
|
|
|
|
engine_state
|
|
.merge_delta(delta)
|
|
.expect("Error merging delta");
|
|
|
|
engine_state
|
|
}
|
|
|
|
pub fn test_dataframe_example(engine_state: &mut Box<EngineState>, example: &Example) {
|
|
// Skip tests that don't have results to compare to
|
|
if example.result.is_none() {
|
|
return;
|
|
}
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
let (block, delta) = {
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
let output = parse(&mut working_set, None, example.example.as_bytes(), false);
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
panic!("test parse error in `{}`: {:?}", example.example, err)
|
|
}
|
|
|
|
(output, working_set.render())
|
|
};
|
|
|
|
engine_state
|
|
.merge_delta(delta)
|
|
.expect("Error merging delta");
|
|
|
|
let mut stack = Stack::new();
|
|
|
|
let result = eval_block(
|
|
engine_state,
|
|
&mut stack,
|
|
&block,
|
|
PipelineData::empty(),
|
|
true,
|
|
true,
|
|
)
|
|
.unwrap_or_else(|err| panic!("test eval error in `{}`: {:?}", example.example, err))
|
|
.into_value(Span::test_data());
|
|
|
|
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.clone() {
|
|
if result != expected {
|
|
panic!("the example result is different to expected value: {result:?} != {expected:?}")
|
|
}
|
|
}
|
|
}
|