Merge remote-tracking branch 'upstream/master' into direnv-rewrite

This commit is contained in:
Sam Hedin
2020-06-22 16:42:54 +02:00
81 changed files with 3459 additions and 1519 deletions

View File

@ -52,6 +52,20 @@ fn cal_rows_in_2020() {
assert!(actual.out.contains("62"));
}
#[test]
fn cal_week_day_start_monday() {
let actual = nu!(
cwd: ".", pipeline(
r#"
cal --full-year 2020 -m --month-names --week-start monday | where month == january | to json
"#
));
let cal_january_json = r#"[{"month":"january","monday":null,"tuesday":null,"wednesday":1,"thursday":2,"friday":3,"saturday":4,"sunday":5},{"month":"january","monday":6,"tuesday":7,"wednesday":8,"thursday":9,"friday":10,"saturday":11,"sunday":12},{"month":"january","monday":13,"tuesday":14,"wednesday":15,"thursday":16,"friday":17,"saturday":18,"sunday":19},{"month":"january","monday":20,"tuesday":21,"wednesday":22,"thursday":23,"friday":24,"saturday":25,"sunday":26},{"month":"january","monday":27,"tuesday":28,"wednesday":29,"thursday":30,"friday":31,"saturday":null,"sunday":null}]"#;
assert_eq!(actual.out, cal_january_json);
}
#[test]
fn cal_sees_pipeline_year() {
let actual = nu!(

View File

@ -432,3 +432,27 @@ fn valuesystem_path_not_found() {
assert!(actual.err.contains("No such path exists"));
})
}
#[cfg(target_os = "windows")]
#[test]
fn test_change_windows_drive() {
Playground::setup("cd_test_20", |dirs, sandbox| {
sandbox.mkdir("test_folder");
let _actual = nu!(
cwd: dirs.test(),
r#"
subst Z: test_folder
Z:
echo "some text" | save test_file.txt
cd ~
subst Z: /d
"#
);
assert!(dirs
.test()
.join("test_folder")
.join("test_file.txt")
.exists());
})
}

View File

@ -4,7 +4,7 @@ use nu_test_support::nu;
fn drop_rows() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"echo '[{"foo": 3}, {"foo": 8}, {"foo": 4}]' | from json | drop 2 | get foo | sum | echo $it"#
r#"echo '[{"foo": 3}, {"foo": 8}, {"foo": 4}]' | from json | drop 2 | get foo | math sum | echo $it"#
);
assert_eq!(actual.out, "3");

View File

@ -5,7 +5,7 @@ fn each_works_separately() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo [1 2 3] | each { echo $it 10 | sum } | to json | echo $it
echo [1 2 3] | each { echo $it 10 | math sum } | to json | echo $it
"#
));

View File

