Skip to content

Commit 0af10b4

Browse files
authored
Merge pull request #65 from Cakefish/rearrange-test-macro
Move constant parts of bauble_test macro out into a utility function
2 parents 8158b02 + bcc97d5 commit 0af10b4

1 file changed

Lines changed: 96 additions & 67 deletions

File tree

bauble/src/lib.rs

Lines changed: 96 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -34,90 +34,115 @@ pub use rust_decimal as decimal;
3434
#[doc(hidden)]
3535
pub mod private {
3636
pub use indexmap::IndexMap;
37-
}
38-
39-
// TODO(@docs)
40-
#[allow(missing_docs)]
41-
#[macro_export]
42-
macro_rules! bauble_test {
43-
( [$($ty:ty),* $(,)?] $source:literal [$($test_value:expr),* $(,)?]) => {
44-
{ $crate::bauble_test!(__TEST_CTX [$($ty),*] [$source] [$($test_value),*]); }
45-
};
46-
( [$($ty:ty),* $(,)?] [$($source:literal),* $(,)?] [$($test_value:expr),* $(,)?]) => {
47-
{ $crate::bauble_test!(__TEST_CTX [$($ty),*] [$($source),*] [$($test_value),*]); }
48-
};
49-
($ctx_static:ident [$($ty:ty),* $(,)?] [$($source:literal),* $(,)?] [$($test_value:expr),* $(,)?]) => {
50-
static $ctx_static: std::sync::OnceLock<std::sync::RwLock<$crate::BaubleContext>> = std::sync::OnceLock::new();
51-
{
52-
let file_sources = [$($source),*];
53-
let file_paths = if file_sources.len() == 1 {
54-
vec![$crate::path::TypePath::new(String::from("test")).unwrap()]
55-
} else {
56-
(0..file_sources.len()).map(|i| {
57-
$crate::path::TypePath::new(format!("test{i}")).unwrap()
58-
}).collect()
59-
};
60-
61-
let ctx = $ctx_static.get_or_init(|| {
62-
let mut ctx = $crate::BaubleContextBuilder::new();
63-
$(ctx.register_type::<$ty, _>();)*
64-
65-
let mut ctx = ctx.build();
66-
ctx.type_registry().validate(true).expect("Invalid type registry");
6737

68-
for (path, source) in file_paths.iter().zip(file_sources) {
69-
ctx.register_file(path.borrow(), format!("\n{}\n", source));
70-
}
38+
use crate::path::TypePath;
39+
use std::sync::{OnceLock, RwLock};
40+
41+
pub fn bauble_test_impl(
42+
ctx: &OnceLock<RwLock<crate::BaubleContext>>,
43+
file_sources: &[&str],
44+
register_types: &dyn Fn(&mut crate::BaubleContextBuilder),
45+
compare_objects: &dyn Fn(Vec<crate::Object>, &RwLock<crate::BaubleContext>),
46+
) {
47+
let file_paths = if file_sources.len() == 1 {
48+
vec![TypePath::new(String::from("test")).unwrap()]
49+
} else {
50+
(0..file_sources.len())
51+
.map(|i| TypePath::new(format!("test{i}")).unwrap())
52+
.collect()
53+
};
54+
55+
let ctx = ctx.get_or_init(|| {
56+
let mut ctx = crate::BaubleContextBuilder::new();
57+
register_types(&mut ctx);
58+
59+
let mut ctx = ctx.build();
60+
ctx.type_registry()
61+
.validate(true)
62+
.expect("Invalid type registry");
63+
64+
for (path, source) in file_paths.iter().zip(file_sources) {
65+
ctx.register_file(path.borrow(), format!("\n{}\n", source));
66+
}
7167

72-
std::sync::RwLock::new(ctx)
73-
});
68+
RwLock::new(ctx)
69+
});
7470

75-
// Test initial parsing from source
76-
let (objects, errors) = ctx.write().unwrap().load_all();
71+
// Test initial parsing from source
72+
let (objects, errors) = ctx.write().unwrap().load_all();
7773

78-
if !errors.is_empty() {
79-
$crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap());
80-
panic!("Error converting");
81-
}
74+
if !errors.is_empty() {
75+
crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap());
76+
panic!("Error converting");
77+
}
8278

83-
// Test round-trip of objects through source format
84-
let mut file_objects = ::std::collections::HashMap::new();
85-
for object in &objects {
86-
use $crate::SpannedValue;
87-
file_objects
88-
.entry(object.value.span().file())
89-
.or_insert(Vec::new())
90-
.push(object.clone());
91-
}
79+
// Test round-trip of objects through source format
80+
let mut file_objects = std::collections::HashMap::new();
81+
for object in &objects {
82+
use crate::SpannedValue;
83+
file_objects
84+
.entry(object.value.span().file())
85+
.or_insert(Vec::new())
86+
.push(object.clone());
87+
}
9288

93-
let re_path_sources = file_objects.iter().map(|(file_id, objects)| {
89+
let re_path_sources = file_objects
90+
.iter()
91+
.map(|(file_id, objects)| {
9492
let ctx = ctx.read().unwrap();
95-
let re_source = bauble::display_formatted(
93+
let re_source = crate::display_formatted(
9694
objects.as_slice(),
9795
ctx.type_registry(),
98-
&$crate::DisplayConfig {
99-
..$crate::DisplayConfig::default()
96+
&crate::DisplayConfig {
97+
..crate::DisplayConfig::default()
10098
},
10199
);
102100
(ctx.get_file_path(*file_id).to_owned(), re_source)
103101
})
104102
.collect::<Vec<_>>();
105103

106-
let (re_objects, errors) = ctx.write().unwrap().reload_paths(re_path_sources.iter().map(|(p, s)| (p.borrow(), s)));
104+
let (re_objects, errors) = ctx
105+
.write()
106+
.unwrap()
107+
.reload_paths(re_path_sources.iter().map(|(p, s)| (p.borrow(), s)));
107108

108-
if !errors.is_empty() {
109-
$crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap());
110-
for (path, re_source) in re_path_sources {
111-
eprintln!("In file \"{path}\": {re_source}");
112-
}
113-
panic!("Error re-converting");
109+
if !errors.is_empty() {
110+
crate::print_errors(Err::<(), _>(errors), &ctx.read().unwrap());
111+
for (path, re_source) in re_path_sources {
112+
eprintln!("In file \"{path}\": {re_source}");
114113
}
114+
panic!("Error re-converting");
115+
}
116+
117+
assert_eq!(objects, re_objects);
118+
119+
// Test that original parsed objects and round-trip objects convert into typed values
120+
// that match the provided test values.
121+
compare_objects(objects, ctx);
122+
compare_objects(re_objects, ctx);
123+
}
124+
}
125+
126+
// TODO(@docs)
127+
#[allow(missing_docs)]
128+
#[macro_export]
129+
macro_rules! bauble_test {
130+
( [$($ty:ty),* $(,)?] $source:literal [$($test_value:expr),* $(,)?]) => {
131+
{ $crate::bauble_test!(__TEST_CTX [$($ty),*] [$source] [$($test_value),*]); }
132+
};
133+
( [$($ty:ty),* $(,)?] [$($source:literal),* $(,)?] [$($test_value:expr),* $(,)?]) => {
134+
{ $crate::bauble_test!(__TEST_CTX [$($ty),*] [$($source),*] [$($test_value),*]); }
135+
};
136+
($ctx_static:ident [$($ty:ty),* $(,)?] [$($source:literal),* $(,)?] [$($test_value:expr),* $(,)?]) => {
137+
static $ctx_static: std::sync::OnceLock<std::sync::RwLock<$crate::BaubleContext>> = std::sync::OnceLock::new();
138+
{
139+
let file_sources = [$($source),*];
115140

116-
assert_eq!(objects, re_objects);
141+
let register_types = |ctx: &mut $crate::BaubleContextBuilder| {
142+
$(ctx.register_type::<$ty, _>();)*
143+
};
117144

118-
// Test that original parsed objects and round-trip objects convert into typed values
119-
// that match the provided test values.
120-
let compare_objects = |mut objects: Vec<$crate::Object>| {
145+
let compare_objects = |mut objects: Vec<$crate::Object>, ctx: &std::sync::RwLock<$crate::BaubleContext>| {
121146
let mut objects = objects.into_iter();
122147

123148
$(
@@ -139,8 +164,12 @@ macro_rules! bauble_test {
139164
}
140165
};
141166

142-
compare_objects(objects);
143-
compare_objects(re_objects);
167+
$crate::private::bauble_test_impl(
168+
&$ctx_static,
169+
&file_sources,
170+
&register_types,
171+
&compare_objects,
172+
)
144173
}
145174
};
146175
}

0 commit comments

Comments
 (0)