go get service for maze.io
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version_test.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "testing"
  4. . "gopkg.in/check.v1"
  5. )
  6. func Test(t *testing.T) { TestingT(t) }
  7. var _ = Suite(&VersionSuite{})
  8. type VersionSuite struct{}
  9. var versionParseTests = []struct {
  10. major int
  11. minor int
  12. patch int
  13. dev bool
  14. s string
  15. }{
  16. {-1, -1, -1, false, "v"},
  17. {-1, -1, -1, false, "v-1"},
  18. {-1, -1, -1, false, "v-deb"},
  19. {-1, -1, -1, false, "v01"},
  20. {-1, -1, -1, false, "v1.01"},
  21. {-1, -1, -1, false, "a1"},
  22. {-1, -1, -1, false, "v1a"},
  23. {-1, -1, -1, false, "v1..2"},
  24. {-1, -1, -1, false, "v1.2.3.4"},
  25. {-1, -1, -1, false, "v1."},
  26. {-1, -1, -1, false, "v1.2."},
  27. {-1, -1, -1, false, "v1.2.3."},
  28. {0, -1, -1, false, "v0"},
  29. {0, -1, -1, true, "v0-unstable"},
  30. {1, -1, -1, false, "v1"},
  31. {1, -1, -1, true, "v1-unstable"},
  32. {1, 2, -1, false, "v1.2"},
  33. {1, 2, -1, true, "v1.2-unstable"},
  34. {1, 2, 3, false, "v1.2.3"},
  35. {1, 2, 3, true, "v1.2.3-unstable"},
  36. {12, 34, 56, false, "v12.34.56"},
  37. {12, 34, 56, true, "v12.34.56-unstable"},
  38. }
  39. func (s *VersionSuite) TestParse(c *C) {
  40. for _, t := range versionParseTests {
  41. got, ok := parseVersion(t.s)
  42. if t.major == -1 {
  43. if ok || got != InvalidVersion {
  44. c.Fatalf("version %q is invalid but parsed as %#v", t.s, got)
  45. }
  46. } else {
  47. want := Version{t.major, t.minor, t.patch, t.dev}
  48. if got != want {
  49. c.Fatalf("version %q must parse as %#v, got %#v", t.s, want, got)
  50. }
  51. if got.String() != t.s {
  52. c.Fatalf("version %q got parsed as %#v and stringified as %q", t.s, got, got.String())
  53. }
  54. }
  55. }
  56. }
  57. var versionLessTests = []struct {
  58. oneMajor, oneMinor, onePatch int
  59. oneUnstable bool
  60. twoMajor, twoMinor, twoPatch int
  61. twoUnstable, less bool
  62. }{
  63. {0, 0, 0, false, 0, 0, 0, false, false},
  64. {1, 0, 0, false, 1, 0, 0, false, false},
  65. {1, 0, 0, false, 1, 1, 0, false, true},
  66. {1, 0, 0, false, 2, 0, 0, false, true},
  67. {0, 1, 0, false, 0, 1, 0, false, false},
  68. {0, 1, 0, false, 0, 1, 1, false, true},
  69. {0, 0, 0, false, 0, 2, 0, false, true},
  70. {0, 0, 1, false, 0, 0, 1, false, false},
  71. {0, 0, 1, false, 0, 0, 2, false, true},
  72. {0, 0, 0, false, 0, 0, 0, true, false},
  73. {0, 0, 0, true, 0, 0, 0, false, true},
  74. {0, 0, 1, true, 0, 0, 0, false, false},
  75. }
  76. func (s *VersionSuite) TestLess(c *C) {
  77. for _, t := range versionLessTests {
  78. one := Version{t.oneMajor, t.oneMinor, t.onePatch, t.oneUnstable}
  79. two := Version{t.twoMajor, t.twoMinor, t.twoPatch, t.twoUnstable}
  80. if one.Less(two) != t.less {
  81. c.Fatalf("version %s < %s returned %v", one, two, !t.less)
  82. }
  83. }
  84. }
  85. var versionContainsTests = []struct {
  86. oneMajor, oneMinor, onePatch int
  87. oneUnstable bool
  88. twoMajor, twoMinor, twoPatch int
  89. twoUnstable, contains bool
  90. }{
  91. {12, 34, 56, false, 12, 34, 56, false, true},
  92. {12, 34, 56, false, 12, 34, 78, false, false},
  93. {12, 34, -1, false, 12, 34, 56, false, true},
  94. {12, 34, -1, false, 12, 78, 56, false, false},
  95. {12, -1, -1, false, 12, 34, 56, false, true},
  96. {12, -1, -1, false, 78, 34, 56, false, false},
  97. {12, -1, -1, true, 12, -1, -1, false, false},
  98. {12, -1, -1, false, 12, -1, -1, true, false},
  99. }
  100. func (s *VersionSuite) TestContains(c *C) {
  101. for _, t := range versionContainsTests {
  102. one := Version{t.oneMajor, t.oneMinor, t.onePatch, t.oneUnstable}
  103. two := Version{t.twoMajor, t.twoMinor, t.twoPatch, t.twoUnstable}
  104. if one.Contains(two) != t.contains {
  105. c.Fatalf("version %s.Contains(%s) returned %v", one, two, !t.contains)
  106. }
  107. }
  108. }
  109. func (s *VersionSuite) TestIsValid(c *C) {
  110. c.Assert(InvalidVersion.IsValid(), Equals, false)
  111. c.Assert(Version{0, 0, 0, false}.IsValid(), Equals, true)
  112. }