2020-06-20 01:20:25 +02:00
|
|
|
mod avg;
|
2020-07-18 06:11:19 +02:00
|
|
|
mod eval;
|
2020-06-18 23:37:18 +02:00
|
|
|
mod median;
|
2020-07-18 06:11:19 +02:00
|
|
|
mod sum;
|
2020-06-18 23:37:18 +02:00
|
|
|
|
2020-04-18 03:50:58 +02:00
|
|
|
use nu_test_support::{nu, pipeline};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_arg() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "1");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 + 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "2");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-02 05:24:32 +01:00
|
|
|
fn add_compound() {
|
2020-04-18 03:50:58 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 + 2 + 2
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "5");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn precedence_of_operators() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 + 2 * 2
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "5");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn precedence_of_operators2() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 + 2 * 2 + 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "6");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn division_of_ints() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 4 / 2
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "2");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn division_of_ints2() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 / 4
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "0.25");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
2020-06-21 22:50:43 +02:00
|
|
|
#[test]
|
|
|
|
fn error_zero_division_int_int() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 / 0
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("division by zero"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_zero_division_decimal_int() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1.0 / 0
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("division by zero"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_zero_division_int_decimal() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1 / 0.0
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("division by zero"));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_zero_division_decimal_decimal() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 1.0 / 0.0
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert!(actual.err.contains("division by zero"));
|
|
|
|
}
|
|
|
|
|
2020-06-11 19:17:08 +02:00
|
|
|
#[test]
|
|
|
|
fn proper_precedence_history() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 2 / 2 / 2 + 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1.5");
|
|
|
|
}
|
|
|
|
|
2020-04-18 03:50:58 +02:00
|
|
|
#[test]
|
|
|
|
fn parens_precedence() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 4 * (6 - 3)
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "12");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 02:12:55 +02:00
|
|
|
#[test]
|
|
|
|
fn modulo() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 9 mod 2
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
|
|
|
assert_eq!(actual.out, "1");
|
|
|
|
}
|
|
|
|
|
2020-05-11 03:44:49 +02:00
|
|
|
#[test]
|
|
|
|
fn duration_math() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2020-08-15 21:03:28 +02:00
|
|
|
= 1wk + 1day
|
2020-05-11 03:44:49 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-08-15 21:03:28 +02:00
|
|
|
assert_eq!(actual.out, "8day");
|
2020-07-10 19:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_math_with_nanoseconds() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2020-08-15 21:03:28 +02:00
|
|
|
= 1wk + 10ns
|
2020-07-10 19:48:11 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-08-15 21:03:28 +02:00
|
|
|
assert_eq!(actual.out, "7day 10ns");
|
2020-07-10 19:48:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn duration_math_with_negative() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2020-08-15 21:03:28 +02:00
|
|
|
= 1day - 1wk
|
2020-07-10 19:48:11 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-08-15 21:03:28 +02:00
|
|
|
assert_eq!(actual.out, "-6day");
|
2020-05-11 03:44:49 +02:00
|
|
|
}
|
|
|
|
|
2020-04-18 03:50:58 +02:00
|
|
|
#[test]
|
|
|
|
fn compound_comparison() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 4 > 3 && 2 > 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "true");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compound_comparison2() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
|
|
|
= 4 < 3 || 2 > 1
|
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, "true");
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compound_where() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2020-05-04 10:44:33 +02:00
|
|
|
echo '[{"a": 1, "b": 1}, {"a": 2, "b": 1}, {"a": 2, "b": 2}]' | from json | where a == 2 && b == 1 | to json
|
2020-04-18 03:50:58 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, r#"{"a":2,"b":1}"#);
|
2020-04-18 03:50:58 +02:00
|
|
|
}
|
2020-04-18 20:39:06 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn compound_where_paren() {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: "tests/fixtures/formats", pipeline(
|
|
|
|
r#"
|
2020-05-04 10:44:33 +02:00
|
|
|
echo '[{"a": 1, "b": 1}, {"a": 2, "b": 1}, {"a": 2, "b": 2}]' | from json | where (a == 2 && b == 1) || b == 2 | to json
|
2020-04-18 20:39:06 +02:00
|
|
|
"#
|
|
|
|
));
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(actual.out, r#"[{"a":2,"b":1},{"a":2,"b":2}]"#);
|
2020-04-18 20:39:06 +02:00
|
|
|
}
|