Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split test_parse #1170

Merged
merged 5 commits into from
Jul 7, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 51 additions & 21 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,28 +564,26 @@ mod tests {
use crate::format::*;
use crate::{DateTime, FixedOffset, TimeZone, Timelike, Utc};

#[test]
fn test_parse() {
use crate::format::InternalInternal::*;
use crate::format::Item::{Literal, Space};
use crate::format::Numeric::*;
fn parse_all(s: &str, items: &[Item]) -> ParseResult<Parsed> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: helper below the tests, please.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll learn at some point 😄

let mut parsed = Parsed::new();
parse(&mut parsed, s, items.iter())?;
Ok(parsed)
}

fn parse_all(s: &str, items: &[Item]) -> ParseResult<Parsed> {
let mut parsed = Parsed::new();
parse(&mut parsed, s, items.iter())?;
Ok(parsed)
}
macro_rules! check {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this particular change, but since you're cleaning this area up: does this need to be a macro?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You thought: let's ask something easy? 😆
I still use a macro to created the Parsed structs to compare against, but the test now uses more regular rust code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't ask you to change anything about it. 😋

($fmt:expr, $items:expr; $err:tt) => (
assert_eq!(parse_all($fmt, &$items), Err($err))
);
($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
let mut expected = Parsed::new();
$(expected.$k = Some($v);)*
assert_eq!(parse_all($fmt, &$items), Ok(expected))
});
}

macro_rules! check {
($fmt:expr, $items:expr; $err:tt) => (
assert_eq!(parse_all($fmt, &$items), Err($err))
);
($fmt:expr, $items:expr; $($k:ident: $v:expr),*) => (#[allow(unused_mut)] {
let mut expected = Parsed::new();
$(expected.$k = Some($v);)*
assert_eq!(parse_all($fmt, &$items), Ok(expected))
});
}
#[test]
fn test_parse_whitespace_and_literal() {
use crate::format::Item::{Literal, Space};

// empty string
check!("", []; );
Expand Down Expand Up @@ -634,6 +632,12 @@ mod tests {
check!("x y", [Literal("x"), Literal("y")]; INVALID);
check!("xy", [Literal("x"), Space(""), Literal("y")]; );
check!("x y", [Literal("x"), Space(""), Literal("y")]; );
}

#[test]
fn test_parse_numeric() {
use crate::format::Item::{Literal, Space};
use crate::format::Numeric::*;

// numeric
check!("1987", [num(Year)]; year: 1987);
Expand Down Expand Up @@ -712,6 +716,12 @@ mod tests {
[num(Hour), num(Minute), num(Second), num(Nanosecond), num(Timestamp)];
hour_div_12: 1, hour_mod_12: 11, minute: 45, second: 6, nanosecond: 78_901_234,
timestamp: 567_890_123);
}

#[test]
fn test_parse_fixed() {
use crate::format::Fixed;
use crate::format::Item::Literal;

// fixed: month and weekday names
check!("apr", [fixed(Fixed::ShortMonthName)]; month: 4);
Expand Down Expand Up @@ -762,6 +772,13 @@ mod tests {
check!("x", [fixed(Fixed::LowerAmPm)]; TOO_SHORT);
check!("xx", [fixed(Fixed::LowerAmPm)]; INVALID);
check!("", [fixed(Fixed::LowerAmPm)]; TOO_SHORT);
}

#[test]
fn test_parse_fixed_nanosecond() {
use crate::format::Fixed;
use crate::format::InternalInternal::*;
use crate::format::Numeric::*;

// fixed: dot plus nanoseconds
check!("", [fixed(Fixed::Nanosecond)]; ); // no field set, but not an error
Expand Down Expand Up @@ -817,8 +834,14 @@ mod tests {
check!("00000000x", [internal_fixed(Nanosecond9NoDot)]; INVALID);
check!(" 4", [internal_fixed(Nanosecond9NoDot)]; INVALID);
check!(".42100000", [internal_fixed(Nanosecond9NoDot)]; INVALID);
}

// fixed: timezone offsets
#[test]
fn test_parse_fixed_timezone_offset() {
use crate::format::Fixed;
use crate::format::InternalInternal::*;
use crate::format::Item::Literal;
use crate::format::Numeric::*;

// TimezoneOffset
check!("1", [fixed(Fixed::TimezoneOffset)]; INVALID);
Expand Down Expand Up @@ -1200,6 +1223,13 @@ mod tests {
check!("CEST ", [fixed(Fixed::TimezoneName)]; TOO_LONG);
check!(" CEST", [fixed(Fixed::TimezoneName)]; TOO_LONG);
check!("CE ST", [fixed(Fixed::TimezoneName)]; TOO_LONG);
}

#[test]
fn test_parse_practical_examples() {
use crate::format::InternalInternal::*;
use crate::format::Item::{Literal, Space};
use crate::format::Numeric::*;

// some practical examples
check!("2015-02-04T14:37:05+09:00",
Expand Down