From 1192188cb0ad8e684edddfda37c272be5d215d42 Mon Sep 17 00:00:00 2001 From: Josh Mitchell Date: Wed, 11 Nov 2020 18:58:32 +1100 Subject: [PATCH] Test for to_i32 --- src/lib.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fda4610..5d7b2c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -120,7 +120,7 @@ fn to_i32(value: usize, task: ErrorTask) -> Result { /// Convert an error code from a C call to an Error /// /// `code` should be an integer return code returned from the C API. -/// If `code` indicates the function returned successfully, Nothing is returned; +/// If `code` indicates the function returned successfully, None is returned; /// otherwise, the code is converted into the appropriate `Error`. pub fn check_code(code: impl Into, task: ErrorTask) -> Option { let code: ErrorCode = code.into(); @@ -798,4 +798,22 @@ mod tests { assert!(check_code(code, ErrorTask::Read).is_some()); } } + + #[test] + fn test_to_i32() -> Result<()> { + assert_eq!(24234_i32, to_i32(24234_usize, ErrorTask::Read)?); + + let try_from_int_err = match u8::try_from(-1) { + Err(e) => e, + _ => panic!("Conversion from -1 to u8 succeeded"), + }; + let expected = Error::NumericCastFailed { + source: try_from_int_err, + task: ErrorTask::Write, + value: 3_294_967_295_usize, + }; + assert_eq!(Err(expected), to_i32(3_294_967_295_usize, ErrorTask::Write)); + + Ok(()) + } }