Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Step time in SpriteAnimation must be positive (#1387)
- Loading branch information
Showing
3 changed files
with
158 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import 'package:flame/src/sprite_animation.dart'; | ||
import 'package:flame_test/flame_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('SpriteAnimation', () { | ||
test('Throw assertion error on empty list of frames', () { | ||
expect( | ||
() => SpriteAnimation.spriteList([], stepTime: 1), | ||
failsAssert('There must be at least one animation frame'), | ||
); | ||
}); | ||
|
||
test('Throw assertion error on non-positive step time', () { | ||
final sprite = MockSprite(); | ||
expect( | ||
() => SpriteAnimation.spriteList([sprite], stepTime: 0), | ||
failsAssert('All frames must have positive durations'), | ||
); | ||
expect( | ||
() => SpriteAnimation.variableSpriteList( | ||
[sprite, sprite, sprite], | ||
stepTimes: [1, -1, 0], | ||
), | ||
failsAssert('All frames must have positive durations'), | ||
); | ||
}); | ||
|
||
test('Throw assertion error when setting non-positive step time', () { | ||
final sprite = MockSprite(); | ||
final animation = | ||
SpriteAnimation.spriteList([sprite, sprite, sprite], stepTime: 1); | ||
expect( | ||
() => animation.stepTime = 0, | ||
failsAssert('Step time must be positive'), | ||
); | ||
expect( | ||
() => animation.variableStepTimes = [3, 2, 0], | ||
failsAssert('All step times must be positive'), | ||
); | ||
}); | ||
|
||
test('onComplete called for single-frame animation', () { | ||
var counter = 0; | ||
final sprite = MockSprite(); | ||
final animation = | ||
SpriteAnimation.spriteList([sprite], stepTime: 1, loop: false) | ||
..onComplete = () => counter++; | ||
expect(counter, 0); | ||
animation.update(0.5); | ||
expect(counter, 0); | ||
animation.update(0.5); | ||
expect(counter, 1); | ||
animation.update(1); | ||
expect(counter, 1); | ||
}); | ||
}); | ||
} | ||
|
||
class MockSprite extends Mock implements Sprite {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters