From c33e89ba58c69096e041a54d210195d3520376b7 Mon Sep 17 00:00:00 2001 From: bogay Date: Wed, 11 Oct 2023 22:15:10 +0800 Subject: [PATCH] test(Export): add UI tests --- gdnative-derive/src/export.rs | 27 ++++++++++++------------- gdnative/tests/ui.rs | 3 +++ gdnative/tests/ui/export_fail_02.rs | 8 ++++++++ gdnative/tests/ui/export_fail_02.stderr | 5 +++++ gdnative/tests/ui/export_fail_03.rs | 8 ++++++++ gdnative/tests/ui/export_fail_03.stderr | 11 ++++++++++ gdnative/tests/ui/export_pass.rs | 11 ++++++++++ 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 gdnative/tests/ui/export_fail_02.rs create mode 100644 gdnative/tests/ui/export_fail_02.stderr create mode 100644 gdnative/tests/ui/export_fail_03.rs create mode 100644 gdnative/tests/ui/export_fail_03.stderr create mode 100644 gdnative/tests/ui/export_pass.rs diff --git a/gdnative-derive/src/export.rs b/gdnative-derive/src/export.rs index 8efa519f0..7e11b5b38 100644 --- a/gdnative-derive/src/export.rs +++ b/gdnative-derive/src/export.rs @@ -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 { 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())); } }; @@ -20,14 +25,8 @@ fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result tests/ui/export_fail_02.rs:4:5 + | +4 | pub struct Foo { + | ^^^^^^ diff --git a/gdnative/tests/ui/export_fail_03.rs b/gdnative/tests/ui/export_fail_03.rs new file mode 100644 index 000000000..d89f24118 --- /dev/null +++ b/gdnative/tests/ui/export_fail_03.rs @@ -0,0 +1,8 @@ +use gdnative::prelude::*; + +#[derive(Export, ToVariant)] +pub union Foo { + bar: i32, +} + +fn main() {} diff --git a/gdnative/tests/ui/export_fail_03.stderr b/gdnative/tests/ui/export_fail_03.stderr new file mode 100644 index 000000000..304eff3b3 --- /dev/null +++ b/gdnative/tests/ui/export_fail_03.stderr @@ -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 { + | ^^^ diff --git a/gdnative/tests/ui/export_pass.rs b/gdnative/tests/ui/export_pass.rs new file mode 100644 index 000000000..a5b12b5de --- /dev/null +++ b/gdnative/tests/ui/export_pass.rs @@ -0,0 +1,11 @@ +use gdnative::prelude::*; + +#[derive(Export, ToVariant, Clone, Copy)] +#[variant(enum = "repr")] +#[repr(i32)] +pub enum Foo { + Bar, + Baz, +} + +fn main() {}