From 77e11f1347dff5d73fd50ac2b64c0c99b1393637 Mon Sep 17 00:00:00 2001 From: JFtechOfficial <36900161+JFtechOfficial@users.noreply.github.com> Date: Wed, 30 Nov 2022 12:12:28 +0100 Subject: [PATCH 1/2] Add tryParse method --- lib/version.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/version.dart b/lib/version.dart index 51199af..55c7184 100644 --- a/lib/version.dart +++ b/lib/version.dart @@ -251,6 +251,18 @@ class Version implements Comparable { } return 0; } + + /// Creates a [Version] instance from a string. + /// + /// The string must conform to the specification at http://semver.org/ + /// Returns null if the string is empty or does not conform to the spec. + static Version? tryParse(String source) { + try { + return Version.parse(source); + } on FormatException { + return null; + } + } static bool _isNumeric(String? s) { if (s == null) { From 906a4b8c7e4b83f8de88a88fb2e0396033aa91b8 Mon Sep 17 00:00:00 2001 From: JFtechOfficial <36900161+JFtechOfficial@users.noreply.github.com> Date: Wed, 30 Nov 2022 12:18:00 +0100 Subject: [PATCH 2/2] Update version_test.dart --- test/version_test.dart | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/test/version_test.dart b/test/version_test.dart index 95dd64d..7c4fd69 100644 --- a/test/version_test.dart +++ b/test/version_test.dart @@ -270,6 +270,43 @@ void main() { expect(() => Version.parse("1.0.0+not^safe"), throwsFormatException); expect(() => Version.parse("1.0.0-not^safe"), throwsFormatException); }); + + + test("TryParse tests", () { + expect(Version.tryParse("0"), equals(new Version(0, 0, 0))); + expect(Version.tryParse("0.0.0"), equals(new Version(0, 0, 0))); + expect(Version.tryParse("1"), equals(new Version(1, 0, 0))); + expect(Version.tryParse("1.0"), equals(new Version(1, 0, 0))); + expect(Version.tryParse("1.2.1"), equals(new Version(1, 2, 1))); + expect(Version.tryParse("0.5.3"), equals(new Version(0, 5, 3))); + expect(Version.tryParse("0.0.3"), equals(new Version(0, 0, 3))); + expect(Version.tryParse("1.2.3.5"), equals(new Version(1, 2, 3))); + expect(Version.tryParse("99999.55465.5456"), + equals(new Version(99999, 55465, 5456))); + expect(Version.tryParse("1.0.0-alpha"), + equals(new Version(1, 0, 0, preRelease: ["alpha"]))); + expect(Version.tryParse("1.0.0+build"), + equals(new Version(1, 0, 0, build: "build"))); + expect( + Version.tryParse("1.0.0-alpha+build"), + equals(new Version(1, 0, 0, + build: "build", preRelease: ["alpha"]))); + expect( + Version.tryParse("1.0.0-alpha.beta+build"), + equals(new Version(1, 0, 0, + build: "build", preRelease: ["alpha", "beta"]))); + + expect( + Version.tryParse("1.0.0-az.AZ.12-3+az.AZ.12-3"), + equals(new Version(1, 0, 0, + build: "az.AZ.12-3", preRelease: ["az", "AZ", "12-3"]))); + + expect(Version.tryParse("a"), isNull); + expect(Version.tryParse("123,4322"), isNull); + expect(Version.tryParse("123a"), isNull); + expect(Version.tryParse("1.0.0+not^safe"), isNull); + expect(Version.tryParse("1.0.0-not^safe"), isNull); + }); test("Increment tests", () { expect(new Version(1, 0, 0).incrementMajor(), equals(new Version(2, 0, 0)));