From 24816dc3eaf1eb4c67f5dd9d7c3114a7d8cd183e Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 25 Jun 2018 23:08:03 -0400 Subject: [PATCH] Fix #2 --- src/lib.rs | 16 ++++++---------- src/tests.rs | 8 ++++++++ 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 src/tests.rs diff --git a/src/lib.rs b/src/lib.rs index eff279f..f8a52ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,9 @@ use std::vec::Vec; mod weekday; +#[cfg(test)] +mod tests; + use weekday::day_of_week; use weekday::DayOfWeek; @@ -64,7 +67,7 @@ impl From for ParseInternalError { } } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum ParseError { AmbiguousWeekday, InternalError(ParseInternalError), @@ -471,7 +474,6 @@ fn days_in_month(year: i32, month: i32) -> Result { 1 | 3 | 5 | 7 | 8 | 10 | 12 => Ok(31), 4 | 6 | 9 | 11 => Ok(30), _ => { - println!("Invalid month: {}", month); Err(ParseError::InvalidMonth) } } @@ -523,8 +525,6 @@ impl YMD { fn append(&mut self, val: i32, token: &str, label: Option) -> ParseIResult<()> { let mut label = label; - println!("Val: {}, Token: {}", val, token); - // Python auto-detects strings using the '__len__' function here. // We instead take in both and handle as necessary. if Decimal::from_str(token).is_ok() && token.len() > 2 { @@ -725,7 +725,7 @@ impl YMD { } } -#[derive(Default, Debug)] +#[derive(Default, Debug, PartialEq)] pub struct ParsingResult { year: Option, month: Option, @@ -989,7 +989,6 @@ impl Parser { let d_offset = if res.weekday.is_some() && res.day.is_none() { // TODO: Unwrap not justified let dow = day_of_week(y as u32, m, default.day()).unwrap(); - println!("dow: {:?}", dow); // UNWRAP: We've already check res.weekday() is some let actual_weekday = (res.weekday.unwrap() + 1) % 7; @@ -1003,11 +1002,9 @@ impl Parser { let mut d = NaiveDate::from_ymd( y, m, - min(res.day.unwrap_or(default.day() as i32) as u32, days_in_month(y, m as i32).unwrap()) + min(res.day.unwrap_or(default.day() as i32) as u32, days_in_month(y, m as i32)?) ); - println!("d: {:?}, d_offset: {:?}", d, d_offset); - let d = d + d_offset; let t = NaiveTime::from_hms_micro( @@ -1291,7 +1288,6 @@ impl Parser { let sec_remainder = value - value.floor(); if sec_remainder != *ZERO { - println!("{}", *SIXTY * sec_remainder); second = Some((*SIXTY * sec_remainder).floor().to_i64().unwrap() as i32); } diff --git a/src/tests.rs b/src/tests.rs new file mode 100644 index 0000000..fc9a0f0 --- /dev/null +++ b/src/tests.rs @@ -0,0 +1,8 @@ +use ParseError; +use parse; + +#[test] +fn test_fuzz() { + + assert_eq!(parse("\x2D\x38\x31\x39\x34\x38\x34"), Err(ParseError::InvalidMonth)); +} \ No newline at end of file