• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is partially derived from src/panicking.rs in the Rust libcore,
3  * used under the Apache License, Version 2.0. The following is the original
4  * copyright information from the Rust project:
5  *
6  * Copyrights in the Rust project are retained by their contributors. No
7  * copyright assignment is required to contribute to the Rust project.
8  *
9  * Some files include explicit copyright notices and/or license notices.
10  * For full authorship information, see the version control history or
11  * https://thanks.rust-lang.org
12  *
13  * Except as otherwise noted (below and/or in individual files), Rust is
14  * licensed under the Apache License, Version 2.0 <LICENSE-APACHE> or
15  * <http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
16  * <LICENSE-MIT> or <http://opensource.org/licenses/MIT>, at your option.
17  *
18  *
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  *      http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31 
32 use crate::context::CONTEXT;
33 use std::fmt;
34 use std::panic::Location;
35 
36 #[derive(Debug)]
37 pub enum AssertKind {
38     Eq,
39     Ne,
40     Match,
41 }
42 
43 #[cold]
44 #[track_caller]
assert_failed<T, U>(kind: AssertKind, left: &T, right: &U, args: Option<fmt::Arguments<'_>>) where T: fmt::Debug + ?Sized, U: fmt::Debug + ?Sized,45 pub fn assert_failed<T, U>(kind: AssertKind, left: &T, right: &U, args: Option<fmt::Arguments<'_>>)
46 where
47     T: fmt::Debug + ?Sized,
48     U: fmt::Debug + ?Sized,
49 {
50     assert_failed_inner(kind, &left, &right, args)
51 }
52 
53 #[track_caller]
assert_failed_inner( kind: AssertKind, left: &dyn fmt::Debug, right: &dyn fmt::Debug, args: Option<fmt::Arguments<'_>>, )54 fn assert_failed_inner(
55     kind: AssertKind,
56     left: &dyn fmt::Debug,
57     right: &dyn fmt::Debug,
58     args: Option<fmt::Arguments<'_>>,
59 ) {
60     let op = match kind {
61         AssertKind::Eq => "==",
62         AssertKind::Ne => "!=",
63         AssertKind::Match => "matches",
64     };
65 
66     match args {
67         Some(args) => eprintln!(
68             r#"assertion failed: `(left {} right)`
69   left: `{:?}`,
70  right: `{:?}`: {}, {}"#,
71             op,
72             left,
73             right,
74             args,
75             Location::caller(),
76         ),
77         None => eprintln!(
78             r#"assertion failed: `(left {} right)`
79   left: `{:?}`,
80  right: `{:?}`, {}"#,
81             op,
82             left,
83             right,
84             Location::caller(),
85         ),
86     }
87 
88     CONTEXT.fail(false);
89 }
90 
91 #[cold]
92 #[track_caller]
assert_ok_failed<E: fmt::Display>( result: &'static str, err: E, args: Option<fmt::Arguments<'_>>, )93 pub fn assert_ok_failed<E: fmt::Display>(
94     result: &'static str,
95     err: E,
96     args: Option<fmt::Arguments<'_>>,
97 ) {
98     assert_ok_failed_inner(result, &err, args);
99 }
100 
101 #[track_caller]
assert_ok_failed_inner( result: &'static str, err: &dyn fmt::Display, args: Option<fmt::Arguments<'_>>, )102 fn assert_ok_failed_inner(
103     result: &'static str,
104     err: &dyn fmt::Display,
105     args: Option<fmt::Arguments<'_>>,
106 ) {
107     match args {
108         Some(args) => eprintln!(
109             r#"assertion failed: `{}` not Ok
110 error: `{}` : {}, {}`"#,
111             result,
112             err,
113             args,
114             Location::caller(),
115         ),
116         None => eprintln!(
117             r#"assertion failed: `{}` not Ok
118 error: `{}`, {}`"#,
119             result,
120             err,
121             Location::caller(),
122         ),
123     }
124     CONTEXT.fail(false);
125 }
126 
127 #[track_caller]
simple_assert_failed(cond: &'static str, args: Option<fmt::Arguments<'_>>)128 pub fn simple_assert_failed(cond: &'static str, args: Option<fmt::Arguments<'_>>) {
129     match args {
130         Some(args) => eprintln!("assertion failed: {}, {}", args, Location::caller()),
131         None => eprintln!("assertion failed: {}, {}", cond, Location::caller()),
132     }
133 
134     CONTEXT.fail(false);
135 }
136