RustAudio / cpal

Cross-platform audio I/O library in pure Rust
Apache License 2.0
2.52k stars 338 forks source link

Mocking DeviceTrait #535

Open ghost opened 3 years ago

ghost commented 3 years ago

Hi

Thanks for making this library. I've hit a problem when trying to write unit tests for a function I've written that determines the best config to use for a given device. The function is generic over the DeviceTrait, so I started out by defining a mock struct that implements DeviceTrait and a mock struct that implements Iterator<Item = SupportedStreamConfigRange> for the SupportedInputConfigs and SupportedOutputConfigs types. However, I can't actually create a SupportedStreamConfigRange because it has no public constructors! Hence, I can't put dummy configs into my iterator to unit test my function.

Would it be possible to provide a way to create SupportedStreamConfigRanges in test code or make the Item of the iterators for SupportedInputConfigs and SupportedOutputConfigs on the DeviceTrait also a an associated type so I could mock it?

yamadapc commented 2 years ago

I also think it could be useful to expose some of the types that'd be required to mock cpal or to have some sort of virtual host for testing applications using cpal.

Otherwise the way around it would be to add a layer and mock that.

At the moment it seems like indeed it is not possible to implement the cpal::traits.

edit: *Thanks!