have a separate SemVerMatcher for parsing semver versions in 2 variants: strict or partial. Fixes SemVerTest

This commit is contained in:
irengrig
2017-03-09 12:03:34 +01:00
parent 7ba6c5dab1
commit 0ec02fd482
3 changed files with 64 additions and 33 deletions
@@ -15,7 +15,6 @@
*/
package com.intellij.util.text;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -86,39 +85,10 @@ public class SemVer implements Comparable<SemVer> {
return myRawVersion;
}
@Nullable
public static SemVer parseFromText(@NotNull String text) {
int majorEndInd = text.indexOf('.');
if (majorEndInd < 0) {
final int major = StringUtil.parseInt(text, -1);
return major < 0 ? null : new SemVer(text, major, 0, 0);
}
int major = StringUtil.parseInt(text.substring(0, majorEndInd), -1);
int minorEndInd = text.indexOf('.', majorEndInd + 1);
if (minorEndInd < 0) {
final int minor = StringUtil.parseInt(text.substring(majorEndInd + 1), -1);
return new SemVer(text, major, minor < 0 ? 0 : minor, 0);
}
int minor = StringUtil.parseInt(text.substring(majorEndInd + 1, minorEndInd), -1);
final String patchStr;
int dashInd = text.indexOf('-', minorEndInd + 1);
if (dashInd >= 0) {
patchStr = text.substring(minorEndInd + 1, dashInd);
}
else {
patchStr = text.substring(minorEndInd + 1);
}
int patch = StringUtil.parseInt(patchStr, -1);
if (major >= 0 && minor >= 0 && patch >= 0) {
return new SemVer(text, major, minor, patch);
}
return null;
}
@NotNull
public static SemVer parseFromTextNonNullize(@Nullable final String text) {
if (text == null) return UNKNOWN;
final SemVer ver = parseFromText(text);
final SemVer ver = SemVerMatcher.parseFromText(text);
return ver == null ? UNKNOWN : ver;
}
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util.text;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Irina.Chernushina on 3/9/2017.
*/
public class SemVerMatcher {
@Nullable
public static SemVer parseFromText(@NotNull String text) {
return parseFromText(text, false);
}
@Nullable
public static SemVer parseFromText(@NotNull String text, final boolean allowPartial) {
int majorEndInd = text.indexOf('.');
if (majorEndInd < 0) {
if (!allowPartial) return null;
final int major = StringUtil.parseInt(text, -1);
return major < 0 ? null : new SemVer(text, major, 0, 0);
}
int major = StringUtil.parseInt(text.substring(0, majorEndInd), -1);
int minorEndInd = text.indexOf('.', majorEndInd + 1);
if (minorEndInd < 0) {
if (!allowPartial) return null;
final int minor = StringUtil.parseInt(text.substring(majorEndInd + 1), -1);
return new SemVer(text, major, minor < 0 ? 0 : minor, 0);
}
int minor = StringUtil.parseInt(text.substring(majorEndInd + 1, minorEndInd), -1);
final String patchStr;
int dashInd = text.indexOf('-', minorEndInd + 1);
if (dashInd >= 0) {
patchStr = text.substring(minorEndInd + 1, dashInd);
}
else {
patchStr = text.substring(minorEndInd + 1);
}
int patch = StringUtil.parseInt(patchStr, -1);
if (major >= 0 && minor >= 0 && patch >= 0) {
return new SemVer(text, major, minor, patch);
}
return null;
}
}
@@ -47,7 +47,7 @@ public class SemVerTest extends TestCase {
}
private static void checkNotParsed(@NotNull String version) {
assertNull(SemVer.parseFromText(version));
assertNull(SemVerMatcher.parseFromText(version));
}
public void testCompare() throws Exception {
@@ -65,7 +65,7 @@ public class SemVerTest extends TestCase {
@NotNull
private static SemVer parseNotNull(@NotNull String text) {
SemVer semVer = SemVer.parseFromText(text);
SemVer semVer = SemVerMatcher.parseFromText(text);
assertNotNull(semVer);
return semVer;
}