github.com/searKing/golang/go@v1.2.117/version/version_test.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package version_test 6 7 import ( 8 "fmt" 9 "testing" 10 "time" 11 12 "github.com/searKing/golang/go/version" 13 ) 14 15 func TestVersion_String(t *testing.T) { 16 testCases := []struct { 17 ver version.Version 18 expect string 19 }{ 20 { 21 ver: version.Version{ 22 Major: 1, 23 Minor: 2, 24 Patch: 3, 25 PreRelease: "fix", 26 BuildTime: time.Now().String(), 27 GitHash: "0xFFFF", 28 MetaData: []string{"M", "E", "T", "A"}, 29 }, 30 expect: "v1.2.3-fix(0xFFFF)", 31 }, 32 { 33 ver: version.Version{ 34 Major: 1, 35 Minor: 2, 36 Patch: 3, 37 PreRelease: "devel", 38 BuildTime: "NOW", 39 GitHash: "0xFFFF", 40 MetaData: []string{"M", "E", "T", "A"}, 41 }, 42 expect: "v1.2.3-devel(0xFFFF)+M.E.T.A", 43 }, 44 { 45 ver: version.Version{ 46 Major: 1, 47 Minor: 2, 48 Patch: 3, 49 RawVersion: "v7.8.9", 50 PreRelease: "fix", 51 BuildTime: "NOW", 52 GitHash: "0xFFFF", 53 MetaData: []string{"M", "E", "T", "A"}, 54 }, 55 expect: "v7.8.9", 56 }, 57 } 58 for i, tc := range testCases { 59 if tc.ver.String() != tc.expect { 60 t.Errorf("#%d expect %s, got %s", i, tc.expect, tc.ver.String()) 61 } 62 } 63 } 64 65 func TestVersion_Format(t *testing.T) { 66 testCases := []struct { 67 ver version.Version 68 fmt string 69 expect string 70 }{ 71 { 72 ver: version.Version{ 73 Major: 1, 74 Minor: 2, 75 Patch: 3, 76 PreRelease: "fix", 77 BuildTime: "NOW", 78 GitHash: "0xFFFF", 79 MetaData: []string{"M", "E", "T", "A"}, 80 }, 81 fmt: "%s", 82 expect: "v1.2.3-fix(0xFFFF)", 83 }, 84 { 85 ver: version.Version{ 86 Major: 1, 87 Minor: 2, 88 Patch: 3, 89 PreRelease: "fix", 90 BuildTime: "NOW", 91 GitHash: "0xFFFF", 92 MetaData: []string{"M", "E", "T", "A"}, 93 }, 94 fmt: "%q", 95 expect: "v1.2.3-fix(0xFFFF)", 96 }, 97 { 98 ver: version.Version{ 99 Major: 1, 100 Minor: 2, 101 Patch: 3, 102 PreRelease: "fix", 103 BuildTime: "NOW", 104 GitHash: "0xFFFF", 105 MetaData: []string{"M", "E", "T", "A"}, 106 }, 107 fmt: "%v", 108 expect: "v1.2.3-fix(0xFFFF)", 109 }, 110 { 111 ver: version.Version{ 112 Major: 1, 113 Minor: 2, 114 Patch: 3, 115 PreRelease: "fix", 116 BuildTime: "NOW", 117 GitHash: "0xFFFF", 118 MetaData: []string{"M", "E", "T", "A"}, 119 }, 120 fmt: "%+v", 121 expect: "v1.2.3-fix(0xFFFF), Build #gc-go1.15.6-darwin/amd64, built on NOW", 122 }, 123 } 124 for i, tc := range testCases { 125 ver := fmt.Sprintf(tc.fmt, tc.ver) 126 if ver != tc.expect { 127 t.Skipf("#%d expect %s, got %s", i, tc.expect, ver) 128 } 129 130 } 131 }