Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: document and test panic behavior of string formatting #87

Merged
merged 1 commit into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (v *Version) DateString() string {
}

// String returns the string representation of the version in
// YYYY-mm-dd~Stability form.
// YYYY-mm-dd~Stability form. This method will panic if the value is empty.
func (v *Version) String() string {
d := v.Date.Format("2006-01-02")
if v.Stability != StabilityGA {
Expand Down Expand Up @@ -51,6 +51,8 @@ const (
StabilityGA Stability = iota
)

// String returns a string representation of the stability level. This method
// will panic if the value is empty.
func (s Stability) String() string {
switch s {
case StabilityWIP:
Expand All @@ -62,7 +64,7 @@ func (s Stability) String() string {
case StabilityGA:
return "ga"
}
panic(fmt.Sprintf("invalid stability value: %d", int(s)))
panic(fmt.Sprintf("invalid stability (%d)", int(s)))
}

// ParseVersion parses a version string into a Version type, returning an error
Expand Down
12 changes: 12 additions & 0 deletions version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ func TestParseVersion(t *testing.T) {
}
}

func TestVersionStringPanics(t *testing.T) {
c := qt.New(t)
var v Version
c.Assert(func() {
c.Log(v.String())
}, qt.PanicMatches, "invalid stability.*")
var s Stability
c.Assert(func() {
c.Log(s.String())
}, qt.PanicMatches, "invalid stability.*")
}

func TestVersionOrder(t *testing.T) {
c := qt.New(t)
tests := []struct {
Expand Down