github.com/wtfutil/wtf@v0.43.0/wtf/datetime_test.go (about) 1 package wtf 2 3 import ( 4 "testing" 5 "time" 6 ) 7 8 func Test_IsToday(t *testing.T) { 9 tests := []struct { 10 name string 11 date time.Time 12 expected bool 13 }{ 14 { 15 name: "when yesterday", 16 date: time.Now().Local().AddDate(0, 0, -1), 17 expected: false, 18 }, 19 { 20 name: "when today", 21 date: time.Now().Local(), 22 expected: true, 23 }, 24 { 25 name: "when tomorrow", 26 date: time.Now().Local().AddDate(0, 0, +1), 27 expected: false, 28 }, 29 } 30 31 for _, tt := range tests { 32 t.Run(tt.name, func(t *testing.T) { 33 actual := IsToday(tt.date) 34 35 if tt.expected != actual { 36 t.Errorf("\nexpected: %t\n got: %t", tt.expected, actual) 37 } 38 }) 39 } 40 } 41 42 func Test_PrettyDate(t *testing.T) { 43 tests := []struct { 44 name string 45 date string 46 expected string 47 }{ 48 { 49 name: "with empty date", 50 date: "", 51 expected: "", 52 }, 53 { 54 name: "with invalid date", 55 date: "10-21-1999", 56 expected: "10-21-1999", 57 }, 58 { 59 name: "with valid date", 60 date: "1999-10-21", 61 expected: "Oct 21, 1999", 62 }, 63 } 64 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 actual := PrettyDate(tt.date) 68 69 if tt.expected != actual { 70 t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) 71 } 72 }) 73 } 74 } 75 func Test_UnixTime(t *testing.T) { 76 tests := []struct { 77 name string 78 unixVal int64 79 expected string 80 }{ 81 { 82 name: "with 0 time", 83 unixVal: 0, 84 expected: "1970-01-01 00:00:00 +0000 UTC", 85 }, 86 { 87 name: "with explicit time", 88 unixVal: 1564883266, 89 expected: "2019-08-04 01:47:46 +0000 UTC", 90 }, 91 } 92 93 for _, tt := range tests { 94 t.Run(tt.name, func(t *testing.T) { 95 actual := UnixTime(tt.unixVal).UTC() 96 97 if tt.expected != actual.String() { 98 t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual) 99 } 100 }) 101 } 102 }