Skip to content

Commit 2fc5df9

Browse files
committed
Throw an error if either operand is invalid
1 parent 5eeacf2 commit 2fc5df9

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

pkgs/package_config/lib/src/package_config.dart

+38-32
Original file line numberDiff line numberDiff line change
@@ -410,80 +410,86 @@ abstract class InvalidLanguageVersion implements LanguageVersion {
410410
/// Relational operators for [LanguageVersion] that
411411
/// compare valid versions with [LanguageVersion.compareTo].
412412
///
413-
/// An [InvalidLanguageVersion] is not less or greater than other versions
414-
/// and is only equal to itself.
413+
/// If either operand is an [InvalidLanguageVersion], a [StateError] is thrown.
414+
/// Versions should be verified as valid after parsing and before using them.
415415
extension LanguageVersionRelationalOperators on LanguageVersion {
416416
/// Whether this language version is less than [other].
417417
///
418-
/// An [InvalidLanguageVersion] is never less than or greater than
419-
/// any other language version.
418+
/// If either version being compared is an [InvalidLanguageVersion],
419+
/// a [StateError] is thrown. Verify versions are valid before comparing them.
420420
///
421421
/// For details on how valid language versions are compared,
422422
/// check out [LanguageVersion.compareTo].
423423
bool operator <(LanguageVersion other) {
424-
// Account for invalid language versions which aren't
425-
// greater or less than any other version.
426-
if (this is InvalidLanguageVersion || other is InvalidLanguageVersion) {
427-
return false;
424+
// Throw an error if comparing as or with an invalid language version.
425+
if (this is InvalidLanguageVersion) {
426+
_throwThisInvalid();
427+
} else if (other is InvalidLanguageVersion) {
428+
_throwOtherInvalid();
428429
}
429430

430431
return compareTo(other) < 0;
431432
}
432433

433434
/// Whether this language version is less than or equal to [other].
434435
///
435-
/// An [InvalidLanguageVersion] is never less than or greater than
436-
/// any other language version. It is equal only to itself.
436+
/// If either version being compared is an [InvalidLanguageVersion],
437+
/// a [StateError] is thrown. Verify versions are valid before comparing them.
437438
///
438439
/// For details on how valid language versions are compared,
439440
/// check out [LanguageVersion.compareTo].
440441
bool operator <=(LanguageVersion other) {
441-
// Account for invalid language versions only being equal to themselves.
442-
if (identical(this, other)) {
443-
return true;
444-
}
445-
446-
if (this is InvalidLanguageVersion || other is InvalidLanguageVersion) {
447-
return false;
442+
// Throw an error if comparing as or with an invalid language version.
443+
if (this is InvalidLanguageVersion) {
444+
_throwThisInvalid();
445+
} else if (other is InvalidLanguageVersion) {
446+
_throwOtherInvalid();
448447
}
449448

450449
return compareTo(other) <= 0;
451450
}
452451

453452
/// Whether this language version is greater than [other].
454453
///
455-
/// An [InvalidLanguageVersion] is never less than or greater than
456-
/// any other language version.
454+
/// If either version being compared is an [InvalidLanguageVersion],
455+
/// a [StateError] is thrown. Verify versions are valid before comparing them.
457456
///
458457
/// For details on how valid language versions are compared,
459458
/// check out [LanguageVersion.compareTo].
460459
bool operator >(LanguageVersion other) {
461-
// Account for invalid language versions which aren't
462-
// greater or less than any other version.
463-
if (this is InvalidLanguageVersion || other is InvalidLanguageVersion) {
464-
return false;
460+
// Throw an error if comparing as or with an invalid language version.
461+
if (this is InvalidLanguageVersion) {
462+
_throwThisInvalid();
463+
} else if (other is InvalidLanguageVersion) {
464+
_throwOtherInvalid();
465465
}
466466

467467
return compareTo(other) > 0;
468468
}
469469

470470
/// Whether this language version is greater than or equal to [other].
471471
///
472-
/// An [InvalidLanguageVersion] is never less than or greater than
473-
/// any other language version. It is equal only to itself.
472+
/// If either version being compared is an [InvalidLanguageVersion],
473+
/// a [StateError] is thrown. Verify versions are valid before comparing them.
474474
///
475475
/// For details on how valid language versions are compared,
476476
/// check out [LanguageVersion.compareTo].
477477
bool operator >=(LanguageVersion other) {
478-
// Account for invalid language versions only being equal to themselves.
479-
if (identical(this, other)) {
480-
return true;
481-
}
482-
483-
if (this is InvalidLanguageVersion || other is InvalidLanguageVersion) {
484-
return false;
478+
// Throw an error if comparing as or with an invalid language version.
479+
if (this is InvalidLanguageVersion) {
480+
_throwThisInvalid();
481+
} else if (other is InvalidLanguageVersion) {
482+
_throwOtherInvalid();
485483
}
486484

487485
return compareTo(other) >= 0;
488486
}
487+
488+
static Never _throwThisInvalid() => throw StateError(
489+
'Can\'t compare an invalid language version to another language version. '
490+
'Verify language versions are valid after parsing.');
491+
492+
static Never _throwOtherInvalid() => throw StateError(
493+
'Can\'t compare a language version to an invalid language version. '
494+
'Verify language versions are valid after parsing.');
489495
}

pkgs/package_config/test/package_config_impl_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ void main() {
133133
) {
134134
expect(version == otherVersion, identical(version, otherVersion));
135135

136-
expect(version < otherVersion, false);
137-
expect(version <= otherVersion, identical(version, otherVersion));
136+
expect(() => version < otherVersion, throwsA(isA<StateError>()));
137+
expect(() => version <= otherVersion, throwsA(isA<StateError>()));
138138

139-
expect(version > otherVersion, false);
140-
expect(version >= otherVersion, identical(version, otherVersion));
139+
expect(() => version > otherVersion, throwsA(isA<StateError>()));
140+
expect(() => version >= otherVersion, throwsA(isA<StateError>()));
141141
}
142142

143143
var validVersion = LanguageVersion(3, 5);

0 commit comments

Comments
 (0)