Add unit-tests to `node::Taffy` (#178)
* Creating scaffolding for new tests
* Moving and adding tests
* Fleshing out more tests
* Add test for find-node
* fix import using #[cfg]
* adding child at index text
* Adding child-count test
* 0-index consistency pass on naming
* adding children test
* Finishing up last tests
* revert uneccesary changes
* Move tests inside `tests` module + fix comment placement
* Removing unnecessary test
* fix type of vec?
* std::vec -> sys::vec
* Further attempt at fixing vec
* Exposing some methods so we can use them for testing
* Fix vec type
* Add helper comment
* attempting pushing to vec
* Rebase onto new changes
* Cleanup
* Migrate tests to new_leaf
61e084218bf7c7d8918544435c2e055c85af3f47 · por Andreas Weibye · 2022-06-19T15:50:04Z (hace 4 años) · padre cdac14745 · ver el árbol en este commit
diff --git a/src/indexmap.rs b/src/indexmap.rs
index 21ca76be6..92a7ec7b2 100644
--- a/src/indexmap.rs
+++ b/src/indexmap.rs
@@ -1545,7 +1545,7 @@ where
/* Public API */
/// Returns the number of elements the map can hold
#[allow(dead_code)]
- fn capacity(&self) -> usize {
+ pub(crate) fn capacity(&self) -> usize {
N
}
@@ -1654,7 +1654,7 @@ where
///
/// Computes in **O(1)** time (average).
#[allow(dead_code)]
- fn contains_key<Q>(&self, key: &Q) -> bool
+ pub(crate) fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: ?Sized + Eq + Hash,
diff --git a/src/node.rs b/src/node.rs
index 8dc41bd5f..b86d3fd5a 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -11,6 +11,33 @@ use crate::sys::Box;
use crate::sys::{new_map_with_capacity, ChildrenVec, Map, Vec};
use core::sync::atomic::{AtomicUsize, Ordering};
+/// Internal node id.
+pub(crate) type NodeId = usize;
+
+/// The identifier of a [`Node`]
+#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
+#[cfg_attr(not(any(feature = "std", feature = "alloc")), derive(hash32_derive::Hash32))]
+pub(crate) struct Id(usize);
+
+/// An bump-allocator index that tracks how many [`Nodes`](Node) have been allocated in a [`Taffy`].
+pub(crate) struct Allocator {
+ /// The last reserved [`NodeId`]
+ last_id: AtomicUsize,
+}
+
+impl Allocator {
+ /// Creates a fresh [`Allocator`]
+ #[must_use]
+ pub const fn new() -> Self {
+ Self { last_id: AtomicUsize::new(0) }
+ }
+
+ /// Allocates space for one more [`Node`]
+ pub fn allocate(&self) -> Id {
+ Id(self.last_id.fetch_add(1, Ordering::Relaxed))
+ }
+}
+
/// A function type that can be used in a [`MeasureFunc`]
///
/// This trait is automatically implemented for all types (including closures) that define a function with the appropriate type signature.
@@ -313,36 +340,402 @@ impl Taffy {
}
}
-/// Internal node id.
-pub(crate) type NodeId = usize;
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{
+ style::{Dimension, Display, FlexDirection},
+ sys,
+ };
-/// The identifier of a [`Node`]
-#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
-#[cfg_attr(not(any(feature = "std", feature = "alloc")), derive(hash32_derive::Hash32))]
-pub(crate) struct Id(usize);
+ #[test]
+ fn new_should_allocate_default_capacity() {
+ const DEFAULT_CAPACITY: usize = 16; // This is the capacity defined in the `impl Default`
+ let taffy = Taffy::new();
+
+ assert!(taffy.nodes_to_ids.capacity() >= DEFAULT_CAPACITY);
+ assert!(taffy.ids_to_nodes.capacity() >= DEFAULT_CAPACITY);
+ assert!(taffy.forest.children.capacity() >= DEFAULT_CAPACITY);
+ assert!(taffy.forest.parents.capacity() >= DEFAULT_CAPACITY);
+ assert!(taffy.forest.nodes.capacity() >= DEFAULT_CAPACITY);
+ }
-/// An bump-allocator index that tracks how many [`Nodes`](Node) have been allocated in a [`Taffy`].
-pub(crate) struct Allocator {
- /// The last reserved [`NodeId`]
- last_id: AtomicUsize,
-}
+ #[test]
+ fn test_with_capacity() {
+ const CAPACITY: usize = 8;
+ let taffy = Taffy::with_capacity(CAPACITY);
+
+ assert!(taffy.nodes_to_ids.capacity() >= CAPACITY);
+ assert!(taffy.ids_to_nodes.capacity() >= CAPACITY);
+ assert!(taffy.forest.children.capacity() >= CAPACITY);
+ assert!(taffy.forest.parents.capacity() >= CAPACITY);
+ assert!(taffy.forest.nodes.capacity() >= CAPACITY);
+ }
-impl Allocator {
- /// Creates a fresh [`Allocator`]
- #[must_use]
- pub const fn new() -> Self {
- Self { last_id: AtomicUsize::new(0) }
+ #[test]
+ fn add_node() {
+ let mut taffy = Taffy::new();
+ let node = taffy.allocate_node();
+ let id = taffy.forest.new_leaf(FlexboxLayout::default());
+
+ // Should not contain nodes
+ assert!(!taffy.nodes_to_ids.contains_key(&node));
+ assert!(!taffy.ids_to_nodes.contains_key(&id));
+
+ taffy.add_node(node, id);
+
+ // Should contain nodes
+ assert!(taffy.nodes_to_ids.contains_key(&node));
+ assert!(taffy.ids_to_nodes.contains_key(&id));
}
- /// Allocates space for one more [`Node`]
- pub fn allocate(&self) -> Id {
- Id(self.last_id.fetch_add(1, Ordering::Relaxed))
+ #[test]
+ fn find_node() {
+ let mut taffy = Taffy::new();
+
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ // Should find the nodes
+ assert!(if let Ok(node_id) = taffy.find_node(node) { node_id == node.local.0 } else { false });
+ assert!(if let Ok(node_id) = taffy.find_node(child0) { node_id == child0.local.0 } else { false });
+ assert!(if let Ok(node_id) = taffy.find_node(child1) { node_id == child1.local.0 } else { false });
+
+ let _ = taffy.remove(node);
+ let _ = taffy.remove(child0);
+ let _ = taffy.remove(child1);
+
+ // Should not find the nodes
+ assert!(taffy.find_node(node).is_err());
+ assert!(taffy.find_node(child0).is_err());
+ assert!(taffy.find_node(child1).is_err());
}
-}
-#[cfg(test)]
-mod tests {
- use super::*;
+ #[test]
+ fn test_new_leaf() {
+ let mut taffy = Taffy::new();
+
+ let res = taffy.new_leaf(FlexboxLayout::default());
+ assert!(res.is_ok());
+ let node = res.unwrap();
+
+ // node should be in the taffy tree and have no children
+ assert!(taffy.find_node(node).is_ok());
+ assert!(taffy.child_count(node).unwrap() == 0);
+ }
+
+ #[test]
+ fn new_leaf_with_measure() {
+ let mut taffy = Taffy::new();
+
+ let res = taffy.new_leaf_with_measure(FlexboxLayout::default(), MeasureFunc::Raw(|_| Size::zero()));
+ assert!(res.is_ok());
+ let node = res.unwrap();
+
+ // node should be in the taffy tree and have no children
+ assert!(taffy.find_node(node).is_ok());
+ assert!(taffy.child_count(node).unwrap() == 0);
+ }
+
+ /// Test that new_with_children works as expected
+ #[test]
+ fn test_new_with_children() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ // node should have two children
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+ assert_eq!(taffy.children(node).unwrap()[0], child0);
+ assert_eq!(taffy.children(node).unwrap()[1], child1);
+ }
+
+ #[test]
+ fn clear_should_clear_nodes() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ // Nodes should be present
+ assert!(taffy.find_node(node).is_ok());
+ assert!(taffy.find_node(child0).is_ok());
+ assert!(taffy.find_node(child1).is_ok());
+
+ taffy.clear();
+
+ // nodes should not be present
+ assert!(taffy.find_node(node).is_err());
+ assert!(taffy.find_node(child0).is_err());
+ assert!(taffy.find_node(child1).is_err());
+ }
+
+ #[test]
+ fn remove_node_should_remove() {
+ let mut taffy = Taffy::new();
+
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
+
+ // node should exist
+ assert!(taffy.find_node(node).is_ok());
+
+ let _ = taffy.remove(node).unwrap();
+
+ // node should no longer exist
+ assert!(taffy.find_node(node).is_err());
+ }
+
+ #[test]
+ fn remove_node_should_detach_herarchy() {
+ let mut taffy = Taffy::new();
+
+ // Build a linear tree layout: <0> <- <1> <- <2>
+ let node2 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node1 = taffy.new_with_children(FlexboxLayout::default(), &[node2]).unwrap();
+ let node0 = taffy.new_with_children(FlexboxLayout::default(), &[node1]).unwrap();
+
+ // Node1 should exist
+ assert!(taffy.find_node(node1).is_ok());
+
+ // Both node0 and node1 should have 1 child nodes
+ assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
+ assert_eq!(taffy.children(node1).unwrap().as_slice(), &[node2]);
+
+ // Disconnect the tree: <0> <2>
+ let _ = taffy.remove(node1).unwrap();
+
+ // Node1 should no longer exist
+ assert!(taffy.find_node(node1).is_err());
+
+ // Both remaining nodes should have no child nodes
+ assert!(taffy.children(node0).unwrap().is_empty());
+ assert!(taffy.children(node2).unwrap().is_empty());
+ }
+
+ #[test]
+ fn remove_last_node() {
+ let mut taffy = Taffy::new();
+
+ let parent = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ taffy.add_child(parent, child).unwrap();
+
+ taffy.remove(child).unwrap();
+ taffy.remove(parent).unwrap();
+ }
+
+ #[test]
+ fn set_measure() {
+ let mut taffy = Taffy::new();
+ let node = taffy
+ .new_leaf_with_measure(FlexboxLayout::default(), MeasureFunc::Raw(|_| Size { width: 200.0, height: 200.0 }))
+ .unwrap();
+ taffy.compute_layout(node, Size::undefined()).unwrap();
+ assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
+
+ taffy.set_measure(node, Some(MeasureFunc::Raw(|_| Size { width: 100.0, height: 100.0 }))).unwrap();
+ taffy.compute_layout(node, Size::undefined()).unwrap();
+ assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
+ }
+
+ /// Test that adding `add_child()` works
+ #[test]
+ fn add_child() {
+ let mut taffy = Taffy::new();
+ let node = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 0);
+
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ taffy.add_child(node, child0).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 1);
+
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ taffy.add_child(node, child1).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+ }
+
+ #[test]
+ fn set_children() {
+ let mut taffy = Taffy::new();
+
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+ assert_eq!(taffy.children(node).unwrap()[0], child0);
+ assert_eq!(taffy.children(node).unwrap()[1], child1);
+
+ let child2 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child3 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ taffy.set_children(node, &[child2, child3]).unwrap();
+
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+ assert_eq!(taffy.children(node).unwrap()[0], child2);
+ assert_eq!(taffy.children(node).unwrap()[1], child3);
+ }
+
+ /// Test that removing a child works
+ #[test]
+ fn remove_child() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+
+ taffy.remove_child(node, child0).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 1);
+ assert_eq!(taffy.children(node).unwrap()[0], child1);
+
+ taffy.remove_child(node, child1).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 0);
+ }
+
+ #[test]
+ fn remove_child_at_index() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ assert_eq!(taffy.child_count(node).unwrap(), 2);
+
+ taffy.remove_child_at_index(node, 0).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 1);
+ assert_eq!(taffy.children(node).unwrap()[0], child1);
+
+ taffy.remove_child_at_index(node, 0).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 0);
+ }
+
+ #[test]
+ fn replace_child_at_index() {
+ let mut taffy = Taffy::new();
+
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0]).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 1);
+ assert_eq!(taffy.children(node).unwrap()[0], child0);
+
+ taffy.replace_child_at_index(node, 0, child1).unwrap();
+ assert_eq!(taffy.child_count(node).unwrap(), 1);
+ assert_eq!(taffy.children(node).unwrap()[0], child1);
+ }
+ #[test]
+ fn test_child_at_index() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child2 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1, child2]).unwrap();
+
+ assert!(if let Ok(result) = taffy.child_at_index(node, 0) { result == child0 } else { false });
+ assert!(if let Ok(result) = taffy.child_at_index(node, 1) { result == child1 } else { false });
+ assert!(if let Ok(result) = taffy.child_at_index(node, 2) { result == child2 } else { false });
+ }
+ #[test]
+ fn test_child_count() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ assert!(if let Ok(count) = taffy.child_count(node) { count == 2 } else { false });
+ assert!(if let Ok(count) = taffy.child_count(child0) { count == 0 } else { false });
+ assert!(if let Ok(count) = taffy.child_count(child1) { count == 0 } else { false });
+ }
+ #[test]
+ fn test_children() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ let mut children: sys::Vec<Node> = sys::Vec::new();
+ children.push(child0);
+ children.push(child1);
+
+ let children_result = taffy.children(node).unwrap();
+ assert_eq!(children_result, children);
+
+ assert!(taffy.children(child0).unwrap().len() == 0);
+ }
+ #[test]
+ fn test_set_style() {
+ let mut taffy = Taffy::new();
+
+ let node = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
+
+ taffy.set_style(node, FlexboxLayout { display: Display::None, ..FlexboxLayout::default() }).unwrap();
+ assert_eq!(taffy.style(node).unwrap().display, Display::None);
+ }
+ #[test]
+ fn test_style() {
+ let mut taffy = Taffy::new();
+
+ let style =
+ FlexboxLayout { display: Display::None, flex_direction: FlexDirection::RowReverse, ..Default::default() };
+
+ let node = taffy.new_leaf(style).unwrap();
+
+ let res = taffy.style(node);
+ assert!(res.is_ok());
+ assert!(res.unwrap() == &style);
+ }
+ #[test]
+ fn test_layout() {
+ let mut taffy = Taffy::new();
+ let node = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+
+ // TODO: Improve this test?
+ let res = taffy.layout(node);
+ assert!(res.is_ok());
+ }
+
+ #[test]
+ fn test_mark_dirty() {
+ let mut taffy = Taffy::new();
+ let child0 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let child1 = taffy.new_leaf(FlexboxLayout::default()).unwrap();
+ let node = taffy.new_with_children(FlexboxLayout::default(), &[child0, child1]).unwrap();
+
+ taffy.compute_layout(node, Size::undefined()).unwrap();
+
+ assert_eq!(taffy.dirty(child0).unwrap(), false);
+ assert_eq!(taffy.dirty(child1).unwrap(), false);
+ assert_eq!(taffy.dirty(node).unwrap(), false);
+
+ taffy.mark_dirty(node).unwrap();
+ assert_eq!(taffy.dirty(child0).unwrap(), false);
+ assert_eq!(taffy.dirty(child1).unwrap(), false);
+ assert_eq!(taffy.dirty(node).unwrap(), true);
+
+ taffy.compute_layout(node, Size::undefined()).unwrap();
+ taffy.mark_dirty(child0).unwrap();
+ assert_eq!(taffy.dirty(child0).unwrap(), true);
+ assert_eq!(taffy.dirty(child1).unwrap(), false);
+ assert_eq!(taffy.dirty(node).unwrap(), true);
+ }
+
+ #[test]
+ fn compute_layout_should_produce_valid_result() {
+ let mut taffy = Taffy::new();
+ let node_result = taffy.new_leaf(FlexboxLayout {
+ size: Size { width: Dimension::Points(10f32), height: Dimension::Points(10f32) },
+ ..Default::default()
+ });
+ assert!(node_result.is_ok());
+ let node = node_result.unwrap();
+ let layout_result = taffy.compute_layout(node, Size { width: Some(100.), height: Some(100.) });
+ assert!(layout_result.is_ok());
+ }
#[test]
fn measure_func_is_send_and_sync() {
diff --git a/tests/node.rs b/tests/node.rs
deleted file mode 100644
index 526c09800..000000000
--- a/tests/node.rs
+++ /dev/null
@@ -1,192 +0,0 @@
-#[cfg(test)]
-mod node {
- use taffy::geometry::*;
- use taffy::node::{MeasureFunc, Taffy};
- use taffy::style::*;
-
- #[test]
- fn children() {
- let mut taffy = Taffy::new();
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
-
- assert_eq!(taffy.child_count(node).unwrap(), 2);
- assert_eq!(taffy.children(node).unwrap()[0], child1);
- assert_eq!(taffy.children(node).unwrap()[1], child2);
- }
-
- #[test]
- fn set_measure() {
- let mut taffy = Taffy::new();
- let node = taffy
- .new_leaf_with_measure(FlexboxLayout::default(), MeasureFunc::Raw(|_| Size { width: 200.0, height: 200.0 }))
- .unwrap();
- taffy.compute_layout(node, Size::undefined()).unwrap();
- assert_eq!(taffy.layout(node).unwrap().size.width, 200.0);
-
- taffy.set_measure(node, Some(MeasureFunc::Raw(|_| Size { width: 100.0, height: 100.0 }))).unwrap();
- taffy.compute_layout(node, Size::undefined()).unwrap();
- assert_eq!(taffy.layout(node).unwrap().size.width, 100.0);
- }
-
- #[test]
- fn add_child() {
- let mut taffy = Taffy::new();
- let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 0);
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- taffy.add_child(node, child1).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 1);
-
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- taffy.add_child(node, child2).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 2);
- }
-
- #[test]
- fn remove_child() {
- let mut taffy = Taffy::new();
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
-
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 2);
-
- taffy.remove_child(node, child1).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 1);
- assert_eq!(taffy.children(node).unwrap()[0], child2);
-
- taffy.remove_child(node, child2).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 0);
- }
-
- #[test]
- fn remove_child_at_index() {
- let mut taffy = Taffy::new();
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
-
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 2);
-
- taffy.remove_child_at_index(node, 0).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 1);
- assert_eq!(taffy.children(node).unwrap()[0], child2);
-
- taffy.remove_child_at_index(node, 0).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 0);
- }
-
- #[test]
- fn replace_child_at_index() {
- let mut taffy = Taffy::new();
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
-
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1]).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 1);
- assert_eq!(taffy.children(node).unwrap()[0], child1);
-
- taffy.replace_child_at_index(node, 0, child2).unwrap();
- assert_eq!(taffy.child_count(node).unwrap(), 1);
- assert_eq!(taffy.children(node).unwrap()[0], child2);
- }
-
- #[test]
- fn remove() {
- let mut taffy = Taffy::new();
-
- let style2 = FlexboxLayout { flex_direction: FlexDirection::Column, ..FlexboxLayout::default() };
-
- // Build a linear tree layout: <0> <- <1> <- <2>
- let node2 = taffy.new_with_children(style2, &[]).unwrap();
- let node1 = taffy.new_with_children(FlexboxLayout::default(), &[node2]).unwrap();
- let node0 = taffy.new_with_children(FlexboxLayout::default(), &[node1]).unwrap();
-
- assert_eq!(taffy.children(node0).unwrap().as_slice(), &[node1]);
-
- // Disconnect the tree: <0> <2>
- taffy.remove(node1).unwrap();
-
- assert!(taffy.style(node1).is_err());
-
- assert!(taffy.children(node0).unwrap().is_empty());
- assert!(taffy.children(node2).unwrap().is_empty());
- assert_eq!(taffy.style(node2).unwrap().flex_direction, style2.flex_direction);
- }
-
- #[test]
- fn set_children() {
- let mut taffy = Taffy::new();
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
-
- assert_eq!(taffy.child_count(node).unwrap(), 2);
- assert_eq!(taffy.children(node).unwrap()[0], child1);
- assert_eq!(taffy.children(node).unwrap()[1], child2);
-
- let child3 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child4 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- taffy.set_children(node, &[child3, child4]).unwrap();
-
- assert_eq!(taffy.child_count(node).unwrap(), 2);
- assert_eq!(taffy.children(node).unwrap()[0], child3);
- assert_eq!(taffy.children(node).unwrap()[1], child4);
- }
-
- #[test]
- fn set_style() {
- let mut taffy = Taffy::new();
-
- let node = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- assert_eq!(taffy.style(node).unwrap().display, Display::Flex);
-
- taffy.set_style(node, FlexboxLayout { display: Display::None, ..FlexboxLayout::default() }).unwrap();
- assert_eq!(taffy.style(node).unwrap().display, Display::None);
- }
-
- #[test]
- fn mark_dirty() {
- let mut taffy = Taffy::new();
-
- let child1 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child2 = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let node = taffy.new_with_children(FlexboxLayout::default(), &[child1, child2]).unwrap();
-
- taffy.compute_layout(node, taffy::geometry::Size::undefined()).unwrap();
-
- assert_eq!(taffy.dirty(child1).unwrap(), false);
- assert_eq!(taffy.dirty(child2).unwrap(), false);
- assert_eq!(taffy.dirty(node).unwrap(), false);
-
- taffy.mark_dirty(node).unwrap();
- assert_eq!(taffy.dirty(child1).unwrap(), false);
- assert_eq!(taffy.dirty(child2).unwrap(), false);
- assert_eq!(taffy.dirty(node).unwrap(), true);
-
- taffy.compute_layout(node, taffy::geometry::Size::undefined()).unwrap();
- taffy.mark_dirty(child1).unwrap();
- assert_eq!(taffy.dirty(child1).unwrap(), true);
- assert_eq!(taffy.dirty(child2).unwrap(), false);
- assert_eq!(taffy.dirty(node).unwrap(), true);
- }
-
- #[test]
- fn remove_last_node() {
- let mut taffy = Taffy::new();
-
- let parent = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- let child = taffy.new_with_children(FlexboxLayout::default(), &[]).unwrap();
- taffy.add_child(parent, child).unwrap();
-
- taffy.remove(child).unwrap();
- taffy.remove(parent).unwrap();
- }
-}