Skip to content

Testability of errors #69

Closed
Closed
@dtolnay

Description

@dtolnay

From rust-lang/rust#39381.


In order to test the error behaviour of a function you need an error struct to compare with. For example:
since ParseIntError is private, I have to generate one by doing something wrong, like u8::from_str("257"). This leads to the following test:

let pie_overflow = u8::from_str("257").unwrap_err();
assert_eq!(Missions::parse_enc_opt("ascii,256,U+41..U+67",8).unwrap_err(), pie_overflow);

If error types where public I would rather instantiate one and write:

let pie_overflow = ParseIntError { kind: std::num::Overflow };
assert_eq!(Missions::parse_enc_opt("ascii,256,U+41..U+67",8).unwrap_err(), pie_overflow);

Let's come up with a guideline that empowers library consumers to test failure cases conveniently.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions