Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillsemyonkin committed Nov 5, 2023
1 parent 055ef6e commit 2e85731
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
67 changes: 67 additions & 0 deletions packages/yew-macro/tests/html_macro/dyn-prop-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

#[derive(::yew::Properties, ::std::cmp::PartialEq)]
pub struct SimpleProps {
pub test: ::std::string::String,
}

pub struct Simple;
impl ::yew::Component for Simple {
type Message = ();
type Properties = SimpleProps;

fn create(_ctx: &::yew::Context<Self>) -> Self {
::std::unimplemented!()
}

fn view(&self, _ctx: &::yew::Context<Self>) -> ::yew::Html {
::std::unimplemented!()
}
}

pub struct Fail;

fn main() {
_ = ::yew::html! { <span { Fail }={ "" } /> };

let dyn_prop = || Fail;
_ = ::yew::html! { <span { dyn_prop() }={ "" } /> };

_ = ::yew::html! { <Simple { "test" }={ "" } /> }
}
33 changes: 33 additions & 0 deletions packages/yew-macro/tests/html_macro/dyn-prop-fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
error: expected a valid Rust identifier
--> tests/html_macro/dyn-prop-fail.rs:66:34
|
66 | _ = ::yew::html! { <Simple { "test" }={ "" } /> }
| ^^^^^^

error[E0277]: the trait bound `implicit_clone::unsync::IString: From<Fail>` is not satisfied
--> tests/html_macro/dyn-prop-fail.rs:61:9
|
61 | _ = ::yew::html! { <span { Fail }={ "" } /> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Fail>` is not implemented for `implicit_clone::unsync::IString`
|
= help: the following other types implement trait `From<T>`:
<implicit_clone::unsync::IString as From<&'static str>>
<implicit_clone::unsync::IString as From<Cow<'static, str>>>
<implicit_clone::unsync::IString as From<Rc<str>>>
<implicit_clone::unsync::IString as From<String>>
= note: required because of the requirements on the impl of `Into<implicit_clone::unsync::IString>` for `Fail`
= note: this error originates in the macro `::yew::html` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `implicit_clone::unsync::IString: From<Fail>` is not satisfied
--> tests/html_macro/dyn-prop-fail.rs:64:9
|
64 | _ = ::yew::html! { <span { dyn_prop() }={ "" } /> };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Fail>` is not implemented for `implicit_clone::unsync::IString`
|
= help: the following other types implement trait `From<T>`:
<implicit_clone::unsync::IString as From<&'static str>>
<implicit_clone::unsync::IString as From<Cow<'static, str>>>
<implicit_clone::unsync::IString as From<Rc<str>>>
<implicit_clone::unsync::IString as From<String>>
= note: required because of the requirements on the impl of `Into<implicit_clone::unsync::IString>` for `Fail`
= note: this error originates in the macro `::yew::html` (in Nightly builds, run with -Z macro-backtrace for more info)
53 changes: 53 additions & 0 deletions packages/yew-macro/tests/html_macro/dyn-prop-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![no_implicit_prelude]

// Shadow primitives
#[allow(non_camel_case_types)]
pub struct bool;
#[allow(non_camel_case_types)]
pub struct char;
#[allow(non_camel_case_types)]
pub struct f32;
#[allow(non_camel_case_types)]
pub struct f64;
#[allow(non_camel_case_types)]
pub struct i128;
#[allow(non_camel_case_types)]
pub struct i16;
#[allow(non_camel_case_types)]
pub struct i32;
#[allow(non_camel_case_types)]
pub struct i64;
#[allow(non_camel_case_types)]
pub struct i8;
#[allow(non_camel_case_types)]
pub struct isize;
#[allow(non_camel_case_types)]
pub struct str;
#[allow(non_camel_case_types)]
pub struct u128;
#[allow(non_camel_case_types)]
pub struct u16;
#[allow(non_camel_case_types)]
pub struct u32;
#[allow(non_camel_case_types)]
pub struct u64;
#[allow(non_camel_case_types)]
pub struct u8;
#[allow(non_camel_case_types)]
pub struct usize;

fn main() {
// basic example from https://htmx.org/attributes/hx-on/

// repeating attrs is not valid HTMX nor HTML,
// but valid html! (any checks can happen only during runtime)

// literal
_ = ::yew::html! { <span { "hx-on:click" }={ "alert('Clicked!')" } /> };
_ = ::yew::html! { <span { "hx-on:click" }={ "alert('Clicked!')" } { "hx-on:click" }={ "alert('Clicked!')" } /> };

// expr
let dyn_prop = || ::std::string::ToString::to_string("hx-on:click");
_ = ::yew::html! { <span { dyn_prop() }={ "alert('Clicked!')" } /> };
_ = ::yew::html! { <span { dyn_prop() }={ "alert('Clicked!')" } { dyn_prop() }={ "alert('Clicked!')" } /> };
}

0 comments on commit 2e85731

Please sign in to comment.