sink.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 mut 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 {
  25. h1 {"thing"}
  26. }
  27. }
  28. }
  29. "#
  30. .to_string();
  31. let formatted = fmt_file(&src);
  32. let block = formatted.into_iter().next().unwrap();
  33. src.replace_range(
  34. block.start - 1..block.end + 1,
  35. &format!("{{ {} }}", &block.formatted),
  36. );
  37. }
  38. #[test]
  39. fn formats_multiple_blocks() {
  40. let mut src = r#"
  41. #[inline_props]
  42. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  43. const ICON_SIZE: u32 = 36;
  44. rsx! {
  45. div {
  46. h1 {"thing"}
  47. }
  48. }
  49. rsx! {
  50. div {
  51. Ball {
  52. a: rsx!{
  53. "asdasd"
  54. }
  55. }
  56. }
  57. }
  58. }
  59. #[inline_props]
  60. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  61. const ICON_SIZE: u32 = 36;
  62. rsx! {
  63. div {
  64. h1 {"thing"}
  65. }
  66. }
  67. rsx! {
  68. div {
  69. Ball {
  70. a: rsx!{
  71. "asdasd"
  72. }
  73. }
  74. }
  75. }
  76. }
  77. "#
  78. .to_string();
  79. let formatted = fmt_file(&src);
  80. dbg!(&formatted);
  81. let block = formatted.into_iter().next().unwrap();
  82. src.replace_range(
  83. block.start - 1..block.end + 1,
  84. &format!("{{ {} }}", &block.formatted),
  85. );
  86. }
  87. #[test]
  88. fn empty_blocks() {
  89. let src = r###"
  90. pub fn Alert(cx: Scope) -> Element {
  91. cx.render(rsx! {
  92. div { }
  93. })
  94. }
  95. "###
  96. .to_string();
  97. let formatted = fmt_file(&src);
  98. dbg!(&formatted);
  99. }