Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🥅 feat(quality): strengthen parse exception cases #239

Merged
merged 4 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/src/interval/interval.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ final class Interval implements Comparable<Interval> {
8: 12, // P
};

static final RegExp _intervalRegExp = RegExp(r'(\w+?)(\d+)');

/// Creates a new [Interval] allowing only perfect quality [size]s.
const Interval.perfect(this.size, PerfectQuality this.quality)
: assert(size != 0, 'Size must be non-zero'),
Expand Down Expand Up @@ -223,7 +225,7 @@ final class Interval implements Comparable<Interval> {
/// Interval.parse('z') // throws a FormatException
/// ```
factory Interval.parse(String source) {
final match = RegExp(r'(\w+?)(\d+)').firstMatch(source);
final match = _intervalRegExp.firstMatch(source);
if (match == null) throw FormatException('Invalid Interval', source);

final size = int.parse(match[2]!);
Expand Down
26 changes: 16 additions & 10 deletions lib/src/interval/quality.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class PerfectQuality extends Quality {
/// A triply augmented [PerfectQuality].
static const PerfectQuality triplyAugmented = PerfectQuality(3);

static final RegExp _perfectQualityRegExp = RegExp(r'^(d+|P|A+)$');

/// Parse [source] as a [PerfectQuality] and return its value.
///
/// If the [source] string does not contain a valid [PerfectQuality], a
Expand All @@ -92,13 +94,14 @@ class PerfectQuality extends Quality {
/// PerfectQuality.parse('z') // throws a FormatException
/// ```
factory PerfectQuality.parse(String source) {
final qualityParts = source.split('');
if (!_perfectQualityRegExp.hasMatch(source)) {
throw FormatException('Invalid PerfectQuality', source);
}

return switch (qualityParts.first) {
'A' => PerfectQuality(qualityParts.length),
return switch (source[0]) {
'd' => PerfectQuality(-source.length),
'P' => PerfectQuality.perfect,
'd' => PerfectQuality(-qualityParts.length),
_ => throw FormatException('Invalid perfect Interval', source),
_ /* 'A' */ => PerfectQuality(source.length),
};
}

Expand Down Expand Up @@ -159,6 +162,8 @@ class ImperfectQuality extends Quality {
/// A triply augmented [ImperfectQuality].
static const ImperfectQuality triplyAugmented = ImperfectQuality(4);

static final RegExp _imperfectQualityRegExp = RegExp(r'^(d+|m|M|A+)$');

/// Parse [source] as a [ImperfectQuality] and return its value.
///
/// If the [source] string does not contain a valid [ImperfectQuality], a
Expand All @@ -171,14 +176,15 @@ class ImperfectQuality extends Quality {
/// ImperfectQuality.parse('z') // throws a FormatException
/// ```
factory ImperfectQuality.parse(String source) {
final qualityParts = source.split('');
if (!_imperfectQualityRegExp.hasMatch(source)) {
throw FormatException('Invalid PerfectQuality', source);
}

return switch (qualityParts.first) {
'd' => ImperfectQuality(-qualityParts.length),
return switch (source[0]) {
'd' => ImperfectQuality(-source.length),
'm' => ImperfectQuality.minor,
'M' => ImperfectQuality.major,
'A' => ImperfectQuality(qualityParts.length + 1),
_ => throw FormatException('Invalid imperfect Interval', source),
_ /* 'A' */ => ImperfectQuality(source.length + 1),
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/note/positioned_note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ final class PositionedNote
_subPrimeAlt,
];

static final _scientificNotationRegExp = RegExp(r'(.+?)([-]?\d+)');
static final _helmholtzNotationRegExp =
static final RegExp _scientificNotationRegExp = RegExp(r'(.+?)([-]?\d+)');
static final RegExp _helmholtzNotationRegExp =
RegExp('(^[A-Ga-g${Accidental._symbols.join()}]+)'
'(${_primeSymbols.map((symbol) => '$symbol+').join('|')})?\$');

Expand Down
17 changes: 17 additions & 0 deletions test/src/interval/quality_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,24 @@ void main() {
group('.parse()', () {
test('should throw a FormatException when source is invalid', () {
expect(() => PerfectQuality.parse('x'), throwsFormatException);
expect(() => PerfectQuality.parse('a'), throwsFormatException);
expect(() => PerfectQuality.parse('Abc'), throwsFormatException);
expect(() => PerfectQuality.parse('abc'), throwsFormatException);
expect(() => PerfectQuality.parse('p'), throwsFormatException);
expect(() => PerfectQuality.parse('PP'), throwsFormatException);
expect(() => PerfectQuality.parse('D'), throwsFormatException);
expect(() => PerfectQuality.parse('Def'), throwsFormatException);
expect(() => PerfectQuality.parse('def'), throwsFormatException);

expect(() => ImperfectQuality.parse('x'), throwsFormatException);
expect(() => ImperfectQuality.parse('a'), throwsFormatException);
expect(() => ImperfectQuality.parse('Abc'), throwsFormatException);
expect(() => ImperfectQuality.parse('abc'), throwsFormatException);
expect(() => ImperfectQuality.parse('mm'), throwsFormatException);
expect(() => ImperfectQuality.parse('MM'), throwsFormatException);
expect(() => ImperfectQuality.parse('D'), throwsFormatException);
expect(() => ImperfectQuality.parse('Def'), throwsFormatException);
expect(() => ImperfectQuality.parse('def'), throwsFormatException);
});

test(
Expand Down