Skip to content
Myr

Add pattern tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent e1dcc49 · Browse files · 1 file+84 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+84 added−1 removed

  • tests:+84 added−1 removed

1 file+84 added−1 removed

modified:tests/test_ensure.rs+84 added-1 removed
16 unmodified lines
1718use anyhow::{anyhow, ensure, Chain, Error, Result};19use std::fmt::Debug;20use std::iter;21use std::marker::PhantomData;21use std::marker::{PhantomData, PhantomData as P};22use std::ops::Add;23use std::ptr;2425struct S;
6 unmodified lines
32 }33}3435trait Trait: Sized {36 const V: usize = 0;37 fn t(self, i: i32) -> i32 {38 i39 }40}
560 unmodified lines
601602 let test = || Ok(ensure!(0 as int! {...} != 0));603 assert_err(test, "Condition failed: `0 as int! { ... } != 0` (0 vs 0)");604}605606#[test]607fn test_pat() {608 let test = || Ok(ensure!(if let ref mut _x @ 0 = 0 { 0 } else { 1 } == 1));609 assert_err(610 test,611 "Condition failed: `if let ref mut _x @ 0 = 0 { 0 } else { 1 } == 1` (0 vs 1)",612 );613614 let test = || Ok(ensure!(if let -1..=1 = 0 { 0 } else { 1 } == 1));615 assert_err(616 test,617 "Condition failed: `if let -1 ..=1 = 0 { 0 } else { 1 } == 1` (0 vs 1)",618 );619620 let test = || Ok(ensure!(if let &0 = &0 { 0 } else { 1 } == 1));621 assert_err(622 test,623 "Condition failed: `if let &0 = &0 { 0 } else { 1 } == 1` (0 vs 1)",624 );625626 let test = || Ok(ensure!(if let &&0 = &&0 { 0 } else { 1 } == 1));627 assert_err(628 test,629 "Condition failed: `if let &&0 = &&0 { 0 } else { 1 } == 1` (0 vs 1)",630 );631632 let test = || Ok(ensure!(if let &mut 0 = &mut 0 { 0 } else { 1 } == 1));633 assert_err(634 test,635 "Condition failed: `if let &mut 0 = &mut 0 { 0 } else { 1 } == 1` (0 vs 1)",636 );637638 let test = || Ok(ensure!(if let &&mut 0 = &&mut 0 { 0 } else { 1 } == 1));639 assert_err(640 test,641 "Condition failed: `if let &&mut 0 = &&mut 0 { 0 } else { 1 } == 1` (0 vs 1)",642 );643644 let test = || Ok(ensure!(if let (0, 1) = (0, 1) { 0 } else { 1 } == 1));645 assert_err(646 test,647 "Condition failed: `if let (0, 1) = (0, 1) { 0 } else { 1 } == 1` (0 vs 1)",648 );649650 let test = || Ok(ensure!(if let [0] = b"\0" { 0 } else { 1 } == 1));651 assert_err(652 test,653 "Condition failed: `if let [0] = b\"\\0\" { 0 } else { 1 } == 1` (0 vs 1)",654 );655656 let p = PhantomData::<u8>;657 let test = || Ok(ensure!(if let P::<u8> {} = p { 0 } else { 1 } == 1));658 assert_err(659 test,660 "Condition failed: `if let P::<u8> { } = p { 0 } else { 1 } == 1` (0 vs 1)",661 );662663 let test = || Ok(ensure!(if let ::std::marker::PhantomData = p {} != ()));664 assert_err(665 test,666 "Condition failed: `if let ::std::marker::PhantomData = p { } != ()` (() vs ())",667 );668669 let test = || Ok(ensure!(if let <S as Trait>::V = 0 { 0 } else { 1 } == 1));670 assert_err(671 test,672 "Condition failed: `if let <S as Trait>::V = 0 { 0 } else { 1 } == 1` (0 vs 1)",673 );674675 let test = || Ok(ensure!(for _ in iter::once(()) {} != ()));676 assert_err(677 test,678 "Condition failed: `for _ in iter::once(()) { } != ()` (() vs ())",679 );680681 let test = || Ok(ensure!(if let stringify!(x) = "x" { 0 } else { 1 } == 1));682 assert_err(683 test,684 "Condition failed: `if let stringify!(x) = \"x\" { 0 } else { 1 } == 1` (0 vs 1)",685 );686}