gitea源码

time_str_test.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2024 Gitea. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestTimeStr(t *testing.T) {
  9. t.Run("Parse", func(t *testing.T) {
  10. // Test TimeEstimateParse
  11. tests := []struct {
  12. input string
  13. output int64
  14. err bool
  15. }{
  16. {"1h", 3600, false},
  17. {"1m", 60, false},
  18. {"1s", 1, false},
  19. {"1h 1m 1s", 3600 + 60 + 1, false},
  20. {"1d1x", 0, true},
  21. }
  22. for _, test := range tests {
  23. t.Run(test.input, func(t *testing.T) {
  24. output, err := TimeEstimateParse(test.input)
  25. if test.err {
  26. assert.Error(t, err)
  27. } else {
  28. assert.NoError(t, err)
  29. }
  30. assert.Equal(t, test.output, output)
  31. })
  32. }
  33. })
  34. t.Run("String", func(t *testing.T) {
  35. tests := []struct {
  36. input int64
  37. output string
  38. }{
  39. {3600, "1h"},
  40. {60, "1m"},
  41. {1, "1s"},
  42. {3600 + 1, "1h 1s"},
  43. }
  44. for _, test := range tests {
  45. t.Run(test.output, func(t *testing.T) {
  46. output := TimeEstimateString(test.input)
  47. assert.Equal(t, test.output, output)
  48. })
  49. }
  50. })
  51. }