Create incrementors for Pre-release version & Build metadata
This commit is contained in:
parent
98abdb4c02
commit
b39868a540
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -224,9 +224,53 @@ public class VersionTest {
|
|||
assertEquals("1.0.0+build", v2.toString());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user