From 6edf91dcaecd59a67a9316e599cdef1ce48da308 Mon Sep 17 00:00:00 2001 From: tomoda <40169443+yukitomoda@users.noreply.github.com> Date: Sun, 21 Jan 2024 22:43:40 +0900 Subject: [PATCH] Allow string to copmpare with another string (#11590) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Nushell parser now reject comparison operator with 2 strings (e.g. `"abc" < "cba"`). This pr fixes it. ## before ```nu ~ ❯ "abc" < "bca" Error: nu::parser::unsupported_operation × less-than comparison is not supported on values of type string ╭─[entry #43:1:1] 1 │ "abc" < "bca" · ──┬── ┬ · │ ╰── doesn't support this value · ╰── string ╰──── ~ ❯ def foo []: nothing -> string { "abc" } ~ ❯ (foo) < "bca" Error: nu::parser::unsupported_operation × less-than comparison is not supported on values of type string ╭─[entry #53:1:1] 1 │ (foo) < "bca" · ──┬── ┬ · │ ╰── doesn't support this value · ╰── string ╰──── ``` ## after ```nu ~ ❯ "abc" < "bca" true ~ ❯ def foo []: nothing -> string { "abc" } ~ ❯ (foo) < "bca" true ``` # User-Facing Changes Following pattern will be allowed. | operator | type of lhs | type of rhs | result | | -------- | ----------- | ----------- | ------ | | `<` | string | string | bool | | `<=` | string | string | bool | | `>` | string | string | bool | | `>=` | string | string | bool | # Tests + Formatting - [x] `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - [x] `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - [x] `cargo run -- -c "use std testing; testing run-tests --path crates/nu-std"` to run the tests for the standard library # After Submitting --- crates/nu-parser/src/type_check.rs | 4 ++++ crates/nu-parser/tests/test_parser.rs | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/crates/nu-parser/src/type_check.rs b/crates/nu-parser/src/type_check.rs index be355e087a..8c2eda8a7e 100644 --- a/crates/nu-parser/src/type_check.rs +++ b/crates/nu-parser/src/type_check.rs @@ -429,6 +429,7 @@ pub fn math_result_type( (Type::Int, Type::Number) => (Type::Bool, None), (Type::Number, Type::Float) => (Type::Bool, None), (Type::Float, Type::Number) => (Type::Bool, None), + (Type::String, Type::String) => (Type::Bool, None), (Type::Duration, Type::Duration) => (Type::Bool, None), (Type::Date, Type::Date) => (Type::Bool, None), (Type::Filesize, Type::Filesize) => (Type::Bool, None), @@ -478,6 +479,7 @@ pub fn math_result_type( (Type::Int, Type::Number) => (Type::Bool, None), (Type::Number, Type::Float) => (Type::Bool, None), (Type::Float, Type::Number) => (Type::Bool, None), + (Type::String, Type::String) => (Type::Bool, None), (Type::Duration, Type::Duration) => (Type::Bool, None), (Type::Date, Type::Date) => (Type::Bool, None), (Type::Filesize, Type::Filesize) => (Type::Bool, None), @@ -527,6 +529,7 @@ pub fn math_result_type( (Type::Int, Type::Number) => (Type::Bool, None), (Type::Number, Type::Float) => (Type::Bool, None), (Type::Float, Type::Number) => (Type::Bool, None), + (Type::String, Type::String) => (Type::Bool, None), (Type::Duration, Type::Duration) => (Type::Bool, None), (Type::Date, Type::Date) => (Type::Bool, None), (Type::Filesize, Type::Filesize) => (Type::Bool, None), @@ -576,6 +579,7 @@ pub fn math_result_type( (Type::Int, Type::Number) => (Type::Bool, None), (Type::Number, Type::Float) => (Type::Bool, None), (Type::Float, Type::Number) => (Type::Bool, None), + (Type::String, Type::String) => (Type::Bool, None), (Type::Duration, Type::Duration) => (Type::Bool, None), (Type::Date, Type::Date) => (Type::Bool, None), (Type::Filesize, Type::Filesize) => (Type::Bool, None), diff --git a/crates/nu-parser/tests/test_parser.rs b/crates/nu-parser/tests/test_parser.rs index 0f7faf6fa9..b984f97ba9 100644 --- a/crates/nu-parser/tests/test_parser.rs +++ b/crates/nu-parser/tests/test_parser.rs @@ -1915,3 +1915,27 @@ mod input_types { ) } } + +#[cfg(test)] +mod operator { + use super::*; + + #[rstest] + #[case(br#""abc" < "bca""#, "string < string")] + #[case(br#""abc" <= "bca""#, "string <= string")] + #[case(br#""abc" > "bca""#, "string > string")] + #[case(br#""abc" >= "bca""#, "string >= string")] + fn parse_comparison_operators_with_string_and_string( + #[case] expr: &[u8], + #[case] test_tag: &str, + ) { + let engine_state = EngineState::new(); + let mut working_set = StateWorkingSet::new(&engine_state); + parse(&mut working_set, None, expr, false); + assert_eq!( + working_set.parse_errors.len(), + 0, + "{test_tag}: expected to be parsed successfully, but failed." + ); + } +}