semver: add a Version.str() method with unit tests (#12779)

pull/13335/head^2
Subhomoy Haldar 2021-12-10 00:55:55 +05:30 committed by GitHub
parent 8f9f681e81
commit 4c95127cbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -90,6 +90,16 @@ pub fn (v1 Version) le(v2 Version) bool {
return compare_le(v1, v2) return compare_le(v1, v2)
} }
// str returns the `string` representation of the `Version`.
pub fn (ver Version) str() string {
common_string := '${ver.major}.${ver.minor}.$ver.patch'
prerelease_string := if ver.prerelease.len > 0 { '-$ver.prerelease' } else { '' }
metadata_string := if ver.metadata.len > 0 { '+$ver.metadata' } else { '' }
return '$common_string$prerelease_string$metadata_string'
}
// * Utilites. // * Utilites.
// coerce converts the `input` version to a `Version` struct. // coerce converts the `input` version to a `Version` struct.
// coerce will strip any contents *after* the parsed version string: // coerce will strip any contents *after* the parsed version string:

View File

@ -86,6 +86,7 @@ fn test_from() {
assert ver.patch == item.patch assert ver.patch == item.patch
assert ver.metadata == item.metadata assert ver.metadata == item.metadata
assert ver.prerelease == item.prerelease assert ver.prerelease == item.prerelease
assert ver.str() == item.raw
} }
for ver in invalid_versions_to_test { for ver in invalid_versions_to_test {
semver.from(ver) or { semver.from(ver) or {