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 config = $crate::config::SnapshotConfigBuilder::default()
70            .chart_title($name)
71            .build()
72            .unwrap();
73        let svg =
74            $crate::snapshot::snapshot_audio_unit_with_input_and_options($unit, $input, config);
75
76        ::insta::assert_binary_snapshot!(&format!("{}.svg", $name), svg.as_bytes().to_vec());
77    }};
78
79    // With input source and config
80    ($name:literal, $unit:expr, $input:expr, $config:expr) => {{
81        let config = if $config.chart_title.is_some() {
82            $config
83        } else {
84            $crate::config::SnapshotConfig {
85                chart_title: Some($name.to_string()),
86                ..$config
87            }
88        };
89        let svg =
90            $crate::snapshot::snapshot_audio_unit_with_input_and_options($unit, $input, config);
91
92        ::insta::assert_binary_snapshot!(&format!("{}.svg", $name), svg.as_bytes().to_vec());
93    }};
94
95    // With unit and config
96    ($unit:expr, $config:expr) => {{
97        let svg = $crate::snapshot::snapshot_audio_unit_with_options($unit, $config);
98
99        ::insta::assert_binary_snapshot!(".svg", svg.as_bytes().to_vec());
100    }};
101}