rskit_testutil/
assertions.rs1use rskit_errors::{AppResult, ErrorCode};
2
3#[track_caller]
7pub fn assert_ok<T>(result: AppResult<T>) -> T {
8 match result {
9 Ok(v) => v,
10 Err(e) => panic!("expected Ok, got Err: {e}"),
11 }
12}
13
14#[track_caller]
16pub fn assert_err_code(result: AppResult<impl std::fmt::Debug>, code: ErrorCode) {
17 match result {
18 Ok(v) => panic!("expected Err({code:?}), got Ok({v:?})"),
19 Err(e) => {
20 assert_eq!(
21 e.code(),
22 code,
23 "expected error code {code:?}, got {:?}: {e}",
24 e.code(),
25 );
26 }
27 }
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33 use rskit_errors::AppError;
34
35 #[test]
36 fn assert_ok_passes() {
37 let val: AppResult<i32> = Ok(42);
38 assert_eq!(assert_ok(val), 42);
39 }
40
41 #[test]
42 #[should_panic(expected = "expected Ok")]
43 fn assert_ok_panics_on_err() {
44 let val: AppResult<i32> = Err(AppError::new(ErrorCode::Internal, "boom"));
45 assert_ok(val);
46 }
47
48 #[test]
49 fn assert_err_code_passes() {
50 let val: AppResult<i32> = Err(AppError::not_found("gone", None));
51 assert_err_code(val, ErrorCode::NotFound);
52 }
53
54 #[test]
55 #[should_panic(expected = "expected Err")]
56 fn assert_err_code_panics_on_ok() {
57 let val: AppResult<i32> = Ok(42);
58 assert_err_code(val, ErrorCode::Internal);
59 }
60
61 #[test]
62 #[should_panic(expected = "expected error code")]
63 fn assert_err_code_panics_on_wrong_code() {
64 let val: AppResult<i32> = Err(AppError::not_found("nope", None));
65 assert_err_code(val, ErrorCode::Internal);
66 }
67}