dart-lang / clock Goto Github PK
View Code? Open in Web Editor NEWA fakeable wrapper for dart:core clock APIs.
Home Page: https://pub.dev/packages/clock
License: Apache License 2.0
A fakeable wrapper for dart:core clock APIs.
Home Page: https://pub.dev/packages/clock
License: Apache License 2.0
The pubspec sdk constraint is >=2.12.0-0 <3.0.0
; this package needs to be migrated to null safety.
If flutter app starts with:
void main() {
WidgetsFlutterBinding.ensureInitialized();
Then clock.now()
prints not mocked value if outside of build
method.
Minimum reproduceable code:
import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
withClock(Clock.fixed(DateTime(1990)), () {
runApp(const MyApp());
});
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
void _printClockNow() {
print('_printClockNow is: ${clock.now()}'); // prints real NOW
setState(() {});
}
@override
Widget build(BuildContext context) {
print('build clock.now() is: ${clock.now()}'); // prints mocked NOW
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[Dummy()],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _printClockNow,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}
}
class Dummy extends StatelessWidget {
@override
Widget build(Object context) {
return Text('t: ${clock.now()}');
}
}
Expected behavior:
clock.now()
to have mocked value despite WidgetsFlutterBinding.ensureInitialized()
EDIT:
Changed because I discovered that culprit is WidgetsFlutterBinding.ensureInitialized()
.
This package still uses deprecated Dart 1 SCREAMING CAPS CONSTANTS. These will be removed shortly in Dart 2. This rewriting tool is available for the migration: This tool is available for the migration: https://github.com/dart-lang/dart2_fix .
Consider this code:
import 'package:clock/clock.dart';
import 'package:test/test.dart';
void main() {
final fakeClock = Clock.fixed(DateTime(2020, 1, 1));
test('inner withClock', () {
withClock(fakeClock, () {
expect(clock, same(fakeClock));
});
});
withClock(fakeClock, () {
test('outer withClock', () {
expect(clock, same(fakeClock));
});
});
}
I don't see anything in the withClock
documentation that indicates whether one form is preferred over the other. When I run this with dart this_test.dart
, I get:
00:00 +0: inner withClock
00:00 +1: outer withClock
00:00 +1 -1: outer withClock [E]
Expected: same instance as <Instance of 'Clock'>
Actual: <Instance of 'Clock'>
If I reverse the tests, they pass when running it through dart
.
The "outer withClock" test by itself fails with flutter test
or with pub run test
. Each test passes individually when run directly through dart
.
Ultimately I'd like to do:
withClock(fakeClock, () {
test(...);
test(...);
test(...);
});
but all of this weirdness seems to prevent that.
(I'm using Dart 2.8.1 with clock 1.0.1.)
Hello! According to the Appendix of Apache License 2.0, if you want to license your software under this License you should "attach the boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information". This condition is not met now. Please add a copyright notice in the appropriate form, including the year of the software development and your name and surname. Thank you in advance
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
info: The imported package 'clock' isn't a dependency of the importing package. (depend_on_referenced_packages at [AppName] test/path/to/hoge_test.dart:4)
But pubspec.yaml
file, I added this.
dependencies:
clock: ^1.1.1
If I use withClock with a fixed time, I get a different time if I use it in a new BuildContext in WidgetTesting. I expect that all of the test should have the fixed time I set with withClock.
Here is a small example.
import 'package:clock/clock.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:golden_toolkit/golden_toolkit.dart';
void main() {
final fixedClock = Clock.fixed(DateTime(2023));
setUpAll(loadAppFonts);
testWidgets('Test clock with raw widget', (tester) async {
// returns 2023-01-01
final widget = withClock(
fixedClock,
() => MaterialApp(
home: Scaffold(
body: Center(child: Text(clock.now().toString())),
),
),
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('raw_widget.png'),
);
});
testWidgets('Test clock with builder', (tester) async {
// returns 2023-04-24
final widget = withClock(
fixedClock,
() => MaterialApp(
home: Scaffold(
body: Center(
child: Builder(
builder: (context) => Text(
clock.now().toString(),
),
),
),
),
),
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('builder.png'),
);
});
testWidgets(
'Test widget with clock only wrapped around widget',
(tester) async {
// returns 2023-04-24
final widget = withClock(
fixedClock,
TestWidget.new,
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('clock_wrap_widget_.png'),
);
},
);
testWidgets(
'Test widget with clock wrapped around whole test content',
(tester) async {
// returns 2023-01-01
await withClock(fixedClock, () async {
final widget = withClock(
fixedClock,
TestWidget.new,
);
await tester.pumpWidget(widget);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('clock_wrap_test_content.png'),
);
});
},
);
}
class TestWidget extends StatelessWidget {
const TestWidget({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text(clock.now().toString()),
),
),
);
}
}
I'm trying to figure out who the copyright holder is for this package.
CONTRIBUTING.md instructs contributors to us a copyright in the source files of:
Copyright (c) 2017, the Dart project authors.
but the source files actually have copyrights for Google Inc., the pub spec has an author of "Dart Team", and the LICENSE file does not contain a copyright.
Many open source packages have a copyright statement at the beginning of the LICENSE file which is what I'd suggest if possible.
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.