@@ -133,3 +133,174 @@ func TestIntervalTarantoolEncoding(t *testing.T) {
133133 })
134134 }
135135}
136+
137+ func TestIntervalString (t * testing.T ) {
138+ tests := []struct {
139+ name string
140+ interval Interval
141+ expected string
142+ }{
143+ {
144+ name : "empty interval" ,
145+ interval : Interval {},
146+ expected : "0 seconds" ,
147+ },
148+ {
149+ name : "single component - years" ,
150+ interval : Interval {
151+ Year : 1 ,
152+ },
153+ expected : "1 year" ,
154+ },
155+ {
156+ name : "multiple years" ,
157+ interval : Interval {
158+ Year : 5 ,
159+ },
160+ expected : "5 years" ,
161+ },
162+ {
163+ name : "multiple components" ,
164+ interval : Interval {
165+ Year : 1 ,
166+ Month : 2 ,
167+ Day : 3 ,
168+ },
169+ expected : "1 year, 2 months and 3 days" ,
170+ },
171+ {
172+ name : "time components" ,
173+ interval : Interval {
174+ Hour : 1 ,
175+ Min : 30 ,
176+ Sec : 45 ,
177+ },
178+ expected : "1 hour, 30 minutes and 45 seconds" ,
179+ },
180+ {
181+ name : "seconds with nanoseconds same sign" ,
182+ interval : Interval {
183+ Sec : 5 ,
184+ Nsec : 123456789 ,
185+ },
186+ expected : "5.123456789 seconds" ,
187+ },
188+ {
189+ name : "negative seconds with nanoseconds" ,
190+ interval : Interval {
191+ Sec : - 5 ,
192+ Nsec : - 123456789 ,
193+ },
194+ expected : "-5.123456789 seconds" ,
195+ },
196+ {
197+ name : "seconds and nanoseconds different signs" ,
198+ interval : Interval {
199+ Sec : 5 ,
200+ Nsec : - 123456789 ,
201+ },
202+ expected : "5 seconds and -123456789 nanoseconds" ,
203+ },
204+ {
205+ name : "only nanoseconds" ,
206+ interval : Interval {
207+ Nsec : 500000000 ,
208+ },
209+ expected : "500000000 nanoseconds" ,
210+ },
211+ {
212+ name : "weeks" ,
213+ interval : Interval {
214+ Week : 2 ,
215+ },
216+ expected : "2 weeks" ,
217+ },
218+ {
219+ name : "complex interval" ,
220+ interval : Interval {
221+ Year : 1 ,
222+ Month : 6 ,
223+ Week : 2 ,
224+ Day : 3 ,
225+ Hour : 12 ,
226+ Min : 30 ,
227+ Sec : 45 ,
228+ Nsec : 123456789 ,
229+ },
230+ expected : "1 year, 6 months, 2 weeks, 3 days, 12 hours, 30 minutes and 45.123456789 seconds" ,
231+ },
232+ {
233+ name : "negative components" ,
234+ interval : Interval {
235+ Year : - 1 ,
236+ Day : - 2 ,
237+ Hour : - 3 ,
238+ },
239+ expected : "-1 year, -2 days and -3 hours" ,
240+ },
241+ }
242+
243+ for _ , tt := range tests {
244+ t .Run (tt .name , func (t * testing.T ) {
245+ result := tt .interval .String ()
246+ if result != tt .expected {
247+ t .Errorf ("Interval.String() = %v, want %v" , result , tt .expected )
248+ }
249+ })
250+ }
251+ }
252+
253+ func TestIntervalStringIntegration (t * testing.T ) {
254+ t .Run ("implements Stringer" , func (t * testing.T ) {
255+ var _ fmt.Stringer = Interval {}
256+ })
257+
258+ t .Run ("works with fmt package" , func (t * testing.T ) {
259+ ival := Interval {Hour : 2 , Min : 30 }
260+
261+ // Проверяем разные форматы fmt
262+ result := fmt .Sprintf ("%s" , ival )
263+ expected := "2 hours and 30 minutes"
264+ if result != expected {
265+ t .Errorf ("fmt.Sprintf('%%s') = %v, want %v" , result , expected )
266+ }
267+
268+ result = fmt .Sprintf ("%v" , ival )
269+ if result != expected {
270+ t .Errorf ("fmt.Sprintf('%%v') = %v, want %v" , result , expected )
271+ }
272+ })
273+ }
274+
275+ func TestIntervalStringEdgeCases (t * testing.T ) {
276+ tests := []struct {
277+ name string
278+ interval Interval
279+ }{
280+ {
281+ name : "max values" ,
282+ interval : Interval {Year : 1 << 63 - 1 , Month : 1 << 63 - 1 },
283+ },
284+ {
285+ name : "min values" ,
286+ interval : Interval {Year : - 1 << 63 , Month : - 1 << 63 },
287+ },
288+ {
289+ name : "mixed signs complex" ,
290+ interval : Interval {Year : 1 , Month : - 1 , Day : 1 , Hour : - 1 },
291+ },
292+ }
293+
294+ for _ , tt := range tests {
295+ t .Run (tt .name , func (t * testing.T ) {
296+ result := tt .interval .String ()
297+ // Проверяем что не паникает и возвращает непустую строку
298+ if result == "" {
299+ t .Error ("Interval.String() returned empty string" )
300+ }
301+ if len (result ) > 1000 { // Разумный лимит
302+ t .Error ("Interval.String() returned unexpectedly long string" )
303+ }
304+ })
305+ }
306+ }
0 commit comments