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.

46 lines
958 B

  1. package common
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. var (
  8. date = time.Date(2015, time.Month(12), 31, 0, 0, 0, 0, time.UTC)
  9. date2 = time.Date(2016, time.Month(12), 31, 0, 0, 0, 0, time.UTC)
  10. zero time.Time
  11. )
  12. func TestParseDateRange(t *testing.T) {
  13. assert := assert.New(t)
  14. var testDates = []struct {
  15. dateStr string
  16. start time.Time
  17. end time.Time
  18. errNil bool
  19. }{
  20. {"2015-12-31:2016-12-31", date, date2, true},
  21. {"2015-12-31:", date, zero, true},
  22. {":2016-12-31", zero, date2, true},
  23. {"2016-12-31", zero, zero, false},
  24. {"2016-31-12:", zero, zero, false},
  25. {":2016-31-12", zero, zero, false},
  26. }
  27. for _, test := range testDates {
  28. start, end, err := ParseDateRange(test.dateStr)
  29. if test.errNil {
  30. assert.Nil(err)
  31. testPtr := func(want, have time.Time) {
  32. assert.True(have.Equal(want))
  33. }
  34. testPtr(test.start, start)
  35. testPtr(test.end, end)
  36. } else {
  37. assert.NotNil(err)
  38. }
  39. }
  40. }