Skip to content
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

Add unit test for uncovered regions #1149

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,120 @@ mod tests {
assert_eq!(space("a "), Err(INVALID));
}

#[test]
fn test_timezone_name_skip(){
let s = "\r";
let ans = crate::format::scan::timezone_name_skip(s);
assert!(ans.is_ok());
}

#[test]
fn test_timezone_offset_2822() {
let s = "cSt";
let ans = crate::format::scan::timezone_offset_2822(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", Some(-21600)))");

let s = "pSt";
let ans = crate::format::scan::timezone_offset_2822(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", Some(-28800)))");

let s = "mSt";
let ans = crate::format::scan::timezone_offset_2822(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", Some(-25200)))");

let s = "Gp";
let ans = crate::format::scan::timezone_offset_2822(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", None))");

let s = "-1551";
let ans = crate::format::scan::timezone_offset_2822(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", Some(-57060)))");
}

#[test]
fn test_short_or_long_month0(){
let s = "JUn";
CXWorks marked this conversation as resolved.
Show resolved Hide resolved
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 5))");

let s = "mAy";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 4))");

let s = "AuG";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 7))");

let s = "Aprâ";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"â\", 3))");

let s = "JUl";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 6))");

let s = "mAr";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 2))");

let s = "Jan";
let ans = crate::format::scan::short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 0))");
}

#[test]
fn test_short_or_long_weekday() {
let s = "sAtu";
let ans = crate::format::scan::short_or_long_weekday(s);
assert_eq!(format!("{:?}", ans), "Ok((\"u\", Sat))");

let s = "thu";
let ans = crate::format::scan::short_or_long_weekday(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", Thu))");
}

#[test]
fn test_nanosecond_fixed() {
let s = "";
let num = 0usize;
let ans = crate::format::scan::nanosecond_fixed(s, num);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 0))");

let s = "";
let num = 1usize;
let ans = crate::format::scan::nanosecond_fixed(s, num);
assert_eq!(format!("{:?}", ans), "Err(ParseError(TooShort))");
}

#[test]
fn test_nanosecond(){
let s = "2Ù";
let ans = crate::format::scan::nanosecond(s);
assert_eq!(format!("{:?}", ans), "Ok((\"Ù\", 200000000))");

let s = "8";
let ans = crate::format::scan::nanosecond(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 800000000))");
}

#[test]
fn test_equals(){
let s = b"\x5b";
let pattern = "[";
let ans = crate::format::scan::equals(s, pattern);
assert!(ans);

let s = b"\x0a\x5b\x4b";
let pattern = "[K";
let ans = crate::format::scan::equals(s, pattern);
assert!(!ans);

let s = b"\x00";
let pattern = "";
let ans = crate::format::scan::equals(s, pattern);
assert!(!ans);
}

#[test]
fn test_s_next() {
assert_eq!(s_next(""), "");
Expand Down
28 changes: 28 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2997,6 +2997,34 @@ mod tests {
}
}


#[test]
fn test_date_from_str2(){
let str = "-2-2-2";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(-0002-02-02)");

let str = "-1-1-2";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(-0001-01-02)");

let str = "4-4-4";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(0004-04-04)");

let str = "9";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Err(ParseError(TooShort))");

let str = "4-44-44";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Err(ParseError(OutOfRange))");

let str = "4-4-9";
let ans = <crate::naive::NaiveDate as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(0004-04-09)");
}