@ -0,0 +1,245 @@
use nu_test_support::fs::Stub::EmptyFile;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};
#[test]
fn gets_all_rows_by_every_zero() {
Playground::setup("every_test_1", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 0
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ amigos.txt arepas.clu los.txt tres.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_no_rows_by_every_skip_zero() {
Playground::setup("every_test_2", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 0 --skip
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_all_rows_by_every_one() {
Playground::setup("every_test_3", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 1
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ amigos.txt arepas.clu los.txt tres.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_no_rows_by_every_skip_one() {
Playground::setup("every_test_4", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 1 --skip
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_first_row_by_every_too_much() {
Playground::setup("every_test_5", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 999
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ amigos.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_all_rows_except_first_by_every_skip_too_much() {
Playground::setup("every_test_6", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 999 --skip
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ arepas.clu los.txt tres.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn gets_every_third_row() {
Playground::setup("every_test_7", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("quatro.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 3
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ amigos.txt quatro.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}
#[test]
fn skips_every_third_row() {
Playground::setup("every_test_8", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("los.txt"),
EmptyFile("tres.txt"),
EmptyFile("quatro.txt"),
EmptyFile("amigos.txt"),
EmptyFile("arepas.clu"),
]);
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
ls
| get name
| every 3 --skip
"#
));
let expected = nu!(
cwd: dirs.test(), pipeline(
r#"
echo [ arepas.clu los.txt tres.txt ]
"#
));
assert_eq!(actual.out, expected.out);
})
}

View File

@ -22,7 +22,7 @@ fn adds_value_provided_if_column_is_empty() {
open likes.csv
| empty? likes 1
| get likes
| sum
| math sum
| echo $it
"#
));
@ -43,7 +43,7 @@ fn adds_value_provided_for_columns_that_are_empty() {
{"boost": 1, "check": {}},
{"boost": null, "check": ["" {} [] ""]}
]
"#,
)]);
@ -53,7 +53,7 @@ fn adds_value_provided_for_columns_that_are_empty() {
open checks.json
| empty? boost check 1
| get boost check
| sum
| math sum
| echo $it
"#
));

View File

@ -22,7 +22,7 @@ fn rows() {
open caballeros.csv
| keep 3
| get lucky_code
| sum
| math sum
| echo $it
"#
));

View File

@ -41,7 +41,7 @@ fn condition_is_met() {
| keep-until "Chicken Collection" == "Red Chickens"
| str to-int "31/04/2020"
| get "31/04/2020"
| sum
| math sum
| echo $it
"#
));

View File

@ -41,7 +41,7 @@ fn condition_is_met() {
| keep-while "Chicken Collection" != "Blue Chickens"
| str to-int "31/04/2020"
| get "31/04/2020"
| sum
| math sum
| echo $it
"#
));

View File

@ -7,11 +7,11 @@ fn can_average_numbers() {
r#"
open sgml_description.json
| get glossary.GlossDiv.GlossList.GlossEntry.Sections
| math average
| math avg
| echo $it
"#
));
println!("{:?}", actual.err);
assert_eq!(actual.out, "101.5")
}
@ -19,7 +19,7 @@ fn can_average_numbers() {
fn can_average_bytes() {
let actual = nu!(
cwd: "tests/fixtures/formats",
"ls | sort-by name | skip 1 | first 2 | get size | math average | format \"{$it}\" | echo $it"
"ls | sort-by name | skip 1 | first 2 | get size | math avg | format \"{$it}\" | echo $it"
);
assert_eq!(actual.out, "1.6 KB");

View File

@ -0,0 +1,43 @@
use nu_test_support::{nu, pipeline};
#[test]
fn median_numbers_with_even_rows() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo [10 6 19 21 4]
| math median
| echo $it
"#
));
assert_eq!(actual.out, "10")
}
#[test]
fn median_numbers_with_odd_rows() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo [3 8 9 12 12 15]
| math median
| echo $it
"#
));
assert_eq!(actual.out, "10.5")
}
#[test]
fn median_mixed_numbers() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo [-11.5 -13.5 10]
| math median
| echo $it
"#
));
assert_eq!(actual.out, "-11.5")
}

View File

