Skip to content

Commit

Permalink
chore: add local insta aliases (with_settings) to shorten snaphot n…
Browse files Browse the repository at this point in the history
…ames
  • Loading branch information
dj8yf0μl committed Aug 4, 2023
1 parent 880efe0 commit b432f65
Show file tree
Hide file tree
Showing 29 changed files with 64 additions and 40 deletions.
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/attributes/field/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) static BOUNDS_FIELD_PARSE_MAP: Lazy<BTreeMap<Symbol, Box<ParseFn>>> =
m
});

#[derive(Default)]
#[derive(Default, Clone)]
pub(crate) struct Bounds {
pub serialize: Option<Vec<WherePredicate>>,
pub deserialize: Option<Vec<WherePredicate>>,
Expand Down
67 changes: 36 additions & 31 deletions borsh-derive/src/internals/attributes/field/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(unused)]
// TODO: remove unused when unsplit is done
use std::collections::BTreeMap;

use once_cell::sync::Lazy;
Expand Down Expand Up @@ -67,7 +65,7 @@ static BORSH_FIELD_PARSE_MAP: Lazy<BTreeMap<Symbol, Box<ParseFn>>> = Lazy::new(|
m
});

#[derive(Default)]
#[derive(Default, Clone)]
pub(crate) struct Attributes {
pub bounds: Option<bounds::Bounds>,
pub serialize_with: Option<syn::ExprPath>,
Expand Down Expand Up @@ -229,7 +227,10 @@ mod tests {
Ok(borsh_attrs.bounds)
}

use crate::internals::test_helpers::{debug_print_tokenizable, debug_print_vec_of_tokenizable};
use crate::internals::test_helpers::{
debug_print_tokenizable, debug_print_vec_of_tokenizable, local_insta_assert_debug_snapshot,
local_insta_assert_snapshot,
};

use super::{bounds, Attributes};

Expand All @@ -249,7 +250,7 @@ mod tests {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -269,8 +270,8 @@ mod tests {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let attrs = parse_bounds(&first_field.attrs).unwrap().unwrap();
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.serialize));
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.serialize.clone()));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
}

#[test]
Expand All @@ -290,8 +291,8 @@ mod tests {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let attrs = parse_bounds(&first_field.attrs).unwrap().unwrap();
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.serialize));
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.serialize.clone()));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
}

#[test]
Expand All @@ -310,8 +311,8 @@ mod tests {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let attrs = parse_bounds(&first_field.attrs).unwrap().unwrap();
assert_eq!(attrs.serialize.unwrap().len(), 0);
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
assert_eq!(attrs.serialize.as_ref().unwrap().len(), 0);
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
}

#[test]
Expand All @@ -328,7 +329,7 @@ mod tests {
let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let attrs = parse_bounds(&first_field.attrs).unwrap().unwrap();
assert!(attrs.serialize.is_none());
insta::assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(attrs.deserialize));
}

