Skip to content

Commit f6f5278

Browse files
committed
begin testing for anathema-widgets
1 parent 2be7f98 commit f6f5278

File tree

5 files changed

+156
-41
lines changed

5 files changed

+156
-41
lines changed

anathema-backend/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<'rt, 'bp, T: Backend> WidgetCycle<'rt, 'bp, T> {
114114
let mut for_each = LayoutForEach::new(tree, &scope, filter, None);
115115
let constraints = self.constraints;
116116
_ = for_each.each(ctx, |ctx, widget, children| {
117-
let _ = widget.layout(children, constraints, ctx)?;
117+
_ = widget.layout(children, constraints, ctx)?;
118118
Ok(ControlFlow::Break(()))
119119
})?;
120120
Ok(())

anathema-widgets/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ pub mod query;
2020
pub mod tabindex;
2121
pub mod tree;
2222
mod widget;
23+
24+
25+
mod testing;

anathema-widgets/src/query/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,27 @@ where
166166
self.a.filter(arg, attributes) | self.b.filter(arg, attributes)
167167
}
168168
}
169+
170+
#[cfg(test)]
171+
mod test {
172+
use anathema_templates::{Document, ToSourceKind};
173+
174+
use super::*;
175+
use crate::{eval_blueprint, WidgetTree};
176+
177+
#[test]
178+
fn filter_by_tag() {
179+
let tpl = "
180+
many
181+
many
182+
text '1'
183+
text '2'
184+
many
185+
many
186+
text '3'
187+
";
188+
189+
crate::testing::with_template(tpl, |tree| {
190+
});
191+
}
192+
}

anathema-widgets/src/testing.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
use std::ops::ControlFlow;
2+
3+
use anathema_geometry::Size;
4+
use anathema_state::States;
5+
use anathema_store::tree::root_node;
6+
use anathema_templates::Document;
7+
use anathema_value_resolver::{AttributeStorage, Attributes, Scope};
8+
9+
use crate::components::ComponentRegistry;
10+
use crate::layout::{LayoutCtx, Viewport};
11+
use crate::{eval_blueprint, Components, Factory, FloatingWidgets, GlyphMap, LayoutForEach, Widget, WidgetTree, WidgetTreeView};
12+
13+
pub struct TestCase {}
14+
15+
pub fn with_template<F>(tpl: &str, f: F)
16+
where
17+
F: Fn(WidgetTreeView<'_, '_>),
18+
{
19+
let mut tree = WidgetTree::empty();
20+
let mut doc = Document::new(tpl);
21+
let (blueprint, globals) = doc.compile().unwrap();
22+
let globals = Box::leak(Box::new(globals));
23+
let blueprint = Box::leak(Box::new(blueprint));
24+
25+
let mut factory = Factory::new();
26+
factory.register_default::<Many>("many");
27+
factory.register_default::<Text>("text");
28+
29+
let mut states = States::new();
30+
let mut attribute_storage = AttributeStorage::empty();
31+
32+
let mut components = Components::new();
33+
let mut component_registry = ComponentRegistry::new();
34+
35+
let mut viewport = Viewport::new((80, 25));
36+
37+
let mut floating = FloatingWidgets::empty();
38+
let mut glyph_map = GlyphMap::empty();
39+
40+
let mut layout_ctx = LayoutCtx::new(
41+
globals,
42+
&factory,
43+
&mut states,
44+
&mut attribute_storage,
45+
&mut components,
46+
&mut component_registry,
47+
&mut floating,
48+
&mut glyph_map,
49+
&mut viewport,
50+
);
51+
52+
let mut ctx = layout_ctx.eval_ctx(None);
53+
let scope = Scope::root();
54+
55+
eval_blueprint(blueprint, &mut ctx, &scope, root_node(), &mut tree.view_mut()).unwrap();
56+
57+
let filter = crate::layout::LayoutFilter::fixed();
58+
let mut for_each = LayoutForEach::new(tree.view_mut(), &scope, filter, None);
59+
for_each.each(&mut layout_ctx, |ctx, widget, children| {
60+
_ = widget.layout(children, ctx.viewport.constraints(), ctx)?;
61+
Ok(ControlFlow::Break(()))
62+
}).unwrap();
63+
64+
65+
f(tree.view_mut());
66+
}
67+
68+
// -----------------------------------------------------------------------------
69+
// - Test widgets -
70+
// -----------------------------------------------------------------------------
71+
72+
#[derive(Debug, Default)]
73+
pub(crate) struct Many;
74+
75+
impl Widget for Many {
76+
fn layout<'bp>(
77+
&mut self,
78+
mut children: crate::LayoutForEach<'_, 'bp>,
79+
constraints: crate::layout::Constraints,
80+
id: crate::WidgetId,
81+
ctx: &mut LayoutCtx<'_, 'bp>,
82+
) -> crate::error::Result<anathema_geometry::Size> {
83+
let mut size = Size::ZERO;
84+
children.each(ctx, |ctx, child, children| {
85+
let child_size: Size = child.layout(children, constraints, ctx)?.into();
86+
size.width = size.width.max(child_size.width);
87+
size.height += child_size.height;
88+
Ok(ControlFlow::Continue(()))
89+
});
90+
91+
Ok(size)
92+
}
93+
94+
fn position<'bp>(
95+
&mut self,
96+
children: crate::ForEach<'_, 'bp, crate::layout::PositionFilter>,
97+
id: crate::WidgetId,
98+
attribute_storage: &AttributeStorage<'bp>,
99+
ctx: crate::layout::PositionCtx,
100+
) {
101+
//
102+
}
103+
}
104+
105+
#[derive(Debug, Default)]
106+
pub(crate) struct Text;
107+
108+
impl Widget for Text {
109+
fn layout<'bp>(
110+
&mut self,
111+
children: crate::LayoutForEach<'_, 'bp>,
112+
constraints: crate::layout::Constraints,
113+
id: crate::WidgetId,
114+
ctx: &mut LayoutCtx<'_, 'bp>,
115+
) -> crate::error::Result<anathema_geometry::Size> {
116+
Ok(Size::ZERO)
117+
}
118+
119+
fn position<'bp>(
120+
&mut self,
121+
children: crate::ForEach<'_, 'bp, crate::layout::PositionFilter>,
122+
id: crate::WidgetId,
123+
attribute_storage: &AttributeStorage<'bp>,
124+
ctx: crate::layout::PositionCtx,
125+
) {
126+
}
127+
}
128+

