Skip to content

Commit

Permalink
clap: tests for entry point
Browse files Browse the repository at this point in the history
  • Loading branch information
micahrj committed Apr 2, 2024
1 parent cc98b2b commit 93b9aba
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/format/clap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ mod gui;
mod host;
mod instance;

#[cfg(test)]
mod tests;

#[doc(hidden)]
pub use factory::Factory;

Expand Down
133 changes: 133 additions & 0 deletions src/format/clap/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
use std::ffi::{c_char, CStr};
use std::io::{self, Read, Write};

use crate::buffers::Buffers;
use crate::editor::{Editor, Parent, Size};
use crate::events::Events;

use clap_sys::plugin_factory::{clap_plugin_factory, CLAP_PLUGIN_FACTORY_ID};
use clap_sys::version::CLAP_VERSION;

use crate::host::Host;
use crate::params::{ParamId, ParamValue};
use crate::plugin::{Plugin, PluginInfo};
use crate::process::{Config, Processor};

use super::{ClapInfo, ClapPlugin, Factory};

const NAME: &str = "test plugin";
const VERSION: &str = "1.2.3";
const VENDOR: &str = "test vendor";
const URL: &str = "https://example.com/";
const EMAIL: &str = "[email protected]";
const ID: &str = "com.example.plugin";

struct TestPlugin;

impl Plugin for TestPlugin {
type Processor = TestProcessor;
type Editor = TestEditor;

fn info() -> PluginInfo {
PluginInfo {
name: NAME.to_string(),
version: VERSION.to_string(),
vendor: VENDOR.to_string(),
url: URL.to_string(),
email: EMAIL.to_string(),
buses: Vec::new(),
layouts: vec![],
params: Vec::new(),
has_editor: false,
}
}
fn new(_host: Host) -> Self {
TestPlugin
}
fn set_param(&mut self, _id: ParamId, _value: ParamValue) {}
fn get_param(&self, _id: ParamId) -> ParamValue {
0.0
}
fn save(&self, _output: &mut impl Write) -> io::Result<()> {
Ok(())
}
fn load(&mut self, _input: &mut impl Read) -> io::Result<()> {
Ok(())
}
fn processor(&mut self, _config: Config) -> Self::Processor {
TestProcessor
}
fn editor(&mut self, _parent: Parent) -> Self::Editor {
TestEditor
}

#[allow(unused_variables)]
fn latency(&self, _config: &Config) -> u64 {
0
}
}

impl ClapPlugin for TestPlugin {
fn clap_info() -> ClapInfo {
ClapInfo { id: ID.to_string() }
}
}

struct TestProcessor;

impl Processor for TestProcessor {
fn reset(&mut self) {}
fn process(&mut self, _buffers: Buffers, _events: Events) {}
}

struct TestEditor;

impl Editor for TestEditor {
fn size(&self) -> Size {
Size {
width: 0.0,
height: 0.0,
}
}
fn param_changed(&mut self, _id: ParamId, _value: ParamValue) {}
}

unsafe fn str_from_ptr<'a>(ptr: *const c_char) -> Result<&'a str, std::str::Utf8Error> {
CStr::from_ptr(ptr).to_str()
}

#[test]
fn factory() {
let factory = Factory::<TestPlugin>::new();

let result = unsafe { factory.init() };
assert!(result);

let plugin_factory =
unsafe { factory.get(CLAP_PLUGIN_FACTORY_ID.as_ptr()) as *const clap_plugin_factory };

let plugin_count = unsafe { ((*plugin_factory).get_plugin_count).unwrap()(plugin_factory) };
assert_eq!(plugin_count, 1);

let desc_ptr = unsafe { ((*plugin_factory).get_plugin_descriptor).unwrap()(plugin_factory, 1) };
assert!(desc_ptr.is_null());

let desc_ptr = unsafe { ((*plugin_factory).get_plugin_descriptor).unwrap()(plugin_factory, 0) };
assert!(!desc_ptr.is_null());

let desc = unsafe { &*desc_ptr };
assert_eq!(desc.clap_version.major, CLAP_VERSION.major);
assert_eq!(desc.clap_version.minor, CLAP_VERSION.minor);
assert_eq!(desc.clap_version.revision, CLAP_VERSION.revision);
assert_eq!(unsafe { str_from_ptr(desc.id).unwrap() }, ID);
assert_eq!(unsafe { str_from_ptr(desc.name).unwrap() }, NAME);
assert_eq!(unsafe { str_from_ptr(desc.vendor).unwrap() }, VENDOR);
assert_eq!(unsafe { str_from_ptr(desc.url).unwrap() }, URL);
assert_eq!(unsafe { str_from_ptr(desc.manual_url).unwrap() }, "");
assert_eq!(unsafe { str_from_ptr(desc.support_url).unwrap() }, "");
assert_eq!(unsafe { str_from_ptr(desc.version).unwrap() }, VERSION);
assert_eq!(unsafe { str_from_ptr(desc.description).unwrap() }, "");
assert!(unsafe { *desc.features }.is_null());

unsafe { factory.deinit() };
}

0 comments on commit 93b9aba

Please sign in to comment.