diffing.rs 8.9 KB

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