Skip to content
This repository
Browse code

Test for negative version.

  • Loading branch information...
commit 11818cf4ed789bd4dc79a109241fa72469120d7a 1 parent 82a1e1a
Alexey Palazhchenko authored

Showing 2 changed files with 15 additions and 5 deletions. Show diff stats Hide diff stats

  1. +2 2 version.go
  2. +13 3 version_test.go
4 version.go
@@ -27,7 +27,7 @@ func NewVersion(version string) (v *Version, err error) {
27 27 func (v Version) String() string {
28 28 res := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
29 29 if !VersionRegexp.MatchString(res) { // sanity check
30   - panic(fmt.Sprintf("%s not matches %s", res, VersionRegexp))
  30 + panic(fmt.Errorf("%s not matches %s", res, VersionRegexp))
31 31 }
32 32 return res
33 33 }
@@ -69,7 +69,7 @@ func (v *Version) UnmarshalJSON(b []byte) error {
69 69 func (v *Version) setVersion(version string) (err error) {
70 70 parsed := VersionRegexp.FindAllStringSubmatch(version, -1)
71 71 if (parsed == nil) || (len(parsed[0]) != 4) {
72   - err = fmt.Errorf("Bad format for version %q: parsed as %#v\nSee http://gonuts.io/-/doc/versioning", version, parsed)
  72 + err = fmt.Errorf("Bad format for version %q. See http://gonuts.io/-/doc/versioning", version)
73 73 return
74 74 }
75 75
16 version_test.go
@@ -8,12 +8,13 @@ import (
8 8 )
9 9
10 10 type V struct {
11   - versions []string
  11 + versions []string
  12 + badVersions []string
12 13 }
13 14
14 15 var _ = Suite(&V{})
15 16
16   -func (f *V) SetUpTest(c *C) {
  17 +func (f *V) SetUpSuite(c *C) {
17 18 f.versions = []string{
18 19 "0.0.0", "0.0.1", "0.0.2",
19 20 "0.1.0", "0.1.1", "0.1.2",
@@ -21,14 +22,23 @@ func (f *V) SetUpTest(c *C) {
21 22 "1.1.0", "1.1.1", "1.1.2",
22 23 "1.1.10", "1.10.1", "10.1.1", "10.10.10",
23 24 }
  25 + f.badVersions = []string{
  26 + "1.0.-1",
  27 + }
24 28 }
25 29
26 30 func (f *V) TestNew(c *C) {
27 31 for _, vs := range f.versions {
28 32 v, err := NewVersion(vs)
29   - c.Check(err, Equals, nil)
  33 + c.Check(err, IsNil)
30 34 c.Check(v.String(), Equals, vs)
31 35 }
  36 +
  37 + for _, vs := range f.badVersions {
  38 + v, err := NewVersion(vs)
  39 + c.Check(err, Not(IsNil))
  40 + c.Check(v.String(), Equals, "0.0.0")
  41 + }
32 42 }
33 43
34 44 func (f *V) TestLess(c *C) {

0 comments on commit 11818cf

Please sign in to comment.
Something went wrong with that request. Please try again.