Fix self-comparison and assert typos in TestSemantics#185975
Fix self-comparison and assert typos in TestSemantics#185975Sanaullah49 wants to merge 3 commits into
Conversation
Fixes flutter#185900 ## Summary Three small bugs in `packages/flutter/test/widgets/semantics_tester.dart` where the code accidentally compared a value to itself or shifted part of a logical expression into an assert message. ## What changed 1. **`controlsNodes` self-comparison** (line 511). The guard `controlsNodes != controlsNodes` is always false, so the `setEquals(...)` mismatch path was unreachable. Replaced with `controlsNodes != null`, matching the null-guard style used by neighbouring optional fields (e.g. `headingLevel`, `tags`, `scrollPosition`). 2. **`LocaleStringAttribute.locale` self-comparison** (line 718). `(second[i] as LocaleStringAttribute).locale != (second[i] as LocaleStringAttribute).locale` was always false. Changed the left-hand side to `first[i]` to actually compare the two attributes' locales, mirroring the existing `second[i].range != first[i].range` check immediately above. 3. **Stray comma in `_IncludesNodeWith` assert** (line 1183). The comma after `inputType != null` made `minValue != null || maxValue != null` the assert message argument (a `bool` rather than a `String`). Replaced the comma with `||` so `minValue` / `maxValue` count as valid constructor arguments, matching the matcher's other named parameters. No tests pass `controlsNodes` or `LocaleStringAttribute` to these matchers today, so these fixes only restore intended behaviour without disturbing existing assertions. ## Testing * `../../bin/flutter analyze test/widgets/semantics_tester.dart` * `../../bin/flutter test test/widgets/semantics_tester_test.dart` * `../../bin/flutter test test/widgets/semantics_tester_generate_test_semantics_expression_for_current_semantics_tree_test.dart` * `../../bin/flutter test test/widgets/semantics_test.dart`
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Code Review
This pull request corrects logic errors in TestSemantics and SemanticsTester where variables were incorrectly compared to themselves. It also refactors an assertion in the _IncludesNodeWith constructor to consolidate multiple null checks. Feedback suggests adding a descriptive error message to this assertion to comply with the style guide and verifying that the parameters are correctly stored as fields.
| inputType != null || | ||
| minValue != null || | ||
| maxValue != null, |
There was a problem hiding this comment.
There are two points to consider regarding this change:
- Missing Assert Message: The assertion is missing a descriptive error message. According to the Flutter style guide (line 37), error messages should be useful and provide context. Adding a message like
'At least one property must be provided.'would improve the developer experience when the assertion fails. - Potential Missing Fields: The parameters
minValueandmaxValue(and others likescrollExtentMin) are used in this assertion, but they do not appear to be stored as fields in the_IncludesNodeWithclass (onlyattributedLabelandattributedValueare visible at lines 1187-1188). If these parameters are not stored in fields, the matcher will not be able to verify them during the matching process, rendering them ineffective. Please ensure they are properly stored and utilized in the matching logic.
| inputType != null || | |
| minValue != null || | |
| maxValue != null, | |
| inputType != null || | |
| minValue != null || | |
| maxValue != null, 'At least one property must be provided.', |
References
- Error messages should be useful: Every error message is an opportunity to make someone love our product. (link)
There was a problem hiding this comment.
Looks like the bot is correct. There should have a message for the assertion failure. Developer should be notified what's wrong.
Add a descriptive error message to the `_IncludesNodeWith` constructor assert so it complies with the Flutter style guide's requirement that asserts include a useful message.
| } | ||
|
|
||
| if (controlsNodes != controlsNodes && !setEquals(controlsNodes, node.controlsNodes)) { | ||
| if (controlsNodes != null && !setEquals(controlsNodes, node.controlsNodes)) { |
There was a problem hiding this comment.
Hi @Sanaullah49 , I'm not from the flutter team. But I've talk about this change. I think, we should not do null check here. Let me help you understand why not.
TestSemantics(controlsNodes: null)
SemanticsNode(controlsNodes: {'a'})
what do you expect? the controlsNodes are not same, so the test shouldnot pass, right?
when you run test for this case. according to your code, the left side will be false, consequently, the test will pass event the contrlNodes are not same.
I suggest you to fix it
| if (controlsNodes != null && !setEquals(controlsNodes, node.controlsNodes)) { | |
| if (controlsNodes != node.controlsNodes && !setEquals(controlsNodes, node.controlsNodes)) { |
There was a problem hiding this comment.
The intention is to make this an optional matcher, if not provided, it won't check againt the field. I think this is fine as is since this matcher is already quite noisy and deprecated, we shouldn't make it more noisy
|
I think this changes requires to add tests also. But at the same time a PR already tracking the attached issue #185921 |
|
There's a competing PR #185921 fixing the same three bugs. Flagging the differences in approach for maintainer visibility: 1.
2. Assert fix in
3. Tests
|
|
I think this PR lgtm, just need a test before this can merge. Since there are three different pr, and this seems to be closest. Let's try to get this to merge |
victorsanni
left a comment
There was a problem hiding this comment.
These will need tests to land.
| } | ||
|
|
||
| if (controlsNodes != controlsNodes && !setEquals(controlsNodes, node.controlsNodes)) { | ||
| if (controlsNodes != null && !setEquals(controlsNodes, node.controlsNodes)) { |
There was a problem hiding this comment.
Should we also apply these changes in material/semantics_tester.dart?
victorsanni
left a comment
There was a problem hiding this comment.
Should we also apply these changes in material/semantics_tester.dart (or cupertino)?
justinmc
left a comment
There was a problem hiding this comment.
See Victor's comment https://github.com/flutter/flutter/pull/185975/changes#r3277507799, this file has been duplicated for now unfortunately, so we'll have to make these changes twice.
Fixes #185900
Summary
Three small bugs in
packages/flutter/test/widgets/semantics_tester.dartwhere the code accidentally compared a value to itself or shifted part of a logical expression into an assert message.What changed
controlsNodesself-comparison (line 511). The guardcontrolsNodes != controlsNodesis alwaysfalse, so thesetEquals(...)mismatch path was unreachable. Replaced withcontrolsNodes != null, matching the null-guard style used by neighbouring optional fields (e.g.headingLevel,tags,scrollPosition).LocaleStringAttribute.localeself-comparison (line 718).(second[i] as LocaleStringAttribute).locale != (second[i] as LocaleStringAttribute).localewas alwaysfalse. Changed the left-hand side tofirst[i]to actually compare the two attributes' locales, mirroring the existingsecond[i].range != first[i].rangecheck immediately above._IncludesNodeWithassert (line 1183). The comma afterinputType != nullmademinValue != null || maxValue != nullthe assert message argument (aboolrather than aString). Replaced the comma with||sominValue/maxValuecount as valid constructor arguments, matching the matcher's other named parameters.No tests pass
controlsNodesorLocaleStringAttributeto these matchers today, so these fixes only restore intended behaviour without disturbing existing assertions.Testing
../../bin/flutter analyze test/widgets/semantics_tester.dart../../bin/flutter test test/widgets/semantics_tester_test.dart../../bin/flutter test test/widgets/semantics_tester_generate_test_semantics_expression_for_current_semantics_tree_test.dart../../bin/flutter test test/widgets/semantics_test.dart