summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid E. Wheeler <david@justatheory.com>2020-05-08 18:41:53 -0400
committerDavid E. Wheeler <david@justatheory.com>2020-05-08 18:41:53 -0400
commit0023a806e96670723c7475687fe3e5ffcfd9a378 (patch)
tree01987af8683ed1d0d862b4d798f866015b3f1c23
parent118c51cad8319ae681edd7f2aae7d688139505cd (diff)
Explicitly overload bool operations
Some implementations of version::vpp sadly raise an error by calling `SemVer->new("0")`, which is not a strictly valid SemVer. So overload it to compare to `SemVer->parse("0.0.0")`, instead, to ensure consistent behavior.
-rw-r--r--Changes2
-rw-r--r--lib/SemVer.pm13
-rw-r--r--t/base.t8
-rw-r--r--t/corpus.t9
4 files changed, 15 insertions, 17 deletions
diff --git a/Changes b/Changes
index a4718b2..f38a336 100644
--- a/Changes
+++ b/Changes
@@ -15,6 +15,8 @@ Revision history for Perl extension SemVer.
- Added tests for the official SemVer test corpus as linked under the
FAQ about an official regular expression:
https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
+ - Added explicit boolean overloading to ensure consistent behavior
+ between different implementations of the version parent class.
0.7.0 2018-07-24T11:09:17Z
- Implemented Semantic Versioning 2.0.0 specification
diff --git a/lib/SemVer.pm b/lib/SemVer.pm
index 9ba0994..9a7f3c9 100644
--- a/lib/SemVer.pm
+++ b/lib/SemVer.pm
@@ -9,6 +9,7 @@ use overload (
'""' => 'stringify',
'<=>' => 'vcmp',
'cmp' => 'vcmp',
+ 'bool' => 'vbool',
);
our @ISA = qw(version);
@@ -106,6 +107,10 @@ sub normal {
sub numify { _die 'Semantic versions cannot be numified'; }
sub is_alpha { !!shift->{extra} }
+sub vbool {
+ my $self = shift;
+ return version::vcmp($self, $self->declare("0.0.0"), 1);
+}
# Sort Ordering:
# Precedence refers to how versions are compared to each other when ordered. Precedence MUST be calculated by
@@ -356,6 +361,14 @@ Returns true if an ASCII string is appended to the end of the version string.
This also means that the version number is a "special version", in the
semantic versioning specification meaning of the phrase.
+=head3 C<vbool>
+
+ say "Version $semver" if $semver;
+ say "Not a $semver" if !$semver;
+
+Returns true for a non-zero semantic semantic version object, without regard
+to the prerelease or build metadata parts. Overload boolean operations.
+
=head3 C<vcmp>
Compares the semantic version object to another version object or string and
diff --git a/t/base.t b/t/base.t
index 4e930d9..b0d3812 100644
--- a/t/base.t
+++ b/t/base.t
@@ -12,14 +12,6 @@ my $CLASS;
BEGIN {
$CLASS = 'SemVer';
use_ok $CLASS or die;
-
- unless (eval { my $x = !$CLASS->new('1.0.0') }) {
- # Borked version:vpp. Overload bool.
- diag 'here';
- $CLASS->overload::OVERLOAD(bool => sub {
- version::vcmp(shift, $CLASS->declare('0.0.0'), 1);
- });
- }
}
diag 'Testing with version v', version->VERSION;
diff --git a/t/corpus.t b/t/corpus.t
index fd21afd..d5dbcb2 100644
--- a/t/corpus.t
+++ b/t/corpus.t
@@ -11,15 +11,6 @@ use FindBin qw($Bin);
use lib "$Bin/../lib";
use SemVer;
-BEGIN {
- unless (eval { my $x = !SemVer->new('1.0.0') }) {
- # Borked version:vpp. Overload bool.
- SemVer->overload::OVERLOAD(bool => sub {
- version::vcmp(shift, SemVer->declare('0.0.0'), 1);
- });
- }
-}
-
# Valid Semantic Versions
for my $v (qw(
0.0.4