flutter_flutter/packages/flutter/test/physics/friction_simulation_test.dart
Chris Bracken c0ea00ed3f
Prefer moreOrLessEquals over closeTo (#64915)
Flutter's `moreOrLessEquals` has a few advantages over `closeTo` from
the `matcher` package:

   * It emits the epsilon value in the test result on failure.
   * It uses a named parameter for epsilon, which improves readability
     at the call site.
   * It has a reasonable default for epsilon in cases where something
     more specific isn't required.

Using it also has the nice property that it aids in its own discovery
when when people go looking for such functionality in new tests.

This change also includes a couple unrelated whitespace formatting cleanups.
2020-08-30 22:20:16 -07:00

51 lines
2.0 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/physics.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('Friction simulation positive velocity', () {
final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, 100.0);
expect(friction.x(0.0), moreOrLessEquals(100.0));
expect(friction.dx(0.0), moreOrLessEquals(100.0));
expect(friction.x(0.1), moreOrLessEquals(110.0, epsilon: 1.0));
expect(friction.x(0.5), moreOrLessEquals(131.0, epsilon: 1.0));
expect(friction.x(2.0), moreOrLessEquals(149.0, epsilon: 1.0));
expect(friction.finalX, moreOrLessEquals(149.0, epsilon: 1.0));
expect(friction.timeAtX(100.0), 0.0);
expect(friction.timeAtX(friction.x(0.1)), moreOrLessEquals(0.1));
expect(friction.timeAtX(friction.x(0.5)), moreOrLessEquals(0.5));
expect(friction.timeAtX(friction.x(2.0)), moreOrLessEquals(2.0));
expect(friction.timeAtX(-1.0), double.infinity);
expect(friction.timeAtX(200.0), double.infinity);
});
test('Friction simulation negative velocity', () {
final FrictionSimulation friction = FrictionSimulation(0.135, 100.0, -100.0);
expect(friction.x(0.0), moreOrLessEquals(100.0));
expect(friction.dx(0.0), moreOrLessEquals(-100.0));
expect(friction.x(0.1), moreOrLessEquals(91.0, epsilon: 1.0));
expect(friction.x(0.5), moreOrLessEquals(68.0, epsilon: 1.0));
expect(friction.x(2.0), moreOrLessEquals(51.0, epsilon: 1.0));
expect(friction.finalX, moreOrLessEquals(50, epsilon: 1.0));
expect(friction.timeAtX(100.0), 0.0);
expect(friction.timeAtX(friction.x(0.1)), moreOrLessEquals(0.1));
expect(friction.timeAtX(friction.x(0.5)), moreOrLessEquals(0.5));
expect(friction.timeAtX(friction.x(2.0)), moreOrLessEquals(2.0));
expect(friction.timeAtX(101.0), double.infinity);
expect(friction.timeAtX(40.0), double.infinity);
});
}