Skip to content

Commit

Permalink
Merge pull request #4 from jbowes/prerelease-method
Browse files Browse the repository at this point in the history
Add a prerelease method to version
  • Loading branch information
jbowes authored Sep 11, 2021
2 parents 801194f + 1c9530a commit dee7127
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,16 @@ func (v *buildlessVersion) comparePre(other *Version) int {

// build is not used for semver comparison.
}

// Prerelease returns the prerelease portion of a Version. If the version had
// no prerelease or is nil, the empty string is returned.
//
// Note: This method is considered experimental, and may be removed or changed
// before v1.0.0
func (v *Version) Prerelease() string {
if v == nil {
return ""
}

return strings.Join(v.pre, ".")
}
26 changes: 26 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,29 @@ func TestCompare_nil(t *testing.T) {
t.Errorf("two nil versions are not equal")
}
}

func TestVersion_Prerelease(t *testing.T) {
mustParse := func(s string) *semver.Version {
v, _ := semver.Parse(s)
return v
}

tcs := map[string]struct {
in *semver.Version
out string
}{
"no prerelease": {mustParse("1.0.0"), ""},
"prerelease": {mustParse("1.0.0-alpha"), "alpha"},
"multi-part prerelease": {mustParse("1.0.0-rc.0"), "rc.0"},
"nil version": {nil, ""},
}

for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
out := tc.in.Prerelease()
if out != tc.out {
t.Error("bad prerelease. got:", out, "wanted:", tc.out)
}
})
}
}

0 comments on commit dee7127

Please sign in to comment.