Skip to content

Commit

Permalink
test: extract schema_map! macro and common imports for schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yf0μl committed Feb 27, 2024
1 parent 4aa7fcc commit 972a302
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 323 deletions.
36 changes: 36 additions & 0 deletions borsh/tests/common_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,39 @@ macro_rules! map_wrong_order_test [
}
]
];

#[allow(unused)]
macro_rules! schema_map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);

#[allow(unused)]
#[cfg(feature = "unstable__schema")]
pub mod schema_imports {
#[cfg(feature = "std")]
pub use std::collections::BTreeMap;

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
pub use alloc::{
boxed::Box,
collections::BTreeMap,
format,
string::{String, ToString},
vec,
vec::Vec,
};

pub use borsh::schema::{BorshSchemaContainer, Definition, Fields};
pub use borsh::{schema_container_of, BorshSchema};
}
28 changes: 9 additions & 19 deletions borsh/tests/test_ascii_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,37 +84,27 @@ mod de_errors {
}
}

#[macro_use]
mod common_macro;

#[cfg(feature = "unstable__schema")]
mod schema {
use alloc::{collections::BTreeMap, string::ToString};
use borsh::schema::{BorshSchema, Definition};
macro_rules! map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);
use super::common_macro::schema_imports::*;

#[test]
fn test_ascii_strings() {
assert_eq!("AsciiString", ascii::AsciiStr::declaration());
assert_eq!("AsciiString", ascii::AsciiString::declaration());
assert_eq!("AsciiChar", ascii::AsciiChar::declaration());

let want_char = map! {
let want_char = schema_map! {
"AsciiChar" => Definition::Primitive(1)
};
let mut actual_defs = map!();
let mut actual_defs = schema_map!();
ascii::AsciiChar::add_definitions_recursively(&mut actual_defs);
assert_eq!(want_char, actual_defs);

let want = map! {
let want = schema_map! {
"AsciiString" => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
Expand All @@ -123,11 +113,11 @@ mod schema {
"AsciiChar" => Definition::Primitive(1)
};

let mut actual_defs = map!();
let mut actual_defs = schema_map!();
ascii::AsciiStr::add_definitions_recursively(&mut actual_defs);
assert_eq!(want, actual_defs);

let mut actual_defs = map!();
let mut actual_defs = schema_map!();
ascii::AsciiString::add_definitions_recursively(&mut actual_defs);
assert_eq!(want, actual_defs);
}
Expand Down
30 changes: 8 additions & 22 deletions borsh/tests/test_cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,22 @@ mod de_errors {
}
}

#[macro_use]
mod common_macro;

#[cfg(feature = "unstable__schema")]
mod schema {

use alloc::{
collections::BTreeMap,
string::{String, ToString},
vec::Vec,
};
use borsh::schema::{BorshSchema, Definition};
macro_rules! map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);
use super::common_macro::schema_imports::*;
fn common_map_i32() -> BTreeMap<String, Definition> {
map! {
schema_map! {

"i32" => Definition::Primitive(4)
}
}

fn common_map_slice_i32() -> BTreeMap<String, Definition> {
map! {
schema_map! {
"Vec<i32>" => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
Expand All @@ -85,7 +71,7 @@ mod schema {
fn test_cell() {
assert_eq!("i32", <core::cell::Cell<i32> as BorshSchema>::declaration());

let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<core::cell::Cell<i32> as BorshSchema>::add_definitions_recursively(&mut actual_defs);
assert_eq!(common_map_i32(), actual_defs);
}
Expand All @@ -97,7 +83,7 @@ mod schema {
<core::cell::RefCell<Vec<i32>> as BorshSchema>::declaration()
);

let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<core::cell::RefCell<Vec<i32>> as BorshSchema>::add_definitions_recursively(
&mut actual_defs,
);
Expand Down
31 changes: 6 additions & 25 deletions borsh/tests/test_enum_discriminants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,31 +185,12 @@ fn test_discriminant_serde() {
}
}

#[macro_use]
mod common_macro;

#[cfg(feature = "unstable__schema")]
mod schema {
#[cfg(not(feature = "std"))]
use alloc::{collections::BTreeMap, string::ToString, vec};

#[cfg(feature = "std")]
use std::collections::BTreeMap;

macro_rules! map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);

use borsh::{
schema::{Definition, Fields},
BorshSchema,
};
use super::common_macro::schema_imports::*;

#[allow(unused)]
#[derive(BorshSchema)]
Expand All @@ -230,7 +211,7 @@ mod schema {
let mut defs = Default::default();
XY::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"XY" => Definition::Enum {
tag_width: 1,
variants: vec![
Expand Down Expand Up @@ -282,7 +263,7 @@ mod schema {
let mut defs = Default::default();
XYNoDiscriminant::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"XYNoDiscriminant" => Definition::Enum {
tag_width: 1,
variants: vec![
Expand Down
36 changes: 12 additions & 24 deletions borsh/tests/test_rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,23 @@ fn test_slice_arc() {
assert_eq!(original, &*deserialized);
}

#[macro_use]
mod common_macro;

#[cfg(feature = "unstable__schema")]
mod schema {
use super::{rc, sync};
use alloc::{
collections::BTreeMap,
string::{String, ToString},
};
use borsh::schema::{BorshSchema, Definition};
macro_rules! map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);
use super::common_macro::schema_imports::*;
use alloc::{rc, sync};

fn common_map_i32() -> BTreeMap<String, Definition> {
map! {
schema_map! {

"i32" => Definition::Primitive(4)
}
}

fn common_map_slice_i32() -> BTreeMap<String, Definition> {
map! {
schema_map! {
"Vec<i32>" => Definition::Sequence {
length_width: Definition::DEFAULT_LENGTH_WIDTH,
length_range: Definition::DEFAULT_LENGTH_RANGE,
Expand All @@ -86,31 +74,31 @@ mod schema {
fn test_rc() {
assert_eq!("i32", <rc::Rc<i32> as BorshSchema>::declaration());

let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<rc::Rc<i32> as BorshSchema>::add_definitions_recursively(&mut actual_defs);
assert_eq!(common_map_i32(), actual_defs);
}

#[test]
fn test_slice_rc() {
assert_eq!("Vec<i32>", <rc::Rc<[i32]> as BorshSchema>::declaration());
let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<rc::Rc<[i32]> as BorshSchema>::add_definitions_recursively(&mut actual_defs);
assert_eq!(common_map_slice_i32(), actual_defs);
}

#[test]
fn test_arc() {
assert_eq!("i32", <sync::Arc<i32> as BorshSchema>::declaration());
let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<sync::Arc<i32> as BorshSchema>::add_definitions_recursively(&mut actual_defs);
assert_eq!(common_map_i32(), actual_defs);
}

#[test]
fn test_slice_arc() {
assert_eq!("Vec<i32>", <sync::Arc<[i32]> as BorshSchema>::declaration());
let mut actual_defs = map!();
let mut actual_defs = schema_map!();
<sync::Arc<[i32]> as BorshSchema>::add_definitions_recursively(&mut actual_defs);
assert_eq!(common_map_slice_i32(), actual_defs);
}
Expand Down
41 changes: 9 additions & 32 deletions borsh/tests/test_schema_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,12 @@
#![allow(dead_code)] // Local structures do not have their fields used.
#![cfg(feature = "unstable__schema")]

use core::fmt::{Debug, Display};
#[cfg(feature = "std")]
use std::collections::BTreeMap;

#[cfg(not(feature = "std"))]
extern crate alloc;
#[cfg(not(feature = "std"))]
use alloc::{
collections::BTreeMap,
format,
string::{String, ToString},
vec,
};

use borsh::schema::*;
use borsh::{try_from_slice_with_schema, try_to_vec_with_schema};
use core::fmt::{Debug, Display};

macro_rules! map(
() => { BTreeMap::new() };
{ $($key:expr => $value:expr),+ } => {
{
let mut m = BTreeMap::new();
$(
m.insert($key.to_string(), $value);
)+
m
}
};
);
#[macro_use]
mod common_macro;
use common_macro::schema_imports::*;

#[test]
pub fn simple_enum() {
Expand All @@ -50,7 +27,7 @@ pub fn simple_enum() {
let mut defs = Default::default();
A::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"ABacon" => Definition::Struct{ fields: Fields::Empty },
"AEggs" => Definition::Struct{ fields: Fields::Empty },
"A" => Definition::Enum {
Expand All @@ -72,7 +49,7 @@ pub fn single_field_enum() {
let mut defs = Default::default();
A::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"ABacon" => Definition::Struct {fields: Fields::Empty},
"A" => Definition::Enum {
tag_width: 1,
Expand Down Expand Up @@ -184,7 +161,7 @@ pub fn complex_enum_with_schema() {
let mut defs = Default::default();
A::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"Cucumber" => Definition::Struct {fields: Fields::Empty},
"ASalad" => Definition::Struct{ fields: Fields::UnnamedFields(vec!["Tomatoes".to_string(), "Cucumber".to_string(), "Oil".to_string()])},
"ABacon" => Definition::Struct {fields: Fields::Empty},
Expand Down Expand Up @@ -244,7 +221,7 @@ pub fn complex_enum_generics() {
let mut defs = Default::default();
<A<Cucumber, Wrapper>>::add_definitions_recursively(&mut defs);
assert_eq!(
map! {
schema_map! {
"Cucumber" => Definition::Struct {fields: Fields::Empty},
"ASalad<Cucumber>" => Definition::Struct{
fields: Fields::UnnamedFields(vec!["Tomatoes".to_string(), "Cucumber".to_string(), "Oil".to_string()])
Expand Down Expand Up @@ -276,7 +253,7 @@ pub fn complex_enum_generics() {
}

fn common_map() -> BTreeMap<String, Definition> {
map! {
schema_map! {
"EnumParametrized<String, u32, i8, u16>" => Definition::Enum {
tag_width: 1,
variants: vec![
Expand Down
Loading

0 comments on commit 972a302

Please sign in to comment.