Skip to content

Commit 76e7910

Browse files
committed
add : tests if easing object is frozen.
related : tweenjs#610
1 parent c705888 commit 76e7910

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/tests.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -630,10 +630,48 @@ export const tests = {
630630
test.done()
631631
},
632632

633+
'Test TWEEN.Tween.EasingFunctionGroup should be frozen'(test: Test): void {
634+
const replaceEasingFunction = (easingGroup: TWEEN.Easing.EasingFunctionGroup) => {
635+
const throwsWithReassigned = () => {
636+
easingGroup.In = (amount: number) => {
637+
return 1.0 + amount
638+
}
639+
easingGroup.Out = (amount: number) => {
640+
return 1.0 + amount
641+
}
642+
easingGroup.InOut = (amount: number) => {
643+
return 1.0 + amount
644+
}
645+
}
646+
test.throws(throwsWithReassigned)
647+
test.equal(easingGroup.In(0.0), 0.0)
648+
test.equal(easingGroup.Out(0.0), 0.0)
649+
test.equal(easingGroup.InOut(0.0), 0.0)
650+
test.equal(easingGroup.In(1.0), 1.0)
651+
test.equal(easingGroup.Out(1.0), 1.0)
652+
test.equal(easingGroup.InOut(1.0), 1.0)
653+
}
654+
655+
// eslint-disable-next-line
656+
const implementsEasingFunctionGroup = (arg: any): arg is TWEEN.Easing.EasingFunctionGroup => {
657+
return (
658+
arg !== null &&
659+
typeof arg === 'object' &&
660+
typeof arg.In === 'function' &&
661+
typeof arg.Out === 'function' &&
662+
typeof arg.InOut === 'function'
663+
)
664+
}
665+
const easingGroups = Object.values(TWEEN.Easing).filter(implementsEasingFunctionGroup)
666+
easingGroups.forEach(replaceEasingFunction)
667+
668+
test.done()
669+
},
670+
633671
'Test TWEEN.Easing should starts at 0.0, ends at 1.0. TWEEN.Easing.InOut() should be 0.5 at midpoint'(
634672
test: Test,
635673
): void {
636-
const checkEdgeValue = (ease: EasingFunctionGroup) => {
674+
const checkEdgeValue = (ease: TWEEN.Easing.EasingFunctionGroup) => {
637675
test.equal(ease.In(0.0), 0.0)
638676
test.equal(ease.Out(0.0), 0.0)
639677
test.equal(ease.InOut(0.0), 0.0)
@@ -660,7 +698,7 @@ export const tests = {
660698

661699
'Test TWEEN.Easing should pass a specific value'(test: Test): void {
662700
const checkEasingGroupPassPoints = (
663-
easingGroup: EasingFunctionGroup,
701+
easingGroup: TWEEN.Easing.EasingFunctionGroup,
664702
expects: {In: number; Out: number; InOut: number},
665703
) => {
666704
checkPassPoint(easingGroup.In, expects.In)
@@ -2022,7 +2060,7 @@ export const tests = {
20222060
'Test TWEEN.Easing.generatePow(1) equals Linear'(test: Test): void {
20232061
const ease1 = TWEEN.Easing.generatePow(1)
20242062

2025-
const compareWithLinear = (ease: EasingFunctionGroup, amount: number) => {
2063+
const compareWithLinear = (ease: TWEEN.Easing.EasingFunctionGroup, amount: number) => {
20262064
const linearResult = TWEEN.Easing.Linear.None(amount)
20272065
test.equal(linearResult, ease.In(amount))
20282066
test.equal(linearResult, ease.Out(amount))
@@ -2040,7 +2078,7 @@ export const tests = {
20402078
},
20412079

20422080
'Test TWEEN.Easing.generatePow(n) should pass 0.0, 0.5, 1.0'(test: Test): void {
2043-
const checkEdgeValue = (ease: EasingFunctionGroup) => {
2081+
const checkEdgeValue = (ease: TWEEN.Easing.EasingFunctionGroup) => {
20442082
test.equal(ease.InOut(0.0), 0.0)
20452083
test.equal(ease.In(0.0), 0.0)
20462084
test.equal(ease.Out(0.0), 0.0)
@@ -2096,15 +2134,10 @@ type Test = {
20962134
equal(a: unknown, b: unknown, failMessage?: string): void
20972135
deepEqual(a: unknown, b: unknown, failMessage?: string): void
20982136
expect(n: number): void
2137+
throws(block: unknown, error?: unknown, message?: string): void
20992138
done(): void
21002139
}
21012140

2102-
type EasingFunctionGroup = {
2103-
In(amount: number): number
2104-
Out(amount: number): number
2105-
InOut(amount: number): number
2106-
}
2107-
21082141
function toBeCloseTo(test: Test, numberA: number, numberB: number, numDigits = 2): void {
21092142
const diff = Math.abs(numberA - numberB)
21102143
test.ok(

0 commit comments

Comments
 (0)