Skip to content

Commit

Permalink
fixing version tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kashishm committed Apr 22, 2015
1 parent ed595ef commit 43e8b57
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 82 deletions.
6 changes: 3 additions & 3 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func ParseVersion(versionText string) (*Version, error) {
}
Major, err := strconv.Atoi(splits[0])
if err != nil {
return nil, VersionError("Major", splits[0], err)
return nil, VersionError("major", splits[0], err)
}
Minor, err := strconv.Atoi(splits[1])
if err != nil {
return nil, VersionError("Minor", splits[1], err)
return nil, VersionError("minor", splits[1], err)
}
Patch, err := strconv.Atoi(splits[2])
if err != nil {
return nil, VersionError("Patch", splits[2], err)
return nil, VersionError("patch", splits[2], err)
}

return &Version{Major, Minor, Patch}, nil
Expand Down
165 changes: 86 additions & 79 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,116 +19,123 @@ package version

import (
. "gopkg.in/check.v1"
"testing"
)

func Test(t *testing.T) { TestingT(t) }

type MySuite struct{}

var _ = Suite(&MySuite{})

func (s *MySuite) TestParsingVersion(c *C) {
Version, err := parseVersion("1.5.9")
Version, err := ParseVersion("1.5.9")
c.Assert(err, Equals, nil)
c.Assert(Version.major, Equals, 1)
c.Assert(Version.minor, Equals, 5)
c.Assert(Version.patch, Equals, 9)
c.Assert(Version.Major, Equals, 1)
c.Assert(Version.Minor, Equals, 5)
c.Assert(Version.Patch, Equals, 9)
}

func (s *MySuite) TestParsingErrorForIncorrectNumberOfDotCharacters(c *C) {
_, err := parseVersion("1.5.9.9")
_, err := ParseVersion("1.5.9.9")
c.Assert(err, ErrorMatches, "Incorrect Version format. Version should be in the form 1.5.7")

_, err = parseVersion("0.")
_, err = ParseVersion("0.")
c.Assert(err, ErrorMatches, "Incorrect Version format. Version should be in the form 1.5.7")
}

func (s *MySuite) TestParsingErrorForNonIntegerVersion(c *C) {
_, err := parseVersion("a.9.0")
_, err := ParseVersion("a.9.0")
c.Assert(err, ErrorMatches, `Error parsing major Version a to integer. strconv.ParseInt: parsing "a": invalid syntax`)

_, err = parseVersion("0.ffhj.78")
_, err = ParseVersion("0.ffhj.78")
c.Assert(err, ErrorMatches, `Error parsing minor Version ffhj to integer. strconv.ParseInt: parsing "ffhj": invalid syntax`)

_, err = parseVersion("8.9.opl")
_, err = ParseVersion("8.9.opl")
c.Assert(err, ErrorMatches, `Error parsing patch Version opl to integer. strconv.ParseInt: parsing "opl": invalid syntax`)
}

