Skip to content

Commit

Permalink
First (failing) tests for the ivy support
Browse files Browse the repository at this point in the history
  • Loading branch information
vdurmont committed Sep 10, 2015
1 parent be80939 commit 20981f1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>1.0.1</version>
<version>1.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>semver4j</name>
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/vdurmont/semver4j/Requirement.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ private static Requirement buildWithTokenizer(String requirement, Semver.SemverT
return evaluateReversePolishNotation(rpn.iterator(), type);
}

/**
* Builds a requirement following the rules of Ivy.
*
* @param requirement the requirement as a string
*
* @return the generated requirement
*/
public static Requirement buildIvy(String requirement) {
return new Requirement(null, null, null, null);
}

/**
* Adaptation of the shutting yard algorithm
*/
Expand Down Expand Up @@ -150,8 +161,8 @@ private static Requirement evaluateReversePolishNotation(Iterator<Tokenizer.Toke
Tokenizer.Token token = iterator.next();

if (token.type == Tokenizer.TokenType.VERSION) {
if ("*".equals(token.value)) {
// Special case for "*"
if ("*".equals(token.value) || (type == Semver.SemverType.NPM && "latest".equals(token.value))) {
// Special case for "*" and "latest" in NPM
return new Requirement(new Range("0.0.0", Range.RangeOperator.GTE), null, null, null);
}
Semver version = new Semver(token.value, type);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/com/vdurmont/semver4j/Semver.java
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,13 @@ public enum SemverType {
* Supports optimistic and comparison operators
* See https://guides.cocoapods.org/using/the-podfile.html
*/
COCOAPODS
COCOAPODS,

/**
* Follows the rules of ivy.
* Supports dynamic parts (eg: 4.2.+) and ranges
* See http://ant.apache.org/ivy/history/latest-milestone/ivyfile/dependency.html
*/
IVY
}
}
62 changes: 59 additions & 3 deletions src/test/java/com/vdurmont/semver4j/RequirementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,44 @@ public class RequirementTest {
assertEquals(new Semver("0.0.0"), req.range.version);
}

@Test public void buildIvy_with_a_dynamic_patch() {
Requirement req = Requirement.buildIvy("1.2.+");
assertEquals(Requirement.RequirementOperator.AND, req.op);
assertNull(req.range);
assertIsRange(req.req1, "1.2.0", Range.RangeOperator.GTE);
assertIsRange(req.req2, "1.3.0", Range.RangeOperator.LT);
}

@Test public void buildIvy_with_a_dynamic_minor() {
Requirement req = Requirement.buildIvy("1.+");
assertEquals(Requirement.RequirementOperator.AND, req.op);
assertNull(req.range);
assertIsRange(req.req1, "1.0.0", Range.RangeOperator.GTE);
assertIsRange(req.req2, "2.0.0", Range.RangeOperator.LT);
}

@Test public void buildIvy_with_latest() {
Requirement req = Requirement.buildIvy("latest.integration");
assertNull(req.op);
assertNull(req.req1);
assertNull(req.req2);
assertIsRange(req, "0.0.0", Range.RangeOperator.GTE);
}

@Test public void buildIvy_with_mathematical_bounded_ranges() {
rangeTest(Requirement.buildIvy("[1.0,2.0]"), "1.0.0", false, "2.0.0", false);
rangeTest(Requirement.buildIvy("[1.0,2.0["), "1.0.0", false, "2.0.0", true);
rangeTest(Requirement.buildIvy("]1.0,2.0]"), "1.0.0", true, "2.0.0", false);
rangeTest(Requirement.buildIvy("]1.0,2.0["), "1.0.0", true, "2.0.0", true);
}

@Test public void buildIvy_with_mathematical_unbounded_ranges() {
assertIsRange(Requirement.buildIvy("[1.0,)"), "1.0.0", Range.RangeOperator.GTE);
assertIsRange(Requirement.buildIvy("]1.0,)"), "1.0.0", Range.RangeOperator.GT);
assertIsRange(Requirement.buildIvy("(,2.0]"), "2.0.0", Range.RangeOperator.LTE);
assertIsRange(Requirement.buildIvy("(,2.0["), "2.0.0", Range.RangeOperator.LT);
}

@Test public void isSatisfiedBy_with_a_complex_example() {
Requirement req = Requirement.buildNPM("1.x || >=2.5.0 || 5.0.0 - 7.2.3");

Expand Down Expand Up @@ -314,6 +352,20 @@ public class RequirementTest {
verify(req2).isSatisfiedBy(version);
}

@Test public void isSatisfiedBy_with_a_star() {
Requirement req = Requirement.buildNPM("*");
assertTrue(req.isSatisfiedBy("1.2.3"));
assertTrue(req.isSatisfiedBy("2.5.2"));
assertTrue(req.isSatisfiedBy("0.2.3"));
}

@Test public void isSatisfiedBy_with_latest() {
Requirement req = Requirement.buildNPM("latest");
assertTrue(req.isSatisfiedBy("1.2.3"));
assertTrue(req.isSatisfiedBy("2.5.2"));
assertTrue(req.isSatisfiedBy("0.2.3"));
}

@Test public void tildeRequirement_cocoapods() {
// '~> 0.1.2' Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher
tildeTest("0.1.2", "0.1.2", "0.2.0", Semver.SemverType.COCOAPODS);
Expand Down Expand Up @@ -360,16 +412,20 @@ private static void hyphenTest(String reqLower, String reqUpper, String lower, S
}

private static void rangeTest(Requirement req, String lower, String upper, boolean upperStrict) {
rangeTest(req, lower, false, upper, upperStrict);
}

private static void rangeTest(Requirement req, String lower, boolean lowerStrict, String upper, boolean upperStrict) {
assertNull(req.range);
assertEquals(Requirement.RequirementOperator.AND, req.op);

Requirement req1 = req.req1;
assertEquals(Range.RangeOperator.GTE, req1.range.op);
Range.RangeOperator lowOp = lowerStrict ? Range.RangeOperator.GT : Range.RangeOperator.GTE;
assertEquals(lowOp, req1.range.op);
assertEquals(lower, req1.range.version.getValue());

Range.RangeOperator upOp = upperStrict ? Range.RangeOperator.LT : Range.RangeOperator.LTE;

Requirement req2 = req.req2;
Range.RangeOperator upOp = upperStrict ? Range.RangeOperator.LT : Range.RangeOperator.LTE;
assertEquals(upOp, req2.range.op);
assertEquals(upper, req2.range.version.getValue());
}
Expand Down

0 comments on commit 20981f1

Please sign in to comment.