Skip to content

Commit

Permalink
move #[function_component(_)] tests
Browse files Browse the repository at this point in the history
missed those before
  • Loading branch information
ranile committed Jul 18, 2021
1 parent 0900198 commit 942bf74
Show file tree
Hide file tree
Showing 32 changed files with 542 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/yew-macro/tests/function_attr_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[allow(dead_code)]
#[rustversion::attr(stable(1.51), test)]
fn tests() {
let t = trybuild::TestCases::new();
t.pass("tests/function_attr/*-pass.rs");
t.compile_fail("tests/function_attr/*-fail.rs");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
struct Test;

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `function_component` attribute can only be applied to functions
--> $DIR/applied-to-non-fn-fail.rs:10:1
|
10 | struct Test;
| ^^^^^^^^^^^^
19 changes: 19 additions & 0 deletions packages/yew-macro/tests/function_component_attr/async-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use yew::prelude::*;
use yew_functional::{function_component};

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
async fn comp(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}


fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: function components can't be async
--> $DIR/async-fail.rs:10:1
|
10 | async fn comp(props: &Props) -> Html {
| ^^^^^
45 changes: 45 additions & 0 deletions packages/yew-macro/tests/function_component_attr/bad-name-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(let)]
fn comp(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

#[function_component(x, y, z)]
fn comp_2(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

#[function_component(124)]
fn comp_3(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

#[function_component(component)]
fn component(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: expected identifier
--> $DIR/bad-name-fail.rs:9:22
|
9 | #[function_component(let)]
| ^^^

error: unexpected token
--> $DIR/bad-name-fail.rs:18:23
|
18 | #[function_component(x, y, z)]
| ^

error: expected identifier
--> $DIR/bad-name-fail.rs:27:22
|
27 | #[function_component(124)]
| ^^^

error: the component must not have the same name as the function
--> $DIR/bad-name-fail.rs:36:22
|
36 | #[function_component(component)]
| ^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
fn comp(props: Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: expected a reference to a `Properties` type (try: `&Props`)
--> $DIR/bad-props-param-fail.rs:10:16
|
10 | fn comp(props: Props) -> Html {
| ^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp1)]
fn comp_1(_props: &Props) {}

#[function_component(Comp)]
fn comp(_props: &Props) -> u32 {
1
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error: function components must return `yew::Html`
--> $DIR/bad-return-type-fail.rs:10:1
|
10 | fn comp_1(_props: &Props) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> $DIR/bad-return-type-fail.rs:14:5
|
13 | fn comp(_props: &Props) -> u32 {
| --- expected `VNode` because of return type
14 | 1
| ^ expected enum `VNode`, found integer
18 changes: 18 additions & 0 deletions packages/yew-macro/tests/function_component_attr/const-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
const fn comp(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: const functions can't be function components
--> $DIR/const-fail.rs:10:1
|
10 | const fn comp(props: &Props) -> Html {
| ^^^^^
18 changes: 18 additions & 0 deletions packages/yew-macro/tests/function_component_attr/extern-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
extern "C" fn comp(props: &Props) -> Html {
html! {
<p>
{ props.a }
</p>
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: extern functions can't be function components
--> $DIR/extern-fail.rs:10:1
|
10 | extern "C" fn comp(props: &Props) -> Html {
| ^^^^^^^^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
fn comp<'a>(props: &'a Props) -> Html {

html! {
<p>
{ props.a }
</p>
}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: function components can't have generic lifetime parameters
--> $DIR/generic-lifetime-fail.rs:10:8
|
10 | fn comp<'a>(props: &'a Props) -> Html {
| ^^^^
39 changes: 39 additions & 0 deletions packages/yew-macro/tests/function_component_attr/generic-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#[derive(Clone, ::yew::Properties, PartialEq)]
struct Props {
a: usize,
}

#[::yew_functional::function_component(Comp)]
fn comp<P>(_props: &P) -> ::yew::Html
where
P: ::yew::Properties + PartialEq,
{
::yew::html! {
<p></p>
}
}

#[::yew_functional::function_component(Comp1)]
fn comp1<T1, T2>(_props: &()) -> ::yew::Html {
::yew::html! {
<p></p>
}
}

#[::yew_functional::function_component(ConstGenerics)]
fn const_generics<const N: i32>() -> ::yew::Html {
::yew::html! {
<div>
{ N }
</div>
}
}

fn compile_pass() {
::yew::html! { <Comp<Props> a=10 /> };
::yew::html! { <Comp1<usize, usize> /> };

::yew::html! { <ConstGenerics<10> /> };
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use yew::prelude::*;
use yew_functional::function_component;

#[derive(Clone, Properties, PartialEq)]
struct Props {
a: usize,
}

#[function_component(Comp)]
fn comp<P>(_props: &P) -> Html
where
P: Properties + PartialEq,
{
html! {
<p></p>
}
}

struct MissingTypeBounds;

fn compile_fail() {
// missing prop 'a'
html! { <Comp<Props> /> };

// invalid type parameter
html! { <Comp<INVALID> /> };
// parameter doesn't match bounds
html! { <Comp<MissingTypeBounds> /> };

// missing type param
html! { <Comp /> };
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
error[E0412]: cannot find type `INVALID` in this scope
--> $DIR/generic-props-fail.rs:26:19
|
21 | fn compile_fail() {
| - help: you might be missing a type parameter: `<INVALID>`
...
26 | html! { <Comp<INVALID> /> };
| ^^^^^^^ not found in this scope

error[E0599]: no method named `build` found for struct `PropsBuilder<PropsBuilderStep_missing_required_prop_a>` in the current scope
--> $DIR/generic-props-fail.rs:23:14
|
4 | #[derive(Clone, Properties, PartialEq)]
| ---------- method `build` not found for this
...
23 | html! { <Comp<Props> /> };
| ^^^^ method not found in `PropsBuilder<PropsBuilderStep_missing_required_prop_a>`

error[E0599]: the function or associated item `new` exists for struct `VChild<FunctionComponent<comp<MissingTypeBounds>>>`, but its trait bounds were not satisfied
--> $DIR/generic-props-fail.rs:28:14
|
28 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ function or associated item cannot be called on `VChild<FunctionComponent<comp<MissingTypeBounds>>>` due to unsatisfied trait bounds
|
::: $WORKSPACE/packages/yew-functional/src/lib.rs:73:1
|
73 | pub struct FunctionComponent<T: FunctionProvider + 'static> {
| ----------------------------------------------------------- doesn't satisfy `_: yew::Component`
|
= note: the following trait bounds were not satisfied:
`FunctionComponent<comp<MissingTypeBounds>>: yew::Component`

error[E0277]: the trait bound `MissingTypeBounds: yew::Properties` is not satisfied
--> $DIR/generic-props-fail.rs:28:14
|
28 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ the trait `yew::Properties` is not implemented for `MissingTypeBounds`
|
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`

error[E0277]: can't compare `MissingTypeBounds` with `MissingTypeBounds`
--> $DIR/generic-props-fail.rs:28:14
|
28 | html! { <Comp<MissingTypeBounds> /> };
| ^^^^ no implementation for `MissingTypeBounds == MissingTypeBounds`
|
= help: the trait `PartialEq` is not implemented for `MissingTypeBounds`
= note: required because of the requirements on the impl of `FunctionProvider` for `comp<MissingTypeBounds>`

error[E0107]: missing generics for type alias `Comp`
--> $DIR/generic-props-fail.rs:31:14
|
31 | html! { <Comp /> };
| ^^^^ expected 1 type argument
|
note: type alias defined here, with 1 type parameter: `P`
--> $DIR/generic-props-fail.rs:9:22
|
9 | #[function_component(Comp)]
| ^^^^
10 | fn comp<P>(_props: &P) -> Html
| -
help: use angle brackets to add missing type argument
|
31 | html! { <Comp<P> /> };
| ^^^
Loading

0 comments on commit 942bf74

Please sign in to comment.