Files
flame/packages/flame_isolate/test/flame_isolate_test.dart
Johannes Löhnn 3c38ee6058 feat: Bumped integral_isolates package for flame_isolate (#2994)
Updated package dependencies for flame_isolate.
Added support for TailoredIsolate and computeStream (called
isolateComputeStream in flame_isolate).

Updated the example package with new sprites created by yours truly, and
updated the license for the package, now that there is no assets with
copy-left CC license remaining.

### Migration instructions

Computation function is renamed from `isolate` to `isolateCompute`.
There are future breaking changes that are, as of now, mitigated with a
`@Deprecated` annotation with information regarding what function to use
instead.
2024-01-22 21:08:33 +00:00

100 lines
2.8 KiB
Dart

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame_isolate/flame_isolate.dart';
import 'package:flame_test/flame_test.dart';
import 'package:test/test.dart';
class TestGame extends FlameGame with FlameIsolate {}
class IsolateComponent extends Component with FlameIsolate {}
void main() {
testWithGame<TestGame>(
'Test running isolateCompute on game',
TestGame.new,
(game) async {
final result = game.isolateCompute(_pow, 10);
await expectLater(result, completion(100));
},
);
testWithGame<TestGame>(
'Test running isolateComputeStream on game',
TestGame.new,
(game) async {
final result = game.isolateComputeStream(_messages, 10);
await expectLater(result, emitsInOrder([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
},
);
group('Test isolate in sub-component', () {
testWithFlameGame(
'Running isolateCompute in sub-component',
(game) async {
final isolateComponent = IsolateComponent();
await game.add(isolateComponent);
await game.ready();
final result = isolateComponent.isolateCompute(_pow, 4);
await expectLater(result, completion(16));
},
);
testWithFlameGame(
'Running isolateComputeStream in sub-component',
(game) async {
final isolateComponent = IsolateComponent();
await game.add(isolateComponent);
await game.ready();
final result = isolateComponent.isolateComputeStream(_messages, 4);
await expectLater(result, emitsInOrder([1, 2, 3, 4]));
},
);
testWithFlameGame(
'Running isolateCompute or isolateComputeStream after remove gives error',
(game) async {
final isolateComponent = IsolateComponent();
await game.add(isolateComponent);
await game.ready();
final result = isolateComponent.isolateCompute(_pow, 4);
await expectLater(result, completion(16));
game.remove(isolateComponent);
await game.ready();
expect(
() => isolateComponent.isolateCompute(_pow, 4),
throwsA(
isA<TypeError>().having(
(error) => error.toString(),
'Explicit non null assertion',
'Null check operator used on a null value',
),
),
);
expect(
() => isolateComponent.isolateComputeStream(_messages, 4),
throwsA(
isA<TypeError>().having(
(error) => error.toString(),
'Explicit non null assertion',
'Null check operator used on a null value',
),
),
);
},
);
});
}
int _pow(int message) {
return message * message;
}
Stream<int> _messages(int amount) async* {
for (var i = 1; i <= amount; i++) {
yield i;
}
}