@@ -133,3 +133,172 @@ 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" +
231+ "and 45.123456789 seconds" ,
232+ },
233+ {
234+ name : "negative components" ,
235+ interval : Interval {
236+ Year : - 1 ,
237+ Day : - 2 ,
238+ Hour : - 3 ,
239+ },
240+ expected : "-1 year, -2 days and -3 hours" ,
241+ },
242+ }
243+
244+ for _ , tt := range tests {
245+ t .Run (tt .name , func (t * testing.T ) {
246+ result := tt .interval .String ()
247+ if result != tt .expected {
248+ t .Errorf ("Interval.String() = %v, want %v" , result , tt .expected )
249+ }
250+ })
251+ }
252+ }
253+
254+ func TestIntervalStringIntegration (t * testing.T ) {
255+ t .Run ("implements Stringer" , func (t * testing.T ) {
256+ var _ fmt.Stringer = Interval {}
257+ })
258+
259+ t .Run ("works with fmt package" , func (t * testing.T ) {
260+ ival := Interval {Hour : 2 , Min : 30 }
261+ result := ival .String ()
262+ expected := "2 hours and 30 minutes"
263+ if result != expected {
264+ t .Errorf ("fmt.Sprintf('%%s') = %v, want %v" , result , expected )
265+ }
266+
267+ result = fmt .Sprintf ("%v" , ival )
268+ if result != expected {
269+ t .Errorf ("fmt.Sprintf('%%v') = %v, want %v" , result , expected )
270+ }
271+ })
272+ }
273+
274+ func TestIntervalStringEdgeCases (t * testing.T ) {
275+ tests := []struct {
276+ name string
277+ interval Interval
278+ }{
279+ {
280+ name : "max values" ,
281+ interval : Interval {Year : 1 << 63 - 1 , Month : 1 << 63 - 1 },
282+ },
283+ {
284+ name : "min values" ,
285+ interval : Interval {Year : - 1 << 63 , Month : - 1 << 63 },
286+ },
287+ {
288+ name : "mixed signs complex" ,
289+ interval : Interval {Year : 1 , Month : - 1 , Day : 1 , Hour : - 1 },
290+ },
291+ }
292+
293+ for _ , tt := range tests {
294+ t .Run (tt .name , func (t * testing.T ) {
295+ result := tt .interval .String ()
296+ if result == "" {
297+ t .Error ("Interval.String() returned empty string" )
298+ }
299+ if len (result ) > 1000 { // Разумный лимит
300+ t .Error ("Interval.String() returned unexpectedly long string" )
301+ }
302+ })
303+ }
304+ }
0 commit comments