insta_fun/macros.rs
1/// Macro for audio unit snapshot testing
2///
3/// This macro processes an audio unit with the given configuration,
4/// generates an SVG visualization, and asserts it against a stored snapshot.
5///
6/// ## Examples
7///
8/// ```
9/// use fundsp::prelude::*;
10/// use insta_fun::prelude::*;
11///
12/// // With a custom name
13/// let unit = saw_hz(220.0);
14/// assert_audio_unit_snapshot!("doc_sawtooth", unit);
15///
16/// ```
17///
18/// ```
19/// use fundsp::prelude::*;
20/// use insta_fun::prelude::*;
21///
22/// // With input source
23/// let unit = lowpass_hz(1000.0, 1.0);
24/// assert_audio_unit_snapshot!("doc_lowpass", unit, InputSource::impulse());
25/// ```
26///
27/// ```
28/// use fundsp::prelude::*;
29/// use insta_fun::prelude::*;
30///
31/// // With input source and custom config
32/// let config = SnapshotConfigBuilder::default().num_samples(512).build().unwrap();
33/// let unit = highpass_hz(2000.0, 0.7);
34/// assert_audio_unit_snapshot!("doc_highpass", unit, InputSource::sine(100.0, 44100.0), config);
35/// ```
36///
37/// ```
38/// use fundsp::prelude::*;
39/// use insta_fun::prelude::*;
40///
41/// // With unit and config
42/// let unit = lowpass_hz(1000.0, 1.0);
43/// let config = SnapshotConfigBuilder::default().num_samples(512).build().unwrap();
44/// assert_audio_unit_snapshot!(unit, config);
45///
46/// ```
47#[macro_export]
48macro_rules! assert_audio_unit_snapshot {
49 // With just the unit
50 ($unit:expr) => {{
51 let svg = $crate::snapshot::snapshot_audio_unit($unit);
52
53 ::insta::assert_binary_snapshot!(".svg", svg.as_bytes().to_vec());
54 }};
55
56 // With name and unit
57 ($name:literal, $unit:expr) => {{
58 let config = $crate::config::SnapshotConfigBuilder::default()
59 .chart_title($name)
60 .build()
61 .unwrap();
62 let svg = $crate::snapshot::snapshot_audio_unit_with_options($unit, config);
63
64 ::insta::assert_binary_snapshot!(&format!("{}.svg", $name), svg.as_bytes().to_vec());
65 }};
66
67 // With input source
68 ($name:literal, $unit:expr, $input:expr) => {{
69 let svg = $crate::snapshot::snapshot_audio_unit_with_input($unit, $input);
70
71 ::insta::assert_binary_snapshot!(&format!("{}.svg", $name), svg.as_bytes().to_vec());
72 }};
73
74 // With input source and config
75 ($name:literal, $unit:expr, $input:expr, $config:expr) => {{
76 let svg =
77 $crate::snapshot::snapshot_audio_unit_with_input_and_options($unit, $input, $config);
78
79 ::insta::assert_binary_snapshot!(&format!("{}.svg", $name), svg.as_bytes().to_vec());
80 }};
81
82 // With unit and config
83 ($unit:expr, $config:expr) => {{
84 let svg = $crate::snapshot::snapshot_audio_unit_with_options($unit, $config);
85
86 ::insta::assert_binary_snapshot!(".svg", svg.as_bytes().to_vec());
87 }};
88}