Skip to content

Commit

Permalink
Add tests for CmdBlockOutcome.
Browse files Browse the repository at this point in the history
  • Loading branch information
azriel91 committed Dec 28, 2023
1 parent 40e5da6 commit fc41336
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
2 changes: 1 addition & 1 deletion crate/cmd_model/src/cmd_block_outcome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use peace_cfg::ItemId;
use crate::{StreamOutcomeAndErrors, ValueAndStreamOutcome};

/// Outcome of running `CmdBlock::exec`.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CmdBlockOutcome<T, E> {
/// Single value returned by the command block.
///
Expand Down
2 changes: 1 addition & 1 deletion crate/cmd_model/src/stream_outcome_and_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use indexmap::IndexMap;
use peace_cfg::ItemId;

/// `CmdBlock` stream outcome and item wise errors.
#[derive(Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamOutcomeAndErrors<T, E> {
/// The `CmdBlock` stream outcome.
pub stream_outcome: StreamOutcome<T>,
Expand Down
2 changes: 1 addition & 1 deletion crate/cmd_model/src/value_and_stream_outcome.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use fn_graph::StreamOutcome;

/// `CmdBlock` outcome value on success, and its `StreamOutcome` if applicable.
#[derive(Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ValueAndStreamOutcome<T> {
/// The value returned by the `CmdBlock`.
pub value: T,
Expand Down
1 change: 1 addition & 0 deletions workspace_tests/src/cmd_model.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod cmd_block_outcome;
mod cmd_outcome;
94 changes: 94 additions & 0 deletions workspace_tests/src/cmd_model/cmd_block_outcome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use peace::{
cfg::{item_id, ItemId},
cmd_model::{CmdBlockOutcome, StreamOutcomeAndErrors, ValueAndStreamOutcome},
rt_model::{fn_graph::StreamOutcome, IndexMap},
};

#[test]
fn is_ok() {
assert!(cmd_block_outcome_single(123).is_ok());
assert!(cmd_block_outcome_item_wise(123, None).is_ok());
assert!(!cmd_block_outcome_item_wise(123, Some("err".to_string())).is_ok());
}

#[test]
fn is_err() {
assert!(!cmd_block_outcome_single(123).is_err());
assert!(!cmd_block_outcome_item_wise(123, None).is_err());
assert!(cmd_block_outcome_item_wise(123, Some("err".to_string())).is_err());
}

#[test]
fn try_into_value() {
assert_eq!(
Ok(ValueAndStreamOutcome {
value: 123,
stream_outcome: None,
}),
cmd_block_outcome_single(123).try_into_value()
);
assert_eq!(
Ok(ValueAndStreamOutcome {
value: 123,
stream_outcome: Some(StreamOutcome::finished_with((), Vec::new())),
}),
cmd_block_outcome_item_wise(123, None).try_into_value()
);

let mut errors = IndexMap::new();
errors.insert(item_id!("mock"), "err".to_string());
assert_eq!(
Err(StreamOutcomeAndErrors {
stream_outcome: StreamOutcome::finished_with(123, Vec::new()),
errors,
}),
cmd_block_outcome_item_wise(123, Some("err".to_string())).try_into_value()
);
}

#[test]
fn map() {
assert_eq!(
cmd_block_outcome_single(124),
cmd_block_outcome_single(123).map(|value| value + 1)
);
assert_eq!(
cmd_block_outcome_item_wise(124, None),
cmd_block_outcome_item_wise(123, None).map(|value| value + 1)
);

assert_eq!(
cmd_block_outcome_item_wise(124, Some("err".to_string())),
cmd_block_outcome_item_wise(123, Some("err".to_string())).map(|value| value + 1)
);
}

#[test]
fn clone() {
let cmd_block_outcome = cmd_block_outcome_single(123);

assert_eq!(cmd_block_outcome, Clone::clone(&cmd_block_outcome));
}

#[test]
fn debug() {
let cmd_block_outcome = cmd_block_outcome_single(123);

assert_eq!("Single(123)", format!("{cmd_block_outcome:?}"));
}

fn cmd_block_outcome_single<T>(value: T) -> CmdBlockOutcome<T, String> {
CmdBlockOutcome::<T, String>::Single(value)
}

fn cmd_block_outcome_item_wise<T>(value: T, error: Option<String>) -> CmdBlockOutcome<T, String> {
let mut errors = IndexMap::new();
if let Some(error) = error {
errors.insert(item_id!("mock"), error);
}

CmdBlockOutcome::<T, String>::ItemWise {
stream_outcome: StreamOutcome::finished_with(value, Vec::new()),
errors,
}
}

0 comments on commit fc41336

Please sign in to comment.