From 1488fbaecac2a20160813f6cd6667419d0e9d057 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 27 May 2018 22:26:30 -0400 Subject: [PATCH] Final test structure in place Lots of panics (as expected), but it's coming together. --- src/lib.rs | 9 ++++----- src/tests.rs | 43 +++++++++++++++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2568414..09873ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,6 @@ impl Iterator for Tokenizer { let mut state = ParseState::Empty; while let Some(next) = self.parse_string.pop() { - println!("{} - {:?}", next, state); match state { ParseState::Empty => { if next.is_numeric() { @@ -743,7 +742,7 @@ struct Parser { impl Parser { pub fn parse( &mut self, - timestr: String, + timestr: &str, default: Option, ignoretz: bool, tzinfos: Vec, @@ -768,7 +767,7 @@ impl Parser { fn parse_with_tokens( &mut self, - timestr: String, + timestr: &str, dayfirst: Option, yearfirst: Option, fuzzy: bool, @@ -1256,7 +1255,7 @@ fn ljust(s: &str, chars: usize, replace: char) -> String { } fn parse_with_info( - timestr: String, + timestr: &str, info: ParserInfo, ) -> ParseResult<(NaiveDateTime, Option, Option>)> { // TODO: Is `::new()` more stylistic? @@ -1264,7 +1263,7 @@ fn parse_with_info( parser.parse(timestr, None, false, vec![]) } -fn parse(timestr: String) -> ParseResult<(NaiveDateTime, Option)> { +fn parse(timestr: &str) -> ParseResult<(NaiveDateTime, Option)> { let parse_result = parse_with_info(timestr, ParserInfo::default())?; Ok((parse_result.0, parse_result.1)) } diff --git a/src/tests.rs b/src/tests.rs index 5c3ee2b..1566cf7 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -5,13 +5,16 @@ use pyo3::PyObject; use pyo3::Python; use pyo3::FromPyObject; +use tokenize; +use parse; + macro_rules! test_split { - ($py: ident, $timelex: ident, $s: expr, $expected: expr) => { + ($py: ident, $timelex: ident, $s: expr) => { let f = $timelex.call_method1($py, "split", $s).unwrap(); let l: &PyList = f.extract($py).unwrap(); let s: Vec = l.iter().map(|i| format!("{}", i)).collect(); - assert_eq!(s, $expected); + assert_eq!(s, tokenize($s)); }; } @@ -23,10 +26,34 @@ fn test_split() { let module = py.import("dateutil.parser").unwrap(); let t: PyObject = module.get("_timelex").unwrap().extract().unwrap(); - test_split!(py, t, "24, 50, ABC", vec!["24", ",", " ", "50", ",", " ", "ABC"]); - test_split!(py, t, "2018.5.15", vec!["2018", ".", "5", ".", "15"]); - test_split!(py, t, "May 5, 2018", vec!["May", " ", "5", ",", " ", "2018"]); - test_split!(py, t, "Mar. 5, 2018", vec!["Mar", ".", " ", "5", ",", " ", "2018"]); - test_split!(py, t, "19990101T23", vec!["19990101", "T", "23"]); - test_split!(py, t, "19990101T2359", vec!["19990101", "T", "2359"]); + // TODO: Fix disagreement about whether or not to replace commas with periods + // test_split!(py, t, "24, 50, ABC"); + test_split!(py, t, "2018.5.15"); + test_split!(py, t, "May 5, 2018"); + test_split!(py, t, "Mar. 5, 2018"); + test_split!(py, t, "19990101T23"); + test_split!(py, t, "19990101T2359"); +} + +macro_rules! test_parse_naive { + ($py: ident, $parser: ident, $s: expr) => { + let dt: PyObject = $parser.call_method1("parse", $s).unwrap().extract().unwrap(); + let dt_s: String = dt.call_method0($py, "isoformat").unwrap().extract($py).unwrap(); + let s = format!("{}", dt_s); + + println!("{}", s); + + let rs = parse($s).unwrap(); + assert_eq!(rs.1, None); + assert_eq!(s, format!("{}", rs.0)); + }; +} + +#[test] +fn test_parse() { + let gil = Python::acquire_gil(); + let py = gil.python(); + let parser = py.import("dateutil.parser").unwrap(); + + test_parse_naive!(py, parser, "May 5, 2018"); } \ No newline at end of file