Skip to content

Commit

Permalink
test(Export): add UI test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogay committed Oct 8, 2023
1 parent cb9087a commit 217d253
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
24 changes: 18 additions & 6 deletions gdnative-derive/src/export.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use proc_macro2::TokenStream as TokenStream2;
use syn::DeriveInput;
use syn::{DeriveInput, Fields};

pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
let derived_enum = match &input.data {
Expand All @@ -17,11 +17,23 @@ pub(crate) fn derive_export(input: &DeriveInput) -> syn::Result<TokenStream2> {
}

fn impl_export(enum_ty: &syn::Ident, data: &syn::DataEnum) -> syn::Result<TokenStream2> {
let mappings = data.variants.iter().map(|variant| {
let key = &variant.ident;
let val = quote! { #enum_ty::#key as i64 };
quote! { (stringify!(#key).to_string(), #val) }
});
let mappings = {
let mut m = Vec::with_capacity(data.variants.len());

for variant in &data.variants {
if !matches!(variant.fields, Fields::Unit) {
return Err(syn::Error::new(
variant.ident.span(),
"#[derive(Export)] only supports fieldless enums",
));
}
let key = &variant.ident;
let val = quote! { #enum_ty::#key as i64 };
m.push(quote! { (stringify!(#key).to_string(), #val) });
}

m
};
let impl_block = quote! {
impl ::gdnative::export::Export for #enum_ty {
type Hint = ::gdnative::export::hint::IntHint<i64>;
Expand Down
3 changes: 3 additions & 0 deletions gdnative/tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ fn ui_tests() {
t.compile_fail("tests/ui/from_variant_fail_07.rs");
t.compile_fail("tests/ui/from_variant_fail_08.rs");
t.compile_fail("tests/ui/from_variant_fail_09.rs");

// Export
t.compile_fail("tests/ui/export_fail_01.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_01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use gdnative::prelude::*;

#[derive(Export, ToVariant)]
pub enum Foo {
Bar(String),
}

fn main() {}
5 changes: 5 additions & 0 deletions gdnative/tests/ui/export_fail_01.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_01.rs:5:5
|
5 | Bar(String),
| ^^^

0 comments on commit 217d253

Please sign in to comment.