How can i add a testcase for `impl From<Ref> for RefOr<Response>`

Looking for help, any reply would be appreciated.

pub struct Response {
    pub description: String
}

impl From<Ref> for RefOr<Response> {
    fn from(r: Ref) -> Self {
        Self::Ref(r)
    }
}

what are you supposed to test exactly?

testing in a strongly typed language is very different from testing in a weakly or dynamically typed language. for such simple function, isn't the type itself enough to guarantee the correctness?

for example, you might have a test in, say, javascript, that a function only accepts numbers, and will throw when given strings, but in rust, the type checker will guarantee you cannot give a string argument to an integer parameter.

2 Likes

A structure for implementing openapi in salvo.

The From trait is for infallible conversions, there's nothing to test here.

Basically i just want to get a RefOr<Response> from a Ref to cover these three lines. But i can't make it somehow.

So is there anyway to cover these lines of code?

What problem are you exactly facing? If i.e. Req is not publicly exposed from the crate (a.k.a. is private), then you can't reach it, which means that you can't test it.

there's nothing to test here, Self::Ref(r) is just a data constructor, you don't need to test a language feature. just like you don't need to test enum patterns are exhaustive.

100% line of code coverage is meaningless in situations like this, and due to optimizations like inlining or monomorphization, the line number mapping might not be accurate at all, not to mention the code emitted by macros etc.

in general, policies based on code lines coverage doesn't fit rust very well. you should either change your policy (at least, ditch the 100% coverage goal), or allow exceptions in the coverage calculation.

Thanks, i'll just leave these code alone.

It's a struct to generate api documentation. Not a really http request action. I'll just leave these code alone as you advice. Thanks a lot.