update to blang/semver HEAD

This commit is contained in:
Dr Nic Williams
2015-04-25 11:57:13 -07:00
parent c9acb4dfc6
commit d5b1f57afc
7 changed files with 72 additions and 28 deletions

4
Godeps/Godeps.json generated
View File

@@ -12,8 +12,8 @@
}, },
{ {
"ImportPath": "github.com/blang/semver", "ImportPath": "github.com/blang/semver",
"Comment": "v2.1.0", "Comment": "v3.0.0",
"Rev": "9bf7bff48b0388cb75991e58c6df7d13e982f1f2" "Rev": "2f3112b6f8f18f9df8743cc75ed51da08094ef6a"
}, },
{ {
"ImportPath": "github.com/google/go-github/github", "ImportPath": "github.com/google/go-github/github",

View File

@@ -12,8 +12,8 @@ Note: Always vendor your dependencies or fix on a specific version tag.
```go ```go
import github.com/blang/semver import github.com/blang/semver
v1, err := semver.New("1.0.0-beta") v1, err := semver.Make("1.0.0-beta")
v2, err := semver.New("2.0.0-beta") v2, err := semver.Make("2.0.0-beta")
v1.Compare(v2) v1.Compare(v2)
``` ```
@@ -53,7 +53,7 @@ Have a look at full examples in [examples/main.go](examples/main.go)
```go ```go
import github.com/blang/semver import github.com/blang/semver
v, err := semver.New("0.0.1-alpha.preview+123.github") v, err := semver.Make("0.0.1-alpha.preview+123.github")
fmt.Printf("Major: %d\n", v.Major) fmt.Printf("Major: %d\n", v.Major)
fmt.Printf("Minor: %d\n", v.Minor) fmt.Printf("Minor: %d\n", v.Minor)
fmt.Printf("Patch: %d\n", v.Patch) fmt.Printf("Patch: %d\n", v.Patch)
@@ -76,7 +76,7 @@ if len(v.Build) > 0 {
} }
} }
v001, err := semver.New("0.0.1") v001, err := semver.Make("0.0.1")
// Compare using helpers: v.GT(v2), v.LT, v.GTE, v.LTE // Compare using helpers: v.GT(v2), v.LT, v.GTE, v.LTE
v001.GT(v) == true v001.GT(v) == true
v.LT(v001) == true v.LT(v001) == true

View File

@@ -32,8 +32,8 @@ func main() {
} }
} }
// New == Parse // Make == Parse (Value), New for Pointer
v001, err := semver.New("0.0.1") v001, err := semver.Make("0.0.1")
fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):") fmt.Println("\nUse Version.Compare for comparisons (-1, 0, 1):")
fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v)) fmt.Printf("%q is greater than %q: Compare == %d\n", v001, v, v001.Compare(v))

View File

