parsing.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. use dioxus_rsx::CallBody;
  2. use quote::ToTokens;
  3. use prettier_please::PrettyUnparse;
  4. #[test]
  5. fn callbody_ctx() {
  6. let item = quote::quote! {
  7. div {
  8. h1 {
  9. id: "Some cool attribute {cool}"
  10. }
  11. for item in items {
  12. "Something {cool}"
  13. }
  14. Component {
  15. "Something {elseish}"
  16. }
  17. Component2 {
  18. "Something {Body}"
  19. Component3 {
  20. prop: "Something {Body3}",
  21. "Something {Body4}"
  22. }
  23. }
  24. something-cool {
  25. "Something {cool}ish"
  26. }
  27. if true {
  28. div {
  29. "hi! {cool}"
  30. }
  31. }
  32. "hi!"
  33. "goodbye!"
  34. }
  35. {some_expr}
  36. };
  37. let cb: CallBody = syn::parse2(item).unwrap();
  38. dbg!(cb.template_idx.get());
  39. }
  40. #[test]
  41. fn simple_case() {
  42. let item = quote::quote! {
  43. div {
  44. something: "cool",
  45. id: "Some cool attribute {cool}",
  46. class: "Some cool attribute {cool2}",
  47. "hi!"
  48. {some_expr}
  49. Component {
  50. boolish: true,
  51. otherish: 123,
  52. otherish2: 123.0,
  53. otherish3: "dang!",
  54. otherish3: "dang! {cool}",
  55. }
  56. }
  57. };
  58. let cb: CallBody = syn::parse2(item).unwrap();
  59. println!("{}", cb.to_token_stream().pretty_unparse());
  60. }
  61. #[test]
  62. fn complex_kitchen_sink() {
  63. let item = quote::quote! {
  64. // complex_carry
  65. button {
  66. class: "flex items-center pl-3 py-3 pr-2 text-gray-500 hover:bg-indigo-50 rounded",
  67. onclick: move |evt| {
  68. show_user_menu.set(!show_user_menu.get());
  69. evt.cancel_bubble();
  70. },
  71. onmousedown: move |evt| show_user_menu.set(!show_user_menu.get()),
  72. span { class: "inline-block mr-4", icons::icon_14 {} }
  73. span { "Settings" }
  74. }
  75. // Complex nesting with handlers
  76. li {
  77. Link {
  78. class: "flex items-center pl-3 py-3 pr-4 {active_class} rounded",
  79. to: "{to}",
  80. span { class: "inline-block mr-3", icons::icon_0 {} }
  81. span { "{name}" }
  82. {children.is_some().then(|| rsx! {
  83. span {
  84. class: "inline-block ml-auto hover:bg-gray-500",
  85. onclick: move |evt| {
  86. // open.set(!open.get());
  87. evt.cancel_bubble();
  88. },
  89. icons::icon_8 {}
  90. }
  91. })}
  92. }
  93. div { class: "px-4", {is_current.then(|| rsx!{ children })} }
  94. }
  95. // No nesting
  96. Component {
  97. adsasd: "asd",
  98. onclick: move |_| {
  99. let blah = 120;
  100. }
  101. }
  102. // Component path
  103. my::thing::Component {
  104. adsasd: "asd",
  105. onclick: move |_| {
  106. let blah = 120;
  107. }
  108. }
  109. for i in 0..10 {
  110. Component { key: "{i}", blah: 120 }
  111. }
  112. for i in 0..10 {
  113. Component { key: "{i}" }
  114. }
  115. for i in 0..10 {
  116. div { key: "{i}", blah: 120 }
  117. }
  118. for i in 0..10 {
  119. div { key: "{i}" }
  120. }
  121. div {
  122. "asdbascasdbasd"
  123. "asbdasbdabsd"
  124. {asbdabsdbasdbas}
  125. }
  126. };
  127. let _cb: CallBody = syn::parse2(item).unwrap();
  128. }