Skip to content

Commit

Permalink
Add tests for size, alignment, trait impls
Browse files Browse the repository at this point in the history
This ensures that nothing will regress without knowing. As it turns out,
there was a missing implementation that was caught during this. As such,
`impl AddAssign<Duration> for `StdDuration` was added in this commit as
well.
  • Loading branch information
jhpratt committed Mar 22, 2022
1 parent 24c36af commit e95ea6d
Show file tree
Hide file tree
Showing 4 changed files with 1,025 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/duration.rs
Expand Up @@ -4,7 +4,7 @@ use core::cmp::Ordering;
use core::convert::{TryFrom, TryInto};
use core::fmt;
use core::iter::Sum;
use core::ops::{Add, Div, Mul, Neg, Sub, SubAssign};
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use core::time::Duration as StdDuration;

use crate::error;
Expand Down Expand Up @@ -837,6 +837,15 @@ impl Add<Duration> for StdDuration {

impl_add_assign!(Duration: Self, StdDuration);

impl AddAssign<Duration> for StdDuration {
fn add_assign(&mut self, rhs: Duration) {
*self = (*self + rhs).try_into().expect(
"Cannot represent a resulting duration in std. Try `let x = x + rhs;`, which will \
change the type.",
);
}
}

impl Neg for Duration {
type Output = Self;

Expand Down
11 changes: 11 additions & 0 deletions tests/integration/duration.rs
Expand Up @@ -471,6 +471,17 @@ fn add_assign_std() {
assert_eq!(duration, 0.seconds());
}

#[test]
fn std_add_assign() {
let mut duration = 1.std_seconds();
duration += 1.seconds();
assert_eq!(duration, 2.seconds());

let mut duration = 500.std_milliseconds();
duration += 500.milliseconds();
assert_eq!(duration, 1.seconds());
}

#[test]
fn neg() {
assert_eq!(-(1.seconds()), (-1).seconds());
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/main.rs
Expand Up @@ -28,7 +28,12 @@
unused_qualifications,
variant_size_differences
)]
#![allow(clippy::cognitive_complexity, clippy::clone_on_copy, clippy::cmp_owned)]
#![allow(
clippy::clone_on_copy,
clippy::cmp_owned,
clippy::cognitive_complexity,
clippy::missing_const_for_fn
)]

extern crate quickcheck_dep as quickcheck;

Expand All @@ -55,6 +60,7 @@ mod format_description;
mod formatting;
mod instant;
mod macros;
mod meta;
mod month;
mod offset_date_time;
mod parse_format_description;
Expand Down

0 comments on commit e95ea6d

Please sign in to comment.