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

[test] add derive macro tests #514

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
12 changes: 6 additions & 6 deletions derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ pub fn derive_schema(input: proc_macro::TokenStream) -> proc_macro::TokenStream
let schema_fields = gen_schema_fields(&input.data);
let output = quote! {
#[automatically_derived]
impl crate::actions::schemas::ToDataType for #struct_ident {
fn to_data_type() -> crate::schema::DataType {
use crate::actions::schemas::{ToDataType, GetStructField, GetNullableContainerStructField};
crate::schema::DataType::struct_type([
impl delta_kernel::actions::schemas::ToDataType for #struct_ident {
fn to_data_type() -> delta_kernel::schema::DataType {
use delta_kernel::actions::schemas::{ToDataType, GetStructField, GetNullableContainerStructField};
delta_kernel::schema::DataType::struct_type([
#schema_fields
])
}
Expand Down Expand Up @@ -124,9 +124,9 @@ fn gen_schema_fields(data: &Data) -> TokenStream {
).to_compile_error()
}
}
quote_spanned! { field.span() => #(#type_path_quoted),* get_nullable_container_struct_field(stringify!(#name))}
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
quote_spanned! { field.span() => #(#type_path_quoted)* get_nullable_container_struct_field(stringify!(#name))}
} else {
quote_spanned! { field.span() => #(#type_path_quoted),* get_struct_field(stringify!(#name))}
quote_spanned! { field.span() => #(#type_path_quoted)* get_struct_field(stringify!(#name))}
}
}
_ => Error::new(field.span(), format!("Can't handle type: {:?}", field.ty)).to_compile_error()
Expand Down
3 changes: 2 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ default:
# run tests
test:
cargo test --features default-engine

cargo test --doc --features developer-visibility
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved

# lint codebase
lint:
cargo clippy --tests --features default-engine
Expand Down
4 changes: 4 additions & 0 deletions kernel/src/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ use serde::{Deserialize, Serialize};
pub mod deletion_vector;
pub mod set_transaction;

#[cfg(feature = "developer-visibility")]
pub mod schemas;
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "developer-visibility"))]
pub(crate) mod schemas;

#[cfg(feature = "developer-visibility")]
pub mod visitors;
#[cfg(not(feature = "developer-visibility"))]
Expand Down
3 changes: 3 additions & 0 deletions kernel/src/actions/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::{HashMap, HashSet};

use crate::schema::{ArrayType, DataType, MapType, StructField};

#[cfg_attr(feature = "developer-visibility", visibility::make(pub))]
pub(crate) trait ToDataType {
fn to_data_type() -> DataType;
}
Expand Down Expand Up @@ -62,10 +63,12 @@ impl<K: ToDataType, V: ToDataType> ToNullableContainerType for HashMap<K, V> {
}
}

#[cfg_attr(feature = "developer-visibility", visibility::make(pub))]
pub(crate) trait GetStructField {
fn get_struct_field(name: impl Into<String>) -> StructField;
}

#[cfg_attr(feature = "developer-visibility", visibility::make(pub))]
pub(crate) trait GetNullableContainerStructField {
fn get_nullable_container_struct_field(name: impl Into<String>) -> StructField;
}
Expand Down
49 changes: 49 additions & 0 deletions kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
rust_2021_compatibility
)]

/// This allows the derive macro to refer to types in this crate using full crate name, instead of using `crate::`,
/// which seem to be the only way to to test the macro generated code and macro compile failures
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
extern crate self as delta_kernel;
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved

use std::any::Any;

use std::sync::Arc;
use std::{cmp::Ordering, ops::Range};

Expand Down Expand Up @@ -416,3 +421,47 @@ pub trait Engine: AsAny {
/// Get the connector provided [`ParquetHandler`].
fn get_parquet_handler(&self) -> Arc<dyn ParquetHandler>;
}

#[cfg(doctest)]
mod doc_tests {
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved

/// ```
/// # use delta_kernel_derive::Schema;
/// #[derive(Schema)]
/// pub struct WithFields {
/// some_name: String,
/// }
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
/// ```
#[cfg(doctest)]
pub struct MacroTestStructWithField;

/// ```compile_fail
/// # use delta_kernel_derive::Schema;
/// #[derive(Schema)]
/// pub struct NoFields;
/// ```
#[cfg(doctest)]
pub struct MacroTestStructWithoutField;

/// ```compile_fail
/// # use delta_kernel_derive::Schema;
/// #[derive(Schema)]
/// pub struct WithInvalidAttributeTarget {
/// #[drop_null_container_values]
/// some_name: String,
/// }
/// ```
#[cfg(doctest)]
pub struct MacroTestStructWithInvalidAttributeTarget;

/// ```compile_fail
/// # use delta_kernel_derive::Schema;
/// # use syn::Token;
/// #[derive(Schema)]
/// pub struct WithInvalidFieldType {
/// token: Token![struct],
r3stl355 marked this conversation as resolved.
Show resolved Hide resolved
/// }
/// ```
#[cfg(doctest)]
pub struct MacroTestStructWithInvalidFieldType;
}
Loading