[jsemver] 29/95: Create incrementors for Pre-release version & Build metadata

Alexandre Viau reazem-guest at moszumanska.debian.org
Mon Feb 16 14:58:27 UTC 2015


This is an automated email from the git hooks/post-receive script.

reazem-guest pushed a commit to branch master
in repository jsemver.

commit b39868a540caa619d35bcd4fa8135aac7d3565d3
Author: Zafar Khaja <zafarkhaja at gmail.com>
Date:   Sat Mar 30 22:21:07 2013 +0400

    Create incrementors for Pre-release version & Build metadata
---
 .../github/zafarkhaja/semver/MetadataVersion.java  | 23 ++++++++++
 .../java/com/github/zafarkhaja/semver/Version.java | 14 ++++++
 .../zafarkhaja/semver/MetadataVersionTest.java     | 21 +++++++++
 .../com/github/zafarkhaja/semver/VersionTest.java  | 52 +++++++++++++++++++++-
 4 files changed, 109 insertions(+), 1 deletion(-)

diff --git a/src/main/java/com/github/zafarkhaja/semver/MetadataVersion.java b/src/main/java/com/github/zafarkhaja/semver/MetadataVersion.java
index 086cd14..70fdf24 100644
--- a/src/main/java/com/github/zafarkhaja/semver/MetadataVersion.java
+++ b/src/main/java/com/github/zafarkhaja/semver/MetadataVersion.java
@@ -23,6 +23,7 @@
  */
 package com.github.zafarkhaja.semver;
 
+import java.util.Arrays;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -50,6 +51,20 @@ class MetadataVersion implements Comparable<MetadataVersion> {
         this.value = matcher.group(0);
     }
 
+    MetadataVersion increment() {
+        String[] ids  = value.split("\\.");
+
+        String lastId = ids[ids.length - 1];
+        if (isInt(lastId)) {
+            int intId = Integer.parseInt(lastId);
+            ids[ids.length - 1] = String.valueOf(++intId);
+        } else {
+            ids = Arrays.copyOf(ids, ids.length + 1);
+            ids[ids.length - 1] = String.valueOf(1);
+        }
+        return new MetadataVersion(joinIdentifiers(ids));
+    }
+
     @Override
     public boolean equals(Object other) {
         if (this == other) {
@@ -115,4 +130,12 @@ class MetadataVersion implements Comparable<MetadataVersion> {
         }
         return true;
     }
+
+    private String joinIdentifiers(String[] ids) {
+        StringBuilder sb = new StringBuilder();
+        for (String id : ids) {
+            sb.append(id).append(".");
+        }
+        return sb.deleteCharAt(sb.lastIndexOf(".")).toString();
+    }
 }
diff --git a/src/main/java/com/github/zafarkhaja/semver/Version.java b/src/main/java/com/github/zafarkhaja/semver/Version.java
index 01e83f3..b5efed6 100644
--- a/src/main/java/com/github/zafarkhaja/semver/Version.java
+++ b/src/main/java/com/github/zafarkhaja/semver/Version.java
@@ -133,6 +133,20 @@ public class Version implements Comparable<Version> {
         );
     }
 
+    public Version incrementPreReleaseVersion() {
+        if (preRelease == null) {
+            throw new NullPointerException("Pre-release version is NULL");
+        }
+        return new Version(normal, preRelease.increment());
+    }
+
+    public Version incrementBuildMetadata() {
+        if (build == null) {
+            throw new NullPointerException("Build metadata is NULL");
+        }
+        return new Version(normal, preRelease, build.increment());
+    }
+
     public Version setPreReleaseVersion(String preRelease) {
         return new Version(normal, new MetadataVersion(preRelease));
     }
