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

Add toBeWithin expectation method #1289

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from

Conversation

tal7aouy
Copy link

@tal7aouy tal7aouy commented Oct 5, 2024

What:

  • Bug Fix
  • 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

@faissaloux
Copy link
Contributor

What's the difference between toBeWithin and toBeBetween?

@tal7aouy
Copy link
Author

tal7aouy commented Oct 5, 2024

@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.

@faissaloux
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants