Skip to content

Commit 819cc3c

Browse files
committed
feat: Improve robustness of DeviceId::from_str, add tests
1 parent 3352bb5 commit 819cc3c

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

src/lib.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,11 @@ impl std::str::FromStr for DeviceId {
287287
}
288288
"jack" => Ok(DeviceId::Jack(data.to_string())),
289289
"webaudio" => Ok(DeviceId::WebAudio(data.to_string())),
290+
"webaudioworklet" => Ok(DeviceId::WebAudioWorklet(data.to_string())),
290291
"emscripten" => Ok(DeviceId::Emscripten(data.to_string())),
291292
"ios" => Ok(DeviceId::IOS(data.to_string())),
292293
"null" => Ok(DeviceId::Null),
293-
&_ => todo!("implement DeviceId::FromStr for {platform}"),
294+
_ => Err(DeviceIdError::UnsupportedPlatform),
294295
}
295296
}
296297
}
@@ -992,3 +993,64 @@ fn test_stream_instant() {
992993
);
993994
assert_eq!(max.add(Duration::from_secs(1)), None);
994995
}
996+
997+
#[test]
998+
fn test_device_id_from_str() {
999+
use std::str::FromStr;
1000+
let cases = [
1001+
("alsa:foo", Ok(DeviceId::ALSA("foo".to_string()))),
1002+
("foo:bar", Err(DeviceIdError::UnsupportedPlatform)),
1003+
("foobar", Err(DeviceIdError::BackendSpecific{
1004+
err: BackendSpecificError {
1005+
description: "Failed to parse device id from: foobar\nCheck if format matches Audio_API:DeviceId".to_owned()
1006+
},
1007+
})),
1008+
];
1009+
1010+
for (input, expected) in cases {
1011+
let actual = DeviceId::from_str(input);
1012+
assert_eq!(actual, expected, "{input:?}");
1013+
}
1014+
}
1015+
1016+
#[test]
1017+
fn test_device_id_display_from_str_identity() {
1018+
use std::str::FromStr;
1019+
1020+
// Just a friendly reminder to add a test case
1021+
// for every DeviceId variant.
1022+
match DeviceId::Null {
1023+
DeviceId::CoreAudio(_) => {}
1024+
DeviceId::WASAPI(_) => {}
1025+
DeviceId::ASIO(_) => {}
1026+
DeviceId::ALSA(_) => {}
1027+
DeviceId::AAudio(_) => {}
1028+
DeviceId::Jack(_) => {}
1029+
DeviceId::WebAudio(_) => {}
1030+
DeviceId::WebAudioWorklet(_) => {}
1031+
DeviceId::Emscripten(_) => {}
1032+
DeviceId::IOS(_) => {}
1033+
DeviceId::Null => {}
1034+
};
1035+
1036+
let cases = [
1037+
DeviceId::AAudio(42),
1038+
DeviceId::ALSA("foo".to_string()),
1039+
DeviceId::ASIO("bar".to_string()),
1040+
DeviceId::CoreAudio("baz".to_string()),
1041+
DeviceId::Emscripten("quux".to_string()),
1042+
DeviceId::IOS("alfa".to_string()),
1043+
DeviceId::Jack("bravo".to_string()),
1044+
DeviceId::WASAPI("charlie".to_string()),
1045+
DeviceId::WebAudio("delta".to_string()),
1046+
DeviceId::WebAudioWorklet("foxtrot".to_string()),
1047+
DeviceId::Null,
1048+
];
1049+
1050+
for input in cases {
1051+
let string = input.to_string();
1052+
let expected = Ok(input);
1053+
let actual = DeviceId::from_str(&string);
1054+
assert_eq!(expected, actual);
1055+
}
1056+
}

0 commit comments

Comments
 (0)