Skip to content

Commit 9379c80

Browse files
committed
Added test for an earlier bugfix with nested collections
1 parent f90a7c0 commit 9379c80

File tree

3 files changed

+105
-25
lines changed

3 files changed

+105
-25
lines changed

tests/nested_collections.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,58 @@
1+
use anathema::component::*;
2+
use anathema::prelude::*;
3+
use anathema_backend::testing::TestBackend;
4+
use testutils::{char_press, BasicComp};
5+
6+
#[derive(Debug, State)]
7+
struct Inner {
8+
list: Value<List<u32>>,
9+
}
10+
11+
impl Default for Inner {
12+
fn default() -> Self {
13+
Self {
14+
list: List::from_iter([1]).into(),
15+
}
16+
}
17+
}
18+
19+
#[derive(Debug, State, Default)]
20+
struct Outer {
21+
inner: Value<Inner>,
22+
}
23+
24+
type Comp<F> = BasicComp<F, Outer>;
25+
26+
fn keypress(key: KeyEvent, state: &mut Outer, mut children: Children<'_, '_>, mut context: Context<'_, '_, Outer>) {
27+
state.inner.set(Inner {
28+
list: List::from_iter([2]).into(),
29+
});
30+
}
31+
132
#[test]
233
fn nested_collections() {
3-
assert_eq!(expected, actual);
34+
let tpl = "
35+
for i in state.inner.list
36+
text i
37+
";
38+
let doc = Document::new("@index");
39+
40+
let mut backend = TestBackend::new((10, 3));
41+
42+
backend.add_event(None);
43+
backend.add_event(Some(ComponentEvent::Key(char_press(' '))));
44+
backend.add_event(Some(ComponentEvent::Stop));
45+
46+
let mut builder = Runtime::builder(doc, &backend);
47+
builder
48+
.component("index", tpl.to_template(), Comp::<_>::new(keypress), Outer::default())
49+
.unwrap();
50+
51+
let res = builder.finish(|runtime| runtime.run(&mut backend));
52+
53+
assert!(backend.at(0, 0).is_char('2'));
54+
55+
if let Err(e) = res {
56+
panic!("{e}");
57+
}
458
}

tests/hoppy.rs renamed to tests/statechange.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,20 @@ use anathema::component::*;
22
use anathema::prelude::*;
33
use anathema_backend::testing::TestBackend;
44
use anathema_runtime::Error;
5-
use testutils::{char_press, Thing};
6-
7-
#[derive(Debug, Default)]
8-
struct Comp;
9-
10-
impl Component for Comp {
11-
type Message = ();
12-
type State = Thing;
13-
14-
fn on_key(
15-
&mut self,
16-
key: KeyEvent,
17-
state: &mut Self::State,
18-
mut children: Children<'_, '_>,
19-
mut context: Context<'_, '_, Self::State>,
20-
) {
21-
state.value.set(9);
22-
}
5+
use testutils::{char_press, BasicComp, BasicState};
6+
7+
fn keypress(
8+
key: KeyEvent,
9+
state: &mut BasicState,
10+
mut children: Children<'_, '_>,
11+
mut context: Context<'_, '_, BasicState>,
12+
) {
13+
state.number.set(9);
2314
}
2415

2516
#[test]
26-
fn lol() {
27-
let tpl = "text state.value";
17+
fn state_change() {
18+
let tpl = "text state.number";
2819
let doc = Document::new("@index");
2920

3021
let mut backend = TestBackend::new((10, 3));
@@ -34,7 +25,14 @@ fn lol() {
3425
backend.add_event(Some(ComponentEvent::Stop));
3526

3627
let mut builder = Runtime::builder(doc, &backend);
37-
builder.from_default::<Comp>("index", tpl.to_template()).unwrap();
28+
builder
29+
.component(
30+
"index",
31+
tpl.to_template(),
32+
BasicComp::<_, BasicState>::new(keypress),
33+
BasicState::default(),
34+
)
35+
.unwrap();
3836

3937
let res = builder.finish(|runtime| runtime.run(&mut backend));
4038

testutils/src/lib.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
use anathema::prelude::*;
1+
use std::marker::PhantomData;
22
use anathema::component::*;
3+
use anathema::prelude::*;
34

45
#[derive(Debug, State, Default)]
5-
pub struct Thing {
6-
pub value: Value<u32>,
6+
pub struct BasicState {
7+
pub number: Value<u32>,
8+
}
9+
10+
pub struct BasicComp<F, T>(F, PhantomData<T>);
11+
12+
impl<F, T> BasicComp<F, T> {
13+
pub fn new(f: F) -> Self {
14+
Self(f, PhantomData)
15+
}
16+
}
17+
18+
impl<F, T> Component for BasicComp<F, T>
19+
where
20+
F: FnMut(KeyEvent, &mut T, Children<'_, '_>, Context<'_, '_, T>) + 'static,
21+
T: State
22+
{
23+
type Message = ();
24+
type State = T;
25+
26+
fn on_key(
27+
&mut self,
28+
key: KeyEvent,
29+
state: &mut Self::State,
30+
mut children: Children<'_, '_>,
31+
mut context: Context<'_, '_, Self::State>,
32+
) {
33+
self.0(key, state, children, context);
34+
}
735
}
836

937
pub fn char_press(c: char) -> KeyEvent {

0 commit comments

Comments
 (0)