-
Notifications
You must be signed in to change notification settings - Fork 70
/
platform_test.rs
115 lines (94 loc) · 2.91 KB
/
platform_test.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
mod shared {
use crux_core::macros::Effect;
use crux_core::render::Render;
use crux_platform::{Platform, PlatformResponse};
use serde::{Deserialize, Serialize};
#[derive(Default)]
pub struct App;
#[derive(Serialize, Deserialize)]
pub enum Event {
PlatformGet,
PlatformSet(PlatformResponse),
}
#[derive(Default, Serialize, Deserialize)]
pub struct Model {
pub platform: String,
}
#[derive(Serialize, Deserialize, Default)]
pub struct ViewModel {
pub platform: String,
}
impl crux_core::App for App {
type Event = Event;
type Model = Model;
type ViewModel = ViewModel;
type Capabilities = Capabilities;
fn update(&self, event: Event, model: &mut Model, caps: &Capabilities) {
match event {
Event::PlatformGet => caps.platform.get(Event::PlatformSet),
Event::PlatformSet(platform) => {
model.platform = platform.0;
caps.render.render()
}
}
}
fn view(&self, model: &Self::Model) -> Self::ViewModel {
ViewModel {
platform: model.platform.clone(),
}
}
}
#[derive(Effect)]
pub struct Capabilities {
pub platform: Platform<Event>,
pub render: Render<Event>,
}
}
mod shell {
use super::shared::{App, Effect, Event};
use crux_core::{Core, Request};
use crux_platform::{PlatformRequest, PlatformResponse};
use std::collections::VecDeque;
pub enum Outcome {
Platform(Request<PlatformRequest>, PlatformResponse),
}
enum CoreMessage {
Event(Event),
Response(Outcome),
}
pub fn run(core: &Core<Effect, App>) {
let mut queue: VecDeque<CoreMessage> = VecDeque::new();
queue.push_back(CoreMessage::Event(Event::PlatformGet));
while !queue.is_empty() {
let msg = queue.pop_front();
let effs = match msg {
Some(CoreMessage::Event(m)) => core.process_event(m),
Some(CoreMessage::Response(Outcome::Platform(mut request, outcome))) => {
core.resolve(&mut request, outcome)
}
_ => vec![],
};
for effect in effs {
if let Effect::Platform(request) = effect {
queue.push_back(CoreMessage::Response(Outcome::Platform(
request,
PlatformResponse("test shell".to_string()),
)));
}
}
}
}
}
mod tests {
use crate::{
shared::{App, Effect},
shell::run,
};
use crux_core::Core;
#[test]
pub fn test_platform() {
let core: Core<Effect, App> = Core::default();
run(&core);
assert_eq!(core.view().platform, "test shell");
}
}