wip: get keyed diffing compiling

This commit is contained in:
Jonathan Kelley committed 2021-07-30 10:35:47 -04:00
1 parent 1749eba8eb
commit 0a0be95c3e
6 files changed
+485 -180

No files matched your search

+207 -6
View File
@@ -1,3 +1,9 @@
//! Diffing Tests
//! -------------
//!
//! These should always compile and run, but the result is not validated for each test.
//! TODO: Validate the results beyond visual inspection.
use bumpalo::Bump;
use anyhow::{Context, Result};
@@ -58,7 +64,7 @@ impl TestDom {
&'a self,
left: LazyNodes<'a, F1>,
right: LazyNodes<'a, F2>,
) -> Vec<DomEdit<'a>>
) -> (Vec<DomEdit<'a>>, Vec<DomEdit<'a>>)
where
F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
F2: FnOnce(NodeFactory<'a>) -> VNode<'a>,
@@ -67,26 +73,26 @@ impl TestDom {
let new = self.bump.alloc(self.render(right));
let mut edits = Vec::new();
let mut create_edits = Vec::new();
let dom = DebugDom::new();
let mut machine = DiffMachine::new_headless(&mut edits, &dom, &self.resources);
let mut machine = DiffMachine::new_headless(&mut create_edits, &dom, &self.resources);
machine.create_vnode(old);
edits.clear();
let mut edits = Vec::new();
let mut machine = DiffMachine::new_headless(&mut edits, &dom, &self.resources);
machine.diff_node(old, new);
edits
(create_edits, edits)
}
}
#[test]
fn diffing_works() {}
/// Should push the text node onto the stack and modify it
#[test]
fn html_and_rsx_generate_the_same_output() {
let dom = TestDom::new();
let edits = dom.lazy_diff(
rsx! ( div { "Hello world" } ),
rsx! ( div { "Goodbye world" } ),
@@ -94,6 +100,7 @@ fn html_and_rsx_generate_the_same_output() {
dbg!(edits);
}
/// Should result in 3 elements on the stack
#[test]
fn fragments_create_properly() {
let dom = TestDom::new();
@@ -109,3 +116,197 @@ fn fragments_create_properly() {
assert_eq!(meta.added_to_stack, 3);
dbg!(edits);
}
/// Should result in the creation of an anchor (placeholder) and then a replacewith
#[test]
fn empty_fragments_create_anchors() {
let dom = TestDom::new();
let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
let right = rsx!({ (0..1).map(|f| rsx! { div {}}) });
let edits = dom.lazy_diff(left, right);
dbg!(edits);
}
/// Should result in the creation of an anchor (placeholder) and then a replacewith m=5
#[test]
fn empty_fragments_create_many_anchors() {
let dom = TestDom::new();
let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
let right = rsx!({ (0..5).map(|f| rsx! { div {}}) });
let edits = dom.lazy_diff(left, right);
dbg!(edits);
}
/// Should result in the creation of an anchor (placeholder) and then a replacewith
/// Includes child nodes inside the fragment
#[test]
fn empty_fragments_create_anchors_with_many_children() {
let dom = TestDom::new();
let left = rsx!({ (0..0).map(|f| rsx! { div {} }) });
let right = rsx!({
(0..5).map(|f| {
rsx! { div { "hello" }}
})
});
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
let last_edit = edits.1.last().unwrap();
assert!(last_edit.is("ReplaceWith"));
}
/// Should result in every node being pushed and then replaced with an anchor
#[test]
fn many_items_become_fragment() {
let dom = TestDom::new();
let left = rsx!({
(0..2).map(|f| {
rsx! { div { "hello" }}
})
});
let right = rsx!({ (0..0).map(|f| rsx! { div {} }) });
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
/// Should result in no edits
#[test]
fn two_equal_fragments_are_equal() {
let dom = TestDom::new();
let left = rsx!({
(0..2).map(|f| {
rsx! { div { "hello" }}
})
});
let right = rsx!({
(0..2).map(|f| {
rsx! { div { "hello" }}
})
});
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
assert!(edits.1.is_empty());
}
/// Should result the creation of more nodes appended after the old last node
#[test]
fn two_fragments_with_differrent_elements_are_differet() {
let dom = TestDom::new();
let left = rsx!(
{(0..2).map(|f| {rsx! { div { }}})}
p {}
);
let right = rsx!(
{(0..5).map(|f| {rsx! { h1 { }}})}
p {}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
/// Should result in multiple nodes destroyed - with changes to the first nodes
#[test]
fn two_fragments_with_differrent_elements_are_differet_shorter() {
let dom = TestDom::new();
let left = rsx!(
{(0..5).map(|f| {rsx! { div { }}})}
p {}
);
let right = rsx!(
{(0..2).map(|f| {rsx! { h1 { }}})}
p {}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
/// Should result in multiple nodes destroyed - with no changes
#[test]
fn two_fragments_with_same_elements_are_differet() {
let dom = TestDom::new();
let left = rsx!(
{(0..2).map(|f| {rsx! { div { }}})}
p {}
);
let right = rsx!(
{(0..5).map(|f| {rsx! { div { }}})}
p {}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
// Similar test from above, but with extra child nodes
#[test]
fn two_fragments_with_same_elements_are_differet_shorter() {
let dom = TestDom::new();
let left = rsx!(
{(0..5).map(|f| {rsx! { div { }}})}
p {"e"}
);
let right = rsx!(
{(0..2).map(|f| {rsx! { div { }}})}
p {"e"}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
/// should result in the removal of elements
#[test]
fn keyed_diffing_order() {
let dom = TestDom::new();
let left = rsx!(
{(0..5).map(|f| {rsx! { div { key: "{f}" }}})}
p {"e"}
);
let right = rsx!(
{(0..2).map(|f| {rsx! { div { key: "{f}" }}})}
p {"e"}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
#[test]
fn fragment_keys() {
let r = 1;
let p = rsx! {
Fragment { key: "asd {r}" }
};
}
/// Should result in moves, but not removals or additions
#[test]
fn keyed_diffing_out_of_order() {
let dom = TestDom::new();
let left = rsx!(
{(0..5).map(|f| {rsx! { div { key: "{f}" }}})}
p {"e"}
);
let right = rsx!(
{(0..5).rev().map(|f| {rsx! { div { key: "{f}" }}})}
p {"e"}
);
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}