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