Skip to content

Commit

Permalink
Added view macro tests to leptos_macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Ar4ys committed Feb 10, 2024
1 parent 445e63f commit 4abbe06
Show file tree
Hide file tree
Showing 43 changed files with 1,416 additions and 0 deletions.
1 change: 1 addition & 0 deletions leptos_macro/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ fn ui() {
t.compile_fail("tests/ui/component.rs");
t.compile_fail("tests/ui/component_absolute.rs");
t.compile_fail("tests/ui/server.rs");
t.compile_fail("tests/ui/view/*.rs");
}
9 changes: 9 additions & 0 deletions leptos_macro/tests/ui/view/block_in_fragment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use leptos::*;

struct A;

fn main() {
view! {
<>{A}</>
};
}
29 changes: 29 additions & 0 deletions leptos_macro/tests/ui/view/block_in_fragment.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error[E0277]: expected a `Fn()` closure, found `A`
--> tests/ui/view/block_in_fragment.rs:7:11
|
6 | / view! {
7 | | <>{A}</>
| | ^-^
| | ||
| | |this tail expression is of type `A`
| | expected an `Fn()` closure, found `A`
8 | | };
| |_____- required by a bound introduced by this call
|
= help: the trait `std::ops::Fn<()>` is not implemented for `A`
= note: wrap the `A` in a closure with no arguments: `|| { /* code */ }`
= help: the following other types implement trait `IntoView`:
bool
char
isize
i8
i16
i32
i64
i128
and $N others
= note: required for `A` to implement `IntoView`
help: you might have meant to create the closure instead of a block
|
7 | <>|_| {A}</>
| +++
36 changes: 36 additions & 0 deletions leptos_macro/tests/ui/view/children_clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use leptos::*;

#[slot]
struct Then {
children: Children,
}

#[component]
fn Component(children: Children) -> impl IntoView {
_ = children;
}

#[component]
fn Slot(then: Then) -> impl IntoView {
_ = then;
}

struct A;

fn main() {
let a = A;

view! {
<Component clone:a>
<p />
</Component>
};

view! {
<Slot>
<Then slot clone:a>
<p/>
</Then>
</Slot>
};
}
35 changes: 35 additions & 0 deletions leptos_macro/tests/ui/view/children_clone.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
error[E0277]: the trait bound `A: Clone` is not satisfied
--> tests/ui/view/children_clone.rs:24:20
|
23 | / view! {
24 | | <Component clone:a>
| | ^^^^^^^ the trait `Clone` is not implemented for `A`
25 | | <p />
26 | | </Component>
27 | | };
| |_____- required by a bound introduced by this call
|
help: consider annotating `A` with `#[derive(Clone)]`
|
18 + #[derive(Clone)]
19 | struct A;
|

error[E0277]: the trait bound `A: Clone` is not satisfied
--> tests/ui/view/children_clone.rs:31:24
|
29 | / view! {
30 | | <Slot>
31 | | <Then slot clone:a>
| | ^^^^^^^ the trait `Clone` is not implemented for `A`
32 | | <p/>
33 | | </Then>
34 | | </Slot>
35 | | };
| |_____- required by a bound introduced by this call
|
help: consider annotating `A` with `#[derive(Clone)]`
|
18 + #[derive(Clone)]
19 | struct A;
|
28 changes: 28 additions & 0 deletions leptos_macro/tests/ui/view/children_projection_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use leptos::*;

#[component]
fn Outer(children: ChildrenFn) -> impl IntoView {
_ = children;
}

#[component]
fn Inner(children: ChildrenFn) -> impl IntoView {
_ = children;
}

#[component]
fn Inmost(name: String) -> impl IntoView {
_ = name;
}

fn main() {
let name = "Alice".to_string();

view! {
<Outer>
<Inner>
<Inmost name=name.clone()/>
</Inner>
</Outer>
};
}
13 changes: 13 additions & 0 deletions leptos_macro/tests/ui/view/children_projection_error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
--> tests/ui/view/children_projection_error.rs:23:13
|
23 | / <Inner>
24 | | <Inmost name=name.clone()/>
| | ---- closure is `FnOnce` because it moves the variable `name` out of its environment
25 | | </Inner>
| | ^
| | |
| |____________________this closure implements `FnOnce`, not `Fn`
| the requirement to implement `Fn` derives from here
|
= note: required for `Rc<dyn std::ops::Fn() -> Fragment>` to implement `ToChildren<{closure@$DIR/tests/ui/view/children_projection_error.rs:23:13: 25:21}>`
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use leptos::*;

#[component]
fn Root(outer: Outer) -> impl IntoView {
_ = outer;
}

#[slot]
struct Outer {
children: ChildrenFn,
}

#[component]
fn Inner(children: ChildrenFn) -> impl IntoView {
_ = children;
}

#[component]
fn Inmost(name: String) -> impl IntoView {
_ = name;
}

