Skip to content

Commit

Permalink
Prevent having negative values for rotationQuarterTurns
Browse files Browse the repository at this point in the history
  • Loading branch information
imaNNeo committed Dec 27, 2024
1 parent c31b99f commit 08a6d7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/src/chart/base/axis_chart/axis_chart_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ class TitleMeta {
required this.formattedValue,
required this.axisSide,
required this.rotationQuarterTurns,
});
}) : assert(
rotationQuarterTurns >= 0,
"TitleMeta.rotationQuarterTurns couldn't be negative",
);

/// min axis value
final double min;
Expand Down
14 changes: 9 additions & 5 deletions lib/src/extensions/size_extension.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import 'dart:ui';

extension SizeExtension on Size {
Size rotateByQuarterTurns(int quarterTurns) => switch (quarterTurns % 4) {
0 || 2 => this,
1 || 3 => Size(height, width),
_ => throw ArgumentError('Invalid quarterTurns $quarterTurns'),
};
Size rotateByQuarterTurns(int quarterTurns) {
if (quarterTurns < 0) {
throw ArgumentError('quarterTurns must be greater than or equal to 0.');
}
return switch (quarterTurns % 4) {
0 || 2 => this,
_ /*2 || 3*/ => Size(height, width),
};
}
}
8 changes: 8 additions & 0 deletions test/extensions/size_extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,13 @@ void main() {
expect(const Size(100, 200).rotateByQuarterTurns(4), const Size(100, 200));
expect(const Size(100, 200).rotateByQuarterTurns(5), const Size(200, 100));
expect(const Size(100, 200).rotateByQuarterTurns(6), const Size(100, 200));
expect(
() => const Size(100, 200).rotateByQuarterTurns(-1),
throwsArgumentError,
);
expect(
() => const Size(100, 200).rotateByQuarterTurns(-3),
throwsArgumentError,
);
});
}

0 comments on commit 08a6d7e

Please sign in to comment.