func (s *MySuite) TestVersionComparisonGreaterLesser(c *C) {
higherVersion, _ := parseVersion("0.0.7")
lowerVersion, _ := parseVersion("0.0.3")
c.Assert(lowerVersion.isLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.isGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = parseVersion("0.7.2")
lowerVersion, _ = parseVersion("0.5.7")
c.Assert(lowerVersion.isLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.isGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = parseVersion("4.7.2")
lowerVersion, _ = parseVersion("3.8.7")
c.Assert(lowerVersion.isLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.isGreaterThan(lowerVersion), Equals, true)

version1, _ := parseVersion("4.7.2")
version2, _ := parseVersion("4.7.2")
c.Assert(version1.isEqualTo(version2), Equals, true)
higherVersion, _ := ParseVersion("0.0.7")
lowerVersion, _ := ParseVersion("0.0.3")
c.Assert(lowerVersion.IsLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.IsGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = ParseVersion("0.7.2")
lowerVersion, _ = ParseVersion("0.5.7")
c.Assert(lowerVersion.IsLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.IsGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = ParseVersion("4.7.2")
lowerVersion, _ = ParseVersion("3.8.7")
c.Assert(lowerVersion.IsLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.IsGreaterThan(lowerVersion), Equals, true)

version1, _ := ParseVersion("4.7.2")
version2, _ := ParseVersion("4.7.2")
c.Assert(version1.IsEqualTo(version2), Equals, true)
}

func (s *MySuite) TestVersionComparisonGreaterThanEqual(c *C) {
higherVersion, _ := parseVersion("0.0.7")
lowerVersion, _ := parseVersion("0.0.3")
c.Assert(higherVersion.isGreaterThanEqualTo(lowerVersion), Equals, true)

higherVersion, _ = parseVersion("0.7.2")
lowerVersion, _ = parseVersion("0.5.7")
c.Assert(higherVersion.isGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = parseVersion("4.7.2")
lowerVersion, _ = parseVersion("3.8.7")
c.Assert(lowerVersion.isLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.isGreaterThan(lowerVersion), Equals, true)

version1, _ := parseVersion("6.7.2")
version2, _ := parseVersion("6.7.2")
c.Assert(version1.isGreaterThanEqualTo(version2), Equals, true)
higherVersion, _ := ParseVersion("0.0.7")
lowerVersion, _ := ParseVersion("0.0.3")
c.Assert(higherVersion.IsGreaterThanEqualTo(lowerVersion), Equals, true)

higherVersion, _ = ParseVersion("0.7.2")
lowerVersion, _ = ParseVersion("0.5.7")
c.Assert(higherVersion.IsGreaterThan(lowerVersion), Equals, true)

higherVersion, _ = ParseVersion("4.7.2")
lowerVersion, _ = ParseVersion("3.8.7")
c.Assert(lowerVersion.IsLesserThan(higherVersion), Equals, true)
c.Assert(higherVersion.IsGreaterThan(lowerVersion), Equals, true)

version1, _ := ParseVersion("6.7.2")
version2, _ := ParseVersion("6.7.2")
c.Assert(version1.IsGreaterThanEqualTo(version2), Equals, true)
}

func (s *MySuite) TestVersionComparisonLesserThanEqual(c *C) {
higherVersion, _ := parseVersion("0.0.7")
lowerVersion, _ := parseVersion("0.0.3")
c.Assert(lowerVersion.isLesserThanEqualTo(higherVersion), Equals, true)
higherVersion, _ := ParseVersion("0.0.7")
lowerVersion, _ := ParseVersion("0.0.3")
c.Assert(lowerVersion.IsLesserThanEqualTo(higherVersion), Equals, true)

higherVersion, _ = parseVersion("0.7.2")
lowerVersion, _ = parseVersion("0.5.7")
c.Assert(lowerVersion.isLesserThanEqualTo(higherVersion), Equals, true)
higherVersion, _ = ParseVersion("0.7.2")
lowerVersion, _ = ParseVersion("0.5.7")
c.Assert(lowerVersion.IsLesserThanEqualTo(higherVersion), Equals, true)

higherVersion, _ = parseVersion("5.8.2")
lowerVersion, _ = parseVersion("2.9.7")
c.Assert(lowerVersion.isLesserThanEqualTo(higherVersion), Equals, true)
higherVersion, _ = ParseVersion("5.8.2")
lowerVersion, _ = ParseVersion("2.9.7")
c.Assert(lowerVersion.IsLesserThanEqualTo(higherVersion), Equals, true)

version1, _ := parseVersion("6.7.2")
version2, _ := parseVersion("6.7.2")
c.Assert(version1.isLesserThanEqualTo(version2), Equals, true)
version1, _ := ParseVersion("6.7.2")
version2, _ := ParseVersion("6.7.2")
c.Assert(version1.IsLesserThanEqualTo(version2), Equals, true)
}

func (s *MySuite) TestVersionIsBetweenTwoVersions(c *C) {
higherVersion, _ := parseVersion("0.0.9")
lowerVersion, _ := parseVersion("0.0.7")
middleVersion, _ := parseVersion("0.0.8")
c.Assert(middleVersion.isBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = parseVersion("0.7.2")
lowerVersion, _ = parseVersion("0.5.7")
middleVersion, _ = parseVersion("0.6.9")
c.Assert(middleVersion.isBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = parseVersion("4.7.2")
lowerVersion, _ = parseVersion("3.8.7")
middleVersion, _ = parseVersion("4.0.1")
c.Assert(middleVersion.isBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = parseVersion("4.7.2")
lowerVersion, _ = parseVersion("4.0.1")
middleVersion, _ = parseVersion("4.0.1")
c.Assert(middleVersion.isBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = parseVersion("0.0.2")
lowerVersion, _ = parseVersion("0.0.1")
middleVersion, _ = parseVersion("0.0.2")
c.Assert(middleVersion.isBetween(lowerVersion, higherVersion), Equals, true)
higherVersion, _ := ParseVersion("0.0.9")
lowerVersion, _ := ParseVersion("0.0.7")
middleVersion, _ := ParseVersion("0.0.8")
c.Assert(middleVersion.IsBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = ParseVersion("0.7.2")
lowerVersion, _ = ParseVersion("0.5.7")
middleVersion, _ = ParseVersion("0.6.9")
c.Assert(middleVersion.IsBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = ParseVersion("4.7.2")
lowerVersion, _ = ParseVersion("3.8.7")
middleVersion, _ = ParseVersion("4.0.1")
c.Assert(middleVersion.IsBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = ParseVersion("4.7.2")
lowerVersion, _ = ParseVersion("4.0.1")
middleVersion, _ = ParseVersion("4.0.1")
c.Assert(middleVersion.IsBetween(lowerVersion, higherVersion), Equals, true)

higherVersion, _ = ParseVersion("0.0.2")
lowerVersion, _ = ParseVersion("0.0.1")
middleVersion, _ = ParseVersion("0.0.2")
c.Assert(middleVersion.IsBetween(lowerVersion, higherVersion), Equals, true)
}

0 comments on commit 43e8b57

Please sign in to comment.