@@ -86,6 +86,56 @@ func TestFromDurationP(t *testing.T) {
86
86
}
87
87
}
88
88
89
+ func TestToDuration (t * testing.T ) {
90
+ tests := []struct {
91
+ name string
92
+ input timetypes.GoDuration
93
+ expected * durationpb.Duration
94
+ expectError bool
95
+ }{
96
+ {
97
+ name : "null duration" ,
98
+ input : timetypes .NewGoDurationNull (),
99
+ expected : nil ,
100
+ },
101
+ {
102
+ name : "unknown duration" ,
103
+ input : timetypes .NewGoDurationUnknown (),
104
+ expected : nil ,
105
+ },
106
+ {
107
+ name : "zero duration" ,
108
+ input : timetypes .NewGoDurationValueFromStringMust ("0s" ),
109
+ expected : durationpb .New (0 ),
110
+ },
111
+ {
112
+ name : "normal duration" ,
113
+ input : timetypes .NewGoDurationValueFromStringMust ("1h1m0s" ),
114
+ expected : durationpb .New (time .Hour + time .Minute ),
115
+ },
116
+ }
117
+
118
+ for _ , tt := range tests {
119
+ t .Run (tt .name , func (t * testing.T ) {
120
+ var result * durationpb.Duration
121
+ diagnostics := diag.Diagnostics {}
122
+ provider .ToDuration (& result , tt .input , & diagnostics )
123
+
124
+ if tt .expectError {
125
+ assert .True (t , diagnostics .HasError ())
126
+ return
127
+ }
128
+
129
+ assert .False (t , diagnostics .HasError ())
130
+ if tt .expected == nil {
131
+ assert .Nil (t , result )
132
+ } else {
133
+ assert .Equal (t , tt .expected .AsDuration (), result .AsDuration ())
134
+ }
135
+ })
136
+ }
137
+ }
138
+
89
139
func TestToStringList (t * testing.T ) {
90
140
ctx := context .Background ()
91
141
tests := []struct {
0 commit comments