Skip to content

Commit f66ceaf

Browse files
lrhnCommit Queue
authored andcommitted
Update date_time_extremes_test to not make assumptions.
The test assumes that seconds, milliseconds and occasionally microseconds will necessarily be zero, which is to optimistic in practice. The changed test will use all the values from a source `DateTime`, and not stop when it thinks it knows the rest. (Even for UTC values, where it actually does know.) Change-Id: Ie88e6c33e721f9c7cfba605f0e2cb5f4d2f72218 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411021 Reviewed-by: Nicholas Shahan <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]>
1 parent 57e5b28 commit f66ceaf

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

tests/corelib/date_time_extremes_test.dart

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,51 @@ void testExtremes() {
6969
);
7070
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
7171
Expect.throws(
72-
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1),
72+
() => DateTime(
73+
dt.year,
74+
dt.month,
75+
dt.day,
76+
dt.hour,
77+
dt.minute,
78+
dt.second,
79+
dt.millisecond + 1,
80+
),
7381
);
7482
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
7583
Expect.throws(
76-
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 1),
84+
() => DateTime.utc(
85+
dt.year,
86+
dt.month,
87+
dt.day,
88+
dt.hour,
89+
dt.minute,
90+
dt.second,
91+
dt.millisecond + 1,
92+
),
7793
);
7894
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
7995
Expect.throws(
80-
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1),
96+
() => DateTime(
97+
dt.year,
98+
dt.month,
99+
dt.day,
100+
dt.hour,
101+
dt.minute,
102+
dt.second,
103+
dt.millisecond - 1,
104+
),
81105
);
82106
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
83107
Expect.throws(
84-
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, -1),
108+
() => DateTime.utc(
109+
dt.year,
110+
dt.month,
111+
dt.day,
112+
dt.hour,
113+
dt.minute,
114+
dt.second,
115+
dt.millisecond - 1,
116+
),
85117
);
86118

87119
/// The nearest value to [base] in the direction [delta]. For native `int`s,
@@ -96,11 +128,27 @@ void testExtremes() {
96128
}
97129

98130
dt = DateTime.fromMicrosecondsSinceEpoch(_MAX_MILLISECONDS * 1000);
99-
dt = DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
131+
dt = DateTime(
132+
dt.year,
133+
dt.month,
134+
dt.day,
135+
dt.hour,
136+
dt.minute,
137+
dt.second,
138+
dt.millisecond,
139+
);
100140
Expect.equals(_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
101141
print(-_MAX_MILLISECONDS * 1000);
102142
dt = DateTime.fromMicrosecondsSinceEpoch(-_MAX_MILLISECONDS * 1000);
103-
dt = DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second);
143+
dt = DateTime(
144+
dt.year,
145+
dt.month,
146+
dt.day,
147+
dt.hour,
148+
dt.minute,
149+
dt.second,
150+
dt.millisecond,
151+
);
104152
Expect.equals(-_MAX_MILLISECONDS * 1000, dt.microsecondsSinceEpoch);
105153
Expect.throws(
106154
() => DateTime.fromMicrosecondsSinceEpoch(
@@ -138,28 +186,64 @@ void testExtremes() {
138186

139187
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS);
140188
Expect.throws(
141-
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1),
189+
() => DateTime(
190+
dt.year,
191+
dt.month,
192+
dt.day,
193+
dt.hour,
194+
dt.minute,
195+
dt.second,
196+
dt.millisecond,
197+
dt.microsecond + 1,
198+
),
142199
);
143200
Expect.throws(() => dt.copyWith(microsecond: 1));
144201
Expect.isTrue(dt.copyWith(microsecond: -1).toString().endsWith('.999999'));
145202

146203
dt = DateTime.fromMillisecondsSinceEpoch(_MAX_MILLISECONDS, isUtc: true);
147204
Expect.throws(
148-
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, 1),
205+
() => DateTime.utc(
206+
dt.year,
207+
dt.month,
208+
dt.day,
209+
dt.hour,
210+
dt.minute,
211+
dt.second,
212+
dt.millisecond,
213+
dt.microsecond + 1,
214+
),
149215
);
150216
Expect.throws(() => dt.copyWith(microsecond: 1));
151217
Expect.isTrue(dt.copyWith(microsecond: -1).toString().endsWith('.999999Z'));
152218

153219
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS);
154220
Expect.throws(
155-
() => DateTime(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1),
221+
() => DateTime(
222+
dt.year,
223+
dt.month,
224+
dt.day,
225+
dt.hour,
226+
dt.minute,
227+
dt.second,
228+
dt.millisecond,
229+
dt.microsecond - 1,
230+
),
156231
);
157232
Expect.throws(() => dt.copyWith(microsecond: -1));
158233
Expect.isTrue(dt.copyWith(microsecond: 1).toString().endsWith('.000001'));
159234

160235
dt = DateTime.fromMillisecondsSinceEpoch(-_MAX_MILLISECONDS, isUtc: true);
161236
Expect.throws(
162-
() => DateTime.utc(dt.year, dt.month, dt.day, dt.hour, dt.minute, 0, 0, -1),
237+
() => DateTime.utc(
238+
dt.year,
239+
dt.month,
240+
dt.day,
241+
dt.hour,
242+
dt.minute,
243+
dt.second,
244+
dt.millisecond,
245+
dt.microsecond - 1,
246+
),
163247
);
164248
Expect.throws(() => dt.copyWith(microsecond: -1));
165249
Expect.isTrue(dt.copyWith(microsecond: 1).toString().endsWith('.000001Z'));

0 commit comments

Comments
 (0)