mirror of
https://github.com/nushell/nushell.git
synced 2025-04-29 15:44:28 +02:00
# Description This PR adds two new `ParseError` and `ShellError` cases for type errors relating to operators. - `OperatorUnsupportedType` is used when a type is not supported by an operator in any way, shape, or form. E.g., `+` does not support `bool`. - `OperatorIncompatibleTypes` is used when a operator is used with types it supports, but the combination of types provided cannot be used together. E.g., `filesize + duration` is not a valid combination. The other preexisting error cases related to operators have been removed and replaced with the new ones above. Namely: - `ShellError::OperatorMismatch` - `ShellError::UnsupportedOperator` - `ParseError::UnsupportedOperationLHS` - `ParseError::UnsupportedOperationRHS` - `ParseError::UnsupportedOperationTernary` # User-Facing Changes - `help operators` now lists the precedence of `not` as 55 instead of 0 (above the other boolean operators). Fixes #13675. - `math median` and `math mode` now ignore NaN values so that `[NaN NaN] | math median` and `[NaN NaN] | math mode` no longer trigger a type error. Instead, it's now an empty input error. Fixing this in earnest can be left for a future PR. - Comparisons with `nan` now return false instead of causing an error. E.g., `1 == nan` is now `false`. - All the operator type errors have been standardized and reworked. In particular, they can now have a help message, which is currently used for types errors relating to `++`. ```nu [1] ++ 2 ``` ``` Error: nu::parser::operator_unsupported_type × The '++' operator does not work on values of type 'int'. ╭─[entry #1:1:5] 1 │ [1] ++ 2 · ─┬ ┬ · │ ╰── int · ╰── does not support 'int' ╰──── help: if you meant to append a value to a list or a record to a table, use the `append` command or wrap the value in a list. For example: `$list ++ $value` should be `$list ++ [$value]` or `$list | append $value`. ```
178 lines
4.8 KiB
Rust
178 lines
4.8 KiB
Rust
use crate::repl::tests::{fail_test, run_test, TestResult};
|
|
|
|
#[test]
|
|
fn cjk_in_substrings() -> TestResult {
|
|
run_test(
|
|
r#"let s = '[Rust 程序设计语言](title-page.md)'; let start = ($s | str index-of '('); let end = ($s | str index-of ')'); $s | str substring ($start + 1)..<($end)"#,
|
|
"title-page.md",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn string_not_in_string() -> TestResult {
|
|
run_test(r#"'d' not-in 'abc'"#, "true")
|
|
}
|
|
|
|
#[test]
|
|
fn string_in_string() -> TestResult {
|
|
run_test(r#"'z' in 'abc'"#, "false")
|
|
}
|
|
|
|
#[test]
|
|
fn non_string_in_string() -> TestResult {
|
|
fail_test(r#"42 in 'abc'"#, "nu::parser::operator_incompatible_types")
|
|
}
|
|
|
|
#[test]
|
|
fn string_in_record() -> TestResult {
|
|
run_test(r#""a" in ('{ "a": 13, "b": 14 }' | from json)"#, "true")
|
|
}
|
|
|
|
#[test]
|
|
fn non_string_in_record() -> TestResult {
|
|
fail_test(
|
|
r#"4 in ('{ "a": 13, "b": 14 }' | from json)"#,
|
|
"nu::shell::operator_incompatible_types",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn unbalance_string() -> TestResult {
|
|
fail_test(r#""aaaab"cc"#, "invalid characters")?;
|
|
fail_test(r#"'aaaab'cc"#, "invalid characters")
|
|
}
|
|
|
|
#[test]
|
|
fn string_in_valuestream() -> TestResult {
|
|
run_test(
|
|
r#"
|
|
'Hello' in ("Hello
|
|
World" | lines)"#,
|
|
"true",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn single_tick_interpolation() -> TestResult {
|
|
run_test(r#"$'(3 + 4)'"#, "7")
|
|
}
|
|
|
|
#[test]
|
|
fn detect_newlines() -> TestResult {
|
|
run_test("'hello\r\nworld' | lines | get 0 | str length", "5")
|
|
}
|
|
|
|
#[test]
|
|
fn case_insensitive_sort() -> TestResult {
|
|
run_test(
|
|
r#"[a, B, d, C, f] | sort -i | to json --raw"#,
|
|
"[\"a\",\"B\",\"C\",\"d\",\"f\"]",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn case_insensitive_sort_columns() -> TestResult {
|
|
run_test(
|
|
r#"[[version, package]; ["two", "Abc"], ["three", "abc"], ["four", "abc"]] | sort-by -i package version | to json --raw"#,
|
|
r#"[{"version":"four","package":"abc"},{"version":"three","package":"abc"},{"version":"two","package":"Abc"}]"#,
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn raw_string() -> TestResult {
|
|
run_test(r#"r#'abcde""fghi"''''jkl'#"#, r#"abcde""fghi"''''jkl"#)?;
|
|
run_test(r#"r##'abcde""fghi"''''#jkl'##"#, r#"abcde""fghi"''''#jkl"#)?;
|
|
run_test(
|
|
r#"r###'abcde""fghi"'''##'#jkl'###"#,
|
|
r#"abcde""fghi"'''##'#jkl"#,
|
|
)?;
|
|
run_test("r#''#", "")?;
|
|
run_test(
|
|
r#"r#'a string with sharp inside # and ends with #'#"#,
|
|
"a string with sharp inside # and ends with #",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn raw_string_inside_parentheses() -> TestResult {
|
|
let (left, right) = ('(', ')');
|
|
run_test(
|
|
&format!(r#"{left}r#'abcde""fghi"''''jkl'#{right}"#),
|
|
r#"abcde""fghi"''''jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"{left}r##'abcde""fghi"''''#jkl'##{right}"#),
|
|
r#"abcde""fghi"''''#jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"{left}r###'abcde""fghi"'''##'#jkl'###{right}"#),
|
|
r#"abcde""fghi"'''##'#jkl"#,
|
|
)?;
|
|
run_test(&format!("{left}r#''#{right}"), "")?;
|
|
run_test(
|
|
&format!(r#"{left}r#'a string with sharp inside # and ends with #'#{right}"#),
|
|
"a string with sharp inside # and ends with #",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn raw_string_inside_list() -> TestResult {
|
|
let (left, right) = ('[', ']');
|
|
run_test(
|
|
&format!(r#"{left}r#'abcde""fghi"''''jkl'#{right} | get 0"#),
|
|
r#"abcde""fghi"''''jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"{left}r##'abcde""fghi"''''#jkl'##{right} | get 0"#),
|
|
r#"abcde""fghi"''''#jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"{left}r###'abcde""fghi"'''##'#jkl'###{right} | get 0"#),
|
|
r#"abcde""fghi"'''##'#jkl"#,
|
|
)?;
|
|
run_test(&format!("{left}r#''#{right} | get 0"), "")?;
|
|
run_test(
|
|
&format!(r#"{left}r#'a string with sharp inside # and ends with #'#{right} | get 0"#),
|
|
"a string with sharp inside # and ends with #",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn raw_string_inside_closure() -> TestResult {
|
|
let (left, right) = ('{', '}');
|
|
run_test(
|
|
&format!(r#"do {left}r#'abcde""fghi"''''jkl'#{right}"#),
|
|
r#"abcde""fghi"''''jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"do {left}r##'abcde""fghi"''''#jkl'##{right}"#),
|
|
r#"abcde""fghi"''''#jkl"#,
|
|
)?;
|
|
run_test(
|
|
&format!(r#"do {left}r###'abcde""fghi"'''##'#jkl'###{right}"#),
|
|
r#"abcde""fghi"'''##'#jkl"#,
|
|
)?;
|
|
run_test(&format!("do {left}r#''#{right}"), "")?;
|
|
run_test(
|
|
&format!(r#"do {left}r#'a string with sharp inside # and ends with #'#{right}"#),
|
|
"a string with sharp inside # and ends with #",
|
|
)
|
|
}
|
|
|
|
#[test]
|
|
fn incomplete_string() -> TestResult {
|
|
fail_test("r#abc", "expected '")?;
|
|
fail_test("r#'bc", "expected closing '#")?;
|
|
fail_test("'ab\"", "expected closing '")?;
|
|
fail_test("\"ab'", "expected closing \"")?;
|
|
fail_test(
|
|
r#"def func [] {
|
|
{
|
|
"A": ""B" # <- the quote is bad
|
|
}
|
|
}
|
|
"#,
|
|
"expected closing \"",
|
|
)
|
|
}
|