• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 use super::*;
18 use crate::{cbor::value::Value, util::expect_err};
19 use alloc::{borrow::ToOwned, boxed::Box, vec};
20 
21 #[test]
test_cbor_type_error()22 fn test_cbor_type_error() {
23     let cases = vec![
24         (Value::Null, "nul"),
25         (Value::Bool(true), "bool"),
26         (Value::Bool(false), "bool"),
27         (Value::from(128), "int"),
28         (Value::from(-1), "int"),
29         (Value::Bytes(vec![1, 2]), "bstr"),
30         (Value::Text("string".to_owned()), "tstr"),
31         (Value::Array(vec![Value::from(0)]), "array"),
32         (Value::Map(vec![]), "map"),
33         (Value::Tag(1, Box::new(Value::from(0))), "tag"),
34         (Value::Float(1.054571817), "float"),
35     ];
36     for (val, want) in cases {
37         let e = cbor_type_error::<()>(&val, "a");
38         expect_err(e, want);
39     }
40 }
41 
42 #[test]
43 #[should_panic]
test_expect_err_but_ok()44 fn test_expect_err_but_ok() {
45     let result: Result<i32, crate::CoseError> = Ok(42);
46     expect_err(result, "absent text");
47 }
48 
49 #[test]
50 #[should_panic]
test_expect_err_wrong_msg()51 fn test_expect_err_wrong_msg() {
52     let err = cbor_type_error::<()>(&Value::Bool(true), "a");
53     expect_err(err, "incorrect text");
54 }
55 
56 #[test]
57 #[should_panic]
test_expect_err_wrong_display_msg()58 fn test_expect_err_wrong_display_msg() {
59     // Error type where `Debug` shows the message but `Display` doesn't
60     #[allow(dead_code)]
61     #[derive(Debug)]
62     struct Error(&'static str);
63     impl core::fmt::Display for Error {
64         fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
65             write!(f, "other")
66         }
67     }
68 
69     let err: Result<i32, Error> = Err(Error("text"));
70     // The expected text appears in the `Debug` output but not the `Display` output.
71     expect_err(err, "text");
72 }
73