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

Continue adding tests

This commit is contained in:
Bradlee Speice 2018-05-28 13:21:34 -04:00
parent 1cb981b8f6
commit 23865d64dd
2 changed files with 76 additions and 24 deletions

View File

@ -590,13 +590,23 @@ impl YMD {
// TODO: Why do I have to clone &usize? Isn't it Copy?
Ok((
Some(self._ymd[strids.get(&YMDLabel::Year).unwrap().clone()]),
Some(self._ymd[strids.get(&YMDLabel::Month).unwrap().clone()]),
Some(self._ymd[strids.get(&YMDLabel::Day).unwrap().clone()]),
strids
.get(&YMDLabel::Year)
.map(|i| self._ymd.get(*i).unwrap().clone()),
strids
.get(&YMDLabel::Month)
.map(|i| self._ymd.get(*i).unwrap().clone()),
strids
.get(&YMDLabel::Day)
.map(|i| self._ymd.get(*i).unwrap().clone()),
))
}
fn resolve_ymd(&mut self, yearfirst: bool, dayfirst: bool) -> ParseIResult<(Option<i32>, Option<i32>, Option<i32>)> {
fn resolve_ymd(
&mut self,
yearfirst: bool,
dayfirst: bool,
) -> ParseIResult<(Option<i32>, Option<i32>, Option<i32>)> {
let len_ymd = self._ymd.len();
let mut year: Option<i32> = None;
let mut month: Option<i32> = None;
@ -612,7 +622,9 @@ impl YMD {
.map(|u| strids.insert(YMDLabel::Day, u.clone()));
// TODO: More Rustiomatic way of doing this?
if self._ymd.len() == strids.len() && strids.len() > 0 || (self._ymd.len() == 3 && strids.len() == 2) {
if self._ymd.len() == strids.len() && strids.len() > 0
|| (self._ymd.len() == 3 && strids.len() == 2)
{
return self.resolve_from_stridxs(&mut strids);
};
@ -625,7 +637,11 @@ impl YMD {
} else if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) {
if self.mstridx.is_some() {
month = Some(self._ymd[self.mstridx.unwrap()]);
other = if len_ymd == 1 { Some(self._ymd[0]) } else { Some(self._ymd[1 - self.mstridx.unwrap()]) };
other = if len_ymd == 1 {
Some(self._ymd[0])
} else {
Some(self._ymd[1 - self.mstridx.unwrap()])
};
} else {
other = Some(self._ymd[0]);
}
@ -983,7 +999,6 @@ impl Parser {
res: &ParsingResult,
default: &NaiveDateTime,
) -> ParseResult<Option<FixedOffset>> {
// TODO: Actual timezone support
if res.tzname.is_none() && res.tzoffset.is_none() || res.tzname == Some(" ".to_owned()) {
Ok(None)
@ -1173,9 +1188,7 @@ impl Parser {
hms_idx = Some(idx + 2)
} else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() {
hms_idx = Some(idx - 1)
}
// TODO: The condition for this in Python seems a bit ambiguous
else if idx == len_l - 1 && tokens[idx - 1] == " "
} else if len_l > 0 && idx > 0 && idx == len_l - 1 && tokens[idx - 1] == " "
&& info.get_hms(&tokens[idx - 2]).is_some()
{
hms_idx = Some(idx - 2)
@ -1262,7 +1275,10 @@ fn parse_with_info(
parser.parse(timestr, default, false, vec![])
}
fn parse_with_default(timestr: &str, default: &NaiveDateTime) -> ParseResult<(NaiveDateTime, Option<FixedOffset>)> {
fn parse_with_default(
timestr: &str,
default: &NaiveDateTime,
) -> ParseResult<(NaiveDateTime, Option<FixedOffset>)> {
let parse_result = parse_with_info(timestr, ParserInfo::default(), Some(default))?;
Ok((parse_result.0, parse_result.1))
}

View File

@ -2,21 +2,21 @@ use chrono::Datelike;
use chrono::NaiveDate;
use chrono::NaiveDateTime;
use chrono::NaiveTime;
use pyo3::FromPyObject;
use pyo3::ObjectProtocol;
use pyo3::PyDict;
use pyo3::PyList;
use pyo3::PyObject;
use pyo3::PyObjectRef;
use pyo3::Python;
use pyo3::FromPyObject;
use std::collections::HashMap;
use tokenize;
use parse;
use parse_with_default;
use tokenize;
macro_rules! test_split {
($py: ident, $timelex: ident, $s: 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<String> = l.iter().map(|i| format!("{}", i)).collect();
@ -44,9 +44,16 @@ fn test_split() {
macro_rules! test_parse_naive {
// Handle tests where the times involved are unambiguous
($py: ident, $parser: ident, $s: expr) => {
let dt: PyObject = $parser.call_method1("parse", $s).unwrap().extract().unwrap();
let dt_s: String = dt.call_method1($py, "isoformat", " ").unwrap().extract($py).unwrap();
($py:ident, $parser:ident, $s:expr) => {
let dt: PyObject = $parser
.call_method1("parse", $s)
.unwrap()
.extract()
.unwrap();
let dt_s: String = dt.call_method1($py, "isoformat", " ")
.unwrap()
.extract($py)
.unwrap();
let s = format!("{}", dt_s);
println!("{}", s);
@ -63,7 +70,7 @@ macro_rules! test_parse_naive {
};
// Handle tests with some ambiguity, and thus needing a `default`
($py: ident, $parser: ident, $s: expr, $datetime: ident, $d: expr) => {
($py:ident, $parser:ident, $s:expr, $datetime:ident, $d:expr) => {
let rust_date = $d.date();
let dt_tuple = (rust_date.year(), rust_date.month(), rust_date.day());
let pydefault: &PyObjectRef = $datetime.call_method1("datetime", dt_tuple).unwrap();
@ -71,8 +78,15 @@ macro_rules! test_parse_naive {
let mut kwargs = HashMap::new();
kwargs.insert("default", pydefault);
let dt: PyObject = $parser.call_method("parse", $s, kwargs).unwrap().extract().unwrap();
let dt_s: String = dt.call_method1($py, "isoformat", " ").unwrap().extract($py).unwrap();
let dt: PyObject = $parser
.call_method("parse", $s, kwargs)
.unwrap()
.extract()
.unwrap();
let dt_s: String = dt.call_method1($py, "isoformat", " ")
.unwrap()
.extract($py)
.unwrap();
let s = format!("{}", dt_s);
let r_rs = parse_with_default($s, $d);
@ -84,7 +98,7 @@ macro_rules! test_parse_naive {
let rs = r_rs.unwrap();
assert_eq!(rs.1, None);
assert_eq!(s, format!("{}", rs.0));
}
};
}
#[test]
@ -107,10 +121,32 @@ fn test_dateutil_compat() {
let parser = py.import("dateutil.parser").unwrap();
let datetime = py.import("datetime").unwrap();
let default = NaiveDateTime::new(NaiveDate::from_ymd(2003, 9, 25), NaiveTime::from_hms(0, 0, 0));
let default = NaiveDateTime::new(
NaiveDate::from_ymd(2003, 9, 25),
NaiveTime::from_hms(0, 0, 0),
);
// testDateCommandFormatStrip1
test_parse_naive!(py, parser, "Thu Sep 25 10:36:28 2003");
test_parse_naive!(py, parser, "Thu Sep 25 10:36:28 2003", datetime, &default);
// testDateCommandFormatStrip2
test_parse_naive!(py, parser, "Thu Sep 25 10:36:28", datetime, &default);
}
// testDateCommandFormatStrip3
test_parse_naive!(py, parser, "Thu Sep 10:36:28", datetime, &default);
// testDateCommandFormatStrip4
test_parse_naive!(py, parser, "Thu 10:36:28", datetime, &default);
// testDateCommandFormatStrip5
test_parse_naive!(py, parser, "Sep 10:36:28", datetime, &default);
// testDateCommandFormatStrip6
test_parse_naive!(py, parser, "10:36:28", datetime, &default);
// testDateCommandFormatStrip7
test_parse_naive!(py, parser, "10:36", datetime, &default);
// testDateCommandFormatStrip8
test_parse_naive!(py, parser, "Thu Sep 25 2003", datetime, &default);
// TODO: What happened to testDateCommandFormatStrip9?
// testDateCommandFormatStrip10
test_parse_naive!(py, parser, "Sep 2003", datetime, &default);
// testDateCommandFormatStrip11
test_parse_naive!(py, parser, "Sep", datetime, &default);
// testDateCommandFormatStrip12
test_parse_naive!(py, parser, "2003", datetime, &default);
}