OSDN Git Service

:white_check_mark: Add version comparison test (#1294)
authorHAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
Wed, 29 Aug 2018 14:00:31 +0000 (22:00 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 29 Aug 2018 14:00:31 +0000 (22:00 +0800)
* :white_check_mark: Add version comparison test

* :art: Macros revision len

* :white_check_mark: Add version flag test

* Update comments

* Update comments

* Fix typo

version/version.go
version/version_test.go [new file with mode: 0644]

index df9d0df..c7562bc 100644 (file)
@@ -9,9 +9,15 @@ import (
 )
 
 const (
+       // If needing to edit the iota, please ensure the following:
+       // noUpdate = 0
+       // hasUpdate = 1
+       // hasMUpdate = 2
        noUpdate uint16 = iota
        hasUpdate
        hasMUpdate
+
+       revisionLen = 8 // should falls in [1:16]
 )
 
 var (
@@ -24,7 +30,7 @@ var (
 
 func init() {
        if GitCommit != "" {
-               Version += "-" + GitCommit[:8]
+               Version += "+" + GitCommit[:revisionLen]
        }
        Status = &UpdateStatus{
                maxVerSeen:    Version,
diff --git a/version/version_test.go b/version/version_test.go
new file mode 100644 (file)
index 0000000..195dbfe
--- /dev/null
@@ -0,0 +1,50 @@
+package version
+
+import (
+       "fmt"
+       "math/rand"
+       "testing"
+       "time"
+
+       gover "github.com/hashicorp/go-version"
+)
+
+func TestRevisionLen(t *testing.T) {
+       if revisionLen > 16 {
+               t.Error("revisionLen too long")
+       }
+}
+
+func TestCompare(t *testing.T) {
+       rand.Seed(time.Now().UnixNano())
+       i := rand.Uint64()
+       rev := fmt.Sprintf("%16x", i)[:revisionLen]
+
+       v1, err := gover.NewVersion(Version)
+       if err != nil {
+               t.Error("Version 1 format error.")
+       }
+       v2, err := gover.NewVersion(Version + "+" + rev)
+       if err != nil {
+               t.Error("Version 2 format error.")
+       }
+       if v1.GreaterThan(v2) || v1.GreaterThan(v2) {
+               t.Error("Version comparison error.")
+       }
+}
+
+// In case someone edit the iota part and have the mapping changed:
+// noUpdate: 0
+// hasUpdate: 1
+// hasMUpdate: 2
+func TestFlag(t *testing.T) {
+       if noUpdate != 0 {
+               t.Error("noUpdate value error")
+       }
+       if hasUpdate != 1 {
+               t.Error("hasUpdate value error")
+       }
+       if hasMUpdate != 2 {
+               t.Error("noUpdate value error")
+       }
+}