Skip to content

Tests/Tokenizer: expand default keyword tests #813

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

Conversation

rodrigoprimo
Copy link
Contributor

Description

This PR adds two new tests to the tokenizer tests for the default keyword:

  • Ensure that the default scope is set correctly when the scope closer is shared with a switch.
  • Ensure that the default scope is set correctly when there is an inner if condition with and without braces.

Both tests were copied from similar tests for the case keyword that were added to protect against regressions (see b9809c7 and 61b9e9b). They were suggested in #595 (comment) and #595 (comment).

Related issues/external references

Tests suggested in #595 (comment) and #595 (comment).

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
    • This change is only breaking for integrators, not for external standards or end-users.
  • Documentation improvement

PR checklist

  • I have checked there is no other PR open for the same change.
  • I have read the Contribution Guidelines.
  • I grant the project the right to include and distribute the code under the BSD-3-Clause license (and I have the right to grant these rights).
  • I have added tests to cover my changes.
  • I have verified that the code complies with the projects coding standards.
  • [Required for new sniffs] I have added XML documentation for the sniff.


function switchDefaultNestedIfWithAndWithoutBraces($i, $foo, $baz) {
switch ($i) {
/* testSwitchDefaultNestedIfWithAndWithoutBraces */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test has braces for all if/else leaves, while the point of the test was that it would not (also see the marker name)....

        if ($foo) {
            return true;
        } elseif ($baz)
            return true;
        else {
            echo 'else';
        }

Copy link
Contributor Author

@rodrigoprimo rodrigoprimo Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. It is now fixed in f0e74de

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the other PR:

Should this improvement also be done RecurseScopeMapDefaultKeywordConditionsTest tests ?

it seems to me that adding the closer markers to RecurseScopeMapDefaultKeywordConditionsTest is not necessary as the tests there are different. The code doesn't look for the scope_closer using AbstractTokenizerTestCase::getTargetToken() and the test marker. Instead, a closerOffset is passed, and it is used to determine the scope_closer:

I think that was the whole point of the original remark: having a marker for the closer will make the tests more stable, than manually setting closerOffset expectations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I meant: 82fae8e

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrfnl, thanks for clarifying and for providing an example. Besides the improvements from the commit that you mentioned, I believe I have seen the name of the marker included in the $message parameter passed to the assertion methods in another test file. Something like:

$this->assertArrayHasKey(
    'scope_condition',
    $tokenArray,
    sprintf('Scope condition is not set. Marker: %s.', $testMarker)
);

I like this pattern as it makes it easier to identify the related test case when there is a failure. Since I will improve RecurseScopeMapDefaultKeywordConditionsTest::testSwitchDefault() in this PR, should I add the marker to the error message?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rodrigoprimo Sounds like a good idea 👍🏻

@rodrigoprimo
Copy link
Contributor Author

I marked this PR as "blocked" and created a separate PR to address a bug in the default keyword tests that I found: #850. Both PRs touch the exact same part of the code. So to avoid conflicts, I will wait for a decision about the other PR before asking for another review on this one.

@rodrigoprimo
Copy link
Contributor Author

PR #850 was merged, but while rebasing this PR I noticed something that I missed and created another PR to address it: #870

@rodrigoprimo
Copy link
Contributor Author

@jrfnl, now that both PRs blocking this one have been merged, I have rebased it, fixed a merge conflict and now it is ready for another review. Thanks.

@rodrigoprimo
Copy link
Contributor Author

The label and review requests changes above were done to test #897 and #898.

Comment on lines 144 to 146
public function testSwitchDefault(
$testMarker, $testOpenerMarker=null, $testCloserMarker=null, $conditionStopMarker=null, $testContent='default', $sharedScopeCloser=false
) {
Copy link
Member

@jrfnl jrfnl Mar 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh ... no. That's not how a multi-line function signature should look.

Moreover, I don't see a reason for a multi-line signature in the first place. We know we are in the tests, so the parameters don't need to tell us.

Suggested change
public function testSwitchDefault(
$testMarker, $testOpenerMarker=null, $testCloserMarker=null, $conditionStopMarker=null, $testContent='default', $sharedScopeCloser=false
) {
public function testSwitchDefault($testMarker, $openerMarker=null, $closerMarker=null, $conditionStopMarker=null, $testContent='default', $sharedScopeCloser=false) {

Also, not so sure about the null default values for the changed parameters. It doesn't make things clearer.

Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've discussed this PR in a call. The fourth commit is definitely not acceptable as-is (for various reasons).

The first commit (when combined with the second + fifth) is okay, though I'm in two minds about the new parameter for shared closers as it makes the test inconsistent with the sister-test for the case keyword.

The third commit would be better off as a separate PR.

All in all, I don't think this is ready for merge.

This commit adds two new tests to the tokenizer tests for the `default`
keyword:

- Ensure that the `default` scope is set correctly when the scope closer
is shared with a `switch`.
- Ensure that the `default` scope is set correctly when there is an
inner if condition with and without braces.

Both tests were copied from similar tests for the `case` keyword that
were added to protect against regressions (see b9809c7 and 61b9e9b).
@rodrigoprimo rodrigoprimo force-pushed the improve-default-keyword-tokenizer-tests branch from 6c0b405 to 9c70e44 Compare March 19, 2025 18:36
@rodrigoprimo
Copy link
Contributor Author

Thanks for checking this PR, @jrfnl.

I made the changes we discussed, combining commits 1, 2, and 5 into one and dropping commits 3 and 4. Could you please take another look?

I will create another PR with commit 3 once this one is merged and then another PR for commit 4 after improving it.

@jrfnl jrfnl added this to the 3.12.1 milestone Mar 22, 2025
Copy link
Member

@jrfnl jrfnl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rodrigoprimo ! This set of changes I can accept.

@jrfnl jrfnl merged commit c93eb90 into PHPCSStandards:master Mar 22, 2025
46 checks passed
@jrfnl jrfnl deleted the improve-default-keyword-tokenizer-tests branch March 22, 2025 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants