Skip to content

Commit

Permalink
Change the Segments method to return []int, to match the existing API…
Browse files Browse the repository at this point in the history
…, and add a Segments64 method to return the int64 slice used internally. Add tests, and finish fixing the merge.
  • Loading branch information
ctdk committed Oct 31, 2016
1 parent 0929ab3 commit efb921a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
25 changes: 19 additions & 6 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (v *Version) Compare(other *Version) int {
return 0
}

segmentsSelf := v.Segments()
segmentsOther := other.Segments()
segmentsSelf := v.Segments64()
segmentsOther := other.Segments64()

// If the segments are the same, we must compare on prerelease info
if reflect.DeepEqual(segmentsSelf, segmentsOther) {
Expand Down Expand Up @@ -152,7 +152,7 @@ func (v *Version) Compare(other *Version) int {
return 0
}

func allZero(segs []int) bool {
func allZero(segs []int64) bool {
for _, s := range segs {
if s != 0 {
return false
Expand Down Expand Up @@ -264,12 +264,25 @@ func (v *Version) Prerelease() string {
return v.pre
}

// Segments returns the numeric segments of the version as a slice.
// Segments returns the numeric segments of the version as a slice of ints.
//
// This excludes any metadata or pre-release information. For example,
// for a version "1.2.3-beta", segments will return a slice of
// 1, 2, 3.
func (v *Version) Segments() []int64 {
func (v *Version) Segments() []int {
segmentSlice := make([]int, len(v.segments))
for i, v := range v.segments {
segmentSlice[i] = int(v)
}
return segmentSlice
}

// Segments64 returns the numeric segments of the version as a slice of int64s.
//
// This excludes any metadata or pre-release information. For example,
// for a version "1.2.3-beta", segments will return a slice of
// 1, 2, 3.
func (v *Version) Segments64() []int64 {
return v.segments
}

Expand All @@ -280,7 +293,7 @@ func (v *Version) String() string {
fmtParts := make([]string, len(v.segments))
for i, s := range v.segments {
// We can ignore err here since we've pre-parsed the values in segments
str := strconv.Itoa(s)
str := strconv.FormatInt(s, 10)
fmtParts[i] = str
}
fmt.Fprintf(&buf, strings.Join(fmtParts, "."))
Expand Down
28 changes: 27 additions & 1 deletion version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,31 @@ func TestVersionPrerelease(t *testing.T) {
}

func TestVersionSegments(t *testing.T) {
cases := []struct {
version string
expected []int
}{
{"1.2.3", []int{1, 2, 3}},
{"1.2-beta", []int{1, 2, 0}},
{"1-x.Y.0", []int{1, 0, 0}},
{"1.2.0-x.Y.0+metadata", []int{1, 2, 0}},
}

for _, tc := range cases {
v, err := NewVersion(tc.version)
if err != nil {
t.Fatalf("err: %s", err)
}

actual := v.Segments()
expected := tc.expected
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v\nactual: %#v", expected, actual)
}
}
}

func TestVersionSegments64(t *testing.T) {
cases := []struct {
version string
expected []int64
Expand All @@ -191,6 +216,7 @@ func TestVersionSegments(t *testing.T) {
{"1.2-beta", []int64{1, 2, 0}},
{"1-x.Y.0", []int64{1, 0, 0}},
{"1.2.0-x.Y.0+metadata", []int64{1, 2, 0}},
{"1.4.9223372036854775807", []int64{1, 4, 9223372036854775807}},
}

for _, tc := range cases {
Expand All @@ -199,7 +225,7 @@ func TestVersionSegments(t *testing.T) {
t.Fatalf("err: %s", err)
}

actual := v.Segments()
actual := v.Segments64()
expected := tc.expected
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("expected: %#v\nactual: %#v", expected, actual)
Expand Down

0 comments on commit efb921a

Please sign in to comment.