sink.rs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_exprs() {
  171. let block = r#"
  172. ul {
  173. (0..10).map(|f| rsx!{
  174. li {
  175. "hi"
  176. }
  177. })
  178. }
  179. "#;
  180. let formatted = fmt_block(block).unwrap();
  181. print!("{formatted}");
  182. }
  183. #[test]
  184. fn formats_exprs_neg_indent() {
  185. let block = r#"
  186. ul {
  187. (0..10).map(|f| rsx!{
  188. li {
  189. "hi"
  190. }
  191. })
  192. }
  193. "#;
  194. let formatted = fmt_block(block).unwrap();
  195. print!("{formatted}");
  196. }
  197. #[test]
  198. fn formats_exprs_handlers() {
  199. let block = r#"
  200. button {
  201. class: "flex items-center pl-3 py-3 pr-2 text-gray-500 hover:bg-indigo-50 rounded",
  202. onclick: move |evt| {
  203. show_user_menu.set(!show_user_menu.get()); evt.cancel_bubble(); },
  204. onclick: move |evt|
  205. show_user_menu.set(!show_user_menu.get()),
  206. span { class: "inline-block mr-4",
  207. icons::icon_14 {}
  208. }
  209. span { "Settings" }
  210. }
  211. "#;
  212. let formatted = fmt_block(block).unwrap();
  213. print!("{formatted}");
  214. }
  215. #[test]
  216. fn formats_complex() {
  217. let block = r#"
  218. li {
  219. Link {
  220. class: "flex items-center pl-3 py-3 pr-4 {active_class} rounded",
  221. to: "{to}",
  222. span { class: "inline-block mr-3",
  223. icons::icon_0 {}
  224. }
  225. span { "{name}" }
  226. children.is_some().then(|| rsx!{
  227. span {
  228. class: "inline-block ml-auto hover:bg-gray-500",
  229. onclick: move |evt| {
  230. // open.set(!open.get());
  231. evt.cancel_bubble();
  232. },
  233. icons::icon_8 {}
  234. }
  235. })
  236. }
  237. div {
  238. class: "px-4",
  239. is_current.then(|| rsx!{ children })
  240. // open.then(|| rsx!{ children })
  241. }
  242. }
  243. "#;
  244. let formatted = fmt_block(block).unwrap();
  245. print!("{formatted}");
  246. }
  247. #[test]
  248. fn formats_document() {
  249. let block = r#"
  250. rsx!{
  251. Component {
  252. adsasd: "asd", // this is a comment
  253. onclick: move |_| {
  254. let blah = 120;
  255. },
  256. }
  257. }
  258. "#;
  259. let formatted = get_format_blocks(block);
  260. print!("{formatted:?}");
  261. }
  262. #[test]
  263. fn component_path_mod_style() {
  264. let block = r#"
  265. rsx!{
  266. my::thing::Component {
  267. adsasd: "asd", // this is a comment
  268. onclick: move |_| {
  269. let blah = 120;
  270. },
  271. }
  272. }
  273. "#;
  274. let formatted = get_format_blocks(block);
  275. print!("{formatted:?}");
  276. }
  277. #[test]
  278. fn formats_valid_rust_src() {
  279. let src = r#"
  280. //
  281. rsx! {
  282. div {}
  283. div {
  284. h3 {"asd"
  285. }
  286. }
  287. }
  288. "#;
  289. let formatted = get_format_blocks(src);
  290. println!("{formatted:?}");
  291. }
  292. #[test]
  293. fn formats_valid_rust_src_with_indents() {
  294. let mut src = r#"
  295. #[inline_props]
  296. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  297. const ICON_SIZE: u32 = 36;
  298. rsx! {
  299. div {
  300. h1 {"thing"}
  301. }
  302. }
  303. }
  304. "#
  305. .to_string();
  306. let formatted = get_format_blocks(&src);
  307. let block = formatted.into_iter().next().unwrap();
  308. src.replace_range(
  309. block.start - 1..block.end + 1,
  310. &format!("{{ {} }}", &block.formatted),
  311. );
  312. }
  313. #[test]
  314. fn formats_multiple_blocks() {
  315. let mut src = r#"
  316. #[inline_props]
  317. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  318. const ICON_SIZE: u32 = 36;
  319. rsx! {
  320. div {
  321. h1 {"thing"}
  322. }
  323. }
  324. rsx! {
  325. div {
  326. Ball {
  327. a: rsx!{
  328. "asdasd"
  329. }
  330. }
  331. }
  332. }
  333. }
  334. #[inline_props]
  335. fn NavItem<'a>(cx: Scope, to: &'static str, children: Element<'a>, icon: Shape) -> Element {
  336. const ICON_SIZE: u32 = 36;
  337. rsx! {
  338. div {
  339. h1 {"thing"}
  340. }
  341. }
  342. rsx! {
  343. div {
  344. Ball {
  345. a: rsx!{
  346. "asdasd"
  347. }
  348. }
  349. }
  350. }
  351. }
  352. "#
  353. .to_string();
  354. let formatted = get_format_blocks(&src);
  355. dbg!(&formatted);
  356. let block = formatted.into_iter().next().unwrap();
  357. src.replace_range(
  358. block.start - 1..block.end + 1,
  359. &format!("{{ {} }}", &block.formatted),
  360. );
  361. }
  362. #[test]
  363. fn empty_blocks() {
  364. let mut src = r###"
  365. pub fn Alert(cx: Scope) -> Element {
  366. cx.render(rsx! {
  367. div { }
  368. })
  369. }
  370. "###
  371. .to_string();
  372. let formatted = get_format_blocks(&src);
  373. dbg!(&formatted);
  374. }