dart-sdk/tests/corelib_2/date_time_copy_with_test.dart
Lukas Klingsbo 6e4e12922a DateTime copyWith extension
This change has been discussed for 8+ years and it would of course be preferred
to be added as an instance method, but since that is a breaking change I added it as an extension as discussed here:
https://github.com/dart-lang/sdk/issues/24644#issuecomment-1241695835

Change-Id: Iebb9f300e449920ae8891abac88f30b271321661
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258541
Commit-Queue: Lasse Nielsen <lrn@google.com>
Reviewed-by: Nate Bosch <nbosch@google.com>
Reviewed-by: Lasse Nielsen <lrn@google.com>
2022-10-20 10:50:25 +00:00

91 lines
2.5 KiB
Dart

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// @dart=2.9
import "package:expect/expect.dart";
// Dart test program for DateTime.copyWith.
testCopyWith(
DateTime original,
int year,
int month,
int day,
int hour,
int minute,
int second,
int millisecond,
bool isUtc,
) {
final result = original.copyWith(
year: year,
month: month,
day: day,
hour: hour,
minute: minute,
second: second,
millisecond: millisecond,
isUtc: isUtc,
);
final expected = isUtc
? DateTime.utc(
year != null ? year : original.year,
month != null ? month : original.month,
day != null ? day : original.day,
hour != null ? hour : original.hour,
minute != null ? minute : original.minute,
second != null ? second : original.second,
millisecond != null ? millisecond : original.millisecond,
)
: DateTime(
year != null ? year : original.year,
month != null ? month : original.month,
day != null ? day : original.day,
hour != null ? hour : original.hour,
minute != null ? minute : original.minute,
second != null ? second : original.second,
millisecond != null ? millisecond : original.millisecond,
);
Expect.equals(expected, result);
}
void main() {
final epoch = DateTime.utc(1970, 1, 1);
final dst = DateTime.parse("2015-07-07T12:12:24Z");
final leap = DateTime.parse("2012-02-28T12:12:24");
for (var year in [null, -100, 1917, 2012]) {
for (var month in [null, -1, 1, 2, 12, 14]) {
for (var day in [null, -1, 1, 28, 29, 30, 31, 32]) {
for (var hour in [null, -1, 0, 23, 25]) {
for (var minute in [null, -1, 1, 59, 61]) {
for (var second in [null, -1, 1, 59, 61]) {
for (var millisecond in [null, -1, 1, 999, 1001]) {
for (var isUtc in [false, true]) {
for (var base in [epoch, dst, leap]) {
testCopyWith(
base,
year,
month,
day,
hour,
minute,
second,
millisecond,
isUtc,
);
}
}
}
}
}
}
}
}
}
}