-
Notifications
You must be signed in to change notification settings - Fork 456
feat: Improve robustness of DeviceId::from_str, add tests #1051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
I can't add reviewers but it would be sweet if @xephyris could have a look, especially regarding the addition of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The addition of webaudioworklet seems to work fine.
One question I do have is are the from_str() tests really necessary in this scenario?
The addition of the tests doesn't do any harm, but they also seem unnecessary to me as they don't particularly test the robustness of the DeviceId system but rather just test the match statement in from_str() which should always be valid except for in the case of a user error.
Just read your description and do see why such a test is valid.
Would it be possible though to make it so that the test uses something like a match statement that would fail in the case where an API has not been included in the test? That way it can easily noticed when testing, and users won't forget to update the test when adding new APIs.
I still think that the test_device_id_from_str, seems somewhat unnecessary, as its functionality seems to be encompassed in the second test, and it should always pass except for in the case of a user error.
I would be happy to hear your thoughts on this issue though.
|
Thanks a lot for your prompt feedback @xephyris.
I'll give that a try.
This is an embarrassing copy paste mistake. What I wanted is one test function for backwards compatibility and edge cases of Can't update the PR right now, but I'll get to it in the evening. |
9ce5555 to
e7d2cd1
Compare
|
One way we could do this would be with an empty match statement. While this definitely isn't the cleanest method to do things, I do believe it is the simplest and the easiest to understand. For your demo, something we can do instead of creating a whole new macro could be just like this: let test = TestEnum::A;
// Will produce error because TestEnum::C is missing
match test {
TestEnum::A => {},
TestEnum::B => {}
}which produces an error error[E0004]: non-exhaustive patterns: `TestEnum::C` not covered
--> src/main.rs:34:11
|
34 | match test {
| ^^^^ pattern `TestEnum::C` not covered
|
note: `TestEnum` defined hereallowing us to use the error statements provided by the Do you have any better ideas? |
e7d2cd1 to
819cc3c
Compare
|
|
||
| // Just a friendly reminder to add a test case | ||
| // for every DeviceId variant. | ||
| match DeviceId::Null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this @xephyris ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this seems to work nicely.
I think it would be better if
from_strreturned aErrrather than panicing when it encounters an unknown platform.A real life example where this could happen is when reading a device id from a config file that might have been written by a newer version or on a different platform.
I have also added some tests which then made me discover that
webaudioworkletwas missing from from_str.I assume that this wasn't intentional, so this commit adds this back as well.
The added tests aren't in a
#[cfg(test)]block because the other tests in this file weren't either, so I presume that is the desired style.