parsing.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. use dioxus_rsx::{hot_reload::Empty, CallBody};
  2. use quote::ToTokens;
  3. use dioxus_rsx::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. dbg!(cb.ifmt_idx.get());
  40. let _template = cb.body.to_template::<Empty>();
  41. }
  42. #[test]
  43. fn simple_case() {
  44. let item = quote::quote! {
  45. div {
  46. something: "cool",
  47. id: "Some cool attribute {cool}",
  48. class: "Some cool attribute {cool2}",
  49. "hi!"
  50. {some_expr}
  51. Component {
  52. boolish: true,
  53. otherish: 123,
  54. otherish2: 123.0,
  55. otherish3: "dang!",
  56. otherish3: "dang! {cool}",
  57. }
  58. }
  59. };
  60. let cb: CallBody = syn::parse2(item).unwrap();
  61. println!("{}", cb.to_token_stream().pretty_unparse());
  62. }
  63. #[test]
  64. fn complex_kitchen_sink() {
  65. let item = quote::quote! {
  66. // complex_carry
  67. button {
  68. class: "flex items-center pl-3 py-3 pr-2 text-gray-500 hover:bg-indigo-50 rounded",
  69. onclick: move |evt| {
  70. show_user_menu.set(!show_user_menu.get());
  71. evt.cancel_bubble();
  72. },
  73. onmousedown: move |evt| show_user_menu.set(!show_user_menu.get()),
  74. span { class: "inline-block mr-4", icons::icon_14 {} }
  75. span { "Settings" }
  76. }
  77. // Complex nesting with handlers
  78. li {
  79. Link {
  80. class: "flex items-center pl-3 py-3 pr-4 {active_class} rounded",
  81. to: "{to}",
  82. span { class: "inline-block mr-3", icons::icon_0 {} }
  83. span { "{name}" }
  84. {children.is_some().then(|| rsx! {
  85. span {
  86. class: "inline-block ml-auto hover:bg-gray-500",
  87. onclick: move |evt| {
  88. // open.set(!open.get());
  89. evt.cancel_bubble();
  90. },
  91. icons::icon_8 {}
  92. }
  93. })}
  94. }
  95. div { class: "px-4", {is_current.then(|| rsx!{ children })} }
  96. }
  97. // No nesting
  98. Component {
  99. adsasd: "asd",
  100. onclick: move |_| {
  101. let blah = 120;
  102. }
  103. }
  104. // Component path
  105. my::thing::Component {
  106. adsasd: "asd",
  107. onclick: move |_| {
  108. let blah = 120;
  109. }
  110. }
  111. for i in 0..10 {
  112. Component { key: "{i}", blah: 120 }
  113. }
  114. for i in 0..10 {
  115. Component { key: "{i}" }
  116. }
  117. for i in 0..10 {
  118. div { key: "{i}", blah: 120 }
  119. }
  120. for i in 0..10 {
  121. div { key: "{i}" }
  122. }
  123. div {
  124. "asdbascasdbasd"
  125. "asbdasbdabsd"
  126. {asbdabsdbasdbas}
  127. }
  128. };
  129. let _cb: CallBody = syn::parse2(item).unwrap();
  130. }