From 110391f2fb6ef22784fef25a99e65e6c3eda8fd4 Mon Sep 17 00:00:00 2001 From: lilymonade Date: Tue, 11 Mar 2025 18:42:32 +0100 Subject: [PATCH] finish Result --- practical.yml | 15 +++++++++++++ subject_source/tests/result.rs | 41 +++++++++++++++++++++++++++++----- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/practical.yml b/practical.yml index e8b2d34..c74b803 100644 --- a/practical.yml +++ b/practical.yml @@ -22,3 +22,18 @@ exercises: - "get_or_default_some_diff" - "get_or_panic_none" - "get_or_panic_some" + result: + required_files: + - "src/errors.rs" + - "src/errors/option.rs" + tests: + - "is_err_err" + - "is_ok_err" + - "is_err_ok" + - "is_ok_ok" + - "get_err_or_panic_err" + - "get_err_or_panic_ok" + - "get_val_or_panic_err" + - "get_val_or_panic_ok" + - "discard_err_ok" + - "discard_err_err" diff --git a/subject_source/tests/result.rs b/subject_source/tests/result.rs index c373cf9..6ae82a4 100644 --- a/subject_source/tests/result.rs +++ b/subject_source/tests/result.rs @@ -1,22 +1,53 @@ use subject_source::errors::result; - #[test] pub fn is_err_err() { - assert!(is_err(Err("oh no"))); + assert!(result::is_err(&Err("oh no"))); } #[test] pub fn is_err_ok() { - assert!(!is_err(Ok(420))); + assert!(!result::is_err(&Ok(420))); } #[test] pub fn is_ok_err() { - assert!(!is_ok(Err("oh no"))); + assert!(!result::is_ok(&Err("oh no"))); } #[test] pub fn is_ok_ok() { - assert!(is_ok(Ok(420))); + assert!(result::is_ok(&Ok(420))); +} + +#[test] +pub fn get_err_or_panic_err() { + assert_eq!(result::get_err_or_panic(Err("ayaya")), "ayaya"); +} + +#[test] +#[should_panic] +pub fn get_err_or_panic_ok() { + let _ = std::hint::black_box(result::get_err_or_panic(Ok(10))); +} + +#[test] +pub fn get_val_or_panic_ok() { + assert_eq!(result::get_val_or_panic(Ok(10)), 10); +} + +#[test] +#[should_panic] +pub fn get_val_or_panic_err() { + let _ = std::hint::black_box(result::get_val_or_panic(Err("oaizjd"))); +} + +#[test] +pub fn discard_err_err() { + assert_eq!(result::discard_err(Err("hello")), None); +} + +#[test] +pub fn discard_err_ok() { + assert_eq!(result::discard_err(Ok(128)), Some(128)); }