Skip to content

Commit ba1f6cb

Browse files
authored
Rollup merge of #69690 - thekuom:test/67311-extend-bindings-after-at-tests, r=Centril
test(pattern): add tests for combinations of pattern features Reference issue #67311 Tests combinations of the following pattern features: - bindings_after_at - or_patterns - slice_patterns - box_patterns r? @Centril
2 parents e9c3ddc + ea7b3c3 commit ba1f6cb

File tree

7 files changed

+307
-0
lines changed

7 files changed

+307
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Test or-patterns with box-patterns
2+
3+
// run-pass
4+
5+
#![feature(or_patterns)]
6+
#![feature(box_patterns)]
7+
8+
#[derive(Debug, PartialEq)]
9+
enum MatchArm {
10+
Arm(usize),
11+
Wild,
12+
}
13+
14+
#[derive(Debug)]
15+
enum Test {
16+
Foo,
17+
Bar,
18+
Baz,
19+
Qux,
20+
}
21+
22+
fn test(x: Option<Box<Test>>) -> MatchArm {
23+
match x {
24+
Some(box Test::Foo | box Test::Bar) => MatchArm::Arm(0),
25+
Some(box Test::Baz) => MatchArm::Arm(1),
26+
Some(_) => MatchArm::Arm(2),
27+
_ => MatchArm::Wild,
28+
}
29+
}
30+
31+
fn main() {
32+
assert_eq!(test(Some(Box::new(Test::Foo))), MatchArm::Arm(0));
33+
assert_eq!(test(Some(Box::new(Test::Bar))), MatchArm::Arm(0));
34+
assert_eq!(test(Some(Box::new(Test::Baz))), MatchArm::Arm(1));
35+
assert_eq!(test(Some(Box::new(Test::Qux))), MatchArm::Arm(2));
36+
assert_eq!(test(None), MatchArm::Wild);
37+
}
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Test or-patterns with slice-patterns
2+
3+
// run-pass
4+
5+
#![feature(or_patterns)]
6+
7+
#[derive(Debug, PartialEq)]
8+
enum MatchArm {
9+
Arm(usize),
10+
Wild,
11+
}
12+
13+
#[derive(Debug)]
14+
enum Test {
15+
Foo,
16+
Bar,
17+
Baz,
18+
Qux,
19+
}
20+
21+
fn test(foo: &[Option<Test>]) -> MatchArm {
22+
match foo {
23+
[.., Some(Test::Qux | Test::Foo)] => MatchArm::Arm(0),
24+
[Some(Test::Foo), .., Some(Test::Baz | Test::Bar)] => MatchArm::Arm(1),
25+
[.., Some(Test::Bar | Test::Baz), _] => MatchArm::Arm(2),
26+
_ => MatchArm::Wild,
27+
}
28+
}
29+
30+
fn main() {
31+
let foo = vec![
32+
Some(Test::Foo),
33+
Some(Test::Bar),
34+
Some(Test::Baz),
35+
Some(Test::Qux),
36+
];
37+
38+
// path 1a
39+
assert_eq!(test(&foo), MatchArm::Arm(0));
40+
// path 1b
41+
assert_eq!(test(&[Some(Test::Bar), Some(Test::Foo)]), MatchArm::Arm(0));
42+
// path 2a
43+
assert_eq!(test(&foo[..3]), MatchArm::Arm(1));
44+
// path 2b
45+
assert_eq!(test(&[Some(Test::Foo), Some(Test::Foo), Some(Test::Bar)]), MatchArm::Arm(1));
46+
// path 3a
47+
assert_eq!(test(&foo[1..3]), MatchArm::Arm(2));
48+
// path 3b
49+
assert_eq!(test(&[Some(Test::Bar), Some(Test::Baz), Some(Test::Baz), Some(Test::Bar)]),
50+
MatchArm::Arm(2));
51+
// path 4
52+
assert_eq!(test(&foo[4..]), MatchArm::Wild);
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test bindings-after-at with box-patterns
2+
3+
// run-pass
4+
5+
#![feature(bindings_after_at)]
6+
#![feature(box_patterns)]
7+
8+
#[derive(Debug, PartialEq)]
9+
enum MatchArm {
10+
Arm(usize),
11+
Wild,
12+
}
13+
14+
fn test(x: Option<Box<i32>>) -> MatchArm {
15+
match x {
16+
ref bar @ Some(box n) if n > 0 => {
17+
// bar is a &Option<Box<i32>>
18+
assert_eq!(bar, &x);
19+
20+
MatchArm::Arm(0)
21+
},
22+
Some(ref bar @ box n) if n < 0 => {
23+
// bar is a &Box<i32> here
24+
assert_eq!(**bar, n);
25+
26+
MatchArm::Arm(1)
27+
},
28+
_ => MatchArm::Wild,
29+
}
30+
}
31+
32+
fn main() {
33+
assert_eq!(test(Some(Box::new(2))), MatchArm::Arm(0));
34+
assert_eq!(test(Some(Box::new(-1))), MatchArm::Arm(1));
35+
assert_eq!(test(Some(Box::new(0))), MatchArm::Wild);
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Test bindings-after-at with or-patterns and box-patterns
2+
3+
// run-pass
4+
5+
#![feature(bindings_after_at)]
6+
#![feature(or_patterns)]
7+
#![feature(box_patterns)]
8+
9+
#[derive(Debug, PartialEq)]
10+
enum MatchArm {
11+
Arm(usize),
12+
Wild,
13+
}
14+
15+
#[derive(Debug, PartialEq)]
16+
enum Test {
17+
Foo,
18+
Bar,
19+
Baz,
20+
Qux,
21+
}
22+
23+
fn test(foo: Option<Box<Test>>) -> MatchArm {
24+
match foo {
25+
ref bar @ Some(box Test::Foo | box Test::Bar) => {
26+
assert_eq!(bar, &foo);
27+
28+
MatchArm::Arm(0)
29+
},
30+
Some(ref bar @ box Test::Baz | ref bar @ box Test::Qux) => {
31+
assert!(**bar == Test::Baz || **bar == Test::Qux);
32+
33+
MatchArm::Arm(1)
34+
},
35+
_ => MatchArm::Wild,
36+
}
37+
}
38+
39+
fn main() {
40+
assert_eq!(test(Some(Box::new(Test::Foo))), MatchArm::Arm(0));
41+
assert_eq!(test(Some(Box::new(Test::Bar))), MatchArm::Arm(0));
42+
assert_eq!(test(Some(Box::new(Test::Baz))), MatchArm::Arm(1));
43+
assert_eq!(test(Some(Box::new(Test::Qux))), MatchArm::Arm(1));
44+
assert_eq!(test(None), MatchArm::Wild);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Test bindings-after-at with or-patterns and slice-patterns
2+
3+
// run-pass
4+
5+
#![feature(bindings_after_at)]
6+
#![feature(or_patterns)]
7+
8+
#[derive(Debug, PartialEq)]
9+
enum MatchArm {
10+
Arm(usize),
11+
Wild,
12+
}
13+
14+
#[derive(Debug, PartialEq)]
15+
enum Test {
16+
Foo,
17+
Bar,
18+
Baz,
19+
Qux,
20+
}
21+
22+
fn test(foo: &[Option<Test>]) -> MatchArm {
23+
match foo {
24+
bar @ [Some(Test::Foo), .., Some(Test::Qux | Test::Foo)] => {
25+
assert_eq!(bar, foo);
26+
27+
MatchArm::Arm(0)
28+
},
29+
[.., bar @ Some(Test::Bar | Test::Qux), _] => {
30+
assert!(bar == &Some(Test::Bar) || bar == &Some(Test::Qux));
31+
32+
MatchArm::Arm(1)
33+
},
34+
_ => MatchArm::Wild,
35+
}
36+
}
37+
38+
fn main() {
39+
let foo = vec![
40+
Some(Test::Foo),
41+
Some(Test::Bar),
42+
Some(Test::Baz),
43+
Some(Test::Qux),
44+
];
45+
46+
// path 1a
47+
assert_eq!(test(&foo), MatchArm::Arm(0));
48+
// path 1b
49+
assert_eq!(test(&[Some(Test::Foo), Some(Test::Bar), Some(Test::Foo)]), MatchArm::Arm(0));
50+
// path 2a
51+
assert_eq!(test(&foo[..3]), MatchArm::Arm(1));
52+
// path 2b
53+
assert_eq!(test(&[Some(Test::Bar), Some(Test::Qux), Some(Test::Baz)]), MatchArm::Arm(1));
54+
// path 3
55+
assert_eq!(test(&foo[1..2]), MatchArm::Wild);
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Test bindings-after-at with or-patterns
2+
3+
// run-pass
4+
5+
#![feature(bindings_after_at)]
6+
#![feature(or_patterns)]
7+
8+
#[derive(Debug, PartialEq)]
9+
enum MatchArm {
10+
Arm(usize),
11+
Wild,
12+
}
13+
14+
#[derive(Debug, Clone, Copy, PartialEq)]
15+
enum Test {
16+
Foo,
17+
Bar,
18+
Baz,
19+
Qux,
20+
}
21+
22+
fn test(foo: Option<Test>) -> MatchArm {
23+
match foo {
24+
bar @ Some(Test::Foo | Test::Bar) => {
25+
assert!(bar == Some(Test::Foo) || bar == Some(Test::Bar));
26+
27+
MatchArm::Arm(0)
28+
},
29+
Some(_) => MatchArm::Arm(1),
30+
_ => MatchArm::Wild,
31+
}
32+
}
33+
34+
fn main() {
35+
assert_eq!(test(Some(Test::Foo)), MatchArm::Arm(0));
36+
assert_eq!(test(Some(Test::Bar)), MatchArm::Arm(0));
37+
assert_eq!(test(Some(Test::Baz)), MatchArm::Arm(1));
38+
assert_eq!(test(Some(Test::Qux)), MatchArm::Arm(1));
39+
assert_eq!(test(None), MatchArm::Wild);
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Test bindings-after-at with slice-patterns
2+
3+
// run-pass
4+
5+
#![feature(bindings_after_at)]
6+
7+
#[derive(Debug, PartialEq)]
8+
enum MatchArm {
9+
Arm(usize),
10+
Wild,
11+
}
12+
13+
fn test(foo: &[i32]) -> MatchArm {
14+
match foo {
15+
[bar @ .., n] if n == &5 => {
16+
for i in bar {
17+
assert!(i < &5);
18+
}
19+
20+
MatchArm::Arm(0)
21+
},
22+
bar @ [x0, .., xn] => {
23+
assert_eq!(x0, &1);
24+
assert_eq!(x0, &1);
25+
assert_eq!(xn, &4);
26+
assert_eq!(bar, &[1, 2, 3, 4]);
27+
28+
MatchArm::Arm(1)
29+
},
30+
_ => MatchArm::Wild,
31+
}
32+
}
33+
34+
fn main() {
35+
let foo = vec![1, 2, 3, 4, 5];
36+
37+
assert_eq!(test(&foo), MatchArm::Arm(0));
38+
assert_eq!(test(&foo[..4]), MatchArm::Arm(1));
39+
assert_eq!(test(&foo[0..1]), MatchArm::Wild);
40+
}

0 commit comments

Comments
 (0)