anathema-widgets/src/widget/attributes.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,6 @@ use crate::paint::CellAttributes;
99
use crate::widget::ValueKey;
1010
use crate::WidgetId;
1111

12-
// impl CellAttributes for Attributes<'_> {
13-
// fn with_str(&self, key: &str, f: &mut dyn FnMut(&str)) {
14-
// panic!("maybe this can be removed?");
15-
// // let Some(value) = self.get_val(key).and_then(|value| value.load_common_val()) else { return };
16-
// // let Some(value) = value.to_common() else { return };
17-
// // let CommonVal::Str(s) = value else { return };
18-
// // f(s);
19-
// }
20-
21-
// fn get_i64(&self, key: &str) -> Option<i64> {
22-
// self.get_int(key)
23-
// }
24-
25-
// fn get_u8(&self, key: &str) -> Option<u8> {
26-
// self.get_int(key).map(|i| i as u8)
27-
// }
28-
29-
// fn get_hex(&self, key: &str) -> Option<anathema_state::Hex> {
30-
// let value = self.get_val(key)?;
31-
// let value = value.load_common_val()?;
32-
// match value.to_common()? {
33-
// CommonVal::Hex(hex) => Some(hex),
34-
// _ => None,
35-
// }
36-
// }
37-
38-
// fn get_color(&self, key: &str) -> Option<anathema_state::Color> {
39-
// let value = self.get_val(key)?;
40-
// let value = value.load_common_val()?;
41-
// match value.to_common()? {
42-
// CommonVal::Color(color) => Some(color),
43-
// _ => None,
44-
// }
45-
// }
46-
47-
// fn get_bool(&self, key: &str) -> bool {
48-
// Attributes::get_bool(self, key)
49-
// }
50-
// }
51-
5212
#[cfg(test)]
5313
mod test {
5414
use super::*;

0 commit comments

Comments
 (0)