test: tests for shapes

pull/87/head
RuiAlonso 4 years ago
parent 14b9663f76
commit f0c9697a74

@ -25,7 +25,7 @@ class EllipseShape extends ChainShape {
/// The top left corner of the ellipse. /// The top left corner of the ellipse.
/// ///
/// Where the initial painting begines. /// Where the initial painting begins.
// TODO(ruialonso): Change to use appropiate center. // TODO(ruialonso): Change to use appropiate center.
final Vector2 center; final Vector2 center;
@ -37,9 +37,7 @@ class EllipseShape extends ChainShape {
/// Rotates the ellipse by a given [angle] in radians. /// Rotates the ellipse by a given [angle] in radians.
void rotate(double angle) { void rotate(double angle) {
createChain( vertices.map((vector) => vector..rotate(angle)).toList();
vertices.map((vector) => vector..rotate(angle)).toList(),
);
} }
EllipseShape copyWith({ EllipseShape copyWith({

@ -1,5 +1,35 @@
import 'dart:math' as math;
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
import 'package:pinball_components/src/components/components.dart';
void main() { void main() {
group('ArcShape', () {}); group('BezierCurveShape', () {
group('rotate', () {
test('returns vertices rotated', () {
const rotationAngle = 2 * math.pi;
final controlPoints = [
Vector2(0, 0),
Vector2(10, 0),
Vector2(0, 10),
Vector2(10, 10),
];
final bezierCurveShape = BezierCurveShape(
controlPoints: controlPoints,
);
final bezierCurveShapeRotated = BezierCurveShape(
controlPoints: controlPoints,
)..rotate(rotationAngle);
for (var index = 0; index < bezierCurveShape.vertices.length; index++) {
expect(
bezierCurveShape.vertices[index]..rotate(rotationAngle),
equals(bezierCurveShapeRotated.vertices[index]),
);
}
});
});
});
} }

@ -1,3 +1,4 @@
import 'dart:math' as math;
import 'package:flame/extensions.dart'; import 'package:flame/extensions.dart';
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
@ -5,89 +6,24 @@ import 'package:pinball_components/src/components/components.dart';
void main() { void main() {
group('EllipseShape', () { group('EllipseShape', () {
group('rotate', () {}); group('rotate', () {
test('returns vertices rotated', () {
group('copyWith', () { const rotationAngle = 2 * math.pi;
test('returns same shape when no properties are passed', () {
final ellipseShape = EllipseShape( final ellipseShape = EllipseShape(
center: Vector2.zero(), center: Vector2.zero(),
majorRadius: 10, majorRadius: 10,
minorRadius: 8, minorRadius: 8,
); );
final ellipseShapeCopied = ellipseShape.copyWith(); final ellipseShapeRotated = EllipseShape(
for (var index = 0; index < ellipseShape.vertices.length; index++) {
expect(
ellipseShape.vertices[index],
equals(ellipseShapeCopied.vertices[index]),
);
}
});
test('returns object with updated center when center is passed', () {
final ellipseShapeExpected = EllipseShape(
center: Vector2.all(10),
majorRadius: 10,
minorRadius: 8,
);
final ellipseShapeCopied = EllipseShape(
center: Vector2.zero(),
majorRadius: 10,
minorRadius: 8,
).copyWith(center: Vector2.all(10));
for (var index = 0;
index < ellipseShapeCopied.vertices.length;
index++) {
expect(
ellipseShapeCopied.vertices[index],
equals(ellipseShapeExpected.vertices[index]),
);
}
});
test('returns object with updated majorRadius when majorRadius is passed',
() {
final ellipseShapeExpected = EllipseShape(
center: Vector2.zero(),
majorRadius: 12,
minorRadius: 8,
);
final ellipseShapeCopied = EllipseShape(
center: Vector2.zero(), center: Vector2.zero(),
majorRadius: 10, majorRadius: 10,
minorRadius: 8, minorRadius: 8,
).copyWith(majorRadius: 12); )..rotate(rotationAngle);
for (var index = 0; for (var index = 0; index < ellipseShape.vertices.length; index++) {
index < ellipseShapeCopied.vertices.length;
index++) {
expect(
ellipseShapeCopied.vertices[index],
equals(ellipseShapeExpected.vertices[index]),
);
}
});
test('returns object with updated minorRadius when minorRadius is passed',
() {
final ellipseShapeExpected = EllipseShape(
center: Vector2.zero(),
majorRadius: 12,
minorRadius: 5,
);
final ellipseShapeCopied = EllipseShape(
center: Vector2.zero(),
majorRadius: 12,
minorRadius: 8,
).copyWith(minorRadius: 5);
for (var index = 0;
index < ellipseShapeCopied.vertices.length;
index++) {
expect( expect(
ellipseShapeCopied.vertices[index], ellipseShape.vertices[index]..rotate(rotationAngle),
equals(ellipseShapeExpected.vertices[index]), equals(ellipseShapeRotated.vertices[index]),
); );
} }
}); });

Loading…
Cancel
Save