Skip to content

Rustlings advanced_errs2测试是否不够严谨 #57

Open
@QIUZHILEI

Description

@QIUZHILEI

#[test]
fn test_empty() {
let res = "".parse::();
assert_eq!(res, Err(ParseClimateError::Empty));
assert_eq!(res.unwrap_err().to_string(), "empty input");
}

#[test]
fn test_short() {
let res = "Boston,1991".parse::();
assert_eq!(res, Err(ParseClimateError::BadLen));
assert_eq!(res.unwrap_err().to_string(), "incorrect number of fields");
}
应该在加一个测试,测试的res为 "Boston",原因如下:

如果写的模式匹配是这样的:

fn from_str(s: &str) -> Result<Self, Self::Err> {
let v: Vec<_> = s.split(',').collect();
let (city, year, temp) = match &v[..] {
[some_str] => return Err(ParseClimateError::Empty), //some_str 可能是"" 或 "Boston"
……
};
……
}
考虑以上匹配,如果按照原来的测试,给定的字符串是"",应该返回Empty 错误,但是如果给定的字符串是"Boston",那么也会匹配成功,但是应该返回的是BadLen错误

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions