Skip to content

Commit

Permalink
Fix semver comparison logic (apple#773)
Browse files Browse the repository at this point in the history
Fix `isLessThan` logic between semvers
  • Loading branch information
bioball authored Nov 4, 2024
1 parent 9692504 commit 4b4d81b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ examples {
test.catch(() -> semver.Version("1.02.3"))
test.catch(() -> semver.Version("aaa"))
}

["List.sortWith()"] {
List(
semver.Version("100.0.0"),
semver.Version("0.100.0"),
semver.Version("0.0.100")
)
.sortWith(semver.comparator)
}
}

facts {
Expand Down Expand Up @@ -123,6 +132,9 @@ facts {

!semver.Version("1.2.3-alpha+def").isLessThan(semver.Version("1.2.3-alpha+abc"))
!semver.Version("1.2.3-alpha+abc").isLessThan(semver.Version("1.2.3-alpha+def"))

semver.Version("1.2.3").isLessThan(semver.Version("2.0.100"))
semver.Version("1.2.3").isLessThan(semver.Version("2.100.0"))
}

["Version.isLessThanOrEquals()"] {
Expand All @@ -141,6 +153,10 @@ facts {
semver.Version("1.3.3").isGreaterThan(semver.Version("1.2.3"))
semver.Version("1.2.4").isGreaterThan(semver.Version("1.2.3"))
semver.Version("1.2.3-beta").isGreaterThan(semver.Version("1.2.3-alpha"))

semver.Version("2.0.100").isGreaterThan(semver.Version("1.2.3"))
semver.Version("2.100.0").isGreaterThan(semver.Version("1.2.3"))
semver.Version("2.0.100").isGreaterThan(semver.Version("1.2.3"))
}

["Version.isGreaterThanOrEquals()"] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ facts {
true
true
true
true
true
}
["Version.isLessThanOrEquals()"] {
true
Expand All @@ -108,6 +110,9 @@ facts {
true
true
true
true
true
true
}
["Version.isGreaterThanOrEquals()"] {
true
Expand Down Expand Up @@ -172,4 +177,19 @@ examples {
"`1.02.3` is not a valid semantic version number."
"`aaa` is not a valid semantic version number."
}
["List.sortWith()"] {
List(new {
major = 0
minor = 0
patch = 100
}, new {
major = 0
minor = 100
patch = 0
}, new {
major = 100
minor = 0
patch = 0
})
}
}
20 changes: 12 additions & 8 deletions stdlib/semver.pkl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ class Version {
///
/// Note: `version1.equals(version2)` differs from `version1 == version2` in that it ignores [build].
function equals(other: Version): Boolean =
major == other.major &&
minor == other.minor &&
patch == other.patch &&
preRelease == other.preRelease
major == other.major
&& minor == other.minor
&& patch == other.patch
&& preRelease == other.preRelease

/// Tells whether this version is less than [other] according to semantic versioning rules.
///
Expand All @@ -157,10 +157,14 @@ class Version {
/// semver.Version("1.0.0-rc.1").isLessThan(semver.Version("1.0.0"))
/// ```
function isLessThan(other: Version): Boolean =
major < other.major ||
minor < other.minor ||
patch < other.patch ||
isPreReleaseLessThan(other)
major < other.major
|| major == other.major && minor < other.minor
|| major == other.major && minor == other.minor && patch < other.patch
||
major == other.major
&& minor == other.minor
&& patch == other.patch
&& isPreReleaseLessThan(other)

/// Tells whether this version is less than or equal to [other] according to semantic versioning rules.
function isLessThanOrEquals(other: Version): Boolean =
Expand Down

0 comments on commit 4b4d81b

Please sign in to comment.