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
|
use iced::widget::{button, column, text, Column};
use iced::Center;
pub fn main() -> iced::Result {
iced::run("A cool counter", Counter::update, Counter::view)
}
#[derive(Default)]
struct Counter {
value: i64,
}
#[derive(Debug, Clone, Copy)]
enum Message {
Increment,
Decrement,
}
impl Counter {
fn update(&mut self, message: Message) {
match message {
Message::Increment => {
self.value += 1;
}
Message::Decrement => {
self.value -= 1;
}
}
}
fn view(&self) -> Column<Message> {
column![
button("Increment").on_press(Message::Increment),
text(self.value).size(50),
button("Decrement").on_press(Message::Decrement)
]
.padding(20)
.align_x(Center)
}
}
#[cfg(test)]
mod tests {
use super::*;
use iced_test::selector::text;
use iced_test::{simulator, Error};
#[test]
fn it_counts() -> Result<(), Error> {
let mut counter = Counter { value: 0 };
let mut ui = simulator(counter.view());
let _ = ui.click(text("Increment"))?;
let _ = ui.click(text("Increment"))?;
let _ = ui.click(text("Decrement"))?;
for message in ui.into_messages() {
counter.update(message);
}
assert_eq!(counter.value, 1);
let mut ui = simulator(counter.view());
assert!(ui.find(text("1")).is_ok(), "Counter should display 1!");
Ok(())
}
}
|