pest icon indicating copy to clipboard operation
pest copied to clipboard

Add `toBeWithin` expectation method

Open lhajman opened this issue 1 year ago • 3 comments

What:

  • [ ] Bug Fix
  • [x] New Feature

Description:

In this PR i have added a new toBeWithin expectation method to Pest. This method allows for easy assertion that a value falls within a specified range, inclusive of the boundaries.

Changes

  1. Added toBeWithin method to Expectation class
  2. Added corresponding test suite for toBeWithin

New Functionality

The toBeWithin method can be used as follows:

expect(5)->toBeWithin(1, 10);
expect(3.14)->toBeWithin(3, 4);

Test Coverage

A comprehensive test suite has been added, covering:

  • Basic functionality with integers and floats
  • Edge cases (PHP_INT_MAX, PHP_INT_MIN)
  • Negative numbers
  • Error cases (non-numeric values)
  • Custom error messages

lhajman avatar Oct 05 '24 14:10 lhajman

What's the difference between toBeWithin and toBeBetween?

faissaloux avatar Oct 05 '24 14:10 faissaloux

@faissaloux Thanks for this question.

The main differences between these two methods are:

  1. Parameter types:

    • toBeBetween accepts int, float, or DateTimeInterface for both parameters.
    • toBeWithin only accepts int or float for both parameters.
  2. Value type checking:

    • toBeWithin explicitly checks if the value is numeric before performing the comparison.
    • toBeBetween doesn't perform this check, allowing for comparison of DateTimeInterface objects.
  3. Semantic usage:

    • toBeBetween is more flexible and can be used with dates and times as well as numbers.
    • toBeWithin is specifically for numeric ranges.

In practice, for numeric values, these methods perform the same assertion. The main difference is in their intended use cases and the types of values they can handle. toBeBetween is more versatile, while toBeWithin is more specific to numeric ranges.

lhajman avatar Oct 05 '24 19:10 lhajman

I think we don't need another method for each type, since we have toBeBetween that does the job.

faissaloux avatar Oct 05 '24 20:10 faissaloux

going to table this one for now.

nunomaduro avatar Jul 26 '25 03:07 nunomaduro