dioxus/packages/core/tests/diffing.rs

358 lines
8.6 KiB
Rust
Raw Normal View History

2021-07-30 14:35:47 +00:00
//! Diffing Tests
//! -------------
//!
//! These should always compile and run, but the result is not validated for each test.
//! TODO: Validate the results beyond visual inspection.
2021-07-29 22:04:09 +00:00
use bumpalo::Bump;
use anyhow::{Context, Result};
use dioxus::{
arena::SharedResources, diff::DiffMachine, prelude::*, DiffInstruction, DomEdit, MountType,
2021-07-29 22:04:09 +00:00
};
use dioxus_core as dioxus;
use dioxus_html as dioxus_elements;
2021-08-22 21:08:25 +00:00
use futures_util::FutureExt;
2021-07-29 22:04:09 +00:00
struct TestDom {
bump: Bump,
resources: SharedResources,
}
impl TestDom {
fn new() -> TestDom {
let bump = Bump::new();
let resources = SharedResources::new();
TestDom { bump, resources }
}
fn new_factory<'a>(&'a self) -> NodeFactory<'a> {
NodeFactory::new(&self.bump)
}
fn render<'a, F>(&'a self, lazy_nodes: LazyNodes<'a, F>) -> VNode<'a>
where
F: FnOnce(NodeFactory<'a>) -> VNode<'a>,
{
use dioxus_core::nodes::{IntoVNode, IntoVNodeList};
lazy_nodes.into_vnode(NodeFactory::new(&self.bump))
}
2021-08-22 21:08:25 +00:00
fn diff<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
// let mut edits = Vec::new();
2021-08-09 21:09:33 +00:00
let mut machine = DiffMachine::new_headless(&self.resources);
2021-08-22 21:08:25 +00:00
machine.stack.push(DiffInstruction::DiffNode { new, old });
2021-08-22 21:08:25 +00:00
machine.mutations
2021-07-29 22:04:09 +00:00
}
2021-08-22 21:08:25 +00:00
fn create<'a, F1>(&'a self, left: LazyNodes<'a, F1>) -> Mutations<'a>
2021-07-29 22:04:09 +00:00
where
F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
{
let old = self.bump.alloc(self.render(left));
2021-08-09 21:09:33 +00:00
let mut machine = DiffMachine::new_headless(&self.resources);
2021-08-22 21:08:25 +00:00
machine.stack.create_node(old, MountType::Append);
2021-08-22 21:08:25 +00:00
work_sync(&mut machine);
machine.mutations
2021-07-29 22:04:09 +00:00
}
fn lazy_diff<'a, F1, F2>(
&'a self,
left: LazyNodes<'a, F1>,
right: LazyNodes<'a, F2>,
2021-08-22 21:08:25 +00:00
) -> (Mutations<'a>, Mutations<'a>)
2021-07-29 22:04:09 +00:00
where
F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
F2: FnOnce(NodeFactory<'a>) -> VNode<'a>,
{
let old = self.bump.alloc(self.render(left));
let new = self.bump.alloc(self.render(right));
2021-08-22 21:08:25 +00:00
// let mut create_edits = Vec::new();
2021-07-29 22:04:09 +00:00
2021-08-09 21:09:33 +00:00
let mut machine = DiffMachine::new_headless(&self.resources);
machine.stack.create_node(old, MountType::Append);
2021-08-22 21:08:25 +00:00
work_sync(&mut machine);
let create_edits = machine.mutations;
2021-07-29 22:04:09 +00:00
2021-08-09 21:09:33 +00:00
let mut machine = DiffMachine::new_headless(&self.resources);
2021-08-23 14:43:49 +00:00
machine.stack.push(DiffInstruction::DiffNode { old, new });
2021-08-22 21:08:25 +00:00
work_sync(&mut machine);
let edits = machine.mutations;
2021-07-30 14:35:47 +00:00
(create_edits, edits)
2021-07-29 22:04:09 +00:00
}
}
2021-08-22 21:08:25 +00:00
fn work_sync(machine: &mut DiffMachine) {
let mut fut = machine.work().boxed_local();
while let None = (&mut fut).now_or_never() {
//
}
}
#[test]
fn diffing_works() {}
2021-07-30 14:35:47 +00:00
/// Should push the text node onto the stack and modify it
#[test]
fn html_and_rsx_generate_the_same_output() {
2021-07-29 22:04:09 +00:00
let dom = TestDom::new();
let edits = dom.lazy_diff(
rsx! ( div { "Hello world" } ),
rsx! ( div { "Goodbye world" } ),
);
dbg!(edits);
}
2021-07-30 14:35:47 +00:00
/// Should result in 3 elements on the stack
2021-07-29 22:04:09 +00:00
#[test]
fn fragments_create_properly() {
let dom = TestDom::new();
2021-08-22 21:08:25 +00:00
let Mutations { edits, noderefs } = dom.create(rsx! {
2021-07-29 22:04:09 +00:00
div { "Hello a" }
div { "Hello b" }
div { "Hello c" }
});
assert!(&edits[0].is("CreateElement"));
assert!(&edits[3].is("CreateElement"));
assert!(&edits[6].is("CreateElement"));
2021-08-22 21:08:25 +00:00
assert_eq!(*edits.last().unwrap(), DomEdit::AppendChildren { many: 3 });
2021-07-29 22:04:09 +00:00
dbg!(edits);
}
2021-07-30 14:35:47 +00:00
/// 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);
2021-08-22 21:08:25 +00:00
let last_edit = edits.1.edits.last().unwrap();
2021-07-30 14:35:47 +00:00
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);
2021-08-22 21:08:25 +00:00
assert!(edits.1.edits.is_empty());
2021-07-30 14:35:47 +00:00
}
/// 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();
2021-07-30 20:07:42 +00:00
// 0, 1, 2, 3, 4, 5, 6, 7, 8,
let left = rsx!({
(0..3).chain(3..6).chain(6..9).map(|f| {
rsx! { div { key: "{f}" }}
})
});
// 0, 1, 2, 6, 5, 4, 3, 7, 8, 9
let right = rsx!({
(0..3).chain((3..7).rev()).chain(7..10).map(|f| {
rsx! { div { key: "{f}" }}
})
});
// LIS: 3, 7, 8,
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}
#[test]
fn controlled_keyed_diffing_out_of_order() {
let dom = TestDom::new();
let left = [4, 5, 6, 7];
let left = rsx!({
left.iter().map(|f| {
rsx! { div { key: "{f}" "{f}" }}
})
});
// 0, 1, 2, 6, 5, 4, 3, 7, 8, 9
let right = [0, 5, 9, 6, 4];
let right = rsx!({
right.iter().map(|f| {
rsx! { div { key: "{f}" "{f}" }}
})
});
// LIS: 3, 7, 8,
2021-07-30 14:35:47 +00:00
let edits = dom.lazy_diff(left, right);
dbg!(&edits);
}