#[test]
fn test_date_parse_from_str() {
let ymd = |y, m, d| NaiveDate::from_ymd_opt(y, m, d).unwrap();
Expand Down
12 changes: 12 additions & 0 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1966,3 +1966,15 @@ where
assert!(from_str(r#"{"date":{"ymdf":20},"time":{"secs":0,"frac":0}}"#).is_err());
assert!(from_str(r#"null"#).is_err());
}


#[test]
fn test_from_str(){
let str = "6";
let ans = <NaiveDateTime as std::str::FromStr>::from_str(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be feasible to simplify the code for these by avoiding the fully-qualified trait method call syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks and I will manually address this issue for now. We need some time to address the importing issues.

assert_eq!(format!("{:?}", ans), "Err(ParseError(TooShort))");

let str = "+6666667006";
let ans = <NaiveDateTime as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Err(ParseError(OutOfRange))");
}
15 changes: 15 additions & 0 deletions src/naive/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1451,3 +1451,18 @@ where
assert!(from_str(r#"{"secs":0,"frac":0}"#).is_err());
assert!(from_str(r#"null"#).is_err());
}

#[test]
fn test_from_str(){
let str = "0:0:4.00000000000";
let ans = <NaiveTime as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(00:00:04)");

let str = "0:7:60";
let ans = <NaiveTime as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Ok(00:07:60)");

let str = "7:85:8";
let ans = <NaiveTime as std::str::FromStr>::from_str(str);
assert_eq!(format!("{:?}", ans), "Err(ParseError(OutOfRange))");
}
14 changes: 7 additions & 7 deletions src/offset/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ mod tests {
}
}

/// Test Issue #866
#[test]
fn test_issue_866() {
#[allow(deprecated)]
let local_20221106 = Local.ymd(2022, 11, 6);
let _dt_20221106 = local_20221106.and_hms_milli_opt(1, 2, 59, 1000).unwrap();
}
// /// Test Issue #866
// #[test]
// fn test_issue_866() {
// #[allow(deprecated)]
// let local_20221106 = Local.ymd(2022, 11, 6);
// let _dt_20221106 = local_20221106.and_hms_milli_opt(1, 2, 59, 1000).unwrap();
// }
}
15 changes: 15 additions & 0 deletions src/offset/local/tz_info/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,18 @@ enum Version {
/// Version 3
V3,
}

#[cfg(test)]
mod tests {

#[test]
fn test_parse(){
let data=b"\x54\x5a\x69\x66\x3d";
let ans = crate::offset::local::tz_info::parser::parse(data);
assert_eq!(format!("{:?}", ans), "Err(UnsupportedTzFile(\"unsupported TZif version\"))");

let data=b"\x0a\x0a\x0a\x0a";
let ans = crate::offset::local::tz_info::parser::parse(data);
assert_eq!(format!("{:?}", ans), "Err(InvalidTzFile(\"invalid magic number\"))");
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to keep files ending with a trailing newline (here and others).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks and I will manually address this issue.

48 changes: 48 additions & 0 deletions src/offset/local/tz_info/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,39 @@ mod tests {
Ok(())
}

#[test]
fn test_from_tz_string() {
let tz_string = b"hhh5hxx,5,1";
let use_string_extensions = false;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the offset::local::tz_info library code was imported and is more capable than is needed by the rest of the library. In general, I don't think adding coverage for exact Debug output is particularly valuable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback, could you specify the reason:

  1. offset::local::tz_info is already well-tested.
  2. Using the whole Debug info isn't valuable/precise enough
  3. Or other reasons?

Meanwhile, I assume this comment applies to all the added unit tests under src/offset/local/tz_info folder, is my understanding correct?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the whole Debug isn't precise enough, mostly, and I want to avoid having tests for code that isn't exercised by the outer API.

Meanwhile, I assume this comment applies to all the added unit tests under src/offset/local/tz_info folder, is my understanding correct?

Yes.

"Ok(Alternate(AlternateTime { std: LocalTimeType { ut_offset: -18000, is_dst: false, name: Some(\"hhh\") }, dst: LocalTimeType { ut_offset: -14400, is_dst: true, name: Some(\"hxx\") }, dst_start: Julian0WithLeap(5), dst_start_time: 7200, dst_end: Julian0WithLeap(1), dst_end_time: 7200 }))");

let tz_string = b"BBf3";
let use_string_extensions = false;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
"Ok(Fixed(LocalTimeType { ut_offset: -10800, is_dst: false, name: Some(\"BBf\") }))");

let tz_string = b"V-1,1/1:62[";
let use_string_extensions = false;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
"Err(InvalidTzString(\"invalid day time minute\"))");

let tz_string = b"2,2212";
let use_string_extensions = false;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
"Err(TransitionRule(\"invalid rule day julian day\"))");

let tz_string = b"hhh5,5,1";
let use_string_extensions = false;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
"Err(LocalTimeType(\"time zone name must have between 3 and 7 characters\"))");

let tz_string = b"3,M7.4.8l";
let use_string_extensions = true;
assert_eq!(format!("{:?}", TransitionRule::from_tz_string(tz_string, use_string_extensions)),
"Err(TransitionRule(\"invalid rule day week day\"))");
}

#[test]
fn test_v3_file() -> Result<(), Error> {
let bytes = b"TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x04\0\0\x1c\x20\0\0IST\0TZif3\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\0\0\0\0\x01\0\0\0\x01\0\0\0\x04\0\0\0\0\x7f\xe8\x17\x80\0\0\0\x1c\x20\0\0IST\0\x01\x01\x0aIST-2IDT,M3.4.4/26,M10.5.0\x0a";
Expand Down Expand Up @@ -1008,6 +1041,21 @@ mod tests {
Ok(())
}

#[test]
fn test_from_timespec() {
let unix_time = 8825501086245354106i64;
let ans = crate::offset::local::tz_info::rule::UtcDateTime::from_timespec(unix_time);
assert_eq!(format!("{:?}", ans), "Err(OutOfRange(\"i64 is out of range for i32\"))");

let unix_time = -9223372036846387200i64;
let ans = crate::offset::local::tz_info::rule::UtcDateTime::from_timespec(unix_time);
assert_eq!(format!("{:?}", ans), "Err(OutOfRange(\"out of range operation\"))");

let unix_time = 0;
let ans = crate::offset::local::tz_info::rule::UtcDateTime::from_timespec(unix_time);
assert_eq!(format!("{:?}", ans), "Ok(UtcDateTime { year: 1970, month: 1, month_day: 1, hour: 0, minute: 0, second: 0 })");
}

#[test]
fn test_transition_rule_overflow() -> Result<(), Error> {
let transition_rule_1 = TransitionRule::from(AlternateTime::new(
Expand Down
56 changes: 56 additions & 0 deletions src/offset/local/tz_info/timezone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,22 @@ mod tests {
Ok(())
}

#[test]
fn test_from_tz_data() -> Result<(), Error> {
let bytes = b"\x54\x5a\x69\x66\x00\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5\xb5";
let time_zone = TimeZone::from_tz_data(bytes);
assert_eq!(format!("{:?}", time_zone), "Err(InvalidTzFile(\"invalid header\"))");

let bytes = b"\x0a\x0a\xae\x0a";
let time_zone = TimeZone::from_tz_data(bytes);
assert_eq!(format!("{:?}", time_zone), "Err(InvalidTzFile(\"invalid magic number\"))");

let bytes = b"\x54\x5a\x69\x66\x0a";
let time_zone = TimeZone::from_tz_data(bytes);
assert_eq!(format!("{:?}", time_zone), "Err(UnsupportedTzFile(\"unsupported TZif version\"))");
Ok(())
}

#[test]
fn test_tz_ascii_str() -> Result<(), Error> {
assert!(matches!(TimeZoneName::new(b""), Err(Error::LocalTimeType(_))));
Expand Down Expand Up @@ -823,6 +839,46 @@ mod tests {
Ok(())
}

#[test]
fn test_time_zone_from_posix_tz_extra() -> Result<(), Error> {
//error cases
assert!(TimeZone::from_posix_tz("2,M2.2.").is_err());
assert!(TimeZone::from_posix_tz("8T1").is_err());
assert!(TimeZone::from_posix_tz("1,1,4").is_err());
assert!(TimeZone::from_posix_tz("2,M2.·/").is_err());
assert!(TimeZone::from_posix_tz("+1+").is_err());
assert!(TimeZone::from_posix_tz("1J").is_err());
assert!(TimeZone::from_posix_tz("1122").is_err());
assert!(TimeZone::from_posix_tz("aaa2,2,2").is_err());
assert!(TimeZone::from_posix_tz("2,M2.2.22÷").is_err());
assert!(TimeZone::from_posix_tz("2,M2").is_err());
assert!(TimeZone::from_posix_tz("/Ä").is_err());
assert!(TimeZone::from_posix_tz("1:211").is_err());
assert!(TimeZone::from_posix_tz("9:2:62").is_err());
assert!(TimeZone::from_posix_tz("2,2/29").is_err());
assert!(TimeZone::from_posix_tz("1,1,4$").is_err());
//ok cases
if let Ok(tz) = TimeZone::from_posix_tz("aaa3aaa3,2,22"){
assert_eq!(format!("{:?}", tz), "TimeZone { \
transitions: [], \
local_time_types: [LocalTimeType { ut_offset: -10800, is_dst: false, name: Some(\"aaa\") }, LocalTimeType { ut_offset: -10800, is_dst: true, name: Some(\"aaa\") }], \
leap_seconds: [], \
extra_rule: Some(Alternate(AlternateTime { std: LocalTimeType { ut_offset: -10800, is_dst: false, name: Some(\"aaa\") }, dst: LocalTimeType { ut_offset: -10800, is_dst: true, name: Some(\"aaa\") }, dst_start: Julian0WithLeap(2), dst_start_time: 7200, dst_end: Julian0WithLeap(22), dst_end_time: 7200 })) \
}");
}
if let Ok(tz) = TimeZone::from_posix_tz("TTT8"){
assert_eq!(format!("{:?}", tz), "TimeZone { \
transitions: [], \
local_time_types: [LocalTimeType { ut_offset: -28800, is_dst: false, name: Some(\"TTT\") }], \
leap_seconds: [], \
extra_rule: Some(Fixed(LocalTimeType { ut_offset: -28800, is_dst: false, name: Some(\"TTT\") })) \
}");
}


Ok(())
}

#[test]
fn test_leap_seconds() -> Result<(), Error> {
let time_zone = TimeZone::new(
Expand Down