forked from capnspacehook/taskmaster
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils_test.go
More file actions
140 lines (127 loc) · 3.58 KB
/
Copy pathutils_test.go
File metadata and controls
140 lines (127 loc) · 3.58 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//go:build windows
// +build windows
package taskmaster
import (
"testing"
"time"
"github.com/rickb777/period"
)
func TestTaskDateToTime(t *testing.T) {
tests := []struct {
name string
input string
wantZero bool
wantEqual time.Time
wantErr bool
}{
{name: "empty returns zero", input: "", wantZero: true},
{name: "no timezone parsed as UTC", input: "2024-01-02T15:04:05", wantEqual: time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC)},
{name: "explicit UTC Z", input: "2024-01-02T15:04:05Z", wantEqual: time.Date(2024, 1, 2, 15, 4, 5, 0, time.UTC)},
{name: "positive offset", input: "2024-01-02T15:04:05+02:00", wantEqual: time.Date(2024, 1, 2, 13, 4, 5, 0, time.UTC)},
{name: "negative offset", input: "2024-01-02T15:04:05-05:00", wantEqual: time.Date(2024, 1, 2, 20, 4, 5, 0, time.UTC)},
{name: "invalid", input: "not-a-date", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := TaskDateToTime(tt.input)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error for %q, got none", tt.input)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if tt.wantZero {
if !got.IsZero() {
t.Fatalf("expected zero time, got %v", got)
}
return
}
if !got.Equal(tt.wantEqual) {
t.Fatalf("expected %v, got %v", tt.wantEqual, got)
}
})
}
}
func TestTimeToTaskDate(t *testing.T) {
if got := TimeToTaskDate(time.Time{}); got != "" {
t.Errorf("zero time: want empty string, got %q", got)
}
tm := time.Date(2024, 3, 4, 5, 6, 7, 0, time.UTC)
if got, want := TimeToTaskDate(tm), "2024-03-04T05:06:07"; got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestStringToPeriod(t *testing.T) {
tests := []struct {
name string
input string
want string // expected Period.String()
wantErr bool
}{
{name: "empty is zero period", input: "", want: "P0D"},
{name: "minutes", input: "PT10M", want: "PT10M"},
{name: "hours", input: "PT1H", want: "PT1H"},
{name: "invalid", input: "nope", wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := StringToPeriod(tt.input)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error for %q, got none", tt.input)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got.String() != tt.want {
t.Fatalf("want %q, got %q", tt.want, got.String())
}
})
}
}
func TestPeriodToString(t *testing.T) {
if got := PeriodToString(period.Period{}); got != "" {
t.Errorf("zero period: want empty string, got %q", got)
}
if got, want := PeriodToString(period.NewHMS(0, 30, 0)), "PT30M"; got != want {
t.Errorf("want %q, got %q", want, got)
}
}
func TestIntToDayOfMonth(t *testing.T) {
tests := []struct {
name string
input int
want DayOfMonth
wantErr bool
}{
{name: "zero invalid", input: 0, wantErr: true},
{name: "negative invalid", input: -3, wantErr: true},
{name: "first", input: 1, want: One},
{name: "second", input: 2, want: Two},
{name: "thirty-first", input: 31, want: ThirtyOne},
{name: "thirty-two maps to last day", input: 32, want: LastDayOfMonth},
{name: "thirty-three invalid", input: 33, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := IntToDayOfMonth(tt.input)
if tt.wantErr {
if err == nil {
t.Fatalf("expected error for %d, got none", tt.input)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tt.want {
t.Fatalf("want %d, got %d", tt.want, got)
}
})
}
}