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 #[doc(hidden)]
38 pub enum AssertKind {
39 Eq,
40 Ne,
41 Match,
42 }
43
44 #[cold]
45 #[track_caller]
46 #[doc(hidden)]
assert_failed<T, U>(kind: AssertKind, left: &T, right: &U, args: Option<fmt::Arguments<'_>>) where T: fmt::Debug + ?Sized, U: fmt::Debug + ?Sized,47 pub fn assert_failed<T, U>(kind: AssertKind, left: &T, right: &U, args: Option<fmt::Arguments<'_>>)
48 where
49 T: fmt::Debug + ?Sized,
50 U: fmt::Debug + ?Sized,
51 {
52 assert_failed_inner(kind, &left, &right, args)
53 }
54
55 #[track_caller]
assert_failed_inner( kind: AssertKind, left: &dyn fmt::Debug, right: &dyn fmt::Debug, args: Option<fmt::Arguments<'_>>, )56 pub fn assert_failed_inner(
57 kind: AssertKind,
58 left: &dyn fmt::Debug,
59 right: &dyn fmt::Debug,
60 args: Option<fmt::Arguments<'_>>,
61 ) {
62 let op = match kind {
63 AssertKind::Eq => "==",
64 AssertKind::Ne => "!=",
65 AssertKind::Match => "matches",
66 };
67
68 match args {
69 Some(args) => eprintln!(
70 r#"assertion failed: `(left {} right)`
71 left: `{:?}`,
72 right: `{:?}`: {}, {}"#,
73 op,
74 left,
75 right,
76 args,
77 Location::caller(),
78 ),
79 None => eprintln!(
80 r#"assertion failed: `(left {} right)`
81 left: `{:?}`,
82 right: `{:?}`, {}"#,
83 op,
84 left,
85 right,
86 Location::caller(),
87 ),
88 }
89
90 CONTEXT.fail(false);
91 }
92
93 #[track_caller]
94 #[doc(hidden)]
assert_err<E: fmt::Display>( result: &'static str, err: &E, args: Option<fmt::Arguments<'_>>, )95 pub fn assert_err<E: fmt::Display>(
96 result: &'static str,
97 err: &E,
98 args: Option<fmt::Arguments<'_>>,
99 ) {
100 assert_err_inner(result, &*err, args);
101 }
102
103 #[track_caller]
104 #[doc(hidden)]
assert_err_inner( result: &'static str, err: &dyn fmt::Display, args: Option<fmt::Arguments<'_>>, )105 pub fn assert_err_inner(
106 result: &'static str,
107 err: &dyn fmt::Display,
108 args: Option<fmt::Arguments<'_>>,
109 ) {
110 match args {
111 Some(args) => eprintln!(
112 r#"assertion failed: `{}` not Ok
113 error: `{}` : {}, {}`"#,
114 result,
115 err,
116 args,
117 Location::caller(),
118 ),
119 None => eprintln!(
120 r#"assertion failed: `{}` not Ok
121 error: `{}`, {}`"#,
122 result,
123 err,
124 Location::caller(),
125 ),
126 }
127 CONTEXT.fail(false);
128 }
129
130 #[track_caller]
simple_assert_failed(cond: &'static str, args: Option<fmt::Arguments<'_>>)131 pub fn simple_assert_failed(cond: &'static str, args: Option<fmt::Arguments<'_>>) {
132 match args {
133 Some(args) => eprintln!("assertion failed: {}, {}", args, Location::caller()),
134 None => eprintln!("assertion failed: {}, {}", cond, Location::caller()),
135 }
136
137 CONTEXT.fail(false);
138 }
139