2023-07-12 19:07:20 +02:00
|
|
|
use nu_test_support::nu;
|
2023-04-08 20:52:37 +02:00
|
|
|
use pretty_assertions::assert_eq;
|
2023-09-05 16:35:58 +02:00
|
|
|
use rstest::rstest;
|
2022-12-21 23:21:03 +01:00
|
|
|
|
Recursively export constants from modules (#10049)
<!--
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.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# 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.
-->
2023-08-20 14:51:35 +02:00
|
|
|
const MODULE_SETUP: &str = r#"
|
|
|
|
module spam {
|
|
|
|
export const X = 'x'
|
|
|
|
export module eggs {
|
|
|
|
export const E = 'e'
|
|
|
|
export module bacon {
|
|
|
|
export const viking = 'eats'
|
|
|
|
export module none {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#;
|
|
|
|
|
2022-12-21 23:21:03 +01:00
|
|
|
#[test]
|
|
|
|
fn const_bool() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = false", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "false");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_int() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = 10", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "10");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_float() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = 1.234", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "1.234");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_binary() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = 0x[12]", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("12"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_datetime() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = 2021-02-27T13:55:40+00:00", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert!(actual.out.contains("Sat, 27 Feb 2021 13:55:40"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_list() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = [ a b c ]", "$x | describe"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "list<string>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_record() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = { a: 10, b: 20, c: 30 }", "$x | describe"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "record<a: int, b: int, c: int>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_table() {
|
|
|
|
let inp = &[
|
2023-07-12 19:07:20 +02:00
|
|
|
"const x = [[a b c]; [10 20 30] [100 200 300]]",
|
|
|
|
"$x | describe",
|
2022-12-21 23:21:03 +01:00
|
|
|
];
|
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "table<a: int, b: int, c: int>");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_string() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &[r#"const x = "abc""#, "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "abc");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_nothing() {
|
2023-07-12 19:07:20 +02:00
|
|
|
let inp = &["const x = $nothing", "$x | describe"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
|
|
|
assert_eq!(actual.out, "nothing");
|
|
|
|
}
|
|
|
|
|
2023-09-05 16:35:58 +02:00
|
|
|
#[rstest]
|
|
|
|
#[case(&["const x = not false", "$x"], "true")]
|
|
|
|
#[case(&["const x = false", "const y = not $x", "$y"], "true")]
|
|
|
|
#[case(&["const x = not false", "const y = not $x", "$y"], "false")]
|
|
|
|
fn const_unary_operator(#[case] inp: &[&str], #[case] expect: &str) {
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&["const x = 1 + 2", "$x"], "3")]
|
|
|
|
#[case(&["const x = 1 * 2", "$x"], "2")]
|
|
|
|
#[case(&["const x = 4 / 2", "$x"], "2")]
|
|
|
|
#[case(&["const x = 4 mod 3", "$x"], "1")]
|
|
|
|
#[case(&["const x = 5.0 / 2.0", "$x"], "2.5")]
|
|
|
|
#[case(&[r#"const x = "a" + "b" "#, "$x"], "ab")]
|
|
|
|
#[case(&[r#"const x = "a" ++ "b" "#, "$x"], "ab")]
|
|
|
|
#[case(&[r#"const x = [1,2] ++ [3]"#, "$x | describe"], "list<int>")]
|
|
|
|
#[case(&[r#"const x = 0x[1,2] ++ 0x[3]"#, "$x | describe"], "binary")]
|
|
|
|
#[case(&[r#"const x = 0x[1,2] ++ [3]"#, "$x | describe"], "list<any>")]
|
|
|
|
#[case(&["const x = 1 < 2", "$x"], "true")]
|
|
|
|
#[case(&["const x = (3 * 200) > (2 * 100)", "$x"], "true")]
|
|
|
|
#[case(&["const x = (3 * 200) < (2 * 100)", "$x"], "false")]
|
|
|
|
#[case(&["const x = (3 * 200) == (2 * 300)", "$x"], "true")]
|
|
|
|
fn const_binary_operator(#[case] inp: &[&str], #[case] expect: &str) {
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, expect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&["const x = 1 / 0", "$x"], "division by zero")]
|
|
|
|
#[case(&["const x = 10 ** 10000000", "$x"], "pow operation overflowed")]
|
|
|
|
#[case(&["const x = 2 ** 62 * 2", "$x"], "multiply operation overflowed")]
|
|
|
|
#[case(&["const x = 1 ++ 0", "$x"], "doesn't support this value")]
|
|
|
|
#[case(&["const x = 20 * a", "$x"], "Value is not a parse-time constant")]
|
|
|
|
fn const_operator_error(#[case] inp: &[&str], #[case] expect: &str) {
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert!(actual.err.contains(expect));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rstest]
|
|
|
|
#[case(&["const x = (1..3)", "$x | math sum"], "6")]
|
|
|
|
#[case(&["const x = (1..3)", "$x | describe"], "range")]
|
|
|
|
fn const_range(#[case] inp: &[&str], #[case] expect: &str) {
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, expect);
|
|
|
|
}
|
|
|
|
|
2022-12-21 23:21:03 +01:00
|
|
|
#[test]
|
2023-08-26 15:41:29 +02:00
|
|
|
fn const_subexpression_supported() {
|
|
|
|
let inp = &["const x = ('spam')", "$x"];
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-07-12 19:07:20 +02:00
|
|
|
let actual = nu!(&inp.join("; "));
|
2022-12-21 23:21:03 +01:00
|
|
|
|
2023-08-26 15:41:29 +02:00
|
|
|
assert_eq!(actual.out, "spam");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_command_supported() {
|
|
|
|
let inp = &["const x = ('spam' | str length)", "$x"];
|
|
|
|
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "4");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_command_unsupported() {
|
|
|
|
let inp = &["const x = (loop { break })"];
|
|
|
|
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("not_a_const_command"));
|
2022-12-21 23:21:03 +01:00
|
|
|
}
|
2023-07-13 23:02:05 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_in_scope() {
|
|
|
|
let inp = &["do { const x = 'x'; $x }"];
|
|
|
|
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "x");
|
|
|
|
}
|
Recursively export constants from modules (#10049)
<!--
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.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# 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.
-->
2023-08-20 14:51:35 +02:00
|
|
|
|
2023-08-26 15:41:29 +02:00
|
|
|
#[test]
|
|
|
|
fn not_a_const_help() {
|
|
|
|
let actual = nu!("const x = ('abc' | str length -h)");
|
|
|
|
assert!(actual.err.contains("not_a_const_help"));
|
|
|
|
}
|
|
|
|
|
Recursively export constants from modules (#10049)
<!--
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.
-->
https://github.com/nushell/nushell/pull/9773 introduced constants to
modules and allowed to export them, but only within one level. This PR:
* allows recursive exporting of constants from all submodules
* fixes submodule imports in a list import pattern
* makes sure exported constants are actual constants
Should unblock https://github.com/nushell/nushell/pull/9678
### Example:
```nushell
module spam {
export module eggs {
export module bacon {
export const viking = 'eats'
}
}
}
use spam
print $spam.eggs.bacon.viking # prints 'eats'
use spam [eggs]
print $eggs.bacon.viking # prints 'eats'
use spam eggs bacon viking
print $viking # prints 'eats'
```
### Limitation 1:
Considering the above `spam` module, attempting to get `eggs bacon` from
`spam` module doesn't work directly:
```nushell
use spam [ eggs bacon ] # attempts to load `eggs`, then `bacon`
use spam [ "eggs bacon" ] # obviously wrong name for a constant, but doesn't work also for commands
```
Workaround (for example):
```nushell
use spam eggs
use eggs [ bacon ]
print $bacon.viking # prints 'eats'
```
I'm thinking I'll just leave it in, as you can easily work around this.
It is also a limitation of the import pattern in general, not just
constants.
### Limitation 2:
`overlay use` successfully imports the constants, but `overlay hide`
does not hide them, even though it seems to hide normal variables
successfully. This needs more investigation.
# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
Allows recursive constant exports from submodules.
# 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.
-->
2023-08-20 14:51:35 +02:00
|
|
|
#[test]
|
|
|
|
fn complex_const_export() {
|
|
|
|
let inp = &[MODULE_SETUP, "use spam", "$spam.X"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "x");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "use spam", "$spam.eggs.E"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "e");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "use spam", "$spam.eggs.bacon.viking"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "eats");
|
|
|
|
|
|
|
|
let inp = &[
|
|
|
|
MODULE_SETUP,
|
|
|
|
"use spam",
|
|
|
|
"($spam.eggs.bacon.none | is-empty)",
|
|
|
|
];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_const_glob_export() {
|
|
|
|
let inp = &[MODULE_SETUP, "use spam *", "$X"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "x");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "use spam *", "$eggs.E"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "e");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "use spam *", "$eggs.bacon.viking"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "eats");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "use spam *", "($eggs.bacon.none | is-empty)"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_const_drill_export() {
|
|
|
|
let inp = &[
|
|
|
|
MODULE_SETUP,
|
|
|
|
"use spam eggs bacon none",
|
|
|
|
"($none | is-empty)",
|
|
|
|
];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn complex_const_list_export() {
|
|
|
|
let inp = &[
|
|
|
|
MODULE_SETUP,
|
|
|
|
"use spam [X eggs]",
|
|
|
|
"[$X $eggs.E] | str join ''",
|
|
|
|
];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "xe");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn exported_const_is_const() {
|
|
|
|
let module1 = "module foo {
|
|
|
|
export def main [] { 'foo' }
|
|
|
|
}";
|
|
|
|
|
|
|
|
let module2 = "module spam {
|
|
|
|
export const MOD_NAME = 'foo'
|
|
|
|
}";
|
|
|
|
|
|
|
|
let inp = &[module1, module2, "use spam", "use $spam.MOD_NAME", "foo"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn const_captures_work() {
|
|
|
|
let module = "module spam {
|
|
|
|
export const X = 'x'
|
|
|
|
const Y = 'y'
|
|
|
|
|
|
|
|
export-env { $env.SPAM = $X + $Y }
|
|
|
|
export def main [] { $X + $Y }
|
|
|
|
}";
|
|
|
|
|
|
|
|
let inp = &[module, "use spam", "$env.SPAM"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "xy");
|
|
|
|
|
|
|
|
let inp = &[module, "use spam", "spam"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "xy");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ignore = "TODO: Need to fix `overlay hide` to hide the constants brough by `overlay use`"]
|
|
|
|
#[test]
|
|
|
|
fn complex_const_overlay_use_hide() {
|
|
|
|
let inp = &[MODULE_SETUP, "overlay use spam", "$X"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "x");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "overlay use spam", "$eggs.E"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "e");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "overlay use spam", "$eggs.bacon.viking"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "eats");
|
|
|
|
|
|
|
|
let inp = &[
|
|
|
|
MODULE_SETUP,
|
|
|
|
"overlay use spam",
|
|
|
|
"($eggs.bacon.none | is-empty)",
|
|
|
|
];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
|
|
|
|
let inp = &[MODULE_SETUP, "overlay use spam", "overlay hide", "$eggs"];
|
|
|
|
let actual = nu!(&inp.join("; "));
|
|
|
|
assert!(actual.err.contains("nu::parser::variable_not_found"));
|
|
|
|
}
|
2023-08-26 15:41:29 +02:00
|
|
|
|
|
|
|
// const implementations of commands without dedicated tests
|
|
|
|
#[test]
|
|
|
|
fn describe_const() {
|
|
|
|
let actual = nu!("const x = ('abc' | describe); $x");
|
|
|
|
assert_eq!(actual.out, "string");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn ignore_const() {
|
2023-09-12 20:35:01 +02:00
|
|
|
let actual = nu!(r#"const x = ("spam" | ignore); $x == null"#);
|
2023-08-26 15:41:29 +02:00
|
|
|
assert_eq!(actual.out, "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn version_const() {
|
|
|
|
let actual = nu!("const x = (version); $x");
|
|
|
|
assert!(actual.err.is_empty());
|
|
|
|
}
|