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

Add unit test for easing function #608

Merged

Conversation

MasatoMakino
Copy link
Contributor

Related Issues

Purpose of this Pull Request

This PR is for adding unit tests to TWEEN.Easing functions.

Details of the changes

'Test TWEEN.Easing should starts at 0.0, ends at 1.0. TWEEN.Easing.InOut() should be 0.5 at midpoint'

Based on #507, this test checks that all easing functions satisfy conditions f(0) = 0.0, f(1.0) = 1.0. This test also checks that Easing.InOut(0.5) = 0.5.

'Test TWEEN.Easing should pass a specific value'

This test verifies that a result of f(Math.LOG10E) is not broken. The purpose of this test is to check if results of the function changes depending on the version of node.js.

toBeCloseTo are based on jest's functions.

fix Sinusoidal easing

return 1 - Math.cos((amount * Math.PI) / 2)

Because 1 - Math.cos(1.0 * Math.PI / 2) = 0.99999999999 , it does not snap to 1.0.
By replacing Math.cos with Math.sin, this function snap to 1.0.

fix Back easing

Added truncation of 1.0 and 0.0.

Back.in() and Back.Out() now have limits. Sinusoidal.In() and .InOut() are replaced with Math.sin from Math.cos. Because 1 - Math.cos(Math.PI / 2) = 0.99999999999
and it does not snap to 1.
@@ -159,11 +159,11 @@ const Easing = {
Back: {
In: function (amount: number): number {
const s = 1.70158
return amount * amount * ((s + 1) * amount - s)
return amount === 1 ? 1 : amount * amount * ((s + 1) * amount - s)
Copy link
Member

Choose a reason for hiding this comment

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

Does this fix a jump?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes.
Back.In(1.0) = 0.9999999999999998
This jump is not visually problematic, but it is not enough to pass the test.
As the jump is very small, I did not modify the process and added a truncation.

@trusktr
Copy link
Member

trusktr commented May 1, 2021

Hey, nice stuff! What do you mean by "The purpose of this test is to check if results of the function changes depending on the version of node.js."?

In(amount: number): number
Out(amount: number): number
InOut(amount: number): number
}
Copy link
Member

Choose a reason for hiding this comment

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

Cool. I suppose this can be moved to the definition of the function groups in https://github.com/tweenjs/tween.js/blob/master/src/Easing.ts, then imported here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

During the writing of this test, I considered how to add the definition to Easing.ts.

In order to add the EasingFunctionGroup type definition, we had to modify the export of index.ts.
I thought it would be excessive to modify index.ts to add tests, so I added a type definition to test.ts.
In the future, I also think that adding a type definition to Easing.ts is the best way to go.

@trusktr trusktr merged commit 775536c into tweenjs:master May 1, 2021
@MasatoMakino
Copy link
Contributor Author

Hey, nice stuff! What do you mean by "The purpose of this test is to check if results of the function changes depending on the version of node.js."?

If node.js is updated and the decimal point handling or Math class handling changes, this test will detect the change.

CI of tween.js supports multiple versions of node.js. Therefore, it is easy to determine which version the change occurred in.

A possibility of such a disruptive change in node.js is very low. However, tracking with CI does not waste developers' time. I think it's a good deal to add a few dozen lines of test code to save our collaborators' valuable time.

@MasatoMakino MasatoMakino deleted the add-unit-test-for-easing-function branch May 27, 2021 05:49
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