-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
171 lines (160 loc) · 5.17 KB
/
types_test.go
File metadata and controls
171 lines (160 loc) · 5.17 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2025 Kim Wittenburg. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package asn1
import (
"testing"
"time"
)
func ExampleEnumerated() {
type Option int
type MyType struct {
I int // ASN.1 INTEGER
J Option // ASN.1 ENUMERATED
}
}
func TestTime_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"Example": {time.Date(1985, 11, 06, 21, 06, 21, 0, time.UTC), "1985-11-06T21:06:21Z"},
"Nanos": {time.Date(1985, 11, 06, 21, 06, 21, 500000000, time.UTC), "1985-11-06T21:06:21.5Z"},
"LocalTime": {time.Date(1985, 11, 06, 21, 06, 21, 500000000, time.Local), "1985-11-06T21:06:21.5"},
"FixedTime": {time.Date(2582, 11, 06, 21, 06, 21, 500000000, time.FixedZone("", 5*3600)), "2582-11-06T21:06:21.5+05:00"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := Time(tt.t).String(); got != tt.want {
t.Errorf("Time.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestUTCTime_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"EarlyUTC": {time.Date(1962, 7, 23, 16, 12, 3, 0, time.UTC), "620723161203Z"},
"LateUTC": {time.Date(2048, 7, 23, 8, 12, 0, 0, time.UTC), "480723081200Z"},
"PositiveOffset": {time.Date(2048, 7, 23, 23, 12, 0, 0, time.FixedZone("", 3*60*60)), "480723231200+0300"},
"NegativeOffset": {time.Date(2048, 7, 23, 2, 12, 0, 0, time.FixedZone("", -(5*60+30)*60)), "480723021200-0530"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := UTCTime(tt.t).String(); got != tt.want {
t.Errorf("UTCTime.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestGeneralizedTime_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"Example": {time.Date(1985, 11, 06, 21, 06, 27, 300000000, time.Local), "19851106210627.3"},
"ExampleUTC": {time.Date(1985, 11, 06, 21, 06, 27, 300000000, time.UTC), "19851106210627.3Z"},
"Fractional": {time.Date(1985, 11, 06, 21, 06, 27, 30000000, time.UTC), "19851106210627.03Z"},
"ExampleOffset": {time.Date(1985, 11, 06, 21, 06, 27, 300000000, time.FixedZone("", -5*3600)), "19851106210627.3-0500"},
"Example2": {time.Date(1985, 11, 06, 21, 06, 00, 456000000, time.Local), "19851106210600.456"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := GeneralizedTime(tt.t).String(); got != tt.want {
t.Errorf("GeneralizedTime.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestDate_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"Simple": {time.Date(6352, 4, 23, 0, 0, 0, 0, time.UTC), "6352-04-23"},
"LocalTime": {time.Date(6352, 4, 23, 0, 0, 0, 0, time.Local), "6352-04-23"},
"WithTime": {time.Date(6352, 4, 23, 18, 2, 4, 62, time.Local), "6352-04-23"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := Date(tt.t).String(); got != tt.want {
t.Errorf("Date.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestTimeOfDay_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"Simple": {time.Date(0, 0, 0, 15, 12, 8, 0, time.Local), "15:12:08"},
"IgnoreDate": {time.Date(1985, 12, 5, 15, 12, 8, 0, time.Local), "15:12:08"},
"IgnoreLocation": {time.Date(1985, 12, 5, 15, 12, 8, 0, time.UTC), "15:12:08"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := TimeOfDay(tt.t).String(); got != tt.want {
t.Errorf("TimeOfDay.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestDateTime_String(t *testing.T) {
tests := map[string]struct {
t time.Time
want string
}{
"Simple": {time.Date(1985, 12, 5, 15, 12, 8, 0, time.Local), "1985-12-05T15:12:08"},
"IgnoreTimeZone": {time.Date(1985, 12, 5, 15, 12, 8, 0, time.UTC), "1985-12-05T15:12:08"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := DateTime(tt.t).String(); got != tt.want {
t.Errorf("DateTime.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestDuration_String(t *testing.T) {
tests := map[string]struct {
t time.Duration
want string
}{
"Zero": {0, "PT0S"},
"Hour": {time.Hour, "PT1H"},
"Minute": {time.Minute, "PT1M"},
"Second": {time.Second, "PT1S"},
"Mixed": {2*time.Hour + 23*time.Minute + 15*time.Second, "PT2H23M15S"},
"Fractional": {15*time.Second + 13*time.Millisecond, "PT15.013S"},
"Negative": {-2*time.Hour - 15*time.Minute - 4*time.Second, "-PT2H15M4S"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := Duration(tt.t).String(); got != tt.want {
t.Errorf("Duration.String() = %v, want %v", got, tt.want)
}
})
}
}
func TestItoaN(t *testing.T) {
tests := map[string]struct {
i int
n int
want string
}{
"2-digit": {23, 2, "23"},
"2-digit-pad": {7, 2, "07"},
"4-digit": {1023, 4, "1023"},
"4-digit-pad": {18, 4, "0018"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := itoaN(tt.i, tt.n); got != tt.want {
t.Errorf("ItoaN() = %v, want %v", got, tt.want)
}
})
}
}