Rust tests
A reference of the soundor-testing skill.
soundor-testing reads this reference when a task needs it. Source on GitHub
The crate in lib/ is tested with Cargo and the SDK's harness. A new
project already has lib/tests/processor.rs, controller.rs and
realtime.rs.
npm run test:rust # gen + cargo test
cargo test --manifest-path lib/Cargo.toml <name filter>
cargo clippy --all-targets --manifest-path lib/Cargo.toml -- -D warnings
cargo fmt --check --manifest-path lib/Cargo.tomlRun npm run gen first after config or package changes: the SDK the tests
use is generated. Commit lib/Cargo.lock.
Processor
use my_plugin::PluginProcessor;
use soundor::testing::TestProcessor;
#[test]
fn gain_halves_the_signal() {
let mut processor = TestProcessor::<PluginProcessor>::new()
.sample_rate(48_000.0)
.block_size(64)
.channels(2)
.parameter("gain", 0.5)
.build();
let input = vec![1.0_f32; 256];
let output = processor.process(&[input.clone(), input]);
// A smoothed gain settles within the first block; check after it.
assert!(output[0][64..].iter().all(|&s| (s - 0.5).abs() < 1e-6));
}Cover: expected gain and frequency response (measure sines with RMS, skipping the settling time), silence, extreme parameter values producing finite output, sample rates from 44.1 to 192 kHz, block sizes from 1 to the maximum (whole and split processing must match), each declared layout, reset, separate instances, and zero allocations:
use soundor::testing::{CountingAllocator, TestProcessor};
#[global_allocator]
static ALLOCATOR: CountingAllocator = CountingAllocator;
#[test]
fn processing_does_not_allocate() {
let mut processor = TestProcessor::<my_plugin::PluginProcessor>::new()
.block_size(512)
.build();
let input = vec![0.25_f32; 4096];
processor.process(&[input.clone(), input.clone()]);
processor.parameters_mut().gain = 0.9;
processor.process(&[input.clone(), input]);
assert_eq!(processor.allocations(), 0);
}Exercise every processing path in the allocation test (each mode, bypass, parameter changes). The count covers allocator calls, not locks or blocking: it is necessary, not sufficient.
Controller
TestController runs native methods as a runtime does. See the generated
lib/tests/controller.rs and the SDK's testing.rs for its API in the
installed version: call methods, inspect parameters and state on its
TestHost, await async methods, and share resources with a
TestProcessor to check that what the controller publishes is what the
processor reads.
Fixtures
Expected values come from the design (formulas in the test, with a link to
their source) or from fixtures generated once, checked independently, and
stored in lib/tests/fixtures/. Never paste the processor's current output
in as the expectation without checking it.