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

Make "switch costume" and "switch backdrop" blocks compatible with 2.0 #1517

Merged
merged 9 commits into from
Oct 29, 2018
Merged

Make "switch costume" and "switch backdrop" blocks compatible with 2.0 #1517

merged 9 commits into from
Oct 29, 2018

Conversation

joker314
Copy link
Contributor

@joker314 joker314 commented Aug 22, 2018

Resolves

Resolves #1490

Proposed Changes

  • switch backdrop to block is (EDIT: almost) compatible with 2.0 behaviour
  • switch costume to block is compatible with 2.0 behaviour
  • unit tests guard against future incompatibility
  • make "random backdrop" have lower priority than a costume with that name

See 2.0 behaviour description.

Reason for Changes

Making sure that 2.0 projects still work in 3.0.

I placed the "random backdrop" feature as less important than a costume name. This means that if a 2.0 project already had a backdrop with the name "random backdrop", references to it by name will still hold. Was this a good idea?

This is different to how "previous backdrop" and "next backdrop" behave -- they can't be overridden (per 2.0); however this is the same behaviour as "previous costume" and "next costume", which can.

Test Coverage

  • 2 automatic unit tests were added. They all pass after my changes. Expand below to see which failed before (more than was initially documented in the issue).
Assertions which failed prior to changes

Costumes

  • switch costume to NaN (e.g. 0/0) should flawlessly switch to the first costume.
  • switch costume to Infinity (e.g. 1/0) should flawlessly switch to the first costume.
  • switch costume to -Infinity (e.g. -1/0) should flawlessly switch to the first costume.
  • switch costume to previous backdrop
  • should do nothing
  • switch costume to next backdrop should do nothing
  • Strings should only be interpreted as indices if they contain at least one numeric digit.
  • switch costume to <1 = 0> should switch to the costume named "false" if it exists Switch costume/backdrop to boolean value switches to costume nr. 0 or 1 #1490

Backdrops

  • switch backdrop to [previous backdrop] decrements the backdrop even if there's a backdrop with that name
  • switch backdrop to [next backdrop] increments the backdrop even if there's a backdrop with that name
  • switch backdrop to NaN (e.g. 0/0) should flawlessly switch to the first backdrop.
  • switch backdrop to Infinity (e.g. 1/0) should flawlessly switch to the first backdrop.
  • switch backdrop to -Infinity (e.g. -1/0) should flawlessly switch to the first backdrop.

  • Manually tested with 239254767
  • Manually tested with 239255071
  • Unable to manually test with 86981455 as it wasn't a minimal example
  • Manually confirmed that switching costume by name still worked

@joker314 joker314 changed the title Make settings costumes and backdrops through blocks compatible with 2.0 Make "switch costume" and "switch backdrop" blocks compatible with 2.0 Aug 22, 2018
@thisandagain thisandagain added this to the September 2018 milestone Aug 23, 2018
@joker314
Copy link
Contributor Author

Regarding the unit tests added, would it be a good idea to split them into a separate file -- and, since the backdrop and costume tests are quite similar, use a loop/function for the common bits?

const numCostumes = target.getCostumes().length;
} else if (requestedCostume === 'previous costume') {
target.setCostume(target.currentCostume - 1);
} else if (!isNaN(requestedCostume) &&

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

} else if (requestedCostume === 'previous costume') {
target.setCostume(target.currentCostume - 1);
} else if (!isNaN(requestedCostume) &&
(typeof requestedCostume !== 'string' || /\d/g.test(requestedCostume))) {

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

} else if (requestedBackdrop === 'next backdrop') {
stage.setCostume(stage.currentCostume + 1);
} else if (requestedBackdrop === 'previous backdrop') {
stage.setCostume(stage.currentCostume - 1);

This comment was marked as abuse.

This comment was marked as abuse.

@thisandagain
Copy link
Contributor

Thanks a ton @joker314. In addition to the PR, I really appreciate the documentation and research in #1490. 😄

// Non-existant costumes do nothing
t.strictEqual(testCostume(['a', 'b', 'c', 'd'], 'e', 3), 3);

// Difference between string and numeric arguments

This comment was marked as abuse.

* @param {number} [currentCostume=1] The 1-indexed default costume for the sprite to start at.
* @return {number} The 1-indexed costume index on which the sprite lands.
*/
const testCostume = (costumes, arg, currentCostume = 1) => {

This comment was marked as abuse.

Copy link
Contributor

@kchadha kchadha left a comment

Choose a reason for hiding this comment

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

@joker314 Thanks for making this pull request and for detailing the Scratch 2.0 behavior in the comment on #1490. I apologize for the delay on this review.

I am requesting some minor changes/code cleanup, and would also like to request adding more comments in the code to illustrate what kinds of cases you're trying to address in which parts of the code.

Thanks for adding the thorough unit tests, they're awesome!

I added a comment about maybe making the switch backdrop behavior similar to switch costume in how it handles backdrops named 'previous backdrop' and 'next backdrop' but I think it's also okay to file that as a follow-up separate issue since what you have now is consistent with Scratch 2.0.

Move the helper functions `testCostume` and `testBackdrop` outside of
their test definitions. In addition, define the latter in terms of the
former.
The `switch costume` block accepts special values like "next costume" and
"previous costume". If you create a costume with these names, these take
priority over the special values. However, the `switch backdrop` block
keeps these special values for values like "next backdrop", "previous
backdrop", "random backdrop". It is impossible to navigate to such a
backdrop by name via block. This commit also modifies tests to allow for
this.

BREAKING CHANGE: specially-named backdrops can now be navigated
@joker314
Copy link
Contributor Author

joker314 commented Oct 10, 2018

Thank you very much for the review @kchadha. I've addressed your thoughtful feedback, and have introduced the breaking change which makes backdrop special arguments be handled in a similar way to costumes.

This pull request is ready for re-review.

Here's a base for what one might want to include in the Compatibility wiki page, if it's useful:

Backdrops named 'previous backdrop' and 'next backdrop' can be switched to via blocks

In Scratch 2.0, switch backdrop to [next backdrop] would switch to the backdrop sequentially following the current backdrop, even if there was a backdrop literally named "next backdrop". The same behaviour is seen in "previous backdrop".

This differed to the behaviour of the switch costume block, which would not perform the block input's special function if there existed a costume with the corresponding name.

To make the blocks more intuitive, the switch backdrop block now uses the switch costume behaviour. This is also likely to be what a user would expect, since the workaround for incrementing a backdrop is trivial and stable, whereas the workaround for switching to a backdrop by name would require trying every costume inefficiently until it is found (unless the order in which the backdrops have been arranged is hard-coded)

We ensure that all code whose purpose may be confusing to grasp is
commented; and we remove information that is no longer required.
Copy link
Contributor

@kchadha kchadha left a comment

Choose a reason for hiding this comment

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

@joker314 Thank you for making the requested changes! This PR looks good to go.

@kchadha kchadha merged commit be238d3 into scratchfoundation:develop Oct 29, 2018
@joker314 joker314 deleted the costume-compatibility branch October 29, 2018 15:44
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.

3 participants