version_test.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package version
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestIsVersionGreaterOrEqualThan(t *testing.T) {
  8. tests := []struct {
  9. version string
  10. target string
  11. want bool
  12. }{
  13. {
  14. version: "0.9.1",
  15. target: "0.9.1",
  16. want: true,
  17. },
  18. {
  19. version: "0.10.0",
  20. target: "0.9.1",
  21. want: true,
  22. },
  23. {
  24. version: "0.9.0",
  25. target: "0.9.1",
  26. want: false,
  27. },
  28. }
  29. for _, test := range tests {
  30. result := IsVersionGreaterOrEqualThan(test.version, test.target)
  31. if result != test.want {
  32. t.Errorf("got result %v, want %v.", result, test.want)
  33. }
  34. }
  35. }
  36. func TestIsVersionGreaterThan(t *testing.T) {
  37. tests := []struct {
  38. version string
  39. target string
  40. want bool
  41. }{
  42. {
  43. version: "0.9.1",
  44. target: "0.9.1",
  45. want: false,
  46. },
  47. {
  48. version: "0.10.0",
  49. target: "0.8.0",
  50. want: true,
  51. },
  52. {
  53. version: "0.23",
  54. target: "0.22",
  55. want: true,
  56. },
  57. {
  58. version: "0.8.0",
  59. target: "0.10.0",
  60. want: false,
  61. },
  62. {
  63. version: "0.9.0",
  64. target: "0.9.1",
  65. want: false,
  66. },
  67. {
  68. version: "0.22",
  69. target: "0.22",
  70. want: false,
  71. },
  72. }
  73. for _, test := range tests {
  74. result := IsVersionGreaterThan(test.version, test.target)
  75. if result != test.want {
  76. t.Errorf("got result %v, want %v.", result, test.want)
  77. }
  78. }
  79. }
  80. func TestSortVersion(t *testing.T) {
  81. tests := []struct {
  82. versionList []string
  83. want []string
  84. }{
  85. {
  86. versionList: []string{"0.9.1", "0.10.0", "0.8.0"},
  87. want: []string{"0.8.0", "0.9.1", "0.10.0"},
  88. },
  89. {
  90. versionList: []string{"1.9.1", "0.9.1", "0.10.0", "0.8.0"},
  91. want: []string{"0.8.0", "0.9.1", "0.10.0", "1.9.1"},
  92. },
  93. }
  94. for _, test := range tests {
  95. sort.Sort(SortVersion(test.versionList))
  96. assert.Equal(t, test.versionList, test.want)
  97. }
  98. }