#[test]
Expand All @@ -347,7 +348,7 @@ mod tests {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -366,7 +367,7 @@ mod tests {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -385,7 +386,7 @@ mod tests {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -404,8 +405,8 @@ mod tests {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let attrs = Attributes::parse(&first_field.attrs, false).unwrap();
insta::assert_snapshot!(debug_print_tokenizable(attrs.serialize_with));
insta::assert_snapshot!(debug_print_tokenizable(attrs.deserialize_with));
local_insta_assert_snapshot!(debug_print_tokenizable(attrs.serialize_with.as_ref()));
local_insta_assert_snapshot!(debug_print_tokenizable(attrs.deserialize_with));
}

#[test]
Expand All @@ -427,7 +428,7 @@ mod tests {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}
}

Expand All @@ -436,8 +437,12 @@ mod tests {
mod tests_schema {
use crate::internals::{
attributes::field::Attributes,
test_helpers::{debug_print_tokenizable, debug_print_vec_of_tokenizable},
test_helpers::{
debug_print_tokenizable, debug_print_vec_of_tokenizable,
local_insta_assert_debug_snapshot, local_insta_assert_snapshot,
},
};

use quote::quote;
use syn::{Attribute, ItemStruct};

Expand Down Expand Up @@ -466,13 +471,13 @@ mod tests_schema {
let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];

let attrs = Attributes::parse(&first_field.attrs, false).unwrap();
let bounds = attrs.bounds.unwrap();
let bounds = attrs.bounds.clone().unwrap();
assert!(bounds.serialize.is_none());
insta::assert_snapshot!(debug_print_vec_of_tokenizable(bounds.deserialize));
let schema = attrs.schema.unwrap();
insta::assert_snapshot!(debug_print_vec_of_tokenizable(schema.params));
insta::assert_snapshot!(debug_print_tokenizable(attrs.serialize_with));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(bounds.deserialize));
assert!(attrs.deserialize_with.is_none());
let schema = attrs.schema.clone().unwrap();
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(schema.params.clone()));
local_insta_assert_snapshot!(debug_print_tokenizable(attrs.serialize_with));
}

#[test]
Expand All @@ -493,7 +498,7 @@ mod tests_schema {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let schema_attrs = parse_schema_attrs(&first_field.attrs).unwrap();
insta::assert_snapshot!(debug_print_vec_of_tokenizable(schema_attrs.unwrap().params));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(schema_attrs.unwrap().params));
}
#[test]
fn test_schema_params_parsing_error() {
Expand All @@ -516,7 +521,7 @@ mod tests_schema {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -540,7 +545,7 @@ mod tests_schema {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}

#[test]
Expand All @@ -561,7 +566,7 @@ mod tests_schema {

let first_field = &item_struct.fields.into_iter().collect::<Vec<_>>()[0];
let schema_attrs = parse_schema_attrs(&first_field.attrs).unwrap();
insta::assert_snapshot!(debug_print_vec_of_tokenizable(schema_attrs.unwrap().params));
local_insta_assert_snapshot!(debug_print_vec_of_tokenizable(schema_attrs.unwrap().params));
}
#[test]
fn test_schema_params_parsing3() {
Expand Down Expand Up @@ -619,8 +624,8 @@ mod tests_schema {
let schema = attrs.schema.unwrap();
let with_funcs = schema.with_funcs.unwrap();

insta::assert_snapshot!(debug_print_tokenizable(with_funcs.declaration));
insta::assert_snapshot!(debug_print_tokenizable(with_funcs.definitions));
local_insta_assert_snapshot!(debug_print_tokenizable(with_funcs.declaration.clone()));
local_insta_assert_snapshot!(debug_print_tokenizable(with_funcs.definitions));
}

// both `declaration` and `definitions` have to be specified
Expand All @@ -644,6 +649,6 @@ mod tests_schema {
Ok(..) => unreachable!("expecting error here"),
Err(err) => err,
};
insta::assert_debug_snapshot!(err);
local_insta_assert_debug_snapshot!(err);
}
}
2 changes: 1 addition & 1 deletion borsh-derive/src/internals/attributes/field/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct ParameterOverride {
}

#[allow(unused)]
#[derive(Default)]
#[derive(Default, Clone)]
pub(crate) struct Attributes {
pub params: Option<Vec<ParameterOverride>>,
pub with_funcs: Option<WithFuncs>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub(crate) static WITH_FUNCS_FIELD_PARSE_MAP: Lazy<BTreeMap<Symbol, Box<ParseFn>
m
});

#[derive(Clone)]
pub(crate) struct WithFuncs {
pub declaration: Option<syn::ExprPath>,
pub definitions: Option<syn::ExprPath>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/field/mod.rs
expression: debug_print_vec_of_tokenizable(attrs.serialize)
expression: debug_print_vec_of_tokenizable(attrs.serialize.clone())
---
K : Hash + Eq + Ord
V : Ord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/field/mod.rs
expression: debug_print_vec_of_tokenizable(attrs.serialize)
expression: debug_print_vec_of_tokenizable(attrs.serialize.clone())
---
K : Hash + Eq + borsh :: ser :: BorshSerialize
V : borsh :: ser :: BorshSerialize
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/field/mod.rs
expression: debug_print_vec_of_tokenizable(schema.params)
expression: debug_print_vec_of_tokenizable(schema.params.clone())
---
T => < T as TraitName > :: Associated
V => Vec < V >
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/field/mod.rs
expression: debug_print_tokenizable(with_funcs.declaration)
expression: debug_print_tokenizable(with_funcs.declaration.clone())
---
third_party_impl :: declaration :: < K , V >

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: borsh-derive/src/internals/attributes/field/mod.rs
expression: debug_print_tokenizable(attrs.serialize_with)
expression: debug_print_tokenizable(attrs.serialize_with.as_ref())
---
third_party_impl :: serialize_third_party

2 changes: 0 additions & 2 deletions borsh-derive/src/internals/generics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: remove this unused attribute, when the unsplit is done
#![allow(unused)]
use std::collections::{HashMap, HashSet};

use quote::{quote, ToTokens};
Expand Down
20 changes: 20 additions & 0 deletions borsh-derive/src/internals/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ pub fn debug_print_tokenizable<T: ToTokens>(optional: Option<T>) -> String {
}
s
}

macro_rules! local_insta_assert_debug_snapshot {
($value:expr) => {{

insta::with_settings!({prepend_module_to_snapshot => false}, {
insta::assert_debug_snapshot!($value);
});
}};
}

macro_rules! local_insta_assert_snapshot {
($value:expr) => {{

insta::with_settings!({prepend_module_to_snapshot => false}, {
insta::assert_snapshot!($value);
});
}};
}

pub(crate) use {local_insta_assert_debug_snapshot, local_insta_assert_snapshot};

0 comments on commit b432f65

Please sign in to comment.