Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing caret ranges (cleanup trailing space) #122

Merged
merged 1 commit into from
Dec 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/main/java/org/semver4j/RangesListFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
import static org.semver4j.internal.range.RangeProcessorPipeline.startWith;

public class RangesListFactory {
private static final Pattern pattern = compile(COMPARATOR);
private static final Pattern splitterPattern = compile("(\\s*)((?:<|>)?=?)\\s*");
private static final Pattern comparatorPattern = compile(COMPARATOR);

public static RangesList create(String range) {
range = range.trim();
RangesList rangesList = new RangesList();

Pattern compile = compile("(\\s*)((?:<|>)?=?)\\s*");

String[] rangeSections = range.split("\\|\\|");
for (String rangeSection : rangeSections) {
rangeSection = stripWhitespacesBetweenRangeOperator(compile, rangeSection);
rangeSection = stripWhitespacesBetweenRangeOperator(rangeSection);
rangeSection = applyProcessors(rangeSection);

List<Range> ranges = addRanges(rangeSection);
Expand All @@ -33,9 +32,9 @@ public static RangesList create(String range) {
return rangesList;
}

private static String stripWhitespacesBetweenRangeOperator(Pattern compile, String rangeSection) {
Matcher matcher = compile.matcher(rangeSection);
return matcher.replaceAll("$1$2");
private static String stripWhitespacesBetweenRangeOperator(String rangeSection) {
Matcher matcher = splitterPattern.matcher(rangeSection);
return matcher.replaceAll("$1$2").trim();
}

private static String applyProcessors(String range) {
Expand All @@ -53,7 +52,7 @@ private static List<Range> addRanges(String range) {

String[] parsedRanges = range.split("\\s+");
for (String parsedRange : parsedRanges) {
Matcher matcher = pattern.matcher(parsedRange);
Matcher matcher = comparatorPattern.matcher(parsedRange);
if (matcher.matches()) {
String rangeOperator = matcher.group(1);
String version = matcher.group(2);
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/semver4j/RangesListFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ void shouldStripWhitespacesBetweenRangeOperator() {
//then
assertThat(range).isEqualTo("<=2.6.8 or (>=3.0.0 and <=3.0.1) or >5.0.0");
}

@Test
void shouldCorrectParseCaretRangesWithSpace() {
//given
RangesList rangesList = RangesListFactory.create("^14.14.20 || ^16.0.0");

//when
String range = rangesList.toString();

//then
assertThat(range).isEqualTo("(>=14.14.20 and <15.0.0) or (>=16.0.0 and <17.0.0)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ void shouldParseRangeWhenPatchIsXAndMajorIsZero() {
assertThat(range).isEqualTo(">=0.1.0 <0.2.0");
}

@Test
void shouldParseRangeWhenHasAllVersionNumbers() {
//when
String range = processor.process("^14.14.20");

//then
assertThat(range).isEqualTo(">=14.14.20 <15.0.0");
}

@Test
void shouldParseRangeWhenPreReleaseIsSetMajorIsZero() {
//when
Expand Down