1
0
mirror of https://github.com/bspeice/dtparse synced 2024-12-22 04:18:09 -05:00

Final test structure in place

Lots of panics (as expected), but it's coming together.
This commit is contained in:
Bradlee Speice 2018-05-27 22:26:30 -04:00
parent a1d83b775d
commit 1488fbaeca
2 changed files with 39 additions and 13 deletions

View File

@ -122,7 +122,6 @@ impl Iterator for Tokenizer {
let mut state = ParseState::Empty; let mut state = ParseState::Empty;
while let Some(next) = self.parse_string.pop() { while let Some(next) = self.parse_string.pop() {
println!("{} - {:?}", next, state);
match state { match state {
ParseState::Empty => { ParseState::Empty => {
if next.is_numeric() { if next.is_numeric() {
@ -743,7 +742,7 @@ struct Parser {
impl Parser { impl Parser {
pub fn parse( pub fn parse(
&mut self, &mut self,
timestr: String, timestr: &str,
default: Option<NaiveDateTime>, default: Option<NaiveDateTime>,
ignoretz: bool, ignoretz: bool,
tzinfos: Vec<String>, tzinfos: Vec<String>,
@ -768,7 +767,7 @@ impl Parser {
fn parse_with_tokens( fn parse_with_tokens(
&mut self, &mut self,
timestr: String, timestr: &str,
dayfirst: Option<bool>, dayfirst: Option<bool>,
yearfirst: Option<bool>, yearfirst: Option<bool>,
fuzzy: bool, fuzzy: bool,
@ -1256,7 +1255,7 @@ fn ljust(s: &str, chars: usize, replace: char) -> String {
} }
fn parse_with_info( fn parse_with_info(
timestr: String, timestr: &str,
info: ParserInfo, info: ParserInfo,
) -> ParseResult<(NaiveDateTime, Option<FixedOffset>, Option<Vec<String>>)> { ) -> ParseResult<(NaiveDateTime, Option<FixedOffset>, Option<Vec<String>>)> {
// TODO: Is `::new()` more stylistic? // TODO: Is `::new()` more stylistic?
@ -1264,7 +1263,7 @@ fn parse_with_info(
parser.parse(timestr, None, false, vec![]) parser.parse(timestr, None, false, vec![])
} }
fn parse(timestr: String) -> ParseResult<(NaiveDateTime, Option<FixedOffset>)> { fn parse(timestr: &str) -> ParseResult<(NaiveDateTime, Option<FixedOffset>)> {
let parse_result = parse_with_info(timestr, ParserInfo::default())?; let parse_result = parse_with_info(timestr, ParserInfo::default())?;
Ok((parse_result.0, parse_result.1)) Ok((parse_result.0, parse_result.1))
} }

View File

@ -5,13 +5,16 @@ use pyo3::PyObject;
use pyo3::Python; use pyo3::Python;
use pyo3::FromPyObject; use pyo3::FromPyObject;
use tokenize;
use parse;
macro_rules! test_split { 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 f = $timelex.call_method1($py, "split", $s).unwrap();
let l: &PyList = f.extract($py).unwrap(); let l: &PyList = f.extract($py).unwrap();
let s: Vec<String> = l.iter().map(|i| format!("{}", i)).collect(); let s: Vec<String> = 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 module = py.import("dateutil.parser").unwrap();
let t: PyObject = module.get("_timelex").unwrap().extract().unwrap(); let t: PyObject = module.get("_timelex").unwrap().extract().unwrap();
test_split!(py, t, "24, 50, ABC", vec!["24", ",", " ", "50", ",", " ", "ABC"]); // TODO: Fix disagreement about whether or not to replace commas with periods
test_split!(py, t, "2018.5.15", vec!["2018", ".", "5", ".", "15"]); // test_split!(py, t, "24, 50, ABC");
test_split!(py, t, "May 5, 2018", vec!["May", " ", "5", ",", " ", "2018"]); test_split!(py, t, "2018.5.15");
test_split!(py, t, "Mar. 5, 2018", vec!["Mar", ".", " ", "5", ",", " ", "2018"]); test_split!(py, t, "May 5, 2018");
test_split!(py, t, "19990101T23", vec!["19990101", "T", "23"]); test_split!(py, t, "Mar. 5, 2018");
test_split!(py, t, "19990101T2359", vec!["19990101", "T", "2359"]); 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");
} }