semver: add a Version.str() method with unit tests (#12779)
parent
8f9f681e81
commit
4c95127cbc
|
@ -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:
|
||||||
|
|
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue