Skip to content

Commit

Permalink
Remove unwrap_lib_err method from UnwrapError test trait
Browse files Browse the repository at this point in the history
This change marks the next step in getting rid of the UnwrapError test
trait. Specifically, it removes its unwrap_lib_err method.
  • Loading branch information
d-e-s-o committed Jul 10, 2020
1 parent efb6fbf commit c461a50
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
12 changes: 0 additions & 12 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ mod unencrypted;
pub trait UnwrapError {
/// Unwrap a Error::CommandError variant.
fn unwrap_cmd_err(self) -> (Option<&'static str>, nitrokey::CommandError);
/// Unwrap a Error::LibraryError variant.
fn unwrap_lib_err(self) -> (Option<&'static str>, nitrokey::LibraryError);
}

impl<T> UnwrapError for crate::Result<T>
Expand All @@ -56,16 +54,6 @@ where
err => panic!("Unexpected error variant found: {:?}", err),
}
}

fn unwrap_lib_err(self) -> (Option<&'static str>, nitrokey::LibraryError) {
match self.unwrap_err() {
crate::Error::NitrokeyError(ctx, err) => match err {
nitrokey::Error::LibraryError(err) => (ctx, err),
err => panic!("Unexpected error variant found: {:?}", err),
},
err => panic!("Unexpected error variant found: {:?}", err),
}
}
}

struct Nitrocli {
Expand Down
17 changes: 9 additions & 8 deletions src/tests/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ fn set_invalid_slot_raw(model: nitrokey::Model) {

#[test_device]
fn set_invalid_slot(model: nitrokey::Model) {
let res = Nitrocli::with_model(model).handle(&["otp", "set", "100", "name", "1234", "-f", "hex"]);

assert_eq!(
res.unwrap_lib_err(),
(
Some("Could not write OTP slot"),
nitrokey::LibraryError::InvalidSlot
)
let err = Nitrocli::with_model(model)
.handle(&["otp", "set", "100", "name", "1234", "-f", "hex"])
.unwrap_err()
.to_string();
let expected = format!(
"Could not write OTP slot: {}",
nitrokey::Error::LibraryError(nitrokey::LibraryError::InvalidSlot)
);

assert_eq!(err, expected);
}

#[test_device]
Expand Down
19 changes: 10 additions & 9 deletions src/tests/pws.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// pws.rs

// *************************************************************************
// * Copyright (C) 2019 Daniel Mueller ([email protected]) *
// * Copyright (C) 2019-2020 Daniel Mueller ([email protected]) *
// * *
// * This program is free software: you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
Expand All @@ -21,15 +21,16 @@ use super::*;

#[test_device]
fn set_invalid_slot(model: nitrokey::Model) {
let res = Nitrocli::with_model(model).handle(&["pws", "set", "100", "name", "login", "1234"]);

assert_eq!(
res.unwrap_lib_err(),
(
Some("Could not write PWS slot"),
nitrokey::LibraryError::InvalidSlot
)
let err = Nitrocli::with_model(model)
.handle(&["pws", "set", "100", "name", "login", "1234"])
.unwrap_err()
.to_string();
let expected = format!(
"Could not write PWS slot: {}",
nitrokey::Error::LibraryError(nitrokey::LibraryError::InvalidSlot)
);

assert_eq!(err, expected);
}

#[test_device]
Expand Down

0 comments on commit c461a50

Please sign in to comment.