Opened 2 years ago
Closed 2 years ago
#55654 closed task (blessed) (fixed)
Tests: Reduce usage of assertEquals for 6.1
Reported by: | hellofromTonya | Owned by: | desrosj |
---|---|---|---|
Milestone: | 6.1 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Build/Test Tools | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description
Follow-up to:
The assertEquals()
test method does not check that the types of the expected and actual values match. This can hide subtle bugs especially when the values are falsey.
Tasks:
- Switch to using
assertSame()
when the type of the value in the assertion is important - Replace overall usage of
assertEquals()
with type-strict assertion methods, with the aim of potentially removing its usage altogether
To help ease the effort of backporting tests, changes should also be made upstream in the Gutenberg repository.
Change History (12)
This ticket was mentioned in PR #1768 on WordPress/wordpress-develop by costdev.
2 years ago
#1
- Keywords has-patch has-unit-tests added
#2
@
2 years ago
- Keywords needs-refresh added
@costdev could you refresh the pull request? Looks like there are merge conflicts.
#3
@
2 years ago
- Keywords needs-refresh removed
@desrosj Done.
- Merge conflicts have been resolved.
- New instances of
assertEquals()
have been replaced with more appropriate assertions. assertArrayEqualsWithObject()
, proposed in the PR, has been removed, asassertEqualSets()
performs more testing and has the same result.
Before the PR
568 instances of assertEquals()
.
With the PR (as of this comment)
34 instances of assertEquals()
.
No source changes.
This ticket was mentioned in PR #3413 on WordPress/wordpress-develop by desrosj.
2 years ago
#5
Trac ticket: https://core.trac.wordpress.org/ticket/55654
#8
follow-up:
↓ 9
@
2 years ago
@costdev Thanks for that PR and all the work so far.
This one is a bear, so I started with tests that did not involve type coercion for the expected values to get the ball rolling. We'll have to work through this in chunks, and possibly push parts for continuing in 6.2.
Some of these just require a deeper look to confirm the intended type is being checked for.
#9
in reply to:
↑ 8
@
2 years ago
Thanks for the commit! [54402] looks great to me.
Replying to desrosj:
Some of these just require a deeper look to confirm the intended type is being checked for.
Right, that is the reason I was careful with this in earlier iterations. As noted in comment:19:ticket:38266:
- In some cases, the tests should be adjusted to use the correct data type.
- In other cases, this points to minor bugs in core, e.g. using
ceil()
for values that are documented as integers, but without explicitly casting toint
, results in them being of typefloat
instead. This affects some properties likemax_num_pages
,max_pages
,total_pages
in various classes, or functions likeget_comment_pages_count()
,wp_embed_defaults()
,get_oembed_response_data()
.
#10
@
2 years ago
Thanks @desrosj!
Following @SergeyBiryukov's comment above regarding ceil()
, dropping a related comment I posted during the 5.9 cycle:
- I've tested one of the proposed solutions, a simple cast to
int
using(int)
before the calls toceil()
, followed by changingassertEquals()
toassertSame()
for relevant tests. All PHPUnit tests pass.- With coverage incomplete this could, theoretically, be a breaking change. However, we are yet to come across a real-world example of this, or even an example scenario where these methods should return a float.
This PR replaces
assertEquals()
with more appropriate, stricter assertions where possible and without making any changes to source.For easier reviewing, commits have been separated based on the replacement assertion and any additional changes required to implement the stricter assertion.
Trac ticket: https://core.trac.wordpress.org/ticket/55654