pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.07k stars 315 forks source link

[2.x] Adds toBePowerOf() Expectation #1090

Open LucasLaurens opened 4 months ago

LucasLaurens commented 4 months ago

What:

Description:

This PR adds a toBePowerOf() expectation, to validate whether a value is a power of another. If the PR is accepted, I could create one for documentation.

[!NOTE] What's more, if this code is accepted then I could also create expectations for exponential and square root, for example.

Related:

expect(8)->toBePowerOf(2);
expect(7)->not->toBePowerOf(2);
owenvoke commented 4 months ago

Just a thought, I feel like these would be quite nice in a maths (or math) plugin for Pest possibly. I'm not sure how commonly they'd be used.

I can see that there could be quite a lot of mathematical expectations that people would want, but which maybe shouldn't be in core as it'd add additional code.

Don't mind either way though, so going to leave this for someone else.

LucasLaurens commented 4 months ago

Just a thought, I feel like these would be quite nice in a maths (or math) plugin for Pest possibly. I'm not sure how commonly they'd be used.

I can see that there could be quite a lot of mathematical expectations that people would want, but which maybe shouldn't be in core as it'd add additional code.

Don't mind either way though, so going to leave this for someone else.

I totally agree with you. I think having an official plugin created by the Pest team to handle all mathematical cases would be really interesting.

If I've done it this way, it's also because I assumed that I was comparing two numbers and that the assertion would return a boolean.

Whether in a plugin or in the main code, I did it because I came across the need on one of my projects and thought it would be interesting to share it with the community.

I really thank you for your feedback, which I find very relevant. In any case, it was a pleasure for me to participate in the evolution of Pest, so I thought I'd propose this feature to help the community in certain cases.

This with the possibility of adding more if this first expectation is accepted and appreciated enough to add more (i mean math expectations).