date_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package sprig
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestHtmlDate(t *testing.T) {
  7. t.Skip()
  8. tpl := `{{ htmlDate 0}}`
  9. if err := runt(tpl, "1970-01-01"); err != nil {
  10. t.Error(err)
  11. }
  12. }
  13. func TestAgo(t *testing.T) {
  14. tpl := "{{ ago .Time }}"
  15. if err := runtv(tpl, "2m5s", map[string]any{"Time": time.Now().Add(-125 * time.Second)}); err != nil {
  16. t.Error(err)
  17. }
  18. if err := runtv(tpl, "2h34m17s", map[string]any{"Time": time.Now().Add(-(2*3600 + 34*60 + 17) * time.Second)}); err != nil {
  19. t.Error(err)
  20. }
  21. if err := runtv(tpl, "-5s", map[string]any{"Time": time.Now().Add(5 * time.Second)}); err != nil {
  22. t.Error(err)
  23. }
  24. }
  25. func TestToDate(t *testing.T) {
  26. tpl := `{{toDate "2006-01-02" "2017-12-31" | date "02/01/2006"}}`
  27. if err := runt(tpl, "31/12/2017"); err != nil {
  28. t.Error(err)
  29. }
  30. }
  31. func TestUnixEpoch(t *testing.T) {
  32. tm, err := time.Parse("02 Jan 06 15:04:05 MST", "13 Jun 19 20:39:39 GMT")
  33. if err != nil {
  34. t.Error(err)
  35. }
  36. tpl := `{{unixEpoch .Time}}`
  37. if err = runtv(tpl, "1560458379", map[string]any{"Time": tm}); err != nil {
  38. t.Error(err)
  39. }
  40. }
  41. func TestDateInZone(t *testing.T) {
  42. tm, err := time.Parse("02 Jan 06 15:04:05 MST", "13 Jun 19 20:39:39 GMT")
  43. if err != nil {
  44. t.Error(err)
  45. }
  46. tpl := `{{ dateInZone "02 Jan 06 15:04 -0700" .Time "UTC" }}`
  47. // Test time.Time input
  48. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": tm}); err != nil {
  49. t.Error(err)
  50. }
  51. // Test pointer to time.Time input
  52. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": &tm}); err != nil {
  53. t.Error(err)
  54. }
  55. // Test no time input. This should be close enough to time.Now() we can test
  56. loc, _ := time.LoadLocation("UTC")
  57. if err = runtv(tpl, time.Now().In(loc).Format("02 Jan 06 15:04 -0700"), map[string]any{"Time": ""}); err != nil {
  58. t.Error(err)
  59. }
  60. // Test unix timestamp as int64
  61. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": int64(1560458379)}); err != nil {
  62. t.Error(err)
  63. }
  64. // Test unix timestamp as int32
  65. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": int32(1560458379)}); err != nil {
  66. t.Error(err)
  67. }
  68. // Test unix timestamp as int
  69. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": int(1560458379)}); err != nil {
  70. t.Error(err)
  71. }
  72. // Test case of invalid timezone
  73. tpl = `{{ dateInZone "02 Jan 06 15:04 -0700" .Time "foobar" }}`
  74. if err = runtv(tpl, "13 Jun 19 20:39 +0000", map[string]any{"Time": tm}); err != nil {
  75. t.Error(err)
  76. }
  77. }
  78. func TestDuration(t *testing.T) {
  79. tpl := "{{ duration .Secs }}"
  80. if err := runtv(tpl, "1m1s", map[string]any{"Secs": "61"}); err != nil {
  81. t.Error(err)
  82. }
  83. if err := runtv(tpl, "1h0m0s", map[string]any{"Secs": "3600"}); err != nil {
  84. t.Error(err)
  85. }
  86. // 1d2h3m4s but go is opinionated
  87. if err := runtv(tpl, "26h3m4s", map[string]any{"Secs": "93784"}); err != nil {
  88. t.Error(err)
  89. }
  90. }
  91. func TestDurationRound(t *testing.T) {
  92. tpl := "{{ durationRound .Time }}"
  93. if err := runtv(tpl, "2h", map[string]any{"Time": "2h5s"}); err != nil {
  94. t.Error(err)
  95. }
  96. if err := runtv(tpl, "1d", map[string]any{"Time": "24h5s"}); err != nil {
  97. t.Error(err)
  98. }
  99. if err := runtv(tpl, "3mo", map[string]any{"Time": "2400h5s"}); err != nil {
  100. t.Error(err)
  101. }
  102. if err := runtv(tpl, "1m", map[string]any{"Time": "-1m1s"}); err != nil {
  103. t.Error(err)
  104. }
  105. }