Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@maspalio
Last active May 6, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maspalio/165676e2e2033947f13fc9bcdf07c201 to your computer and use it in GitHub Desktop.
Save maspalio/165676e2e2033947f13fc9bcdf07c201 to your computer and use it in GitHub Desktop.
semver.t
#!/usr/bin/env perl
use Modern::Perl;
use Path::Tiny;
use Test::More;
my $expected = shift // 'pg-semver/test/expected/base.out';
# https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
my $semver_re = qr/(?x: ^
(?P<major> 0 | [1-9][0-9]* )
\. (?P<minor> 0 | [1-9][0-9]* )
\. (?P<patch> 0 | [1-9][0-9]* )
(?:
-
(?P<prerelease>
(?:
0
| [1-9] [0-9]*
| [0-9]* [a-zA-Z-] [0-9a-zA-Z-]*
)
(?:
\.
(?:
0
| [1-9] [0-9]*
| [0-9]* [a-zA-Z-] [0-9a-zA-Z-]*
)
)*
)
)?
(?:
\+
(?P<buildmetadata>
[0-9a-zA-Z-]+ (?: \. [0-9a-zA-Z-]+ )*
)
)?
$ )/;
my $valid_re = qr/(?x: ^ ok \s \d+ \s - \s " (?<version> [^"]+ ) " \s is \s a \s valid \s semver $ )/;
my $invalid_re = qr/(?x: ^ ok \s \d+ \s - \s " (?<version> [^"]+ ) " \s is \s not \s a \s valid \s semver $ )/;
my $path = path ( $expected );
my @lines = $path->lines ();
my @valids = grep { defined } grep { $_ =~ $valid_re and $_ = $+{version} } @lines;
my @invalids = grep { defined } grep { $_ =~ $invalid_re and $_ = $+{version} } @lines;
plan tests => @valids + @invalids;
ok ( $_ =~ $semver_re, "\"$_\" is a valid semver" ) for @valids;
ok ( $_ !~ $semver_re, "\"$_\" is not a valid semver" ) for @invalids;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment