Skip to content

Commit aa36244

Browse files
committed
Add duration utility tests and remove unused import
1 parent 4c8a7e0 commit aa36244

File tree

2 files changed

+217
-1
lines changed

2 files changed

+217
-1
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
import { Durations, Duration } from './durations';
2+
3+
describe('Durations', () => {
4+
describe('toDuration', () => {
5+
it('should parse years', () => {
6+
expect(Durations.toDuration('P1Y')).toEqual({ sign: 1, years: 1 });
7+
});
8+
9+
it('should parse months', () => {
10+
expect(Durations.toDuration('P2M')).toEqual({ sign: 1, months: 2 });
11+
});
12+
13+
it('should parse weeks', () => {
14+
expect(Durations.toDuration('P3W')).toEqual({ sign: 1, weeks: 3 });
15+
});
16+
17+
it('should parse days', () => {
18+
expect(Durations.toDuration('P4D')).toEqual({ sign: 1, days: 4 });
19+
});
20+
21+
it('should parse hours', () => {
22+
expect(Durations.toDuration('PT5H')).toEqual({ sign: 1, hours: 5 });
23+
});
24+
25+
it('should parse minutes', () => {
26+
expect(Durations.toDuration('PT30M')).toEqual({ sign: 1, minutes: 30 });
27+
});
28+
29+
it('should parse seconds', () => {
30+
expect(Durations.toDuration('PT45S')).toEqual({ sign: 1, seconds: 45 });
31+
});
32+
33+
it('should parse full duration with all components', () => {
34+
expect(Durations.toDuration('P1Y2M3W4DT5H6M7S')).toEqual({
35+
sign: 1,
36+
years: 1,
37+
months: 2,
38+
weeks: 3,
39+
days: 4,
40+
hours: 5,
41+
minutes: 6,
42+
seconds: 7,
43+
});
44+
});
45+
46+
it('should parse date and time components', () => {
47+
expect(Durations.toDuration('P1Y2M3DT4H5M6S')).toEqual({
48+
sign: 1,
49+
years: 1,
50+
months: 2,
51+
days: 3,
52+
hours: 4,
53+
minutes: 5,
54+
seconds: 6,
55+
});
56+
});
57+
58+
it('should parse negative duration with minus sign', () => {
59+
expect(Durations.toDuration('-P1Y2M')).toEqual({ sign: -1, years: 1, months: 2 });
60+
});
61+
62+
it('should parse positive duration with plus sign', () => {
63+
expect(Durations.toDuration('+P1Y2M')).toEqual({ sign: 1, years: 1, months: 2 });
64+
});
65+
66+
it('should parse decimal values', () => {
67+
expect(Durations.toDuration('P1.5Y')).toEqual({ sign: 1, years: 1.5 });
68+
});
69+
70+
it('should parse decimal values in time components', () => {
71+
expect(Durations.toDuration('PT1.5H')).toEqual({ sign: 1, hours: 1.5 });
72+
});
73+
74+
it('should parse comma as decimal separator', () => {
75+
expect(Durations.toDuration('P1,5Y')).toEqual({ sign: 1, years: 1 });
76+
});
77+
78+
it('should throw error for invalid duration string', () => {
79+
expect(() => Durations.toDuration('invalid')).toThrowError(TypeError);
80+
});
81+
82+
it('should throw error for empty string', () => {
83+
expect(() => Durations.toDuration('')).toThrowError(TypeError);
84+
});
85+
86+
it('should throw error for string without P prefix', () => {
87+
expect(() => Durations.toDuration('1Y')).toThrowError(TypeError);
88+
});
89+
90+
it('should throw error for string too short', () => {
91+
expect(() => Durations.toDuration('P')).toThrowError(TypeError);
92+
});
93+
94+
it('should handle zero values', () => {
95+
expect(Durations.toDuration('P0Y')).toEqual({ sign: 1, years: 0 });
96+
});
97+
98+
it('should include zero components', () => {
99+
const result = Durations.toDuration('P1Y0M0DT0H0M0S');
100+
expect(result.years).toBe(1);
101+
expect(result.months).toBe(0);
102+
expect(result.days).toBe(0);
103+
expect(result.hours).toBe(0);
104+
expect(result.minutes).toBe(0);
105+
expect(result.seconds).toBe(0);
106+
});
107+
});
108+
109+
describe('toString', () => {
110+
it('should convert years', () => {
111+
expect(Durations.toString({ years: 1 })).toBe('P1Y');
112+
});
113+
114+
it('should convert months', () => {
115+
expect(Durations.toString({ months: 2 })).toBe('P2M');
116+
});
117+
118+
it('should convert weeks', () => {
119+
expect(Durations.toString({ weeks: 3 })).toBe('P3W');
120+
});
121+
122+
it('should convert days', () => {
123+
expect(Durations.toString({ days: 4 })).toBe('P4D');
124+
});
125+
126+
it('should convert hours', () => {
127+
expect(Durations.toString({ hours: 5 })).toBe('PT5H');
128+
});
129+
130+
it('should convert minutes', () => {
131+
expect(Durations.toString({ minutes: 30 })).toBe('PT30M');
132+
});
133+
134+
it('should convert seconds', () => {
135+
expect(Durations.toString({ seconds: 45 })).toBe('PT45S');
136+
});
137+
138+
it('should convert full duration', () => {
139+
expect(
140+
Durations.toString({
141+
years: 1,
142+
months: 2,
143+
weeks: 3,
144+
days: 4,
145+
hours: 5,
146+
minutes: 6,
147+
seconds: 7,
148+
}),
149+
).toBe('P1Y2M3W4DT5H6M7S');
150+
});
151+
152+
it('should convert date and time components', () => {
153+
expect(
154+
Durations.toString({
155+
years: 1,
156+
months: 2,
157+
days: 3,
158+
hours: 4,
159+
minutes: 5,
160+
seconds: 6,
161+
}),
162+
).toBe('P1Y2M3DT4H5M6S');
163+
});
164+
165+
it('should convert negative duration', () => {
166+
expect(Durations.toString({ sign: -1, years: 1, months: 2 })).toBe('-P1Y2M');
167+
});
168+
169+
it('should convert positive sign explicitly', () => {
170+
expect(Durations.toString({ sign: 1, years: 1 })).toBe('P1Y');
171+
});
172+
173+
it('should omit zero year in output', () => {
174+
expect(Durations.toString({ years: 0 })).toBe('P');
175+
});
176+
177+
it('should omit zero time components when no time present', () => {
178+
expect(Durations.toString({ days: 1, hours: 0 })).toBe('P1D');
179+
});
180+
181+
it('should include T only when time components present', () => {
182+
expect(Durations.toString({ days: 1, hours: 1 })).toBe('P1DT1H');
183+
expect(Durations.toString({ days: 1 })).toBe('P1D');
184+
});
185+
186+
it('should handle empty duration', () => {
187+
expect(Durations.toString({})).toBe('P');
188+
});
189+
190+
it('should convert decimal values', () => {
191+
expect(Durations.toString({ years: 1.5 })).toBe('P1.5Y');
192+
});
193+
});
194+
195+
describe('roundtrip', () => {
196+
it('should parse and convert back the same string', () => {
197+
const original = 'P1Y2M3W4DT5H6M7S';
198+
const parsed = Durations.toDuration(original);
199+
const converted = Durations.toString(parsed);
200+
expect(converted).toBe(original);
201+
});
202+
203+
it('should parse and convert negative duration', () => {
204+
const original = '-P1Y2M';
205+
const parsed = Durations.toDuration(original);
206+
const converted = Durations.toString(parsed);
207+
expect(converted).toBe(original);
208+
});
209+
210+
it('should handle decimal values roundtrip', () => {
211+
const original = 'P1.5Y';
212+
const parsed = Durations.toDuration(original);
213+
const converted = Durations.toString(parsed);
214+
expect(converted).toBe(original);
215+
});
216+
});
217+
});

projects/angular-odata/src/lib/utils/http.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { TestBed } from '@angular/core/testing';
21
import { Http } from './http';
32

43
describe('Http', () => {

0 commit comments

Comments
 (0)