diff --git a/src/test/java/com/github/zafarkhaja/semver/MetadataVersionTest.java b/src/test/java/com/github/zafarkhaja/semver/MetadataVersionTest.java
index f6b73b7..9214dc3 100644
--- a/src/test/java/com/github/zafarkhaja/semver/MetadataVersionTest.java
+++ b/src/test/java/com/github/zafarkhaja/semver/MetadataVersionTest.java
@@ -92,6 +92,27 @@ public class MetadataVersionTest {
             assertTrue(v1.equals(v2));
             assertFalse(v1.equals(v3));
         }
+
+        @Test
+        public void shouldProvideIncrementMethod() {
+            MetadataVersion v1 = new MetadataVersion("alpha.1");
+            MetadataVersion v2 = v1.increment();
+            assertEquals("alpha.2", v2.toString());
+        }
+
+        @Test
+        public void shouldAppendOneAsLastIdentifierIfLastOneIsAlphaNumericWhenIncrementing() {
+            MetadataVersion v1 = new MetadataVersion("alpha");
+            MetadataVersion v2 = v1.increment();
+            assertEquals("alpha.1", v2.toString());
+        }
+
+        @Test
+        public void shouldBeImmutable() {
+            MetadataVersion v1 = new MetadataVersion("alpha.1");
+            MetadataVersion v2 = v1.increment();
+            assertNotSame(v1, v2);
+        }
     }
 
     public static class EqualsMethodTest {
diff --git a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
index e90a974..fcf3e6d 100644
--- a/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
+++ b/src/test/java/com/github/zafarkhaja/semver/VersionTest.java
@@ -225,8 +225,52 @@ public class VersionTest {
         }
 
         @Test
+        public void shouldProvideIncrementPreReleaseVersionMethod() {
+            Version v1 = Version.valueOf("1.0.0-beta.1");
+            Version v2 = v1.incrementPreReleaseVersion();
+            assertEquals("1.0.0-beta.2", v2.toString());
+        }
+
+        @Test
+        public void shouldThrowExceptionWhenIncrementingPreReleaseIfItsNull() {
+            Version v1 = Version.valueOf("1.0.0");
+            try {
+                Version v2 = v1.incrementPreReleaseVersion();
+            } catch (NullPointerException e) {
+                return;
+            }
+            fail("Method was expected to throw NullPointerException");
+        }
+
+        @Test
+        public void shouldDropBuildMetadataWhenIncrementingPreReleaseVersion() {
+            Version v1 = Version.valueOf("1.0.0-beta.1+build");
+            Version v2 = v1.incrementPreReleaseVersion();
+            assertEquals("1.0.0-beta.2", v2.toString());
+        }
+
+
+        @Test
+        public void shouldProvideIncrementBuildMetadataMethod() {
+            Version v1 = Version.valueOf("1.0.0+build.1");
+            Version v2 = v1.incrementBuildMetadata();
+            assertEquals("1.0.0+build.2", v2.toString());
+        }
+
+        @Test
+        public void shouldThrowExceptionWhenIncrementingBuildIfItsNull() {
+            Version v1 = Version.valueOf("1.0.0");
+            try {
+                Version v2 = v1.incrementBuildMetadata();
+            } catch (NullPointerException e) {
+                return;
+            }
+            fail("Method was expected to throw NullPointerException");
+        }
+
+        @Test
         public void shouldBeImmutable() {
-            Version version = Version.valueOf("1.2.3");
+            Version version = Version.valueOf("1.2.3-alpha+build");
 
             Version incementedMajor = version.incrementMajorVersion();
             assertNotSame(version, incementedMajor);
@@ -242,6 +286,12 @@ public class VersionTest {
 
             Version buildSet = version.setBuildMetadata("build");
             assertNotSame(version, buildSet);
+
+            Version incrementedPreRelease = version.incrementPreReleaseVersion();
+            assertNotSame(version, incrementedPreRelease);
+
+            Version incrementedBuild = version.incrementBuildMetadata();
+            assertNotSame(version, incrementedBuild);
         }
     }
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-java/jsemver.git



More information about the pkg-java-commits mailing list