Add `toBeWithin` expectation method
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
- Added
toBeWithinmethod toExpectationclass - 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
What's the difference between toBeWithin and toBeBetween?
@faissaloux Thanks for this question.
The main differences between these two methods are:
-
Parameter types:
toBeBetweenacceptsint,float, orDateTimeInterfacefor both parameters.toBeWithinonly acceptsintorfloatfor both parameters.
-
Value type checking:
toBeWithinexplicitly checks if the value is numeric before performing the comparison.toBeBetweendoesn't perform this check, allowing for comparison ofDateTimeInterfaceobjects.
-
Semantic usage:
toBeBetweenis more flexible and can be used with dates and times as well as numbers.toBeWithinis 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.
I think we don't need another method for each type, since we have toBeBetween that does the job.
going to table this one for now.