Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ui tests for #[component] #198

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sycamore-macro/src/component/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Parse for ComponentFunction {
}
item => Err(syn::Error::new_spanned(
item,
"`function_component` attribute can only be applied to functions",
"`component` attribute can only be applied to functions",
)),
}
}
Expand Down
55 changes: 55 additions & 0 deletions packages/sycamore-macro/tests/component/component-fail.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use sycamore::prelude::*;

/// Missing return type.
#[component(Comp1<G>)]
fn comp1() {
todo!();
}

/// Missing component name.
#[component]
fn comp2() -> Template<G> {
todo!();
}

/// Missing generic param.
#[component(Comp3)]
fn comp3() -> Template<G> {
todo!();
}

#[component(Comp4<G>)]
async fn comp4() -> Template<G> {
todo!();
}

#[component(Comp5<G>)]
const fn comp5() -> Template<G> {
todo!();
}

#[component(Comp6<G>)]
extern fn comp6() -> Template<G> {
todo!();
}

#[component(Comp7<G>)]
fn comp7(self) -> Template<G> {
todo!();
}

#[component(Comp8<G>)]
fn comp8(one: (), two: ()) -> Template<G> {
todo!();
}

#[component(Comp9<G>)]
struct AStruct;

#[allow(non_snake_case)]
#[component(Comp10<G>)]
fn Comp10() -> Template<G> {
todo!();
}

fn main() {}
63 changes: 63 additions & 0 deletions packages/sycamore-macro/tests/component/component-fail.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
error: function must return `sycamore::template::Template`
--> $DIR/component-fail.rs:5:1
|
5 | fn comp1() {
| ^^^^^^^^^^

error: unexpected end of input, expected an identifier for the component
--> $DIR/component-fail.rs:10:1
|
10 | #[component]
| ^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected a single type param
--> $DIR/component-fail.rs:16:1
|
16 | #[component(Comp3)]
| ^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: async functions can't be components
--> $DIR/component-fail.rs:22:1
|
22 | async fn comp4() -> Template<G> {
| ^^^^^

error: const functions can't be components
--> $DIR/component-fail.rs:27:1
|
27 | const fn comp5() -> Template<G> {
| ^^^^^

error: extern functions can't be components
--> $DIR/component-fail.rs:32:1
|
32 | extern fn comp6() -> Template<G> {
| ^^^^^^

error: function components can't accept a receiver
--> $DIR/component-fail.rs:37:10
|
37 | fn comp7(self) -> Template<G> {
| ^^^^

error: function should accept at most one parameter for the prop
--> $DIR/component-fail.rs:42:19
|
42 | fn comp8(one: (), two: ()) -> Template<G> {
| ^^^^^^^

error: `component` attribute can only be applied to functions
--> $DIR/component-fail.rs:47:1
|
47 | struct AStruct;
| ^^^^^^^^^^^^^^^

error: the component must not have the same name as the function
--> $DIR/component-fail.rs:50:13
|
50 | #[component(Comp10<G>)]
| ^^^^^^
13 changes: 13 additions & 0 deletions packages/sycamore-macro/tests/component/component-pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use sycamore::prelude::*;

#[component(Comp1<G>)]
fn comp1() -> Template<G> {
todo!();
}

#[component(Comp2<G>)]
fn comp2(props: ()) -> Template<G> {
todo!();
}

fn main() {}
8 changes: 0 additions & 8 deletions packages/sycamore-macro/tests/template_macro.rs

This file was deleted.

17 changes: 17 additions & 0 deletions packages/sycamore-macro/tests/ui.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[test]
fn template_ui() {
let t = trybuild::TestCases::new();
t.pass("tests/template/*-pass.rs");
if std::env::var("RUN_UI_TESTS").is_ok() {
t.compile_fail("tests/template/*-fail.rs");
}
}

#[test]
fn component_ui() {
let t = trybuild::TestCases::new();
t.pass("tests/component/*-pass.rs");
if std::env::var("RUN_UI_TESTS").is_ok() {
t.compile_fail("tests/component/*-fail.rs");
}
}