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
Provenance
- Commit
da5c3fd- Tree
a628bc0- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
e1dcc49
1 file changed+84 added−1 removed
modified:tests/test_ensure.rs+84 added-1 removed
16 unmodified lines17 18 use anyhow::{anyhow, ensure, Chain, Error, Result};19 use std::fmt::Debug;20 use std::iter;21-use std::marker::PhantomData;21+use std::marker::{PhantomData, PhantomData as P};22 use std::ops::Add;23 use std::ptr;24 25 struct S;6 unmodified lines32 }33 }34 35 trait Trait: Sized {36+ const V: usize = 0;37 fn t(self, i: i32) -> i32 {38 i39 }40 }560 unmodified lines601 602 let test = || Ok(ensure!(0 as int! {...} != 0));603 assert_err(test, "Condition failed: `0 as int! { ... } != 0` (0 vs 0)");604 }605+606+#[test]607+fn 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+ );613+614+ 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+ );619+620+ 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+ );625+626+ 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+ );631+632+ 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+ );637+638+ 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+ );643+644+ 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+ );649+650+ 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+ );655+656+ 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+ );662+663+ 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+ );668+669+ 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+ );674+675+ let test = || Ok(ensure!(for _ in iter::once(()) {} != ()));676+ assert_err(677+ test,678+ "Condition failed: `for _ in iter::once(()) { } != ()` (() vs ())",679+ );680+681+ 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+}