-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from yumemi-inc/improve/remove-pub_semver-pac…
…kage
- Loading branch information
Showing
7 changed files
with
174 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/// A parsed semantic version number. However, only the major and minor | ||
/// versions are processed. | ||
class Version implements Comparable<Version> { | ||
const Version(this._major, this._minor) | ||
: assert(_major >= 0, 'Major version must be non-negative.'), | ||
assert(_minor >= 0, 'Minor version must be non-negative.'); | ||
|
||
factory Version.parse(String text) { | ||
if (text.contains('<') && !text.contains('>=')) { | ||
throw MinimumVersionMissingException(text); | ||
} | ||
|
||
final RegExp regExp; | ||
if (text.contains('>=')) { | ||
regExp = RegExp(r'>=\s*(\d+)\.(\d+)'); | ||
} else { | ||
regExp = RegExp(r'\^?(\d+)\.(\d+)'); | ||
} | ||
final match = regExp.firstMatch(text); | ||
|
||
if (match == null) { | ||
throw UnsupportedVersionFormatException(text); | ||
} | ||
|
||
final major = int.parse(match[1]!); | ||
final minor = int.parse(match[2]!); | ||
return Version(major, minor); | ||
} | ||
|
||
final int _major; | ||
final int _minor; | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
other is Version && _major == other._major && _minor == other._minor; | ||
|
||
@override | ||
int get hashCode => _major ^ _minor; | ||
|
||
@override | ||
int compareTo(Version other) { | ||
if (_major != other._major) { | ||
return _major.compareTo(other._major); | ||
} | ||
if (_minor != other._minor) { | ||
return _minor.compareTo(other._minor); | ||
} | ||
return 0; | ||
} | ||
|
||
bool operator <(Version other) => compareTo(other) < 0; | ||
bool operator >(Version other) => compareTo(other) > 0; | ||
bool operator <=(Version other) => compareTo(other) <= 0; | ||
bool operator >=(Version other) => compareTo(other) >= 0; | ||
|
||
@override | ||
String toString() => '$_major.$_minor'; | ||
} | ||
|
||
abstract class _VersionParseException extends FormatException { | ||
const _VersionParseException({ | ||
required String text, | ||
required String message, | ||
}) : super( | ||
'Could not parse `$text`. $message', | ||
); | ||
} | ||
|
||
class MinimumVersionMissingException extends _VersionParseException { | ||
const MinimumVersionMissingException(String text) | ||
: super( | ||
text: text, | ||
message: 'The minimum version must be specified if the version ' | ||
'is in range format.', | ||
); | ||
} | ||
|
||
class UnsupportedVersionFormatException extends _VersionParseException { | ||
const UnsupportedVersionFormatException(String text) | ||
: super( | ||
text: text, | ||
message: | ||
'Only version formats matching the following regular expressions ' | ||
r'are supported: `>=\s*(\d+)\.(\d+)` or `\^?(\d+)\.(\d+)`.', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'package:test/test.dart'; | ||
import 'package:yumemi_lints/src/models/version.dart'; | ||
|
||
void main() { | ||
group('Version.parse()', () { | ||
// arrange | ||
const successfulScenarios = [ | ||
{ | ||
'input': '^2.17.0', | ||
'expected': Version(2, 17), | ||
}, | ||
{ | ||
'input': '2.17.0', | ||
'expected': Version(2, 17), | ||
}, | ||
{ | ||
'input': '>=2.17.0 <4.0.0', | ||
'expected': Version(2, 17), | ||
}, | ||
{ | ||
'input': '<4.0.0 >=2.17.0', | ||
'expected': Version(2, 17), | ||
}, | ||
{ | ||
'input': '^2.17.1', | ||
'expected': Version(2, 17), | ||
}, | ||
]; | ||
|
||
for (final scenario in successfulScenarios) { | ||
final input = scenario['input']! as String; | ||
final expected = scenario['expected']! as Version; | ||
|
||
test( | ||
'The version is successfully parsed ' | ||
'when the input text is `$input`', | ||
() { | ||
// act | ||
final actual = Version.parse(input); | ||
|
||
// assert | ||
expect(actual, expected); | ||
}, | ||
); | ||
} | ||
|
||
// arrange | ||
final failureScenarios = [ | ||
{ | ||
'input': '<4.0.0', | ||
'expected': throwsA(isA<MinimumVersionMissingException>()), | ||
}, | ||
{ | ||
'input': 'any', | ||
'expected': throwsA(isA<UnsupportedVersionFormatException>()), | ||
}, | ||
]; | ||
|
||
for (final scenario in failureScenarios) { | ||
final input = scenario['input']! as String; | ||
final expected = scenario['expected']! as Matcher; | ||
|
||
test( | ||
'The version fails to be parsed ' | ||
'when the input text is `$input`', | ||
() { | ||
// act | ||
void actual() { | ||
Version.parse(input); | ||
} | ||
|
||
// assert | ||
expect(actual, expected); | ||
}, | ||
); | ||
} | ||
}); | ||
} |