summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2018-07-10 22:02:35 +0200
committerPatrick Spek <p.spek@tyil.nl>2018-07-10 22:02:35 +0200
commit3a2e3f0174d5a9d92be563f5a51585db506850ea (patch)
tree43a8714cb94131b274810f22378383e532cd31f1
parentf16a916f76234e7ecbf1fe49e5d16a07805249a6 (diff)
downloadVersion::Semantic-3a2e3f0174d5a9d92be563f5a51585db506850ea.tar.gz
Version::Semantic-3a2e3f0174d5a9d92be563f5a51585db506850ea.tar.bz2
Initial draft
-rw-r--r--META6.json2
-rw-r--r--lib/Version/Semantic.pm6140
-rw-r--r--t/01-new.t17
-rw-r--r--t/02-bump.t21
-rw-r--r--t/03-retrieve.t19
5 files changed, 198 insertions, 1 deletions
diff --git a/META6.json b/META6.json
index ec4deb8..133c11b 100644
--- a/META6.json
+++ b/META6.json
@@ -12,7 +12,7 @@
"name": "Version::Semantic",
"perl": "6.c",
"provides": {
-
+ "Version::Semantic": "lib/Version/Semantic.pm6"
},
"resources": [
diff --git a/lib/Version/Semantic.pm6 b/lib/Version/Semantic.pm6
new file mode 100644
index 0000000..1842672
--- /dev/null
+++ b/lib/Version/Semantic.pm6
@@ -0,0 +1,140 @@
+#! /usr/bin/env false
+
+use v6.c;
+
+class Version::Semantic
+{
+ has Int $.major;
+ has Int $.minor;
+ has Int $.patch;
+
+ multi method new (
+ Version:D $v,
+ ) {
+ self.new(|$v.parts);
+ }
+
+ multi method new (
+ Str:D $v,
+ ) {
+ self.new(|$v.split(".")>>.Int);
+ }
+
+ multi method new (
+ Int:D $major where -1 < * = 0,
+ Int:D $minor where -1 < * = 0,
+ Int:D $patch where -1 < * = 0,
+ ) {
+ self.bless(
+ :$major,
+ :$minor,
+ :$patch,
+ );
+ }
+
+ multi method new
+ {
+ die "Semantic::Version should be created with .new(Int, Int, Int), where each Int is 0 or higher";
+ }
+
+ method bump-major
+ {
+ $!major++;
+
+ $!minor = 0;
+ $!patch = 0;
+
+ return self;
+ }
+
+ method bump-minor
+ {
+ $!minor++;
+
+ $!patch = 0;
+
+ return self;
+ }
+
+ method bump-patch
+ {
+ $!patch++;
+
+ return self;
+ }
+
+ method parts
+ {
+ return ($!major, $!minor, $!patch);
+ }
+
+ method perl
+ {
+ return "Semantic::Version.new(\"{self.Str}\")";
+ }
+
+ method gist
+ {
+ return "Semantic::Version.new({self.Str})";
+ }
+
+ method Str
+ {
+ return "{$!major}.{$!minor}.{$!patch}";
+ }
+}
+
+=begin pod
+
+=NAME Version::Semantic
+=AUTHOR Patrick Spek <p.spek@tyil.work>
+=VERSION 0.0.0
+
+=head1 SYNOPSIS
+
+=item1 C<Version::Semantic.new>
+=item1 C<Version::Semantic.new(v0)>
+
+=head1 DESCRIPTION
+
+A variant on C<Version> which enforces
+L<Semantic Versioning|https://semver.org>.
+
+=head1 EXAMPLES
+
+=head2 Creating, bumping and saying
+
+=begin input
+use Version::Semantic;
+
+my Version::Semantic $v .= new;
+
+$v.bump-major;
+$v.bump-patch;
+
+say $v;
+=end input
+
+=begin output
+1.0.1
+=end output
+
+=head2 Create from existing Version
+
+=begin input
+use Version::Semantic;
+
+say Version::Semantic.new(v2.6.7);
+=end input
+
+=begin output
+2.6.7
+=end output
+
+=head1 SEE ALSO
+
+=item1 C<Version>
+
+=end pod
+
+# vim: ft=perl6 noet
diff --git a/t/01-new.t b/t/01-new.t
new file mode 100644
index 0000000..0f2b41d
--- /dev/null
+++ b/t/01-new.t
@@ -0,0 +1,17 @@
+#! /usr/bin/env perl6
+
+use v6.c;
+
+use Test;
+use Version::Semantic;
+
+plan 5;
+
+is Version::Semantic.new(v1.9.2).perl, "Semantic::Version.new(\"1.9.2\")", "Initializes with Version";
+is Version::Semantic.new("8.9.1").perl, "Semantic::Version.new(\"8.9.1\")", "Initializes with Str";
+is Version::Semantic.new(7, 8, 1).perl, "Semantic::Version.new(\"7.8.1\")", "Initializes with Int, Int, Int";
+is Version::Semantic.new.perl, "Semantic::Version.new(\"0.0.0\")", "Initializes without arguments";
+
+dies-ok { dd Version::Semantic.new("1.3.3.7") }, "Does not initialize with incorrectly formatted Str";
+
+# vim: ft=perl6 noet
diff --git a/t/02-bump.t b/t/02-bump.t
new file mode 100644
index 0000000..2ddec06
--- /dev/null
+++ b/t/02-bump.t
@@ -0,0 +1,21 @@
+#! /usr/bin/env perl6
+
+use v6.c;
+
+use Test;
+use Version::Semantic;
+
+my Version::Semantic $v .= new;
+
+plan 8;
+
+is $v.bump-major, "1.0.0", "bump-major on 0.0.0";
+is $v.bump-minor, "1.1.0", "bump-minor on 1.0.0";
+is $v.bump-major, "2.0.0", "bump-major on 1.1.0";
+is $v.bump-patch, "2.0.1", "bump-patch on 2.0.0";
+is $v.bump-major, "3.0.0", "bump-major on 2.0.1";
+is $v.bump-minor, "3.1.0", "bump-minor on 3.0.0";
+is $v.bump-patch, "3.1.1", "bump-patch on 3.1.0";
+is $v.bump-major, "4.0.0", "bump-major on 3.1.1";
+
+# vim: ft=perl6 noet
diff --git a/t/03-retrieve.t b/t/03-retrieve.t
new file mode 100644
index 0000000..4584ba1
--- /dev/null
+++ b/t/03-retrieve.t
@@ -0,0 +1,19 @@
+#! /usr/bin/env perl6
+
+use v6.c;
+
+use Test;
+use Version::Semantic;
+
+my Version::Semantic $v .= new(2, 4, 6);
+
+plan 5;
+
+is $v.patch, 6, "Patch level correct";
+is $v.minor, 4, "Minor level correct";
+is $v.major, 2, "Major level correct";
+
+is $v.parts, (2, 4, 6), "Parts are correct";
+is ~$v, "2.4.6", "Stringifies correctly";
+
+# vim: ft=perl6 noet