-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ead76f9
commit 4858269
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package version | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Version is the version number struct. | ||
type Version struct { | ||
Major, Minor, Patch int | ||
Hotfix string | ||
} | ||
|
||
// Gte tests if this version number is greater than or equal to the argument. | ||
func (v Version) Gte(o Version) bool { | ||
if v.Major != o.Major { | ||
return v.Major > o.Major | ||
} | ||
|
||
if v.Minor != o.Minor { | ||
return v.Minor > o.Minor | ||
} | ||
|
||
return v.Patch >= o.Patch | ||
} | ||
|
||
// String returns the version number as a string. | ||
func (v Version) String() string { | ||
if v.Hotfix == "" { | ||
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch) | ||
} else { | ||
return fmt.Sprintf("%d.%d.%d-%s", v.Major, v.Minor, v.Patch, v.Hotfix) | ||
} | ||
} | ||
|
||
// New returns a version number from the given string. | ||
func New(version string) (Version, error) { | ||
parts := strings.Split(version, ".") | ||
if len(parts) != 3 { | ||
return Version{}, errors.New("invalid version") | ||
} | ||
|
||
major, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return Version{}, fmt.Errorf("major %s is not a number: %s", parts[0], err) | ||
} | ||
|
||
minor, err := strconv.Atoi(parts[1]) | ||
if err != nil { | ||
return Version{}, fmt.Errorf("minor %s is not a number: %s", parts[0], err) | ||
} | ||
|
||
patchWithHotfix := strings.Split(parts[2], "-") | ||
|
||
var hotfix string | ||
if len(patchWithHotfix) == 1 { | ||
hotfix = "" | ||
} else if len(patchWithHotfix) == 2 { | ||
hotfix = patchWithHotfix[1] | ||
} else { | ||
return Version{}, fmt.Errorf("patch %s is not formatted as expected", parts[2]) | ||
} | ||
|
||
patch, err := strconv.Atoi(patchWithHotfix[0]) | ||
if err != nil { | ||
return Version{}, fmt.Errorf("patch %s is not a number: %s", parts[2], err) | ||
} | ||
|
||
return Version{major, minor, patch, hotfix}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package version | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestCorrectVersion(t *testing.T) { | ||
// given | ||
|
||
// when | ||
version1011, err1011 := New("10.1.1") | ||
version1011h2, err1011h2 := New("10.1.1-h2") | ||
|
||
// then | ||
assert.NoError(t, err1011) | ||
assert.Equal(t, 10, version1011.Major) | ||
assert.Equal(t, 1, version1011.Minor) | ||
assert.Equal(t, 1, version1011.Patch) | ||
assert.Equal(t, "", version1011.Hotfix) | ||
assert.NoError(t, err1011h2) | ||
assert.Equal(t, 10, version1011h2.Major) | ||
assert.Equal(t, 1, version1011h2.Minor) | ||
assert.Equal(t, 1, version1011h2.Patch) | ||
assert.Equal(t, "h2", version1011h2.Hotfix) | ||
} | ||
|
||
func TestIncorrectVersion(t *testing.T) { | ||
// given | ||
|
||
// when | ||
_, err101 := New("10.1") | ||
_, err1011h2h2 := New("10.1.1-h2-h2") | ||
|
||
// then | ||
assert.Error(t, err101) | ||
assert.Error(t, err1011h2h2) | ||
} | ||
|
||
func TestVersionComparison(t *testing.T) { | ||
// given | ||
|
||
// when | ||
v1, _ := New("10.1.1") | ||
v2, _ := New("10.2.1-h2") | ||
|
||
// then | ||
assert.True(t, v2.Gte(v1)) | ||
} |