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

Enforce connection preconditions for setParent #4999

Merged
merged 8 commits into from
Jul 13, 2021

Conversation

jschanker
Copy link
Contributor

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide

The details

Resolves #4989

Proposed Changes

Throws error when attempting to set a block's parent to a non-null block b if b is not properly connected to it without changing state (i.e., if its previous/output connection is not connected to one of b's input/next connections or when it doesn't have such connections). Also throws error when attempting to set a block's parent to null if it has a connection to a superior block.

Behavior Before Change

Setting a parent of a block already connected to some other superior block would remove the block from that old parent's child list before throwing, leading to potential rendering issues. Setting the parent of a disconnected block could cause rendering issues and errors when attempting to delete that parent.

Behavior After Change

Errors are thrown in these cases without changing state so none of these problems ensue.

Reason for Changes

To enforce the desired precondition for setParent that a non-null inputted parent should already be a superior block via the proper connections and that a block should not be connected to a superior one when attempting to set its new parent to null.

Test Coverage

Tested on Blockly Playground to make sure that it resolved the desired bugs.

Documentation

No additional documentation required.

* Error now thrown when calling `a.setParent(b)` on non-null b if the output/previous connection of a is not connected to an input/next connection of b without removing block from old parent's child list.
* Commented out error for when calling `a.setParent(null)` if a is connected to superior block.
* Adjusted comment to reflect that blocks were no longer being disconnected in this method.
* Also changed == to === per google#4924 (`newParent` and `this.parentBlock_` must both be instances of `Blockly.Block`).
* Error now thrown when calling `a.setParent(b)` on non-null b if the output/previous connection of a is not connected to an input/next connection of b without removing block from old parent's child list.
* Error now thrown when calling `a.setParent(null)` if a is connected to a superior block.
* Adjusted comment to reflect that blocks were no longer being disconnected in this method.
* Also changed == to === per google#4924 (`newParent` and `this.parentBlock_` must both be instances of `Blockly.Block`).
* Error now thrown when calling `a.setParent(b)` on non-null b if the output/previous connection of a is not connected to an input/next connection of b without removing block from old parent's child list.
* Commented out error for when calling `a.setParent(null)` if a is connected to superior block.
* Adjusted comment to reflect that blocks were no longer being disconnected in this method.
* Also changed == to === per google#4924 (`newParent` and `this.parentBlock_` must both be instances of `Blockly.Block`).
* Fixed lint errors.
* Error now thrown when calling `a.setParent(b)` on non-null b if the output/previous connection of a is not connected to an input/next connection of b without removing block from old parent's child list.
* Commented out error for when calling `a.setParent(null)` if a is connected to superior block.
* Adjusted comment to reflect that blocks were no longer being disconnected in this method.
* Also changed == to === per google#4924 (`newParent` and `this.parentBlock_` must both be instances of `Blockly.Block`).
* Fixed lint errors.
* Adjusted comment.
@BeksOmega
Copy link
Collaborator

Hi jschanker, thanks for posting this! I'm just about to head out for today, so I'll get a review back to you on Monday =)

Also sorry about creating all of those linked issues when I was testing some automations stuff (whoops) I'll see if I can get those cleaned up.

Copy link
Collaborator

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

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

Thanks for putting up this fix :D I think it's good to enforce these changes programmatically in addition to fixing the visibility.

I think the only thing this change really needs is some tests. You can either choose to add them, or file an issue so that someone can add them later. I think there are 5 cases that need to be covered:

  • Properly setting the parent to null
  • Properly setting the parent to a new not-null parent
  • Cases for each of your error situations

But if you think of others that's rad.

The best place to add these tests is probably in the block_test.js mocha file. I can't remember if you've written tests before, so here are the mocha docs, and the chai docs just in case. You will probably want to assert equal, throws, and maybe doesNotThrow.

Thank you again for sending this in! The code looks great :D

core/block.js Show resolved Hide resolved
core/block.js Show resolved Hide resolved
* One is failing (commented out), will investigate later
@jschanker jschanker requested a review from a team as a code owner July 13, 2021 06:26
@jschanker
Copy link
Contributor Author

Thanks for putting up this fix :D I think it's good to enforce these changes programmatically in addition to fixing the visibility.

Happy to help!

I think the only thing this change really needs is some tests.

Absolutely! Added the tests.

I can't remember if you've written tests before

I have for #4876, and I hope it's okay that I cleaned up the ones I wrote in this pull request (creating the workspace in the setup and tearing it down upon completion).

Thank you again for sending this in! The code looks great :D

My pleasure again and thanks for reviewing!

Copy link
Collaborator

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

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

Thanks for adding tests, they look great! Just a few formatting changes, then should be gtg :D I think there should be some UI for you to commit them automagically.

I hope it's okay that I cleaned up the ones I wrote in this pull request (creating the workspace in the setup and tearing it down upon completion).

Oh yeah definitely! In my opinion it's way better to clean up code than to worry about being sticklery on PRs.

tests/mocha/block_test.js Outdated Show resolved Hide resolved
tests/mocha/block_test.js Outdated Show resolved Hide resolved
tests/mocha/block_test.js Show resolved Hide resolved
tests/mocha/block_test.js Outdated Show resolved Hide resolved
tests/mocha/block_test.js Outdated Show resolved Hide resolved
Capitalize first letter, period at end

Co-authored-by: Beka Westberg <bekawestberg@gmail.com>
@jschanker
Copy link
Contributor Author

Great, made the formatting changes. Thanks again!

Copy link
Collaborator

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks again for adding these checks :D

@BeksOmega BeksOmega merged commit de1b321 into google:develop Jul 13, 2021
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