rsx.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #[test]
  2. fn rsx() {
  3. let t = trybuild::TestCases::new();
  4. t.compile_fail("tests/rsx/trailing-comma-0.rs");
  5. }
  6. /// This test ensures that automatic `into` conversion occurs for default values.
  7. ///
  8. /// These are compile-time tests.
  9. /// See https://github.com/DioxusLabs/dioxus/issues/2373
  10. #[cfg(test)]
  11. mod test_default_into {
  12. use dioxus::prelude::*;
  13. #[derive(Props, Clone, PartialEq)]
  14. struct MyCoolProps {
  15. // Test different into configurations
  16. #[props(into, default = true)]
  17. pub val_into_w_default_val: u16,
  18. #[props(into, default)]
  19. pub val_into_w_default: u16,
  20. #[props(default = true.into())]
  21. pub val_default: u16,
  22. // Test different into configurations with strings
  23. #[props(into, default = "abc")]
  24. pub str_into_w_default_val: String,
  25. #[props(into, default)]
  26. pub str_into_w_default: String,
  27. #[props(default = "abc".into())]
  28. pub str_default: String,
  29. // Test options
  30. #[props(into, default = Some("abc"))]
  31. pub opt_into_w_default_val: Option<String>,
  32. #[props(into, default)]
  33. pub opt_into_w_default: Option<String>,
  34. #[props(default = Some("abc"))]
  35. pub opt_default: Option<String>,
  36. // Test no default
  37. #[props(into)]
  38. pub some_data: bool,
  39. pub some_other_data: bool,
  40. }
  41. }
  42. /// This test ensures that read-only signals that contain an option (`Signal<Option<u16>>`)
  43. /// are correctly created as default when not provided.
  44. ///
  45. /// These are compile-time tests.
  46. /// See https://github.com/DioxusLabs/dioxus/issues/2648
  47. #[cfg(test)]
  48. #[allow(unused)]
  49. mod test_optional_signals {
  50. use dioxus::prelude::*;
  51. // Test if test components fail to compile.
  52. #[component]
  53. fn UsesComponents() -> Element {
  54. rsx! {
  55. PropsStruct {
  56. regular_read_signal: ReadOnlySignal::new(Signal::new(1234)),
  57. }
  58. PropsStruct {
  59. optional_read_signal: 1234,
  60. regular_read_signal: 123u16,
  61. }
  62. PropParams {}
  63. PropParams {
  64. opt_read_sig: 1234
  65. }
  66. DoubleOption {}
  67. DoubleOption { optional: Some(1234) }
  68. }
  69. }
  70. // Test props as struct param.
  71. #[derive(Props, Clone, PartialEq)]
  72. struct MyTestProps {
  73. pub optional_read_signal: ReadOnlySignal<Option<u16>>,
  74. pub regular_read_signal: ReadOnlySignal<u16>,
  75. }
  76. #[component]
  77. fn PropsStruct(props: MyTestProps) -> Element {
  78. rsx! { "hi" }
  79. }
  80. // Test props as params.
  81. #[component]
  82. fn PropParams(opt_read_sig: ReadOnlySignal<Option<u16>>) -> Element {
  83. rsx! { "hi!" }
  84. }
  85. #[component]
  86. fn DoubleOption(optional: Option<Option<u16>>) -> Element {
  87. rsx! { "hi!" }
  88. }
  89. }