1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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)
}
// 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.
// coerce converts the `input` version to a `Version` struct.
// 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.metadata == item.metadata
assert ver.prerelease == item.prerelease
assert ver.str() == item.raw
}
for ver in invalid_versions_to_test {
semver.from(ver) or {