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 4 commits
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
120 changes: 119 additions & 1 deletion src/format/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,11 @@ enum CommentState {

#[cfg(test)]
mod tests {
use super::{comment_2822, consume_colon_maybe, s_next, space, trim1};
use super::{
comment_2822, consume_colon_maybe, equals, nanosecond, nanosecond_fixed, s_next,
short_or_long_month0, short_or_long_weekday, space, timezone_name_skip,
timezone_offset_2822, trim1,
};
use crate::format::{INVALID, TOO_SHORT};

#[test]
Expand Down Expand Up @@ -463,6 +467,120 @@ mod tests {
assert_eq!(space("a "), Err(INVALID));
}

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

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

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

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

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

let s = "-1551";
let ans = 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 = short_or_long_month0(s);
assert_eq!(format!("{:?}", ans), "Ok((\"\", 5))");

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

#[test]
fn test_s_next() {
assert_eq!(s_next(""), "");
Expand Down
16 changes: 16 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,19 @@ enum Version {
/// Version 3
V3,
}

#[cfg(test)]
mod tests {
use crate::offset::local::tz_info::parser::parse;

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

let data = b"\x0a\x0a\x0a\x0a";
let ans = parse(data);
assert_eq!(format!("{:?}", ans), "Err(InvalidTzFile(\"invalid magic number\"))");
}
}
61 changes: 61 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,49 @@ 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 +1051,24 @@ 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
58 changes: 58 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,25 @@ 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 +842,45 @@ 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