Spread operator for list literals (#11006)

This commit is contained in:
ysthakur
2023-11-22 16:10:08 -05:00
committed by GitHub
parent 95a745e622
commit 823e578c46
12 changed files with 149 additions and 13 deletions

View File

@ -20,6 +20,7 @@ mod test_parser;
mod test_ranges;
mod test_regex;
mod test_signatures;
mod test_spread;
mod test_stdlib;
mod test_strings;
mod test_table_operations;

48
src/tests/test_spread.rs Normal file
View File

@ -0,0 +1,48 @@
use crate::tests::{fail_test, run_test, TestResult};
#[test]
fn spread_in_list() -> TestResult {
run_test(r#"[...[]] | to nuon"#, "[]").unwrap();
run_test(
r#"[1 2 ...[[3] {x: 1}] 5] | to nuon"#,
"[1, 2, [3], {x: 1}, 5]",
)
.unwrap();
run_test(
r#"[...("foo" | split chars) 10] | to nuon"#,
"[f, o, o, 10]",
)
.unwrap();
run_test(
r#"let l = [1, 2, [3]]; [...$l $l] | to nuon"#,
"[1, 2, [3], [1, 2, [3]]]",
)
.unwrap();
run_test(
r#"[ ...[ ...[ ...[ a ] b ] c ] d ] | to nuon"#,
"[a, b, c, d]",
)
}
#[test]
fn not_spread() -> TestResult {
run_test(r#"def ... [x] { $x }; ... ..."#, "...").unwrap();
run_test(
r#"let a = 4; [... $a ... [1] ... (5) ...bare ...] | to nuon"#,
"[..., 4, ..., [1], ..., 5, ...bare, ...]",
)
}
#[test]
fn bad_spread_on_non_list() -> TestResult {
fail_test(r#"let x = 5; [...$x]"#, "cannot spread").unwrap();
fail_test(r#"[...({ x: 1 })]"#, "cannot spread")
}
#[test]
fn spread_type() -> TestResult {
run_test(r#"[1 ...[]] | describe"#, "list<int>").unwrap();
run_test(r#"[1 ...[2]] | describe"#, "list<int>").unwrap();
run_test(r#"["foo" ...[4 5 6]] | describe"#, "list<any>").unwrap();
run_test(r#"[1 2 ...["misfit"] 4] | describe"#, "list<any>")
}