Skip to content

Commit

Permalink
Sarthak | Adds support for MinMaxAssertion within range in collections
Browse files Browse the repository at this point in the history
  • Loading branch information
SarthakMakhija committed Jan 16, 2024
1 parent d786f3e commit 17610a3
Show file tree
Hide file tree
Showing 2 changed files with 334 additions and 8 deletions.
263 changes: 255 additions & 8 deletions src/assertions/collection/min_max.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::fmt::Debug;
use std::ops::{Range, RangeInclusive};

use crate::matchers::collection::min_max::{have_max, have_min};
use crate::matchers::{Should, ShouldNot};
use crate::matchers::collection::min_max::{have_max, have_max_in_exclusive_range, have_max_in_inclusive_range, have_min, have_min_in_exclusive_range, have_min_in_inclusive_range};

pub trait MinMaxAssertion<T: Ord> {
fn should_have_min(&self, min: T) -> &Self;
Expand All @@ -10,7 +11,23 @@ pub trait MinMaxAssertion<T: Ord> {

fn should_have_max(&self, max: T) -> &Self;

fn should_not_have_max(&self, min: T) -> &Self;
fn should_not_have_max(&self, max: T) -> &Self;

fn should_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self;

fn should_not_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self;

fn should_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self;

fn should_not_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self;

fn should_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self;

fn should_not_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self;

fn should_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self;

fn should_not_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self;
}

impl<T> MinMaxAssertion<T> for Vec<T>
Expand All @@ -31,8 +48,48 @@ impl<T> MinMaxAssertion<T> for Vec<T>
self
}

fn should_not_have_max(&self, min: T) -> &Self {
(self as &[T]).should_not_have_max(min);
fn should_not_have_max(&self, max: T) -> &Self {
(self as &[T]).should_not_have_max(max);
self
}

fn should_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_have_min_in_inclusive_range(range);
self
}

fn should_not_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_not_have_min_in_inclusive_range(range);
self
}

fn should_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_have_min_in_exclusive_range(range);
self
}

fn should_not_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_not_have_min_in_exclusive_range(range);
self
}

fn should_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_have_max_in_inclusive_range(range);
self
}

fn should_not_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_not_have_max_in_inclusive_range(range);
self
}

fn should_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_have_max_in_exclusive_range(range);
self
}

fn should_not_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_not_have_max_in_exclusive_range(range);
self
}
}
Expand All @@ -55,8 +112,48 @@ impl<T, const N: usize> MinMaxAssertion<T> for [T; N]
self
}

fn should_not_have_max(&self, min: T) -> &Self {
(self as &[T]).should_not_have_max(min);
fn should_not_have_max(&self, max: T) -> &Self {
(self as &[T]).should_not_have_max(max);
self
}

fn should_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_have_min_in_inclusive_range(range);
self
}

fn should_not_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_not_have_min_in_inclusive_range(range);
self
}

fn should_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_have_min_in_exclusive_range(range);
self
}

fn should_not_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_not_have_min_in_exclusive_range(range);
self
}

fn should_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_have_max_in_inclusive_range(range);
self
}

fn should_not_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
(self as &[T]).should_not_have_max_in_inclusive_range(range);
self
}

fn should_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_have_max_in_exclusive_range(range);
self
}

fn should_not_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
(self as &[T]).should_not_have_max_in_exclusive_range(range);
self
}
}
Expand All @@ -79,12 +176,53 @@ impl<T> MinMaxAssertion<T> for [T]
self
}

fn should_not_have_max(&self, min: T) -> &Self {
self.should_not(&have_max(min));
fn should_not_have_max(&self, max: T) -> &Self {
self.should_not(&have_max(max));
self
}

fn should_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
self.should(&have_min_in_inclusive_range(range));
self
}

fn should_not_have_min_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
self.should_not(&have_min_in_inclusive_range(range));
self
}

fn should_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
self.should(&have_min_in_exclusive_range(range));
self
}

fn should_not_have_min_in_exclusive_range(&self, range: Range<T>) -> &Self {
self.should_not(&have_min_in_exclusive_range(range));
self
}

fn should_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
self.should(&have_max_in_inclusive_range(range));
self
}

fn should_not_have_max_in_inclusive_range(&self, range: RangeInclusive<T>) -> &Self {
self.should_not(&have_max_in_inclusive_range(range));
self
}

fn should_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
self.should(&have_max_in_exclusive_range(range));
self
}

fn should_not_have_max_in_exclusive_range(&self, range: Range<T>) -> &Self {
self.should_not(&have_max_in_exclusive_range(range));
self
}
}


#[cfg(test)]
mod tests {
use crate::assertions::collection::min_max::MinMaxAssertion;
Expand Down Expand Up @@ -140,4 +278,113 @@ mod tests {
let collection: Vec<&str> = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_max("junit");
}
}

#[cfg(test)]
mod range_tests {
use crate::assertions::collection::min_max::MinMaxAssertion;

#[test]
fn should_have_a_min_in_inclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_min_in_inclusive_range("assert"..="junit");
}

#[test]
#[should_panic]
fn should_have_a_min_in_inclusive_range_but_was_not() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_min_in_inclusive_range("clearcheck"..="junit");
}

#[test]
fn should_have_a_min_in_exclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_min_in_exclusive_range("assert".."junit");
}

#[test]
#[should_panic]
fn should_have_a_min_in_exclusive_range_but_was_not() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_min_in_exclusive_range("clearcheck".."junit");
}

#[test]
fn should_have_a_max_in_inclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_max_in_inclusive_range("assert"..="junit");
}

#[test]
#[should_panic]
fn should_have_a_max_in_inclusive_range_but_was_not() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_max_in_inclusive_range("clearcheck"..="gotest");
}

#[test]
fn should_have_a_max_in_exclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_max_in_exclusive_range("assert".."testify");
}

#[test]
#[should_panic]
fn should_have_a_max_in_exclusive_range_but_was_not() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_have_max_in_exclusive_range("clearcheck".."junit");
}

#[test]
fn should_not_have_a_min_in_inclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_min_in_inclusive_range("clearcheck"..="junit");
}

#[test]
#[should_panic]
fn should_not_have_a_min_in_inclusive_range_but_was() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_min_in_inclusive_range("assert"..="junit");
}

#[test]
fn should_not_have_a_min_in_exclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_min_in_exclusive_range("clearcheck".."junit");
}

#[test]
#[should_panic]
fn should_not_have_a_min_in_exclusive_range_but_was() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_min_in_exclusive_range("assert".."testify");
}

#[test]
fn should_not_have_a_max_in_inclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_max_in_inclusive_range("clearcheck"..="gotest");
}

#[test]
#[should_panic]
fn should_not_have_a_max_in_inclusive_range_but_was() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_max_in_inclusive_range("assert"..="junit");
}

#[test]
fn should_not_have_a_max_in_exclusive_range() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_max_in_exclusive_range("assert".."junit");
}

#[test]
#[should_panic]
fn should_not_have_a_max_in_exclusive_range_but_was() {
let collection = vec!["assert", "clearcheck", "junit"];
collection.should_not_have_max_in_exclusive_range("assert".."testify");
}
}
Loading

0 comments on commit 17610a3

Please sign in to comment.