sink.rs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. use dioxus_autofmt::*;
  2. use proc_macro2::TokenStream as TokenStream2;
  3. use syn::{Attribute, Meta};
  4. #[test]
  5. fn formats_block() {
  6. let block = r#"
  7. div {
  8. div {
  9. class: "asd",
  10. class: "asd",class: "asd",class: "asd",class: "asd",class: "asd",
  11. key: "ddd",
  12. onclick: move |_| {
  13. let blah = 120;
  14. true
  15. },
  16. blah: 123,
  17. onclick: move |_| {
  18. let blah = 120;
  19. true
  20. },
  21. onclick: move |_| {
  22. let blah = 120;
  23. true
  24. },
  25. onclick: move |_| {
  26. let blah = 120;
  27. true
  28. },
  29. div {
  30. div {
  31. "hi"
  32. }
  33. h2 {
  34. class: "asd",
  35. }
  36. }
  37. }
  38. }
  39. "#;
  40. let formatted = fmt_block(block).unwrap();
  41. print!("{formatted}");
  42. }
  43. #[test]
  44. fn parse_comment() {
  45. let block = r#"
  46. div {
  47. adsasd: "asd", // this is a comment
  48. }
  49. "#;
  50. let parsed: TokenStream2 = syn::parse_str(block).unwrap();
  51. dbg!(parsed);
  52. }
  53. #[test]
  54. fn formats_component() {
  55. let block = r#"
  56. Component {
  57. adsasd: "asd", // this is a comment
  58. onclick: move |_| {
  59. let blah = 120;
  60. let blah = 120;
  61. },
  62. }
  63. "#;
  64. let formatted = fmt_block(block).unwrap();
  65. print!("{formatted}");
  66. }
  67. #[test]
  68. fn formats_element() {
  69. let block = r#"
  70. div {
  71. a: "1234567891012345678910123456789101234567891012345678910123456789101234567891012345678910123456789101234567891012345678910",
  72. a: "123",
  73. a: "123",
  74. a: "123",
  75. a: "123",
  76. a: "123",
  77. a: "123",
  78. a: "123",
  79. a: "123",
  80. }
  81. "#;
  82. let formatted = fmt_block(block).unwrap();
  83. print!("{formatted}");
  84. }
  85. #[test]
  86. fn formats_element_short() {
  87. let block = r#"
  88. div {
  89. a: "123",
  90. a: "123",
  91. a: "123",
  92. a: "123",
  93. a: "123",
  94. a: "123",
  95. a: "123",
  96. a: "123",
  97. a: "123",
  98. }
  99. "#;
  100. let formatted = fmt_block(block).unwrap();
  101. print!("{formatted}");
  102. }
  103. #[test]
  104. fn formats_element_nested() {
  105. let block = r#"
  106. h3 {
  107. class: "mb-2 text-xl font-bold",
  108. "Invite Member"
  109. }
  110. "#;
  111. let formatted = fmt_block(block).unwrap();
  112. print!("{formatted}");
  113. }
  114. #[test]
  115. fn formats_element_nested_no_trailing_tabs() {
  116. let block = r#"
  117. img { class: "mb-6 mx-auto h-24", src: "artemis-assets/images/friends.png", alt: "", }
  118. "#;
  119. let formatted = fmt_block(block).unwrap();
  120. print!("{formatted}");
  121. }
  122. #[test]
  123. fn formats_element_with_correct_indent() {
  124. let block = r###"
  125. div {
  126. a { class: "py-2 px-3 bg-indigo-500 hover:bg-indigo-600 rounded text-xs text-white", href: "#",
  127. "Send invitation"
  128. }
  129. }
  130. "###;
  131. let formatted = fmt_block(block).unwrap();
  132. print!("{formatted}");
  133. }
  134. #[test]
  135. fn small_elements_and_text_are_small() {
  136. let block = r###"
  137. a { class: " text-white",
  138. "Send invitation"
  139. }
  140. "###;
  141. let formatted = fmt_block(block).unwrap();
  142. print!("{formatted}");
  143. }
  144. #[test]
  145. fn formats_component_man_props() {
  146. let block = r#"
  147. Component {
  148. ..MyProps {
  149. val: 123
  150. },
  151. adsasd: "asd", // this is a comment
  152. onclick: move |_| {
  153. let blah = 120;
  154. },
  155. }
  156. "#;
  157. let formatted = fmt_block(block).unwrap();
  158. print!("{formatted}");
  159. }
  160. #[test]
  161. fn formats_component_tiny() {
  162. let block = r#"
  163. Component { a: 123
  164. }
  165. "#;
  166. let formatted = fmt_block(block).unwrap();
  167. print!("{formatted}");
  168. }
  169. #[test]
  170. fn formats_document() {
  171. let block = r#"
  172. rsx!{
  173. Component {
  174. adsasd: "asd", // this is a comment
  175. onclick: move |_| {
  176. let blah = 120;
  177. },
  178. }
  179. }
  180. "#;
  181. let formatted = get_format_blocks(block);
  182. print!("{formatted:?}");
  183. }
  184. #[test]
  185. fn component_path_mod_style() {
  186. let block = r#"
  187. rsx!{
  188. my::thing::Component {
  189. adsasd: "asd", // this is a comment
  190. onclick: move |_| {
  191. let blah = 120;
  192. },
  193. }
  194. }
  195. "#;
  196. let formatted = get_format_blocks(block);
  197. print!("{formatted:?}");
  198. }
  199. #[test]
  200. fn formats_valid_rust_src() {
  201. let src = r#"
  202. //
  203. rsx! {
  204. div {}
  205. div {
  206. h3 {"asd"
  207. }
  208. }
  209. }
  210. "#;
  211. let formatted = get_format_blocks(src);
  212. println!("{formatted:?}");
  213. }
  214. #[test]
  215. fn formats_valid_rust_src_with_indents() {
  216. let mut src = r#"
  217. #[inline_props]
  218. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  219. const ICON_SIZE: u32 = 36;
  220. rsx! {
  221. div {
  222. h1 {"thing"}
  223. }
  224. }
  225. }
  226. "#
  227. .to_string();
  228. let formatted = get_format_blocks(&src);
  229. let block = formatted.into_iter().next().unwrap();
  230. src.replace_range(
  231. block.start - 1..block.end + 1,
  232. &format!("{{ {} }}", &block.formatted),
  233. );
  234. }
  235. #[test]
  236. fn formats_multiple_blocks() {
  237. let mut src = r#"
  238. #[inline_props]
  239. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  240. const ICON_SIZE: u32 = 36;
  241. rsx! {
  242. div {
  243. h1 {"thing"}
  244. }
  245. }
  246. rsx! {
  247. div {
  248. Ball {
  249. a: rsx!{
  250. "asdasd"
  251. }
  252. }
  253. }
  254. }
  255. }
  256. #[inline_props]
  257. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  258. const ICON_SIZE: u32 = 36;
  259. rsx! {
  260. div {
  261. h1 {"thing"}
  262. }
  263. }
  264. rsx! {
  265. div {
  266. Ball {
  267. a: rsx!{
  268. "asdasd"
  269. }
  270. }
  271. }
  272. }
  273. }
  274. "#
  275. .to_string();
  276. let formatted = get_format_blocks(&src);
  277. dbg!(&formatted);
  278. let block = formatted.into_iter().next().unwrap();
  279. src.replace_range(
  280. block.start - 1..block.end + 1,
  281. &format!("{{ {} }}", &block.formatted),
  282. );
  283. }