@ -1,3 +1,6 @@
mod avg;
mod median;
use nu_test_support::{nu, pipeline};
#[test]
@ -84,6 +87,54 @@ fn division_of_ints2() {
assert_eq!(actual.out, "0.25");
}
#[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"));
}
#[test]
fn proper_precedence_history() {
let actual = nu!(

View File

@ -25,7 +25,7 @@ fn all() {
open meals.json
| get meals
| get calories
| sum
| math sum
| echo $it
"#
));
@ -53,7 +53,7 @@ fn outputs_zero_with_no_input() {
let actual = nu!(
cwd: dirs.test(), pipeline(
r#"
sum
math sum
| echo $it
"#
));
@ -74,7 +74,7 @@ fn compute_sum_of_individual_row() -> Result<(), String> {
for (column_name, expected_value) in answers_for_columns.iter() {
let actual = nu!(
cwd: "tests/fixtures/formats/",
format!("open sample-ps-output.json | select {} | sum | get {}", column_name, column_name)
format!("open sample-ps-output.json | select {} | math sum | get {}", column_name, column_name)
);
let result =
f64::from_str(&actual.out).map_err(|_| String::from("Failed to parse float."))?;
@ -95,7 +95,7 @@ fn compute_sum_of_table() -> Result<(), String> {
for (column_name, expected_value) in answers_for_columns.iter() {
let actual = nu!(
cwd: "tests/fixtures/formats/",
format!("open sample-ps-output.json | select cpu mem virtual | sum | get {}", column_name)
format!("open sample-ps-output.json | select cpu mem virtual | math sum | get {}", column_name)
);
let result =
f64::from_str(&actual.out).map_err(|_| String::from("Failed to parse float."))?;
@ -108,7 +108,7 @@ fn compute_sum_of_table() -> Result<(), String> {
fn sum_of_a_row_containing_a_table_is_an_error() {
let actual = nu!(
cwd: "tests/fixtures/formats/",
"open sample-sys-output.json | sum"
"open sample-sys-output.json | math sum"
);
assert!(actual
.err

View File

@ -33,7 +33,7 @@ fn row() {
| merge { open new_caballeros.csv }
| where country in: ["Guayaquil Ecuador" "New Zealand"]
| get luck
| sum
| math sum
| echo $it
"#
));

View File

@ -13,6 +13,7 @@ mod default;
mod drop;
mod each;
mod enter;
mod every;
mod first;
mod format;
mod get;
@ -47,7 +48,6 @@ mod split_by;
mod split_column;
mod split_row;
mod str_;
mod sum;
mod touch;
mod trim;
mod uniq;

View File

@ -12,20 +12,20 @@ fn condition_is_met() {
--------------------------------------------------------------------
Chicken Collection,29/04/2020,30/04/2020,31/04/2020,
Yellow Chickens,,,
Andrés,1,1,1
Jonathan,1,1,1
Jason,1,1,1
Yehuda,1,1,1
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,1
Blue Chickens,,,
Andrés,1,1,2
Jonathan,1,1,2
Jason,1,1,2
Yehuda,1,1,2
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,2
Red Chickens,,,
Andrés,1,1,3
Jonathan,1,1,3
Jason,1,1,3
Yehuda,1,1,3
Andrés,0,0,1
Jonathan,0,0,1
Jason,0,0,1
Yehuda,0,0,3
"#,
)]);
@ -40,11 +40,11 @@ fn condition_is_met() {
| skip-until "Chicken Collection" == "Red Chickens"
| str to-int "31/04/2020"
| get "31/04/2020"
| sum
| math sum
| echo $it
"#
));
assert_eq!(actual.out, "12");
assert_eq!(actual.out, "6");
})
}

View File

@ -108,7 +108,7 @@ fn converts_to_decimal() {
echo "3.1, 0.0415"
| split row ","
| str to-decimal
| sum
| math sum
"#
));
@ -130,7 +130,7 @@ fn sets() {
cwd: dirs.test(), pipeline(
r#"
open sample.toml
| str set wykittenshell package.name
| str set wykittenshell package.name
| get package.name
| echo $it
"#

View File

@ -140,3 +140,26 @@ fn uniq_when_keys_out_of_order() {
assert_eq!(actual.out, "1");
}
#[test]
fn uniq_counting() {
let actual = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo '["A", "B", "A"]'
| from json
| wrap item
| uniq --count
"#
));
let expected = nu!(
cwd: "tests/fixtures/formats", pipeline(
r#"
echo '[{"item": "A", "count": 2}, {"item": "B", "count": 1}]'
| from json
"#
));
print!("{}", actual.out);
print!("{}", expected.out);
assert_eq!(actual.out, expected.out);
}

View File

@ -14,7 +14,7 @@ fn filters_by_unit_size_comparison() {
fn filters_with_nothing_comparison() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"echo '[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get foo | compact | where $it > 1 | sum | echo $it"#
r#"echo '[{"foo": 3}, {"foo": null}, {"foo": 4}]' | from json | get foo | compact | where $it > 1 | math sum | echo $it"#
);
assert_eq!(actual.out, "7");
@ -24,7 +24,7 @@ fn filters_with_nothing_comparison() {
fn where_in_table() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"echo '[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name in: ["foo"] | get size | sum | echo $it"#
r#"echo '[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name in: ["foo"] | get size | math sum | echo $it"#
);
assert_eq!(actual.out, "5");
@ -34,7 +34,7 @@ fn where_in_table() {
fn where_not_in_table() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"echo '[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name not-in: ["foo"] | get size | sum | echo $it"#
r#"echo '[{"name": "foo", "size": 3}, {"name": "foo", "size": 2}, {"name": "bar", "size": 4}]' | from json | where name not-in: ["foo"] | get size | math sum | echo $it"#
);
assert_eq!(actual.out, "4");