1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-12 15:58:32 +00:00

Web charts common update (#111)

This commit is contained in:
Brett Morgan
2019-07-10 06:37:53 +10:00
committed by GitHub
parent eac7833d1d
commit 3af5bbf125
145 changed files with 2491 additions and 2687 deletions

View File

@@ -19,7 +19,8 @@ import 'package:test/test.dart';
const EPSILON = 0.001;
typedef bool IsTransitionFunction(DateTime tickValue, DateTime prevTickValue);
typedef IsTransitionFunction = bool Function(
DateTime tickValue, DateTime prevTickValue);
class FakeTimeTickFormatter implements TimeTickFormatter {
static const firstTick = '-firstTick-';
@@ -56,41 +57,41 @@ void main() {
TimeTickFormatter timeFormatter3;
setUp(() {
timeFormatter1 = new FakeTimeTickFormatter('fake1');
timeFormatter2 = new FakeTimeTickFormatter('fake2');
timeFormatter3 = new FakeTimeTickFormatter('fake3');
timeFormatter1 = FakeTimeTickFormatter('fake1');
timeFormatter2 = FakeTimeTickFormatter('fake2');
timeFormatter3 = FakeTimeTickFormatter('fake3');
});
group('Uses formatter', () {
test('with largest interval less than diff between tickValues', () {
final formatter = new DateTimeTickFormatter.withFormatters(
final formatter = DateTimeTickFormatter.withFormatters(
{10: timeFormatter1, 100: timeFormatter2, 1000: timeFormatter3});
final formatterCache = <DateTime, String>{};
final ticksWith10Diff = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(10),
new DateTime.fromMillisecondsSinceEpoch(20)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(10),
DateTime.fromMillisecondsSinceEpoch(20)
];
final ticksWith20Diff = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(20),
new DateTime.fromMillisecondsSinceEpoch(40)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(20),
DateTime.fromMillisecondsSinceEpoch(40)
];
final ticksWith100Diff = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(100),
new DateTime.fromMillisecondsSinceEpoch(200)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(100),
DateTime.fromMillisecondsSinceEpoch(200)
];
final ticksWith200Diff = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(200),
new DateTime.fromMillisecondsSinceEpoch(400)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(200),
DateTime.fromMillisecondsSinceEpoch(400)
];
final ticksWith1000Diff = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(1000),
new DateTime.fromMillisecondsSinceEpoch(2000)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(1000),
DateTime.fromMillisecondsSinceEpoch(2000)
];
final expectedLabels10Diff = [
@@ -139,14 +140,14 @@ void main() {
});
test('with smallest interval when no smaller one exists', () {
final formatter = new DateTimeTickFormatter.withFormatters(
final formatter = DateTimeTickFormatter.withFormatters(
{10: timeFormatter1, 100: timeFormatter2});
final formatterCache = <DateTime, String>{};
final ticks = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(1),
new DateTime.fromMillisecondsSinceEpoch(2)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(1),
DateTime.fromMillisecondsSinceEpoch(2)
];
final expectedLabels = [
'fake1-firstTick-0',
@@ -159,11 +160,11 @@ void main() {
});
test('with smallest interval for single tick input', () {
final formatter = new DateTimeTickFormatter.withFormatters(
final formatter = DateTimeTickFormatter.withFormatters(
{10: timeFormatter1, 100: timeFormatter2});
final formatterCache = <DateTime, String>{};
final ticks = [new DateTime.fromMillisecondsSinceEpoch(5000)];
final ticks = [DateTime.fromMillisecondsSinceEpoch(5000)];
final expectedLabels = ['fake1-firstTick-5000'];
final actualLabels = formatter.format(ticks, formatterCache, stepSize: 0);
expect(actualLabels, equals(expectedLabels));
@@ -171,7 +172,7 @@ void main() {
test('on empty input doesnt break', () {
final formatter =
new DateTimeTickFormatter.withFormatters({10: timeFormatter1});
DateTimeTickFormatter.withFormatters({10: timeFormatter1});
final formatterCache = <DateTime, String>{};
final actualLabels =
@@ -180,19 +181,19 @@ void main() {
});
test('that formats transition tick with transition format', () {
final timeFormatter = new FakeTimeTickFormatter('fake',
isTransitionFunction: (DateTime tickValue, _) =>
final timeFormatter = FakeTimeTickFormatter('fake',
isTransitionFunction: (tickValue, _) =>
tickValue.millisecondsSinceEpoch == 20);
final formatterCache = <DateTime, String>{};
final formatter =
new DateTimeTickFormatter.withFormatters({10: timeFormatter});
DateTimeTickFormatter.withFormatters({10: timeFormatter});
final ticks = [
new DateTime.fromMillisecondsSinceEpoch(0),
new DateTime.fromMillisecondsSinceEpoch(10),
new DateTime.fromMillisecondsSinceEpoch(20),
new DateTime.fromMillisecondsSinceEpoch(30)
DateTime.fromMillisecondsSinceEpoch(0),
DateTime.fromMillisecondsSinceEpoch(10),
DateTime.fromMillisecondsSinceEpoch(20),
DateTime.fromMillisecondsSinceEpoch(30)
];
final expectedLabels = [
@@ -212,21 +213,21 @@ void main() {
test('throws arugment error if time resolution key is not positive', () {
// -1 is reserved for any, if there is only one formatter, -1 is allowed.
expect(
() => new DateTimeTickFormatter.withFormatters(
() => DateTimeTickFormatter.withFormatters(
{-1: timeFormatter1, 2: timeFormatter2}),
throwsArgumentError);
});
test('throws argument error if formatters is null or empty', () {
expect(() => new DateTimeTickFormatter.withFormatters(null),
throwsArgumentError);
expect(() => new DateTimeTickFormatter.withFormatters({}),
expect(() => DateTimeTickFormatter.withFormatters(null),
throwsArgumentError);
expect(
() => DateTimeTickFormatter.withFormatters({}), throwsArgumentError);
});
test('throws arugment error if formatters are not sorted', () {
expect(
() => new DateTimeTickFormatter.withFormatters({
() => DateTimeTickFormatter.withFormatters({
3: timeFormatter1,
1: timeFormatter2,
2: timeFormatter3,
@@ -234,7 +235,7 @@ void main() {
throwsArgumentError);
expect(
() => new DateTimeTickFormatter.withFormatters({
() => DateTimeTickFormatter.withFormatters({
1: timeFormatter1,
3: timeFormatter2,
2: timeFormatter3,
@@ -242,7 +243,7 @@ void main() {
throwsArgumentError);
expect(
() => new DateTimeTickFormatter.withFormatters({
() => DateTimeTickFormatter.withFormatters({
2: timeFormatter1,
3: timeFormatter2,
1: timeFormatter3,

View File

@@ -23,7 +23,7 @@ class SimpleDateTimeFactory implements DateTimeFactory {
@override
DateTime createDateTimeFromMilliSecondsSinceEpoch(
int millisecondsSinceEpoch) =>
new DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
DateTime.fromMillisecondsSinceEpoch(millisecondsSinceEpoch);
@override
DateTime createDateTime(int year,
@@ -34,9 +34,9 @@ class SimpleDateTimeFactory implements DateTimeFactory {
int second = 0,
int millisecond = 0,
int microsecond = 0]) =>
new DateTime(
DateTime(
year, month, day, hour, minute, second, millisecond, microsecond);
@override
DateFormat createDateFormat(String pattern) => new DateFormat(pattern);
DateFormat createDateFormat(String pattern) => DateFormat(pattern);
}

View File

@@ -25,16 +25,16 @@ import 'simple_date_time_factory.dart' show SimpleDateTimeFactory;
const EPSILON = 0.001;
void main() {
const dateTimeFactory = const SimpleDateTimeFactory();
const dateTimeFactory = SimpleDateTimeFactory();
const millisecondsInHour = 3600 * 1000;
setUp(() {});
group('Day time stepper', () {
test('get steps with 1 day increments', () {
final stepper = new DayTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 8, 20), end: new DateTime(2017, 8, 25));
final stepper = DayTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 8, 20), end: DateTime(2017, 8, 25));
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -42,20 +42,20 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8, 20),
new DateTime(2017, 8, 21),
new DateTime(2017, 8, 22),
new DateTime(2017, 8, 23),
new DateTime(2017, 8, 24),
new DateTime(2017, 8, 25),
DateTime(2017, 8, 20),
DateTime(2017, 8, 21),
DateTime(2017, 8, 22),
DateTime(2017, 8, 23),
DateTime(2017, 8, 24),
DateTime(2017, 8, 25),
]));
});
test('get steps with 5 day increments', () {
final stepper = new DayTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 8, 10),
end: new DateTime(2017, 8, 26),
final stepper = DayTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 8, 10),
end: DateTime(2017, 8, 26),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(5);
@@ -66,19 +66,19 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8, 11),
new DateTime(2017, 8, 16),
new DateTime(2017, 8, 21),
new DateTime(2017, 8, 26),
DateTime(2017, 8, 11),
DateTime(2017, 8, 16),
DateTime(2017, 8, 21),
DateTime(2017, 8, 26),
]));
});
test('step through daylight saving forward change', () {
final stepper = new DayTimeStepper(dateTimeFactory);
final stepper = DayTimeStepper(dateTimeFactory);
// DST for PST 2017 begin on March 12
final extent = new DateTimeExtents(
start: new DateTime(2017, 3, 11),
end: new DateTime(2017, 3, 13),
final extent = DateTimeExtents(
start: DateTime(2017, 3, 11),
end: DateTime(2017, 3, 13),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -87,18 +87,18 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 3, 11),
new DateTime(2017, 3, 12),
new DateTime(2017, 3, 13),
DateTime(2017, 3, 11),
DateTime(2017, 3, 12),
DateTime(2017, 3, 13),
]));
});
test('step through daylight saving backward change', () {
final stepper = new DayTimeStepper(dateTimeFactory);
final stepper = DayTimeStepper(dateTimeFactory);
// DST for PST 2017 end on November 5
final extent = new DateTimeExtents(
start: new DateTime(2017, 11, 4),
end: new DateTime(2017, 11, 6),
final extent = DateTimeExtents(
start: DateTime(2017, 11, 4),
end: DateTime(2017, 11, 6),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -107,19 +107,19 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 11, 4),
new DateTime(2017, 11, 5),
new DateTime(2017, 11, 6),
DateTime(2017, 11, 4),
DateTime(2017, 11, 5),
DateTime(2017, 11, 6),
]));
});
});
group('Hour time stepper', () {
test('gets steps in 1 hour increments', () {
final stepper = new HourTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 8, 20, 10),
end: new DateTime(2017, 8, 20, 15),
final stepper = HourTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 8, 20, 10),
end: DateTime(2017, 8, 20, 15),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -128,20 +128,20 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8, 20, 10),
new DateTime(2017, 8, 20, 11),
new DateTime(2017, 8, 20, 12),
new DateTime(2017, 8, 20, 13),
new DateTime(2017, 8, 20, 14),
new DateTime(2017, 8, 20, 15),
DateTime(2017, 8, 20, 10),
DateTime(2017, 8, 20, 11),
DateTime(2017, 8, 20, 12),
DateTime(2017, 8, 20, 13),
DateTime(2017, 8, 20, 14),
DateTime(2017, 8, 20, 15),
]));
});
test('gets steps in 4 hour increments', () {
final stepper = new HourTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 8, 20, 10),
end: new DateTime(2017, 8, 21, 10),
final stepper = HourTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 8, 20, 10),
end: DateTime(2017, 8, 21, 10),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(4);
final steps = stepIterable.toList();
@@ -150,22 +150,22 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8, 20, 12),
new DateTime(2017, 8, 20, 16),
new DateTime(2017, 8, 20, 20),
new DateTime(2017, 8, 21, 0),
new DateTime(2017, 8, 21, 4),
new DateTime(2017, 8, 21, 8),
DateTime(2017, 8, 20, 12),
DateTime(2017, 8, 20, 16),
DateTime(2017, 8, 20, 20),
DateTime(2017, 8, 21, 0),
DateTime(2017, 8, 21, 4),
DateTime(2017, 8, 21, 8),
]));
});
test('step through daylight saving forward change in 1 hour increments',
() {
final stepper = new HourTimeStepper(dateTimeFactory);
final stepper = HourTimeStepper(dateTimeFactory);
// DST for PST 2017 begin on March 12. At 2am clocks are turned to 3am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 3, 12, 0),
end: new DateTime(2017, 3, 12, 5),
final extent = DateTimeExtents(
start: DateTime(2017, 3, 12, 0),
end: DateTime(2017, 3, 12, 5),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -174,21 +174,21 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 3, 12, 0),
new DateTime(2017, 3, 12, 1),
new DateTime(2017, 3, 12, 3),
new DateTime(2017, 3, 12, 4),
new DateTime(2017, 3, 12, 5),
DateTime(2017, 3, 12, 0),
DateTime(2017, 3, 12, 1),
DateTime(2017, 3, 12, 3),
DateTime(2017, 3, 12, 4),
DateTime(2017, 3, 12, 5),
]));
});
test('step through daylight saving backward change in 1 hour increments',
() {
final stepper = new HourTimeStepper(dateTimeFactory);
final stepper = HourTimeStepper(dateTimeFactory);
// DST for PST 2017 end on November 5. At 2am, clocks are turned to 1am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 11, 5, 0),
end: new DateTime(2017, 11, 5, 4),
final extent = DateTimeExtents(
start: DateTime(2017, 11, 5, 0),
end: DateTime(2017, 11, 5, 4),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(1);
final steps = stepIterable.toList();
@@ -197,24 +197,24 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 11, 5, 0),
new DateTime(2017, 11, 5, 0)
.add(new Duration(milliseconds: millisecondsInHour)),
new DateTime(2017, 11, 5, 0)
.add(new Duration(milliseconds: millisecondsInHour * 2)),
new DateTime(2017, 11, 5, 2),
new DateTime(2017, 11, 5, 3),
new DateTime(2017, 11, 5, 4),
DateTime(2017, 11, 5, 0),
DateTime(2017, 11, 5, 0)
.add(Duration(milliseconds: millisecondsInHour)),
DateTime(2017, 11, 5, 0)
.add(Duration(milliseconds: millisecondsInHour * 2)),
DateTime(2017, 11, 5, 2),
DateTime(2017, 11, 5, 3),
DateTime(2017, 11, 5, 4),
]));
});
test('step through daylight saving forward change in 4 hour increments',
() {
final stepper = new HourTimeStepper(dateTimeFactory);
final stepper = HourTimeStepper(dateTimeFactory);
// DST for PST 2017 begin on March 12. At 2am clocks are turned to 3am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 3, 12, 0),
end: new DateTime(2017, 3, 13, 0),
final extent = DateTimeExtents(
start: DateTime(2017, 3, 12, 0),
end: DateTime(2017, 3, 13, 0),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(4);
final steps = stepIterable.toList();
@@ -223,23 +223,23 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 3, 12, 4),
new DateTime(2017, 3, 12, 8),
new DateTime(2017, 3, 12, 12),
new DateTime(2017, 3, 12, 16),
new DateTime(2017, 3, 12, 20),
new DateTime(2017, 3, 13, 0),
DateTime(2017, 3, 12, 4),
DateTime(2017, 3, 12, 8),
DateTime(2017, 3, 12, 12),
DateTime(2017, 3, 12, 16),
DateTime(2017, 3, 12, 20),
DateTime(2017, 3, 13, 0),
]));
});
test('step through daylight saving backward change in 4 hour increments',
() {
final stepper = new HourTimeStepper(dateTimeFactory);
final stepper = HourTimeStepper(dateTimeFactory);
// DST for PST 2017 end on November 5.
// At 2am, clocks are turned to 1am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 11, 5, 0),
end: new DateTime(2017, 11, 6, 0),
final extent = DateTimeExtents(
start: DateTime(2017, 11, 5, 0),
end: DateTime(2017, 11, 6, 0),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(4);
final steps = stepIterable.toList();
@@ -248,24 +248,24 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 11, 5, 0)
.add(new Duration(milliseconds: millisecondsInHour)),
new DateTime(2017, 11, 5, 4),
new DateTime(2017, 11, 5, 8),
new DateTime(2017, 11, 5, 12),
new DateTime(2017, 11, 5, 16),
new DateTime(2017, 11, 5, 20),
new DateTime(2017, 11, 6, 0),
DateTime(2017, 11, 5, 0)
.add(Duration(milliseconds: millisecondsInHour)),
DateTime(2017, 11, 5, 4),
DateTime(2017, 11, 5, 8),
DateTime(2017, 11, 5, 12),
DateTime(2017, 11, 5, 16),
DateTime(2017, 11, 5, 20),
DateTime(2017, 11, 6, 0),
]));
});
});
group('Minute time stepper', () {
test('gets steps with 5 minute increments', () {
final stepper = new MinuteTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 8, 20, 3, 46),
end: new DateTime(2017, 8, 20, 4, 02),
final stepper = MinuteTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 8, 20, 3, 46),
end: DateTime(2017, 8, 20, 4, 02),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(5);
final steps = stepIterable.toList();
@@ -274,18 +274,18 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8, 20, 3, 50),
new DateTime(2017, 8, 20, 3, 55),
new DateTime(2017, 8, 20, 4),
DateTime(2017, 8, 20, 3, 50),
DateTime(2017, 8, 20, 3, 55),
DateTime(2017, 8, 20, 4),
]));
});
test('step through daylight saving forward change', () {
final stepper = new MinuteTimeStepper(dateTimeFactory);
final stepper = MinuteTimeStepper(dateTimeFactory);
// DST for PST 2017 begin on March 12. At 2am clocks are turned to 3am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 3, 12, 1, 40),
end: new DateTime(2017, 3, 12, 4, 02),
final extent = DateTimeExtents(
start: DateTime(2017, 3, 12, 1, 40),
end: DateTime(2017, 3, 12, 4, 02),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(15);
final steps = stepIterable.toList();
@@ -294,21 +294,21 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 3, 12, 1, 45),
new DateTime(2017, 3, 12, 3),
new DateTime(2017, 3, 12, 3, 15),
new DateTime(2017, 3, 12, 3, 30),
new DateTime(2017, 3, 12, 3, 45),
new DateTime(2017, 3, 12, 4),
DateTime(2017, 3, 12, 1, 45),
DateTime(2017, 3, 12, 3),
DateTime(2017, 3, 12, 3, 15),
DateTime(2017, 3, 12, 3, 30),
DateTime(2017, 3, 12, 3, 45),
DateTime(2017, 3, 12, 4),
]));
});
test('steps correctly after daylight saving forward change', () {
final stepper = new MinuteTimeStepper(dateTimeFactory);
final stepper = MinuteTimeStepper(dateTimeFactory);
// DST for PST 2017 begin on March 12. At 2am clocks are turned to 3am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 3, 12, 3, 02),
end: new DateTime(2017, 3, 12, 4, 02),
final extent = DateTimeExtents(
start: DateTime(2017, 3, 12, 3, 02),
end: DateTime(2017, 3, 12, 4, 02),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(30);
final steps = stepIterable.toList();
@@ -317,19 +317,18 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 3, 12, 3, 30),
new DateTime(2017, 3, 12, 4),
DateTime(2017, 3, 12, 3, 30),
DateTime(2017, 3, 12, 4),
]));
});
test('step through daylight saving backward change', () {
final stepper = new MinuteTimeStepper(dateTimeFactory);
final stepper = MinuteTimeStepper(dateTimeFactory);
// DST for PST 2017 end on November 5.
// At 2am, clocks are turned to 1am.
final extent = new DateTimeExtents(
start: new DateTime(2017, 11, 5)
.add(new Duration(hours: 1, minutes: 29)),
end: new DateTime(2017, 11, 5, 3, 02));
final extent = DateTimeExtents(
start: DateTime(2017, 11, 5).add(Duration(hours: 1, minutes: 29)),
end: DateTime(2017, 11, 5, 3, 02));
final stepIterable = stepper.getSteps(extent)..iterator.reset(30);
final steps = stepIterable.toList();
@@ -338,27 +337,27 @@ void main() {
steps,
equals([
// The first 1:30am
new DateTime(2017, 11, 5).add(new Duration(hours: 1, minutes: 30)),
DateTime(2017, 11, 5).add(Duration(hours: 1, minutes: 30)),
// The 2nd 1am.
new DateTime(2017, 11, 5).add(new Duration(hours: 2)),
DateTime(2017, 11, 5).add(Duration(hours: 2)),
// The 2nd 1:30am
new DateTime(2017, 11, 5).add(new Duration(hours: 2, minutes: 30)),
DateTime(2017, 11, 5).add(Duration(hours: 2, minutes: 30)),
// 2am
new DateTime(2017, 11, 5).add(new Duration(hours: 3)),
DateTime(2017, 11, 5).add(Duration(hours: 3)),
// 2:30am
new DateTime(2017, 11, 5).add(new Duration(hours: 3, minutes: 30)),
DateTime(2017, 11, 5).add(Duration(hours: 3, minutes: 30)),
// 3am
new DateTime(2017, 11, 5, 3)
DateTime(2017, 11, 5, 3)
]));
});
});
group('Month time stepper', () {
test('steps crosses the year', () {
final stepper = new MonthTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 5),
end: new DateTime(2018, 9),
final stepper = MonthTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 5),
end: DateTime(2018, 9),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(4);
final steps = stepIterable.toList();
@@ -367,18 +366,18 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 8),
new DateTime(2017, 12),
new DateTime(2018, 4),
new DateTime(2018, 8),
DateTime(2017, 8),
DateTime(2017, 12),
DateTime(2018, 4),
DateTime(2018, 8),
]));
});
test('steps within one year', () {
final stepper = new MonthTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017, 1),
end: new DateTime(2017, 5),
final stepper = MonthTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017, 1),
end: DateTime(2017, 5),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(2);
final steps = stepIterable.toList();
@@ -387,66 +386,66 @@ void main() {
expect(
steps,
equals([
new DateTime(2017, 2),
new DateTime(2017, 4),
DateTime(2017, 2),
DateTime(2017, 4),
]));
});
test('step before would allow ticks to include last month of the year', () {
final stepper = new MonthTimeStepper(dateTimeFactory);
final time = new DateTime(2017, 10);
final stepper = MonthTimeStepper(dateTimeFactory);
final time = DateTime(2017, 10);
expect(stepper.getStepTimeBeforeInclusive(time, 1),
equals(new DateTime(2017, 10)));
equals(DateTime(2017, 10)));
// Months - 3, 6, 9, 12
expect(stepper.getStepTimeBeforeInclusive(time, 3),
equals(new DateTime(2017, 9)));
equals(DateTime(2017, 9)));
// Months - 6, 12
expect(stepper.getStepTimeBeforeInclusive(time, 6),
equals(new DateTime(2017, 6)));
equals(DateTime(2017, 6)));
});
test('step before for January', () {
final stepper = new MonthTimeStepper(dateTimeFactory);
final time = new DateTime(2017, 1);
final stepper = MonthTimeStepper(dateTimeFactory);
final time = DateTime(2017, 1);
expect(stepper.getStepTimeBeforeInclusive(time, 1),
equals(new DateTime(2017, 1)));
equals(DateTime(2017, 1)));
// Months - 3, 6, 9, 12
expect(stepper.getStepTimeBeforeInclusive(time, 3),
equals(new DateTime(2016, 12)));
equals(DateTime(2016, 12)));
// Months - 6, 12
expect(stepper.getStepTimeBeforeInclusive(time, 6),
equals(new DateTime(2016, 12)));
equals(DateTime(2016, 12)));
});
test('step before for December', () {
final stepper = new MonthTimeStepper(dateTimeFactory);
final time = new DateTime(2017, 12);
final stepper = MonthTimeStepper(dateTimeFactory);
final time = DateTime(2017, 12);
expect(stepper.getStepTimeBeforeInclusive(time, 1),
equals(new DateTime(2017, 12)));
equals(DateTime(2017, 12)));
// Months - 3, 6, 9, 12
expect(stepper.getStepTimeBeforeInclusive(time, 3),
equals(new DateTime(2017, 12)));
equals(DateTime(2017, 12)));
// Months - 6, 12
expect(stepper.getStepTimeBeforeInclusive(time, 6),
equals(new DateTime(2017, 12)));
equals(DateTime(2017, 12)));
});
});
group('Year stepper', () {
test('steps in 10 year increments', () {
final stepper = new YearTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(2017),
end: new DateTime(2042),
final stepper = YearTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(2017),
end: DateTime(2042),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(10);
final steps = stepIterable.toList();
@@ -455,17 +454,17 @@ void main() {
expect(
steps,
equals([
new DateTime(2020),
new DateTime(2030),
new DateTime(2040),
DateTime(2020),
DateTime(2030),
DateTime(2040),
]));
});
test('steps through negative year', () {
final stepper = new YearTimeStepper(dateTimeFactory);
final extent = new DateTimeExtents(
start: new DateTime(-420),
end: new DateTime(240),
final stepper = YearTimeStepper(dateTimeFactory);
final extent = DateTimeExtents(
start: DateTime(-420),
end: DateTime(240),
);
final stepIterable = stepper.getSteps(extent)..iterator.reset(200);
final steps = stepIterable.toList();
@@ -474,10 +473,10 @@ void main() {
expect(
steps,
equals([
new DateTime(-400),
new DateTime(-200),
new DateTime(0),
new DateTime(200),
DateTime(-400),
DateTime(-200),
DateTime(0),
DateTime(200),
]));
});
});

View File

@@ -20,13 +20,13 @@ import 'simple_date_time_factory.dart' show SimpleDateTimeFactory;
const EPSILON = 0.001;
void main() {
const dateTimeFactory = const SimpleDateTimeFactory();
const dateTimeFactory = SimpleDateTimeFactory();
group('Find closest step size from stepper', () {
test('from exactly matching step size', () {
final stepper = AutoAdjustingDateTimeTickProvider.createHourTickProvider(
dateTimeFactory);
final oneHourMs = (new Duration(hours: 1)).inMilliseconds;
final oneHourMs = (Duration(hours: 1)).inMilliseconds;
final closestStepSize = stepper.getClosestStepSize(oneHourMs);
expect(closestStepSize, equals(oneHourMs));
@@ -36,9 +36,9 @@ void main() {
() {
final stepper = AutoAdjustingDateTimeTickProvider.createHourTickProvider(
dateTimeFactory);
final oneHourMs = (new Duration(hours: 1)).inMilliseconds;
final closestStepSize = stepper
.getClosestStepSize((new Duration(minutes: 56)).inMilliseconds);
final oneHourMs = (Duration(hours: 1)).inMilliseconds;
final closestStepSize =
stepper.getClosestStepSize((Duration(minutes: 56)).inMilliseconds);
expect(closestStepSize, equals(oneHourMs));
});
@@ -47,9 +47,9 @@ void main() {
() {
final stepper = AutoAdjustingDateTimeTickProvider.createHourTickProvider(
dateTimeFactory);
final oneDayMs = (new Duration(hours: 24)).inMilliseconds;
final oneDayMs = (Duration(hours: 24)).inMilliseconds;
final closestStepSize =
stepper.getClosestStepSize((new Duration(hours: 25)).inMilliseconds);
stepper.getClosestStepSize((Duration(hours: 25)).inMilliseconds);
expect(closestStepSize, equals(oneDayMs));
});
@@ -57,9 +57,9 @@ void main() {
test('choose closest increment if exact not found', () {
final stepper = AutoAdjustingDateTimeTickProvider.createHourTickProvider(
dateTimeFactory);
final threeHoursMs = (new Duration(hours: 3)).inMilliseconds;
final closestStepSize = stepper.getClosestStepSize(
(new Duration(hours: 3, minutes: 28)).inMilliseconds);
final threeHoursMs = (Duration(hours: 3)).inMilliseconds;
final closestStepSize = stepper
.getClosestStepSize((Duration(hours: 3, minutes: 28)).inMilliseconds);
expect(closestStepSize, equals(threeHoursMs));
});