mirror of
https://github.com/bspeice/dtparse
synced 2024-11-14 09:58:09 -05:00
Continue adding tests
This commit is contained in:
parent
1cb981b8f6
commit
23865d64dd
38
src/lib.rs
38
src/lib.rs
@ -590,13 +590,23 @@ impl YMD {
|
|||||||
|
|
||||||
// TODO: Why do I have to clone &usize? Isn't it Copy?
|
// TODO: Why do I have to clone &usize? Isn't it Copy?
|
||||||
Ok((
|
Ok((
|
||||||
Some(self._ymd[strids.get(&YMDLabel::Year).unwrap().clone()]),
|
strids
|
||||||
Some(self._ymd[strids.get(&YMDLabel::Month).unwrap().clone()]),
|
.get(&YMDLabel::Year)
|
||||||
Some(self._ymd[strids.get(&YMDLabel::Day).unwrap().clone()]),
|
.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 len_ymd = self._ymd.len();
|
||||||
let mut year: Option<i32> = None;
|
let mut year: Option<i32> = None;
|
||||||
let mut month: Option<i32> = None;
|
let mut month: Option<i32> = None;
|
||||||
@ -612,7 +622,9 @@ impl YMD {
|
|||||||
.map(|u| strids.insert(YMDLabel::Day, u.clone()));
|
.map(|u| strids.insert(YMDLabel::Day, u.clone()));
|
||||||
|
|
||||||
// TODO: More Rustiomatic way of doing this?
|
// 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);
|
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) {
|
} else if len_ymd == 1 || (self.mstridx.is_some() && len_ymd == 2) {
|
||||||
if self.mstridx.is_some() {
|
if self.mstridx.is_some() {
|
||||||
month = Some(self._ymd[self.mstridx.unwrap()]);
|
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 {
|
} else {
|
||||||
other = Some(self._ymd[0]);
|
other = Some(self._ymd[0]);
|
||||||
}
|
}
|
||||||
@ -983,7 +999,6 @@ impl Parser {
|
|||||||
res: &ParsingResult,
|
res: &ParsingResult,
|
||||||
default: &NaiveDateTime,
|
default: &NaiveDateTime,
|
||||||
) -> ParseResult<Option<FixedOffset>> {
|
) -> ParseResult<Option<FixedOffset>> {
|
||||||
|
|
||||||
// TODO: Actual timezone support
|
// TODO: Actual timezone support
|
||||||
if res.tzname.is_none() && res.tzoffset.is_none() || res.tzname == Some(" ".to_owned()) {
|
if res.tzname.is_none() && res.tzoffset.is_none() || res.tzname == Some(" ".to_owned()) {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@ -1173,9 +1188,7 @@ impl Parser {
|
|||||||
hms_idx = Some(idx + 2)
|
hms_idx = Some(idx + 2)
|
||||||
} else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() {
|
} else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() {
|
||||||
hms_idx = Some(idx - 1)
|
hms_idx = Some(idx - 1)
|
||||||
}
|
} else if len_l > 0 && idx > 0 && idx == len_l - 1 && tokens[idx - 1] == " "
|
||||||
// TODO: The condition for this in Python seems a bit ambiguous
|
|
||||||
else if idx == len_l - 1 && tokens[idx - 1] == " "
|
|
||||||
&& info.get_hms(&tokens[idx - 2]).is_some()
|
&& info.get_hms(&tokens[idx - 2]).is_some()
|
||||||
{
|
{
|
||||||
hms_idx = Some(idx - 2)
|
hms_idx = Some(idx - 2)
|
||||||
@ -1262,7 +1275,10 @@ fn parse_with_info(
|
|||||||
parser.parse(timestr, default, false, vec![])
|
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))?;
|
let parse_result = parse_with_info(timestr, ParserInfo::default(), Some(default))?;
|
||||||
Ok((parse_result.0, parse_result.1))
|
Ok((parse_result.0, parse_result.1))
|
||||||
}
|
}
|
||||||
|
54
src/tests.rs
54
src/tests.rs
@ -2,18 +2,18 @@ use chrono::Datelike;
|
|||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use chrono::NaiveTime;
|
use chrono::NaiveTime;
|
||||||
|
use pyo3::FromPyObject;
|
||||||
use pyo3::ObjectProtocol;
|
use pyo3::ObjectProtocol;
|
||||||
use pyo3::PyDict;
|
use pyo3::PyDict;
|
||||||
use pyo3::PyList;
|
use pyo3::PyList;
|
||||||
use pyo3::PyObject;
|
use pyo3::PyObject;
|
||||||
use pyo3::PyObjectRef;
|
use pyo3::PyObjectRef;
|
||||||
use pyo3::Python;
|
use pyo3::Python;
|
||||||
use pyo3::FromPyObject;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use tokenize;
|
|
||||||
use parse;
|
use parse;
|
||||||
use parse_with_default;
|
use parse_with_default;
|
||||||
|
use tokenize;
|
||||||
|
|
||||||
macro_rules! test_split {
|
macro_rules! test_split {
|
||||||
($py:ident, $timelex:ident, $s:expr) => {
|
($py:ident, $timelex:ident, $s:expr) => {
|
||||||
@ -45,8 +45,15 @@ fn test_split() {
|
|||||||
macro_rules! test_parse_naive {
|
macro_rules! test_parse_naive {
|
||||||
// Handle tests where the times involved are unambiguous
|
// Handle tests where the times involved are unambiguous
|
||||||
($py:ident, $parser:ident, $s:expr) => {
|
($py:ident, $parser:ident, $s:expr) => {
|
||||||
let dt: PyObject = $parser.call_method1("parse", $s).unwrap().extract().unwrap();
|
let dt: PyObject = $parser
|
||||||
let dt_s: String = dt.call_method1($py, "isoformat", " ").unwrap().extract($py).unwrap();
|
.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);
|
let s = format!("{}", dt_s);
|
||||||
|
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
@ -71,8 +78,15 @@ macro_rules! test_parse_naive {
|
|||||||
let mut kwargs = HashMap::new();
|
let mut kwargs = HashMap::new();
|
||||||
kwargs.insert("default", pydefault);
|
kwargs.insert("default", pydefault);
|
||||||
|
|
||||||
let dt: PyObject = $parser.call_method("parse", $s, kwargs).unwrap().extract().unwrap();
|
let dt: PyObject = $parser
|
||||||
let dt_s: String = dt.call_method1($py, "isoformat", " ").unwrap().extract($py).unwrap();
|
.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 s = format!("{}", dt_s);
|
||||||
|
|
||||||
let r_rs = parse_with_default($s, $d);
|
let r_rs = parse_with_default($s, $d);
|
||||||
@ -84,7 +98,7 @@ macro_rules! test_parse_naive {
|
|||||||
let rs = r_rs.unwrap();
|
let rs = r_rs.unwrap();
|
||||||
assert_eq!(rs.1, None);
|
assert_eq!(rs.1, None);
|
||||||
assert_eq!(s, format!("{}", rs.0));
|
assert_eq!(s, format!("{}", rs.0));
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -107,10 +121,32 @@ fn test_dateutil_compat() {
|
|||||||
let parser = py.import("dateutil.parser").unwrap();
|
let parser = py.import("dateutil.parser").unwrap();
|
||||||
let datetime = py.import("datetime").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
|
// 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
|
// testDateCommandFormatStrip2
|
||||||
test_parse_naive!(py, parser, "Thu Sep 25 10:36:28", datetime, &default);
|
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);
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user