#[cfg(test)]
mod tests {
use crate::{State, View, Never};
struct Text {
content: String,
}
impl Text {
fn new(content: impl Into<String>) -> Self {
Self { content: content.into() }
}
}
impl View for Text {
type Body = Never;
fn body(self) -> Self::Body { unreachable!() }
}
struct Button<F> where F: Fn() {
label: String,
action: F,
}
impl<F> Button<F> where F: Fn() {
fn new(label: impl Into<String>, action: F) -> Self {
Self { label: label.into(), action }
}
}
impl<F> View for Button<F> where F: Fn() + Send {
type Body = Never;
fn body(self) -> Self::Body { unreachable!() }
}
struct AppView {
state: State<bool>,
}
impl View for AppView {
type Body = Button<Box<dyn Fn() + Send>>;
fn body(self) -> Self::Body {
let current_state = self.state.get();
let label = if current_state { "ON" } else { "OFF" };
let state_clone = self.state.clone();
Button::new(label, Box::new(move || {
let current = state_clone.get();
state_clone.set(!current);
}))
}
}
#[test]
fn test_minimal_app_compiles_and_toggles_state() {
let app = AppView {
state: State::new(false),
};
assert_eq!(app.state.get(), false);
let body = app.body();
(body.action)();
let state = State::new(false);
let state_clone = state.clone();
let action = move || {
let current = state_clone.get();
state_clone.set(!current);
};
assert_eq!(state.get(), false);
action();
assert_eq!(state.get(), true);
action();
assert_eq!(state.get(), false);
}
}