sink.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use dioxus_autofmt::*;
  2. #[test]
  3. fn formats_valid_rust_src() {
  4. let src = r#"
  5. //
  6. rsx! {
  7. div {}
  8. div {
  9. h3 {"asd"
  10. }
  11. }
  12. }
  13. "#;
  14. let formatted = fmt_file(src);
  15. println!("{formatted:#?}");
  16. }
  17. #[test]
  18. fn formats_valid_rust_src_with_indents() {
  19. let src = r#"
  20. #[inline_props]
  21. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  22. const ICON_SIZE: u32 = 36;
  23. rsx! {
  24. div { h1 { "thing" } }
  25. }
  26. }
  27. "#
  28. .to_string();
  29. let formatted = fmt_file(&src);
  30. assert!(formatted.is_empty());
  31. }
  32. #[test]
  33. fn formats_multiple_blocks() {
  34. let mut src = r#"
  35. #[inline_props]
  36. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  37. const ICON_SIZE: u32 = 36;
  38. rsx! {
  39. div { h1 { "thing" } }
  40. }
  41. rsx! {
  42. div {
  43. Ball {
  44. a: rsx! {
  45. "asdasd"
  46. }
  47. }
  48. }
  49. }
  50. }
  51. #[inline_props]
  52. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  53. const ICON_SIZE: u32 = 36;
  54. rsx! {
  55. div { h1 { "thing" } }
  56. }
  57. rsx! {
  58. div {
  59. Ball {
  60. a: rsx! {
  61. "asdasd"
  62. }
  63. }
  64. }
  65. }
  66. }
  67. "#
  68. .to_string();
  69. let formatted = fmt_file(&src);
  70. dbg!(&formatted);
  71. let block = formatted.into_iter().next().unwrap();
  72. src.replace_range(
  73. block.start - 1..block.end + 1,
  74. &format!("{{ {} }}", &block.formatted),
  75. );
  76. }
  77. #[test]
  78. fn empty_blocks() {
  79. let src = r###"
  80. pub fn Alert(cx: Scope) -> Element {
  81. cx.render(rsx! {
  82. div {}
  83. })
  84. }
  85. "###
  86. .to_string();
  87. let formatted = fmt_file(&src);
  88. dbg!(&formatted);
  89. }