diffing.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. //! Diffing Tests
  2. //! -------------
  3. //!
  4. //! These should always compile and run, but the result is not validated for each test.
  5. //! TODO: Validate the results beyond visual inspection.
  6. use bumpalo::Bump;
  7. use anyhow::{Context, Result};
  8. use dioxus::{
  9. arena::SharedResources,
  10. diff::{DiffInstruction, DiffMachine},
  11. prelude::*,
  12. DomEdit,
  13. };
  14. use dioxus_core as dioxus;
  15. use dioxus_html as dioxus_elements;
  16. use futures_util::FutureExt;
  17. struct TestDom {
  18. bump: Bump,
  19. resources: SharedResources,
  20. }
  21. impl TestDom {
  22. fn new() -> TestDom {
  23. let bump = Bump::new();
  24. let resources = SharedResources::new();
  25. TestDom { bump, resources }
  26. }
  27. fn new_factory<'a>(&'a self) -> NodeFactory<'a> {
  28. NodeFactory::new(&self.bump)
  29. }
  30. fn render<'a, F>(&'a self, lazy_nodes: LazyNodes<'a, F>) -> VNode<'a>
  31. where
  32. F: FnOnce(NodeFactory<'a>) -> VNode<'a>,
  33. {
  34. use dioxus_core::nodes::{IntoVNode, IntoVNodeList};
  35. lazy_nodes.into_vnode(NodeFactory::new(&self.bump))
  36. }
  37. fn diff<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
  38. // let mut edits = Vec::new();
  39. let mut machine = DiffMachine::new_headless(&self.resources);
  40. machine
  41. .stack
  42. .push(dioxus::diff::DiffInstruction::DiffNode { new, old });
  43. machine.mutations
  44. }
  45. fn create<'a, F1>(&'a self, left: LazyNodes<'a, F1>) -> Mutations<'a>
  46. where
  47. F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
  48. {
  49. let old = self.bump.alloc(self.render(left));
  50. let mut machine = DiffMachine::new_headless(&self.resources);
  51. machine.stack.push(dioxus::diff::DiffInstruction::Create {
  52. node: old,
  53. and: dioxus::diff::MountType::Append,
  54. });
  55. work_sync(&mut machine);
  56. machine.mutations
  57. }
  58. fn lazy_diff<'a, F1, F2>(
  59. &'a self,
  60. left: LazyNodes<'a, F1>,
  61. right: LazyNodes<'a, F2>,
  62. ) -> (Mutations<'a>, Mutations<'a>)
  63. where
  64. F1: FnOnce(NodeFactory<'a>) -> VNode<'a>,
  65. F2: FnOnce(NodeFactory<'a>) -> VNode<'a>,
  66. {
  67. let old = self.bump.alloc(self.render(left));
  68. let new = self.bump.alloc(self.render(right));
  69. // let mut create_edits = Vec::new();
  70. let mut machine = DiffMachine::new_headless(&self.resources);
  71. machine.stack.push(dioxus::diff::DiffInstruction::Create {
  72. and: dioxus::diff::MountType::Append,
  73. node: old,
  74. });
  75. work_sync(&mut machine);
  76. let create_edits = machine.mutations;
  77. let mut machine = DiffMachine::new_headless(&self.resources);
  78. machine.stack.push(DiffInstruction::DiffNode { old, new });
  79. work_sync(&mut machine);
  80. let edits = machine.mutations;
  81. (create_edits, edits)
  82. }
  83. }
  84. fn work_sync(machine: &mut DiffMachine) {
  85. let mut fut = machine.work().boxed_local();
  86. while let None = (&mut fut).now_or_never() {
  87. //
  88. }
  89. }
  90. #[test]
  91. fn diffing_works() {}
  92. /// Should push the text node onto the stack and modify it
  93. #[test]
  94. fn html_and_rsx_generate_the_same_output() {
  95. let dom = TestDom::new();
  96. let edits = dom.lazy_diff(
  97. rsx! ( div { "Hello world" } ),
  98. rsx! ( div { "Goodbye world" } ),
  99. );
  100. dbg!(edits);
  101. }
  102. /// Should result in 3 elements on the stack
  103. #[test]
  104. fn fragments_create_properly() {
  105. let dom = TestDom::new();
  106. let Mutations { edits, noderefs } = dom.create(rsx! {
  107. div { "Hello a" }
  108. div { "Hello b" }
  109. div { "Hello c" }
  110. });
  111. assert!(&edits[0].is("CreateElement"));
  112. assert!(&edits[3].is("CreateElement"));
  113. assert!(&edits[6].is("CreateElement"));
  114. assert_eq!(*edits.last().unwrap(), DomEdit::AppendChildren { many: 3 });
  115. dbg!(edits);
  116. }
  117. /// Should result in the creation of an anchor (placeholder) and then a replacewith
  118. #[test]
  119. fn empty_fragments_create_anchors() {
  120. let dom = TestDom::new();
  121. let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
  122. let right = rsx!({ (0..1).map(|f| rsx! { div {}}) });
  123. let edits = dom.lazy_diff(left, right);
  124. dbg!(edits);
  125. }
  126. /// Should result in the creation of an anchor (placeholder) and then a replacewith m=5
  127. #[test]
  128. fn empty_fragments_create_many_anchors() {
  129. let dom = TestDom::new();
  130. let left = rsx!({ (0..0).map(|f| rsx! { div {}}) });
  131. let right = rsx!({ (0..5).map(|f| rsx! { div {}}) });
  132. let edits = dom.lazy_diff(left, right);
  133. dbg!(edits);
  134. }
  135. /// Should result in the creation of an anchor (placeholder) and then a replacewith
  136. /// Includes child nodes inside the fragment
  137. #[test]
  138. fn empty_fragments_create_anchors_with_many_children() {
  139. let dom = TestDom::new();
  140. let left = rsx!({ (0..0).map(|f| rsx! { div {} }) });
  141. let right = rsx!({
  142. (0..5).map(|f| {
  143. rsx! { div { "hello" }}
  144. })
  145. });
  146. let edits = dom.lazy_diff(left, right);
  147. dbg!(&edits);
  148. let last_edit = edits.1.edits.last().unwrap();
  149. assert!(last_edit.is("ReplaceWith"));
  150. }
  151. /// Should result in every node being pushed and then replaced with an anchor
  152. #[test]
  153. fn many_items_become_fragment() {
  154. let dom = TestDom::new();
  155. let left = rsx!({
  156. (0..2).map(|f| {
  157. rsx! { div { "hello" }}
  158. })
  159. });
  160. let right = rsx!({ (0..0).map(|f| rsx! { div {} }) });
  161. let edits = dom.lazy_diff(left, right);
  162. dbg!(&edits);
  163. }
  164. /// Should result in no edits
  165. #[test]
  166. fn two_equal_fragments_are_equal() {
  167. let dom = TestDom::new();
  168. let left = rsx!({
  169. (0..2).map(|f| {
  170. rsx! { div { "hello" }}
  171. })
  172. });
  173. let right = rsx!({
  174. (0..2).map(|f| {
  175. rsx! { div { "hello" }}
  176. })
  177. });
  178. let edits = dom.lazy_diff(left, right);
  179. dbg!(&edits);
  180. assert!(edits.1.edits.is_empty());
  181. }
  182. /// Should result the creation of more nodes appended after the old last node
  183. #[test]
  184. fn two_fragments_with_differrent_elements_are_differet() {
  185. let dom = TestDom::new();
  186. let left = rsx!(
  187. {(0..2).map(|f| {rsx! { div { }}})}
  188. p {}
  189. );
  190. let right = rsx!(
  191. {(0..5).map(|f| {rsx! { h1 { }}})}
  192. p {}
  193. );
  194. let edits = dom.lazy_diff(left, right);
  195. dbg!(&edits);
  196. }
  197. /// Should result in multiple nodes destroyed - with changes to the first nodes
  198. #[test]
  199. fn two_fragments_with_differrent_elements_are_differet_shorter() {
  200. let dom = TestDom::new();
  201. let left = rsx!(
  202. {(0..5).map(|f| {rsx! { div { }}})}
  203. p {}
  204. );
  205. let right = rsx!(
  206. {(0..2).map(|f| {rsx! { h1 { }}})}
  207. p {}
  208. );
  209. let edits = dom.lazy_diff(left, right);
  210. dbg!(&edits);
  211. }
  212. /// Should result in multiple nodes destroyed - with no changes
  213. #[test]
  214. fn two_fragments_with_same_elements_are_differet() {
  215. let dom = TestDom::new();
  216. let left = rsx!(
  217. {(0..2).map(|f| {rsx! { div { }}})}
  218. p {}
  219. );
  220. let right = rsx!(
  221. {(0..5).map(|f| {rsx! { div { }}})}
  222. p {}
  223. );
  224. let edits = dom.lazy_diff(left, right);
  225. dbg!(&edits);
  226. }
  227. // Similar test from above, but with extra child nodes
  228. #[test]
  229. fn two_fragments_with_same_elements_are_differet_shorter() {
  230. let dom = TestDom::new();
  231. let left = rsx!(
  232. {(0..5).map(|f| {rsx! { div { }}})}
  233. p {"e"}
  234. );
  235. let right = rsx!(
  236. {(0..2).map(|f| {rsx! { div { }}})}
  237. p {"e"}
  238. );
  239. let edits = dom.lazy_diff(left, right);
  240. dbg!(&edits);
  241. }
  242. /// should result in the removal of elements
  243. #[test]
  244. fn keyed_diffing_order() {
  245. let dom = TestDom::new();
  246. let left = rsx!(
  247. {(0..5).map(|f| {rsx! { div { key: "{f}" }}})}
  248. p {"e"}
  249. );
  250. let right = rsx!(
  251. {(0..2).map(|f| {rsx! { div { key: "{f}" }}})}
  252. p {"e"}
  253. );
  254. let edits = dom.lazy_diff(left, right);
  255. dbg!(&edits);
  256. }
  257. #[test]
  258. fn fragment_keys() {
  259. let r = 1;
  260. let p = rsx! {
  261. Fragment { key: "asd {r}" }
  262. };
  263. }
  264. /// Should result in moves, but not removals or additions
  265. #[test]
  266. fn keyed_diffing_out_of_order() {
  267. let dom = TestDom::new();
  268. // 0, 1, 2, 3, 4, 5, 6, 7, 8,
  269. let left = rsx!({
  270. (0..3).chain(3..6).chain(6..9).map(|f| {
  271. rsx! { div { key: "{f}" }}
  272. })
  273. });
  274. // 0, 1, 2, 6, 5, 4, 3, 7, 8, 9
  275. let right = rsx!({
  276. (0..3).chain((3..7).rev()).chain(7..10).map(|f| {
  277. rsx! { div { key: "{f}" }}
  278. })
  279. });
  280. // LIS: 3, 7, 8,
  281. let edits = dom.lazy_diff(left, right);
  282. dbg!(&edits);
  283. }
  284. #[test]
  285. fn controlled_keyed_diffing_out_of_order() {
  286. let dom = TestDom::new();
  287. let left = [4, 5, 6, 7];
  288. let left = rsx!({
  289. left.iter().map(|f| {
  290. rsx! { div { key: "{f}" "{f}" }}
  291. })
  292. });
  293. // 0, 1, 2, 6, 5, 4, 3, 7, 8, 9
  294. let right = [0, 5, 9, 6, 4];
  295. let right = rsx!({
  296. right.iter().map(|f| {
  297. rsx! { div { key: "{f}" "{f}" }}
  298. })
  299. });
  300. // LIS: 3, 7, 8,
  301. let edits = dom.lazy_diff(left, right);
  302. dbg!(&edits);
  303. }