mirror of
https://github.com/nushell/nushell.git
synced 2025-07-01 07:00:37 +02:00
Math operators (#1601)
* Add some math operations * WIP for adding compound expressions * precedence parsing * paren expressions * better lhs handling * add compound comparisons and shorthand lefthand parsing * Add or comparison and shorthand paths
This commit is contained in:
133
crates/nu-cli/tests/commands/math.rs
Normal file
133
crates/nu-cli/tests/commands/math.rs
Normal file
@ -0,0 +1,133 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn one_arg() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "1");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1 + 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_compount() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1 + 2 + 2
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precedence_of_operators() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1 + 2 * 2
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "5");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn precedence_of_operators2() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1 + 2 * 2 + 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "6");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn division_of_ints() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 4 / 2
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn division_of_ints2() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 1 / 4
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "0.25");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parens_precedence() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 4 * (6 - 3)
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "12");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_comparison() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 4 > 3 && 2 > 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_comparison2() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
= 4 < 3 || 2 > 1
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compound_where() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
echo '[{"a": 1, "b": 1}, {"a": 2, "b": 1}, {"a": 2, "b": 2}]' | from-json | where a == 2 && b == 1 | to-json
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, r#"{"a":2,"b":1}"#);
|
||||
}
|
@ -18,6 +18,7 @@ mod insert;
|
||||
mod last;
|
||||
mod lines;
|
||||
mod ls;
|
||||
mod math;
|
||||
mod mkdir;
|
||||
mod mv;
|
||||
mod open;
|
||||
|
Reference in New Issue
Block a user