1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| package util_test
import ( "fmt" "testing" "time"
"{{{ .Package }}}/app/util" )
//nolint:paralleltest,tparallel func TestTimeFormatting(t *testing.T) { t.Parallel() testTime := time.Date(2023, 5, 15, 14, 30, 45, 123456789, time.UTC)
tests := []struct { name string function func(*time.Time) string expected string }{ {"TimeToFull", util.TimeToFull, "2023-05-15 14:30:45"}, {"TimeToFullMS", util.TimeToFullMS, "2023-05-15 14:30:45.123456"}, {"TimeToHours", util.TimeToHours, "14:30:45"}, {"TimeToHTML", util.TimeToHTML, "2023-05-15T14:30:45"}, {"TimeToJS", util.TimeToJS, "2023-05-15T14:30:45Z"}, {"TimeToJSFull", util.TimeToJSFull, "2023-05-15T14:30:45.123Z"}, {"TimeToRFC3339", util.TimeToRFC3339, "2023-05-15T14:30:45.123Z"}, {"TimeToVerbose", util.TimeToVerbose, "Mon May 15 14:30:45 2023 +0000"}, {"TimeToYMD", util.TimeToYMD, "2023-05-15"}, }
for _, tt := range tests { x := tt t.Run(x.name, func(t *testing.T) { result := x.function(&testTime) if result != x.expected { t.Errorf("%s = %v, want %v", x.name, result, x.expected) } }) } }
//nolint:paralleltest,tparallel func TestTimeParsing(t *testing.T) { t.Parallel() tests := []struct { name string function func(string) (*time.Time, error) input string expected time.Time }{ {"TimeFromFull", util.TimeFromFull, "2023-05-15 14:30:45", time.Date(2023, 5, 15, 14, 30, 45, 0, time.UTC)}, {"TimeFromFullMS", util.TimeFromFullMS, "2023-05-15 14:30:45.123456", time.Date(2023, 5, 15, 14, 30, 45, 123456000, time.UTC)}, {"TimeFromHTML", util.TimeFromHTML, "2023-05-15T14:30:45", time.Date(2023, 5, 15, 14, 30, 45, 0, time.UTC)}, {"TimeFromJS", util.TimeFromJS, "2023-05-15T14:30:45Z", time.Date(2023, 5, 15, 14, 30, 45, 0, time.UTC)}, {"TimeFromRFC3339", util.TimeFromRFC3339, "2023-05-15T14:30:45.123Z", time.Date(2023, 5, 15, 14, 30, 45, 123000000, time.UTC)}, {"TimeFromVerbose", util.TimeFromVerbose, "Mon May 15 14:30:45 2023 +0000", time.Date(2023, 5, 15, 14, 30, 45, 0, time.UTC)}, {"TimeFromYMD", util.TimeFromYMD, "2023-05-15", time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC)}, }
for _, tt := range tests { x := tt t.Run(x.name, func(t *testing.T) { result, err := x.function(x.input) if err != nil { t.Errorf("%s returned unexpected error: %v", x.name, err) } if !result.Equal(x.expected) { t.Errorf("%s = %v, want %v", x.name, result, x.expected) } }) } }
func TestFormatSeconds(t *testing.T) { t.Parallel() tests := []struct { input float64 expected string }{ {3661.123, "1h 1m 1.123s"}, {60.5, "1m 0.500s"}, {3.0, "3s"}, {0.123, "0.123s"}, }
for _, tt := range tests { x := tt t.Run(fmt.Sprintf("%.3f", x.input), func(t *testing.T) { t.Parallel() result := util.FormatSeconds(x.input) if result != x.expected { t.Errorf("FormatSeconds(%.3f) = %s, want %s", x.input, result, x.expected) } }) } }
//nolint:paralleltest,tparallel func TestFormatSecondsFull(t *testing.T) { t.Parallel() tests := []struct { input float64 expected string }{ {3661.123, "1 hour, 1 minute, 1 second, 123 milliseconds"}, {60.5, "1 minute, 0 seconds, 500 milliseconds"}, {3.0, "3 seconds"}, {0.123, "0 seconds, 123 milliseconds"}, }
for _, tt := range tests { x := tt t.Run(fmt.Sprintf("%.3f", x.input), func(t *testing.T) { result := util.FormatSecondsFull(x.input) if result != x.expected { t.Errorf("FormatSecondsFull(%.3f) = %s, want %s", x.input, result, x.expected) } }) } }
|