Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Oct 22, 2024
1 parent 0b2d1f2 commit 7678785
Show file tree
Hide file tree
Showing 18 changed files with 142 additions and 117 deletions.
12 changes: 6 additions & 6 deletions crux_core/src/capability/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ use channel::Sender;
/// type Output = HttpResponse;
/// }
/// ```
pub trait Operation: serde::Serialize + PartialEq + Send + 'static {
pub trait Operation: serde::Serialize + Clone + PartialEq + Send + 'static {
/// `Output` assigns the type this request results in.
type Output: serde::de::DeserializeOwned + Send + 'static;
}
Expand All @@ -238,7 +238,7 @@ pub trait Operation: serde::Serialize + PartialEq + Send + 'static {
/// # }
///
/// ```
#[derive(Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum Never {}

/// Implement `Operation` for `Never` to allow using it as a capability operation.
Expand All @@ -258,7 +258,7 @@ impl Operation for Never {
/// # pub struct Http<Ev> {
/// # context: CapabilityContext<HttpRequest, Ev>,
/// # }
/// # #[derive(serde::Serialize, serde::Deserialize, PartialEq, Eq)] pub struct HttpRequest;
/// # #[derive(Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq)] pub struct HttpRequest;
/// # impl Operation for HttpRequest {
/// # type Output = ();
/// # }
Expand Down Expand Up @@ -372,8 +372,8 @@ pub trait WithContext<Ev, Ef> {
/// For example (from `crux_time`)
///
/// ```rust
/// # #[derive(PartialEq,serde::Serialize)]pub struct TimeRequest;
/// # #[derive(serde::Deserialize)]pub struct TimeResponse(pub String);
/// # #[derive(Clone, PartialEq, serde::Serialize)] pub struct TimeRequest;
/// # #[derive(Clone, serde::Deserialize)] pub struct TimeResponse(pub String);
/// # impl crux_core::capability::Operation for TimeRequest {
/// # type Output = TimeResponse;
/// # }
Expand Down Expand Up @@ -625,7 +625,7 @@ mod tests {
#[allow(dead_code)]
enum Event {}

#[derive(PartialEq, Serialize)]
#[derive(PartialEq, Clone, Serialize)]
struct Op {}

impl Operation for Op {
Expand Down
2 changes: 1 addition & 1 deletion crux_core/src/capability/shell_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tests {

use crate::capability::{channel, executor_and_spawner, CapabilityContext, Operation};

#[derive(serde::Serialize, PartialEq, Eq, Debug)]
#[derive(serde::Serialize, Clone, PartialEq, Eq, Debug)]
struct TestOperation;

impl Operation for TestOperation {
Expand Down
2 changes: 1 addition & 1 deletion crux_core/src/capability/shell_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ mod tests {

use crate::capability::{channel, executor_and_spawner, CapabilityContext, Operation};

#[derive(serde::Serialize, PartialEq, Eq, Debug)]
#[derive(serde::Serialize, Clone, PartialEq, Eq, Debug)]
struct TestOperation;

impl Operation for TestOperation {
Expand Down
26 changes: 25 additions & 1 deletion crux_core/src/testing.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Testing support for unit testing Crux apps.
use anyhow::Result;
use std::sync::Arc;
use std::{collections::VecDeque, sync::Arc};

use crate::{
capability::{
Expand Down Expand Up @@ -203,6 +203,30 @@ impl<Ef, Ev> Update<Ef, Ev> {
self.events.len()
);
}

/// Take effects matching the `predicate` out of the [`Update`]
/// and return them, mutating the `Update`
pub fn take_effects<P>(&mut self, predicate: P) -> VecDeque<Ef>
where
P: FnMut(&Ef) -> bool,
{
let (matching_effects, other_effects) = self.take_partitioned_effects(predicate);

self.effects = other_effects.into_iter().collect();

matching_effects
}

/// Take all of the effects out of the [`Update`]
/// and split them into those matching `predicate` and the rest
pub fn take_partitioned_effects<P>(&mut self, predicate: P) -> (VecDeque<Ef>, VecDeque<Ef>)
where
P: FnMut(&Ef) -> bool,
{
std::mem::take(&mut self.effects)
.into_iter()
.partition(predicate)
}
}

/// Panics if the pattern doesn't match an `Effect` from the specified `Update`
Expand Down
4 changes: 2 additions & 2 deletions crux_core/tests/capability_orchestration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub mod capabilities {
use crux_core::macros::Capability;
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
pub struct OpOne {
number: usize,
}
Expand Down Expand Up @@ -123,7 +123,7 @@ pub mod capabilities {
use crux_core::macros::Capability;
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
pub struct OpTwo {
number: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion crux_platform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crux_core::capability::{CapabilityContext, Operation};
use crux_core::macros::Capability;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlatformRequest;

// TODO revisit this
Expand Down
4 changes: 2 additions & 2 deletions doctest_support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod compose {
use crux_core::capability::{CapabilityContext, Operation};
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
pub struct OpOne {
number: usize,
}
Expand Down Expand Up @@ -79,7 +79,7 @@ pub mod compose {
use crux_core::capability::{CapabilityContext, Operation};
use serde::{Deserialize, Serialize};

#[derive(PartialEq, Serialize, Deserialize, Debug)]
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
pub struct OpTwo {
number: usize,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bridge_echo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ rust-version = "1.66"

[workspace.dependencies]
anyhow = "1.0.90"
crux_core = "0.9.0"
crux_core = "0.9.1"
serde = "1.0.210"

[workspace.metadata.bin]
Expand Down
14 changes: 7 additions & 7 deletions examples/bridge_echo/shared/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl crux_core::App for App {
#[cfg(test)]
mod test {
use super::*;
use crux_core::{assert_effect, testing::AppTester};
use crux_core::testing::AppTester;

#[test]
fn shows_initial_count() {
Expand Down Expand Up @@ -112,19 +112,19 @@ mod test {
let app = AppTester::<App, _>::default();
let mut model = Model::default();

let update = app.update(Event::Tick, &mut model);

assert_effect!(update, Effect::Render(_));
app.update(Event::Tick, &mut model)
.expect_one_effect()
.expect_render();
}

#[test]
fn renders_on_new_period() {
let app = AppTester::<App, _>::default();
let mut model = Model::default();

let update = app.update(Event::NewPeriod, &mut model);

assert_effect!(update, Effect::Render(_));
app.update(Event::NewPeriod, &mut model)
.expect_one_effect()
.expect_render();
}
}
// ANCHOR_END: test
31 changes: 6 additions & 25 deletions examples/cat_facts/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions examples/cat_facts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ rust-version = "1.66"

[workspace.dependencies]
anyhow = "1.0.90"
crux_core = "0.9.0"
crux_http = "0.10.1"
crux_kv = "0.5.0"
crux_platform = "0.2.0"
crux_time = { version = "0.5.0", features = ["chrono"] }
crux_core = { path = "../../crux_core" }
crux_http = { path = "../../crux_http" }
crux_kv = { path = "../../crux_kv" }
crux_platform = { path = "../../crux_platform" }
crux_time = { path = "../../crux_time", features = ["chrono"] }
# crux_core = "0.9.1"
# crux_http = "0.10.2"
# crux_kv = "0.5.1"
# crux_platform = "0.2.1"
# crux_time = { version = "0.5.1", features = ["chrono"] }
serde = "1.0.210"

[workspace.metadata.bin]
Expand Down
3 changes: 0 additions & 3 deletions examples/cat_facts/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ serde_json = "1.0.132"
uniffi = "0.28.2"
wasm-bindgen = "0.2.95"

[dev-dependencies]
assert_let_bind = "0.1.1"

[target.uniffi-bindgen.dependencies]
uniffi = { version = "0.28.2", features = ["cli"] }

Expand Down
Loading

0 comments on commit 7678785

Please sign in to comment.