Skip to content

Commit

Permalink
More unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchalmers committed Dec 21, 2023
1 parent 1dcb69a commit 9c4d3e6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 15 deletions.
31 changes: 17 additions & 14 deletions execution-plan-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! Proc-macros for implementing execution-plan traits.
use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, quote_spanned};
use syn::Ident;
use syn::{spanned::Spanned, DeriveInput, Fields, GenericParam};
use syn::{spanned::Spanned, DeriveInput, Fields, GenericParam, Ident};

/// This will derive the trait `Value` from the `kittycad-execution-plan-traits` crate.
#[proc_macro_derive(ExecutionPlanValue)]
Expand Down Expand Up @@ -88,7 +86,10 @@ fn impl_value_on_enum(
quote_spanned! {variant.span() =>
#name::#variant_name
},
quote_spanned! {variant.span()=> {Vec::new()}},
quote_spanned! {variant.span()=>
let part = kittycad_execution_plan_traits::Primitive::from(stringify!(#variant_name).to_owned());
vec![part]
}
),
};
quote_spanned! {variant.span() =>
Expand Down Expand Up @@ -291,19 +292,21 @@ fn remove_generics_defaults(mut g: syn::Generics) -> syn::Generics {

#[cfg(test)]
mod tests {
use super::*;
use anyhow::Result;

use super::*;

#[test]
fn test_enum() {
let input = quote! {
enum FooEnum {
A{x: usize},
B{y: usize},
C(usize, String),
D,
}
};
let input =
quote! {
enum FooEnum {
A{x: usize},
B{y: usize},
C(usize, String),
D,
}
};
let input: DeriveInput = syn::parse2(input).unwrap();
let out = impl_derive_value(input);
let formatted = get_text_fmt(&out).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ impl kittycad_execution_plan_traits::Value for FooEnum {
parts.extend(field1.into_parts());
parts
}
FooEnum::D => Vec::new(),
FooEnum::D => {
let part =
kittycad_execution_plan_traits::Primitive::from(stringify!(D).to_owned());
vec![part]
}
}
}

Expand Down
57 changes: 57 additions & 0 deletions execution-plan-macros/tests/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use kittycad_execution_plan_macros::ExecutionPlanValue;
use kittycad_execution_plan_traits::{Primitive, Value};

#[derive(ExecutionPlanValue)]
struct FooConcrete {
Expand Down Expand Up @@ -30,3 +31,59 @@ mod generics {
t: T,
}
}

#[test]
fn test_derive_on_enum() {
#[derive(ExecutionPlanValue, Eq, PartialEq, Debug, Clone)]
enum FooEnum {
A { x: usize },
B { y: Option<usize> },
C(usize, String),
D,
}
for (i, (test_name, input, expected)) in
[
(
"named fields",
FooEnum::A { x: 3 },
vec![Primitive::from("A".to_owned()), Primitive::from(3)],
),
(
"named fields with Option::Some",
FooEnum::B { y: Some(3) },
vec![
Primitive::from("B".to_owned()),
Primitive::from("some".to_owned()),
Primitive::from(3),
],
),
//
(
"named fields with Option::None",
FooEnum::B { y: None },
vec![Primitive::from("B".to_owned()), Primitive::from("none".to_owned())],
),
(
"positional fields",
FooEnum::C(4, "hello".to_owned()),
vec![
Primitive::from("C".to_owned()),
Primitive::from(4),
Primitive::from("hello".to_owned()),
],
),
("unit variant", FooEnum::D, vec![Primitive::from("D".to_owned())]),
]
.into_iter()
.enumerate()
{
let actual = input.clone().into_parts();
assert_eq!(
actual, expected,
"failed test {i}, '{test_name}'.\nActual parts (left) != expected parts (right)"
);
let mut actual_iter = actual.into_iter().map(Some);
let inverted = FooEnum::from_parts(&mut actual_iter).expect("from_parts should succeed");
assert_eq!(inverted, input, "failed test {i}, '{test_name}'.\nInput value (right) != input value turned into parts then back into value (left).");
}
}

0 comments on commit 9c4d3e6

Please sign in to comment.