Skip to content

Commit

Permalink
Move tests into appropriate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Mar 22, 2022
1 parent 733aa10 commit ffb877e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 53 deletions.
53 changes: 0 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,56 +596,3 @@ pub use month::{Month, ParseMonthError};

mod traits;
pub use traits::{Datelike, Timelike};

mod test {
#[allow(unused_imports)]
use super::*;

#[test]
fn test_readme_doomsday() {
use num_iter::range_inclusive;

for y in range_inclusive(naive::MIN_DATE.year(), naive::MAX_DATE.year()) {
// even months
let d4 = NaiveDate::from_ymd(y, 4, 4);
let d6 = NaiveDate::from_ymd(y, 6, 6);
let d8 = NaiveDate::from_ymd(y, 8, 8);
let d10 = NaiveDate::from_ymd(y, 10, 10);
let d12 = NaiveDate::from_ymd(y, 12, 12);

// nine to five, seven-eleven
let d59 = NaiveDate::from_ymd(y, 5, 9);
let d95 = NaiveDate::from_ymd(y, 9, 5);
let d711 = NaiveDate::from_ymd(y, 7, 11);
let d117 = NaiveDate::from_ymd(y, 11, 7);

// "March 0"
let d30 = NaiveDate::from_ymd(y, 3, 1).pred();

let weekday = d30.weekday();
let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117];
assert!(other_dates.iter().all(|d| d.weekday() == weekday));
}
}

#[test]
fn test_month_enum_primitive_parse() {
use num_traits::FromPrimitive;

let jan_opt = Month::from_u32(1);
let feb_opt = Month::from_u64(2);
let dec_opt = Month::from_i64(12);
let no_month = Month::from_u32(13);
assert_eq!(jan_opt, Some(Month::January));
assert_eq!(feb_opt, Some(Month::February));
assert_eq!(dec_opt, Some(Month::December));
assert_eq!(no_month, None);

let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
assert_eq!(Month::from_u32(date.month()), Some(Month::October));

let month = Month::January;
let dt = Utc.ymd(2019, month.number_from_month(), 28).and_hms(9, 10, 11);
assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
}
}
22 changes: 22 additions & 0 deletions src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,28 @@ mod month_serde {
#[cfg(test)]
mod tests {
use super::Month;
use crate::{Datelike, TimeZone, Utc};

#[test]
fn test_month_enum_primitive_parse() {
use num_traits::FromPrimitive;

let jan_opt = Month::from_u32(1);
let feb_opt = Month::from_u64(2);
let dec_opt = Month::from_i64(12);
let no_month = Month::from_u32(13);
assert_eq!(jan_opt, Some(Month::January));
assert_eq!(feb_opt, Some(Month::February));
assert_eq!(dec_opt, Some(Month::December));
assert_eq!(no_month, None);

let date = Utc.ymd(2019, 10, 28).and_hms(9, 10, 11);
assert_eq!(Month::from_u32(date.month()), Some(Month::October));

let month = Month::January;
let dt = Utc.ymd(2019, month.number_from_month(), 28).and_hms(9, 10, 11);
assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
}

#[test]
fn test_month_enum_succ_pred() {
Expand Down
27 changes: 27 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1905,6 +1905,33 @@ mod tests {
use crate::{Datelike, Weekday};
use std::{i32, u32};

#[test]
fn test_readme_doomsday() {
use num_iter::range_inclusive;

for y in range_inclusive(MIN_DATE.year(), MAX_DATE.year()) {
// even months
let d4 = NaiveDate::from_ymd(y, 4, 4);
let d6 = NaiveDate::from_ymd(y, 6, 6);
let d8 = NaiveDate::from_ymd(y, 8, 8);
let d10 = NaiveDate::from_ymd(y, 10, 10);
let d12 = NaiveDate::from_ymd(y, 12, 12);

// nine to five, seven-eleven
let d59 = NaiveDate::from_ymd(y, 5, 9);
let d95 = NaiveDate::from_ymd(y, 9, 5);
let d711 = NaiveDate::from_ymd(y, 7, 11);
let d117 = NaiveDate::from_ymd(y, 11, 7);

// "March 0"
let d30 = NaiveDate::from_ymd(y, 3, 1).pred();

let weekday = d30.weekday();
let other_dates = [d4, d6, d8, d10, d12, d59, d95, d711, d117];
assert!(other_dates.iter().all(|d| d.weekday() == weekday));
}
}

#[test]
fn test_date_from_ymd() {
let ymd_opt = NaiveDate::from_ymd_opt;
Expand Down

0 comments on commit ffb877e

Please sign in to comment.