nih-plug icon indicating copy to clipboard operation
nih-plug copied to clipboard

how to write tests that depend on specific parameter values?

Open universalhandle opened this issue 1 year ago • 0 comments

Hello. I'm new to Rust and audio plugin development, so forgive me if my questions are... basic? academic?

Anyway, while user-testing my plugin I discovered some behavior that suggested some of my functions were not returning what I expected. I wrote a few unit tests and found the problem. Encouraged by the immediate return on investment, I decided to write a bunch more tests.

For some of the tests, I don't need the plugin at all. For others, it suffices to call MyPlugin::default(). But I couldn't figure out how to instantiate the plugin with parameter values other than the default.

Here's a use case: MyPlugin has a chance parameter. If the user sets it to the minimum value, input will never be processed. I thought it would be useful to short-circuit some of the logic in my process method, so I wrote some code like this (abridged):

impl MyPlugin {
    fn is_configured_to_act(&self) -> bool {
        // If the probability of intervention is set to zero...
        if self.params.chance.value() == self.params.chance.default_plain_value()
        // ...then there is nothing for this plugin to do, because the output would always exactly match the input.
        {
            false
        } else {
            true
        }
    }
}

impl Plugin for MyPlugin {
    fn process(
        &mut self,
        _buffer: &mut Buffer,
        _aux: &mut AuxiliaryBuffers,
        context: &mut impl ProcessContext<Self>,
    ) -> ProcessStatus {
        while let Some(event) = context.next_event() {
            match event {
                NoteEvent::NoteOn {
                    timing,
                    voice_id,
                    channel,
                    note,
                    velocity,
                } => {
                    if self.is_configured_to_act() {
                       // do stuff
                    }
            }
            // ...
       }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn does_act_if_chance_is_nonzero() {
        let plugin = MyPlugin::default();
        // somehow change the param values programmatically?
        assert!(plugin.is_configured_to_act());
    }
}

How would I write this test?

universalhandle avatar Nov 03 '24 13:11 universalhandle