fn main() {
let name = "Alice".to_string();

view! {
<Root>
<Outer slot>
<Inner>
<Inmost name=name.clone() />
</Inner>
</Outer>
</Root>
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce`
--> tests/ui/view/children_projection_error_slot_version.rs:29:17
|
29 | / <Inner>
30 | | <Inmost name=name.clone() />
| | ---- closure is `FnOnce` because it moves the variable `name` out of its environment
31 | | </Inner>
| | ^
| | |
| |________________________this closure implements `FnOnce`, not `Fn`
| the requirement to implement `Fn` derives from here
|
= note: required for `Rc<dyn std::ops::Fn() -> Fragment>` to implement `ToChildren<{closure@$DIR/tests/ui/view/children_projection_error_slot_version.rs:29:17: 31:25}>`
12 changes: 12 additions & 0 deletions leptos_macro/tests/ui/view/component_with_props_without_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use leptos::*;

#[allow(unused, non_snake_case)]
fn Component(prop: i32) -> impl IntoView {
_ = prop;
}

fn main() {
view! {
<Component />
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
error[E0277]: the trait bound `fn(i32) -> impl leptos::IntoView {Component}: leptos::Component<_>` is not satisfied
--> tests/ui/view/component_with_props_without_macro.rs:10:10
|
10 | <Component />
| ^^^^^^^^^ the trait `leptos::Component<_>` is not implemented for fn item `fn(i32) -> impl leptos::IntoView {Component}`
|
note: required by a bound in `component_props_builder`
--> $WORKSPACE/leptos/src/lib.rs
|
| pub fn component_props_builder<P: PropsOrNoPropsBuilder>(
| ----------------------- required by a bound in this function
| _f: &impl Component<P>,
| ^^^^^^^^^^^^ required by this bound in `component_props_builder`

error[E0277]: the trait bound `&fn(i32) -> impl leptos::IntoView {Component}: ComponentConstructor<_>` is not satisfied
--> tests/ui/view/component_with_props_without_macro.rs:10:10
|
10 | <Component />
| -^^^^^^^^^---
| ||
| |the trait `ComponentConstructor<_>` is not implemented for `&fn(i32) -> impl leptos::IntoView {Component}`
| required by a bound introduced by this call
|
note: required by a bound in `component_view`
--> $WORKSPACE/leptos/src/lib.rs
|
| pub fn component_view<P>(f: impl ComponentConstructor<P>, props: P) -> View {
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `component_view`
23 changes: 23 additions & 0 deletions leptos_macro/tests/ui/view/directives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use leptos::*;

#[component]
fn Component() -> impl IntoView {}
fn highlight(el: HtmlElement<html::AnyElement>, prop: i32) {
_ = prop;
}

fn main() {
let data = "Hello World!";

view! {
<Component use:highlight />
};

view! {
<Component use:highlight="asd" />
};

view! {
<Component use:highlight=data />
};
}
50 changes: 50 additions & 0 deletions leptos_macro/tests/ui/view/directives.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
error[E0277]: the trait bound `i32: From<()>` is not satisfied
--> tests/ui/view/directives.rs:13:20
|
13 | <Component use:highlight />
| ^^^^^^^^^^^^^ the trait `From<()>` is not implemented for `i32`
|
= help: the following other types implement trait `From<T>`:
<i32 as From<bool>>
<i32 as From<i8>>
<i32 as From<i16>>
<i32 as From<u8>>
<i32 as From<u16>>
<i32 as From<NonZeroI32>>
= note: required for `()` to implement `Into<i32>`

error[E0277]: the trait bound `i32: From<&str>` is not satisfied
--> tests/ui/view/directives.rs:17:34
|
16 | / view! {
17 | | <Component use:highlight="asd" />
| | ^^^^^ the trait `From<&str>` is not implemented for `i32`
18 | | };
| |_____- required by a bound introduced by this call
|
= help: the following other types implement trait `From<T>`:
<i32 as From<bool>>
<i32 as From<i8>>
<i32 as From<i16>>
<i32 as From<u8>>
<i32 as From<u16>>
<i32 as From<NonZeroI32>>
= note: required for `&str` to implement `Into<i32>`

error[E0277]: the trait bound `i32: From<&str>` is not satisfied
--> tests/ui/view/directives.rs:21:34
|
20 | / view! {
21 | | <Component use:highlight=data />
| | ^^^^ the trait `From<&str>` is not implemented for `i32`
22 | | };
| |_____- required by a bound introduced by this call
|
= help: the following other types implement trait `From<T>`:
<i32 as From<bool>>
<i32 as From<i8>>
<i32 as From<i16>>
<i32 as From<u8>>
<i32 as From<u16>>
<i32 as From<NonZeroI32>>
= note: required for `&str` to implement `Into<i32>`
10 changes: 10 additions & 0 deletions leptos_macro/tests/ui/view/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use leptos::*;

#[component]
fn Component() -> impl IntoView {}

fn main() {
view! {
<Component on:click=|| {} />
};
}
12 changes: 12 additions & 0 deletions leptos_macro/tests/ui/view/events.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0593]: closure is expected to take 1 argument, but it takes 0 arguments
--> tests/ui/view/events.rs:8:20
|
8 | <Component on:click=|| {} />
| ^^^^^^^^ -- takes 0 arguments
| |
| expected closure that takes 1 argument
|
help: consider changing the closure to take and ignore the expected argument
|
8 | <Component on:click=|_| {} />
| ~~~
14 changes: 14 additions & 0 deletions leptos_macro/tests/ui/view/expected_children_got_let_bind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use leptos::*;

#[component]
fn Component(children: Children) -> impl IntoView {
_ = children;
}

fn main() {
view! {
<Component let:a>
<p />
</Component>
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error[E0308]: mismatched types
--> tests/ui/view/expected_children_got_let_bind.rs:11:13
|
11 | <p />
| ^^^^^ expected `Box<dyn FnOnce() -> Fragment>`, found closure
|
= note: expected struct `Box<(dyn FnOnce() -> Fragment + 'static)>`
found closure `{closure@$DIR/tests/ui/view/expected_children_got_let_bind.rs:11:13: 11:18}`
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use leptos::*;

#[slot]
struct Then {
children: Children,
}

#[component]
fn Component(then: Then) -> impl IntoView {
_ = then;
}

fn main() {
view! {
<Component>
<Then slot let:a>
<p />
</Then>
</Component>
};
}
Loading

0 comments on commit 4abbe06

Please sign in to comment.