@@ -191,12 +191,19 @@ func (v Version) Validate() error {
return nil return nil
} }
// Alias for Parse, parses version string and returns a validated Version or error // New is an alias for Parse and returns a pointer, parses version string and returns a validated Version or error
func New(s string) (Version, error) { func New(s string) (vp *Version, err error) {
v, err := Parse(s)
vp = &v
return
}
// Make is an alias for Parse, parses version string and returns a validated Version or error
func Make(s string) (Version, error) {
return Parse(s) return Parse(s)
} }
// Parses version string and returns a validated Version or error // Parse parses version string and returns a validated Version or error
func Parse(s string) (Version, error) { func Parse(s string) (Version, error) {
if len(s) == 0 { if len(s) == 0 {
return Version{}, errors.New("Version string empty") return Version{}, errors.New("Version string empty")
@@ -285,6 +292,15 @@ func Parse(s string) (Version, error) {
return v, nil return v, nil
} }
// MustParse is like Parse but panics if the version cannot be parsed.
func MustParse(s string) Version {
v, err := Parse(s)
if err != nil {
panic(`semver: Parse(` + s + `): ` + err.Error())
}
return v
}
// PreRelease Version // PreRelease Version
type PRVersion struct { type PRVersion struct {
VersionStr string VersionStr string

View File

@@ -50,6 +50,19 @@ func TestParse(t *testing.T) {
} }
} }
func TestMustParse(t *testing.T) {
_ = MustParse("32.2.1-alpha")
}
func TestMustParse_panic(t *testing.T) {
defer func() {
if recover() == nil {
t.Errorf("Should have panicked")
}
}()
_ = MustParse("invalid version")
}
func TestValidate(t *testing.T) { func TestValidate(t *testing.T) {
for _, test := range formatTests { for _, test := range formatTests {
if err := test.v.Validate(); err != nil { if err := test.v.Validate(); err != nil {
@@ -259,6 +272,21 @@ func TestNewHelper(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("Unexpected error %q", err) t.Fatalf("Unexpected error %q", err)
} }
// New returns pointer
if v == nil {
t.Fatal("Version is nil")
}
if v.Compare(Version{1, 2, 3, nil, nil}) != 0 {
t.Fatal("Unexpected comparison problem")
}
}
func TestMakeHelper(t *testing.T) {
v, err := Make("1.2.3")
if err != nil {
t.Fatalf("Unexpected error %q", err)
}
if v.Compare(Version{1, 2, 3, nil, nil}) != 0 { if v.Compare(Version{1, 2, 3, nil, nil}) != 0 {
t.Fatal("Unexpected comparison problem") t.Fatal("Unexpected comparison problem")
} }
@@ -269,7 +297,7 @@ func BenchmarkParseSimple(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
New(VERSION) Parse(VERSION)
} }
} }
@@ -278,7 +306,7 @@ func BenchmarkParseComplex(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
New(VERSION) Parse(VERSION)
} }
} }
@@ -287,13 +315,13 @@ func BenchmarkParseAverage(b *testing.B) {
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
New(formatTests[n%l].result) Parse(formatTests[n%l].result)
} }
} }
func BenchmarkStringSimple(b *testing.B) { func BenchmarkStringSimple(b *testing.B) {
const VERSION = "0.0.1" const VERSION = "0.0.1"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -303,7 +331,7 @@ func BenchmarkStringSimple(b *testing.B) {
func BenchmarkStringLarger(b *testing.B) { func BenchmarkStringLarger(b *testing.B) {
const VERSION = "11.15.2012" const VERSION = "11.15.2012"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -313,7 +341,7 @@ func BenchmarkStringLarger(b *testing.B) {
func BenchmarkStringComplex(b *testing.B) { func BenchmarkStringComplex(b *testing.B) {
const VERSION = "0.0.1-alpha.preview+123.456" const VERSION = "0.0.1-alpha.preview+123.456"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -332,7 +360,7 @@ func BenchmarkStringAverage(b *testing.B) {
func BenchmarkValidateSimple(b *testing.B) { func BenchmarkValidateSimple(b *testing.B) {
const VERSION = "0.0.1" const VERSION = "0.0.1"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -342,7 +370,7 @@ func BenchmarkValidateSimple(b *testing.B) {
func BenchmarkValidateComplex(b *testing.B) { func BenchmarkValidateComplex(b *testing.B) {
const VERSION = "0.0.1-alpha.preview+123.456" const VERSION = "0.0.1-alpha.preview+123.456"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -361,7 +389,7 @@ func BenchmarkValidateAverage(b *testing.B) {
func BenchmarkCompareSimple(b *testing.B) { func BenchmarkCompareSimple(b *testing.B) {
const VERSION = "0.0.1" const VERSION = "0.0.1"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
@@ -371,7 +399,7 @@ func BenchmarkCompareSimple(b *testing.B) {
func BenchmarkCompareComplex(b *testing.B) { func BenchmarkCompareComplex(b *testing.B) {
const VERSION = "0.0.1-alpha.preview+123.456" const VERSION = "0.0.1-alpha.preview+123.456"
v, _ := New(VERSION) v, _ := Parse(VERSION)
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {

View File

@@ -6,9 +6,9 @@ import (
) )
func TestSort(t *testing.T) { func TestSort(t *testing.T) {
v100, _ := New("1.0.0") v100, _ := Parse("1.0.0")
v010, _ := New("0.1.0") v010, _ := Parse("0.1.0")
v001, _ := New("0.0.1") v001, _ := Parse("0.0.1")
versions := []Version{v010, v100, v001} versions := []Version{v010, v100, v001}
Sort(versions) Sort(versions)
@@ -19,9 +19,9 @@ func TestSort(t *testing.T) {
} }
func BenchmarkSort(b *testing.B) { func BenchmarkSort(b *testing.B) {
v100, _ := New("1.0.0") v100, _ := Parse("1.0.0")
v010, _ := New("0.1.0") v010, _ := Parse("0.1.0")
v001, _ := New("0.0.1") v001, _ := Parse("0.0.1")
b.ReportAllocs() b.ReportAllocs()
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {

View File

@@ -82,5 +82,5 @@ func (r byVersion) Less(i, j int) bool {
return false return false
} }
return first.LT(second) return first.LT(*second)
} }