Skip to content

Commit

Permalink
test(Export): add UI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogay committed Oct 11, 2023
1 parent a12fd4d commit c33e89b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
27 changes: 13 additions & 14 deletions gdnative-derive/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Span, TokenStream as TokenStream2};
use syn::spanned::Spanned;
use syn::{DeriveInput, Fields};

fn err_only_supports_fieldless_enums(span: Span) -> syn::Error {
syn::Error::new(span, "#[derive(Export)] only supports fieldless enums")
}

pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
let derived_enum = match &input.data {
syn::Data::Enum(data) => data,
_ => {
return Err(syn::Error::new(
input.ident.span(),
"#[derive(Export)] only supports fieldless enums",
))
syn::Data::Struct(data) => {
return Err(err_only_supports_fieldless_enums(data.struct_token.span()));
}
syn::Data::Union(data) => {
return Err(err_only_supports_fieldless_enums(data.union_token.span()));
}
};

Expand All @@ -20,14 +25,8 @@ fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenS
let err = data
.variants
.iter()
.filter_map(|variant| {
(!matches!(variant.fields, Fields::Unit)).then(|| {
syn::Error::new(
variant.ident.span(),
"#[derive(Export)] only supports fieldless enums",
)
})
})
.filter(|variant| !matches!(variant.fields, Fields::Unit))
.map(|variant| err_only_supports_fieldless_enums(variant.ident.span()))
.reduce(|mut acc, err| {
acc.combine(err);
acc
Expand Down
3 changes: 3 additions & 0 deletions gdnative/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ fn ui_tests() {
t.compile_fail("tests/ui/from_variant_fail_09.rs");

// Export
t.pass("tests/ui/export_pass.rs");
t.compile_fail("tests/ui/export_fail_01.rs");
t.compile_fail("tests/ui/export_fail_02.rs");
t.compile_fail("tests/ui/export_fail_03.rs");
}

// FIXME(rust/issues/54725): Full path spans are only available on nightly as of now
Expand Down
8 changes: 8 additions & 0 deletions gdnative/tests/ui/export_fail_02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use gdnative::prelude::*;

#[derive(Export, ToVariant)]
pub struct Foo {
bar: i32,
}

fn main() {}
5 changes: 5 additions & 0 deletions gdnative/tests/ui/export_fail_02.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: #[derive(Export)] only supports fieldless enums
--> tests/ui/export_fail_02.rs:4:5
|
4 | pub struct Foo {
| ^^^^^^
8 changes: 8 additions & 0 deletions gdnative/tests/ui/export_fail_03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use gdnative::prelude::*;

#[derive(Export, ToVariant)]
pub union Foo {
bar: i32,
}

fn main() {}
11 changes: 11 additions & 0 deletions gdnative/tests/ui/export_fail_03.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: #[derive(Export)] only supports fieldless enums
--> tests/ui/export_fail_03.rs:4:5
|
4 | pub union Foo {
| ^^^^^

error: Variant conversion derive macro does not work on unions.
--> tests/ui/export_fail_03.rs:4:1
|
4 | pub union Foo {
| ^^^
11 changes: 11 additions & 0 deletions gdnative/tests/ui/export_pass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use gdnative::prelude::*;

#[derive(Export, ToVariant, Clone, Copy)]
#[variant(enum = "repr")]
#[repr(i32)]
pub enum Foo {
Bar,
Baz,
}

fn main() {}

0 comments on commit c33e89b

Please sign in to comment.