Skip to content

Commit 580b4ba

Browse files
committed
Add RFC 3339 tests (as part of the ISO 8601 parser)
1 parent 7a645dd commit 580b4ba

1 file changed

Lines changed: 104 additions & 45 deletions

File tree

test/src/pick_datetime_test.dart

Lines changed: 104 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -136,51 +136,110 @@ void main() {
136136
});
137137
});
138138

139-
group('official dart tests', () {
140-
test('1', () {
141-
final date = DateTime.utc(1999, DateTime.june, 11, 18, 46, 53);
142-
expect(pick('Fri, 11 Jun 1999 18:46:53 GMT').asDateTimeOrThrow(), date);
143-
expect(
144-
pick('Friday, 11-Jun-1999 18:46:53 GMT').asDateTimeOrThrow(), date);
145-
expect(pick('Fri Jun 11 18:46:53 1999').asDateTimeOrThrow(), date);
146-
});
147-
148-
test('2', () {
149-
final date = DateTime.utc(1970, DateTime.january);
150-
expect(pick('Thu, 1 Jan 1970 00:00:00 GMT').asDateTimeOrThrow(), date);
151-
expect(pick('Thursday, 1-Jan-1970 00:00:00 GMT').asDateTimeOrThrow(),
152-
date);
153-
expect(pick('Thu Jan 1 00:00:00 1970').asDateTimeOrThrow(), date);
154-
});
155-
156-
test('3', () {
157-
final date = DateTime.utc(2012, DateTime.march, 5, 23, 59, 59);
158-
expect(pick('Mon, 5 Mar 2012 23:59:59 GMT').asDateTimeOrThrow(), date);
159-
expect(
160-
pick('Monday, 5-Mar-2012 23:59:59 GMT').asDateTimeOrThrow(), date);
161-
expect(pick('Mon Mar 5 23:59:59 2012').asDateTimeOrThrow(), date);
162-
});
163-
});
164-
165-
group('explicit format uses only one parser', () {
166-
test(
167-
'asDateTimeOrNull: ISO-8601 String ca not be parsed by ansi c asctime',
168-
() {
169-
final iso8601 = '2005-08-15T15:52:01+0000';
170-
final value = pick(iso8601)
171-
.asDateTimeOrNull(format: PickDateFormat.ANSI_C_asctime);
172-
expect(value, isNull);
173-
});
174-
test(
175-
'asDateTimeOrThrow: ISO-8601 String ca not be parsed by ansi c asctime',
176-
() {
177-
final iso8601 = '2005-08-15T15:52:01+0000';
178-
expect(
179-
() => pick(iso8601)
180-
.asDateTimeOrThrow(format: PickDateFormat.ANSI_C_asctime),
181-
throwsA(pickException(
182-
containing: ['2005-08-15T15:52:01+0000', 'DateTime'])),
183-
);
139+
group('ISO 8601', () {
140+
group('official dart tests', () {
141+
test('1', () {
142+
final date = DateTime.utc(1999, DateTime.june, 11, 18, 46, 53);
143+
expect(
144+
pick('Fri, 11 Jun 1999 18:46:53 GMT').asDateTimeOrThrow(), date);
145+
expect(pick('Friday, 11-Jun-1999 18:46:53 GMT').asDateTimeOrThrow(),
146+
date);
147+
expect(pick('Fri Jun 11 18:46:53 1999').asDateTimeOrThrow(), date);
148+
});
149+
150+
test('2', () {
151+
final date = DateTime.utc(1970, DateTime.january);
152+
expect(
153+
pick('Thu, 1 Jan 1970 00:00:00 GMT').asDateTimeOrThrow(), date);
154+
expect(pick('Thursday, 1-Jan-1970 00:00:00 GMT').asDateTimeOrThrow(),
155+
date);
156+
expect(pick('Thu Jan 1 00:00:00 1970').asDateTimeOrThrow(), date);
157+
});
158+
159+
test('3', () {
160+
final date = DateTime.utc(2012, DateTime.march, 5, 23, 59, 59);
161+
expect(
162+
pick('Mon, 5 Mar 2012 23:59:59 GMT').asDateTimeOrThrow(), date);
163+
expect(pick('Monday, 5-Mar-2012 23:59:59 GMT').asDateTimeOrThrow(),
164+
date);
165+
expect(pick('Mon Mar 5 23:59:59 2012').asDateTimeOrThrow(), date);
166+
});
167+
});
168+
169+
group('explicit format uses only one parser', () {
170+
test(
171+
'asDateTimeOrNull: ISO-8601 String ca not be parsed by ansi c asctime',
172+
() {
173+
final iso8601 = '2005-08-15T15:52:01+0000';
174+
final value = pick(iso8601)
175+
.asDateTimeOrNull(format: PickDateFormat.ANSI_C_asctime);
176+
expect(value, isNull);
177+
});
178+
test(
179+
'asDateTimeOrThrow: ISO-8601 String ca not be parsed by ansi c asctime',
180+
() {
181+
final iso8601 = '2005-08-15T15:52:01+0000';
182+
expect(
183+
() => pick(iso8601)
184+
.asDateTimeOrThrow(format: PickDateFormat.ANSI_C_asctime),
185+
throwsA(pickException(
186+
containing: ['2005-08-15T15:52:01+0000', 'DateTime'])),
187+
);
188+
});
189+
});
190+
191+
group('RFC 3339', () {
192+
test('parses the example date', () {
193+
final date = pick('2021-11-01T11:53:15+00:00')
194+
.asDateTimeOrThrow(format: PickDateFormat.ISO_8601);
195+
expect(date.day, equals(1));
196+
expect(date.month, equals(DateTime.november));
197+
expect(date.year, equals(2021));
198+
expect(date.hour, equals(11));
199+
expect(date.minute, equals(53));
200+
expect(date.second, equals(15));
201+
expect(date.timeZoneName, equals('UTC'));
202+
});
203+
204+
// examples from https://datatracker.ietf.org/doc/html/rfc3339#section-5.8
205+
test('rfc339 examples 1', () {
206+
final date = pick('1985-04-12T23:20:50.52Z').asDateTimeOrThrow();
207+
expect(date.day, equals(12));
208+
expect(date.month, equals(DateTime.april));
209+
expect(date.year, equals(1985));
210+
expect(date.hour, equals(23));
211+
expect(date.minute, equals(20));
212+
expect(date.second, equals(50));
213+
expect(date.millisecond, equals(520));
214+
expect(date.timeZoneName, equals('UTC'));
215+
});
216+
test('rfc339 examples 2 - time zone', () {
217+
final date = pick('1996-12-19T16:39:57-08:00').asDateTimeOrThrow();
218+
expect(date.day, equals(20));
219+
expect(date.month, equals(DateTime.december));
220+
expect(date.year, equals(1996));
221+
expect(date.hour, equals(0));
222+
expect(date.minute, equals(39));
223+
expect(date.second, equals(57));
224+
expect(date.millisecond, equals(0));
225+
expect(date.timeZoneName, equals('UTC'));
226+
});
227+
test('rfc339 examples 3 - leap second', () {
228+
final date = pick('1990-12-31T23:59:60Z').asDateTimeOrThrow();
229+
expect(date.day, equals(1));
230+
expect(date.month, equals(DateTime.january));
231+
expect(date.year, equals(1991));
232+
expect(date.hour, equals(0));
233+
expect(date.minute, equals(0));
234+
expect(date.second, equals(0));
235+
expect(date.millisecond, equals(0));
236+
expect(date.timeZoneName, equals('UTC'));
237+
238+
// example 4
239+
// same leap second, different time zone
240+
final date2 = pick('1990-12-31T23:59:60Z').asDateTimeOrThrow();
241+
expect(date2, date);
242+
});
184243
});
185244
});
186245

0 commit comments

Comments
 (0)