version_test.go 901 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package utils
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestCompareVersions(t *testing.T) {
  8. cases := []struct {
  9. ver1 string
  10. ver2 string
  11. operator string
  12. out bool
  13. }{
  14. {"1", "1.0.1", ">", false},
  15. {"1", "0.9.9", ">", true},
  16. {"1", "1.0-1", "<", true},
  17. {"1.0.1", "1-0.1", "<", false},
  18. {"1.0.1", "1.0.1", "==", true},
  19. {"1.0.1", "1.0.2", "==", false},
  20. {"1.1-1", "1.0.2", "==", false},
  21. {"1.0.1", "1.0.2", ">=", false},
  22. {"1.0.2", "1.0.2", ">=", true},
  23. {"1.0.3", "1.0.2", ">=", true},
  24. {"1.0.4", "1.0.2", "<=", false},
  25. {"1.0.4", "1.0.6", "<=", true},
  26. {"1.0.4", "1.0.4", "<=", true},
  27. }
  28. for _, each := range cases {
  29. each := each
  30. t.Run(each.ver1, func(t *testing.T) {
  31. actual := CompareVersions(each.ver1, each.operator, each.ver2)
  32. assert.Equal(t, each.out, actual, fmt.Sprintf("%s vs %s", each.ver1, each.ver2))
  33. })
  34. }
  35. }