Unverified Commit 12ceaefb authored by Alexandre Ardhuin's avatar Alexandre Ardhuin Committed by GitHub

work around const lints (#14416)

* work around const lints

* update nonconst doc
parent 08823595
......@@ -143,7 +143,7 @@ class ArchivePublisher {
jsonData['releases'][revision] = metadata;
final Directory localTempDir = tempDir ?? Directory.systemTemp.createTempSync('flutter_');
final File tempFile = new File(path.join(localTempDir.absolute.path, 'releases.json'));
final JsonEncoder encoder = const JsonEncoder.withIndent(' ');
const JsonEncoder encoder = const JsonEncoder.withIndent(' ');
tempFile.writeAsStringSync(encoder.convert(jsonData));
_cloudCopy(tempFile.absolute.path, metadataGsPath);
if (tempDir == null) {
......
......@@ -1503,8 +1503,7 @@ void main() {
});
test('illegal constructor combinations', () {
final Widget $null = null;
expect(() => new Tab(icon: $null), throwsAssertionError);
expect(() => new Tab(icon: nonconst(null)), throwsAssertionError);
expect(() => new Tab(icon: new Container(), text: 'foo', child: new Container()), throwsAssertionError);
expect(() => new Tab(text: 'foo', child: new Container()), throwsAssertionError);
});
......
......@@ -92,8 +92,7 @@ void main() {
expect(const AlignmentDirectional(0.0, 0.0).resolve(TextDirection.rtl), const Alignment(0.0, 0.0));
expect(const AlignmentDirectional(1.0, 1.0).resolve(TextDirection.ltr), const Alignment(1.0, 1.0));
expect(const AlignmentDirectional(1.0, 1.0).resolve(TextDirection.rtl), const Alignment(-1.0, 1.0));
final double $1 = 1.0; // we want these instances to be separate instances so that we're not just checking with a single object
expect(new AlignmentDirectional($1, 2.0), new AlignmentDirectional($1, 2.0));
expect(new AlignmentDirectional(nonconst(1.0), 2.0), new AlignmentDirectional(nonconst(1.0), 2.0));
expect(const AlignmentDirectional(1.0, 2.0), isNot(const AlignmentDirectional(2.0, 1.0)));
expect(const AlignmentDirectional(-1.0, 0.0).resolve(TextDirection.ltr),
const AlignmentDirectional(1.0, 0.0).resolve(TextDirection.rtl));
......
......@@ -317,11 +317,10 @@ void main() {
});
test('BorderDirectional constructor', () {
final Null $null = null;
expect(() => new BorderDirectional(top: $null), throwsAssertionError);
expect(() => new BorderDirectional(start: $null), throwsAssertionError);
expect(() => new BorderDirectional(end: $null), throwsAssertionError);
expect(() => new BorderDirectional(bottom: $null), throwsAssertionError);
expect(() => new BorderDirectional(top: nonconst(null)), throwsAssertionError);
expect(() => new BorderDirectional(start: nonconst(null)), throwsAssertionError);
expect(() => new BorderDirectional(end: nonconst(null)), throwsAssertionError);
expect(() => new BorderDirectional(bottom: nonconst(null)), throwsAssertionError);
});
test('BorderDirectional.merge', () {
......@@ -622,7 +621,7 @@ void main() {
});
test('BorderDirectional hashCode', () {
final BorderSide side = const BorderSide(width: 2.0);
final BorderSide side = new BorderSide(width: nonconst(2.0));
expect(new BorderDirectional(top: side).hashCode, new BorderDirectional(top: side).hashCode);
expect(new BorderDirectional(top: side).hashCode, isNot(new BorderDirectional(bottom: side).hashCode));
});
......
......@@ -15,13 +15,10 @@ void main() {
style: BorderStyle.solid,
),
);
// so that we can use `new` below, we use these:
final Null $null = null;
final double $minus1 = -1.0;
expect(() => new BorderSide(color: $null), throwsAssertionError);
expect(() => new BorderSide(width: $null), throwsAssertionError);
expect(() => new BorderSide(style: $null), throwsAssertionError);
expect(() => new BorderSide(width: $minus1), throwsAssertionError);
expect(() => new BorderSide(color: nonconst(null)), throwsAssertionError);
expect(() => new BorderSide(width: nonconst(null)), throwsAssertionError);
expect(() => new BorderSide(style: nonconst(null)), throwsAssertionError);
expect(() => new BorderSide(width: nonconst(-1.0)), throwsAssertionError);
expect(
const BorderSide(width: -0.0),
const BorderSide(
......
......@@ -7,11 +7,10 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
test('Border constructor', () {
final Null $null = null;
expect(() => new Border(left: $null), throwsAssertionError);
expect(() => new Border(top: $null), throwsAssertionError);
expect(() => new Border(right: $null), throwsAssertionError);
expect(() => new Border(bottom: $null), throwsAssertionError);
expect(() => new Border(left: nonconst(null)), throwsAssertionError);
expect(() => new Border(top: nonconst(null)), throwsAssertionError);
expect(() => new Border(right: nonconst(null)), throwsAssertionError);
expect(() => new Border(bottom: nonconst(null)), throwsAssertionError);
});
test('Border.merge', () {
......
......@@ -40,7 +40,7 @@ void main() {
});
test('ColorSwatch test', () {
final int color = 0xFF027223;
final int color = nonconst(0xFF027223);
final ColorSwatch<String> greens1 = new ColorSwatch<String>(
color, const <String, Color>{
'2259 C': const Color(0xFF027223),
......
......@@ -73,7 +73,7 @@ void main() {
});
test('EdgeInsets equality', () {
final double $5 = 5.0; // we want these instances to be separate instances so that we're not just checking with a single object
final double $5 = nonconst(5.0);
expect(new EdgeInsetsDirectional.only(top: $5, bottom: 7.0), new EdgeInsetsDirectional.only(top: $5, bottom: 7.0));
expect(new EdgeInsets.only(top: $5, bottom: 7.0), new EdgeInsetsDirectional.only(top: $5, bottom: 7.0));
expect(new EdgeInsetsDirectional.only(top: $5, bottom: 7.0), new EdgeInsets.only(top: $5, bottom: 7.0));
......
......@@ -8,8 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
void main() {
test('NetworkImage non-null url test', () {
expect(() {
final String url = null; // we don't want this instance to be const because otherwise it would throw at compile time.
new NetworkImage(url);
new NetworkImage(nonconst(null));
}, throwsAssertionError);
});
}
......@@ -3,19 +3,17 @@
// found in the LICENSE file.
import 'package:flutter/painting.dart';
import 'package:flutter_test/flutter_test.dart' show nonconst;
import 'package:test/test.dart';
void main() {
test('TextSpan equals', () {
final String text = 'a'; // we want these instances to be separate instances so that we're not just checking with a single object
final TextSpan a1 = new TextSpan(text: text);
final TextSpan a2 = new TextSpan(text: text);
final TextSpan a1 = new TextSpan(text: nonconst('a'));
final TextSpan a2 = new TextSpan(text: nonconst('a'));
final TextSpan b1 = new TextSpan(children: <TextSpan>[ a1 ]);
final TextSpan b2 = new TextSpan(children: <TextSpan>[ a2 ]);
final String nullText = null; // we want these instances to be separate instances so that we're not just checking with a single object
final TextSpan c1 = new TextSpan(text: nullText);
final TextSpan c2 = new TextSpan(text: nullText);
final TextSpan c1 = new TextSpan(text: nonconst(null));
final TextSpan c2 = new TextSpan(text: nonconst(null));
expect(a1 == a2, isTrue);
expect(b1 == b2, isTrue);
......
......@@ -65,8 +65,7 @@ void main() {
testWidgets('Directionality can\'t be null', (WidgetTester tester) async {
expect(() {
final TextDirection textDirection = null; // we don't want this instance to be const because otherwise it would throw at compile time.
new Directionality(textDirection: textDirection, child: const Placeholder());
new Directionality(textDirection: nonconst(null), child: const Placeholder());
}, throwsAssertionError);
});
}
......@@ -19,21 +19,18 @@ class NotEquals {
void main() {
testWidgets('Keys', (WidgetTester tester) async {
final int int3 = 3; // we want these instances to be separate instances so that we're not just checking with a single object
expect(new ValueKey<int>(int3) == new ValueKey<int>(int3), isTrue);
expect(new ValueKey<num>(int3) == new ValueKey<int>(int3), isFalse);
final int int2 = 2; // we want these instances to be separate instances so that we're not just checking with a single object
expect(new ValueKey<int>(int3) == new ValueKey<int>(int2), isFalse);
expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isTrue);
expect(new ValueKey<num>(nonconst(3)) == new ValueKey<int>(nonconst(3)), isFalse);
expect(new ValueKey<int>(nonconst(3)) == new ValueKey<int>(nonconst(2)), isFalse);
expect(const ValueKey<double>(double.NAN) == const ValueKey<double>(double.NAN), isFalse);
final String empty = ''; // we want these instances to be separate instances so that we're not just checking with a single object
expect(new Key(empty) == new ValueKey<String>(empty), isTrue);
expect(new ValueKey<String>(empty) == new ValueKey<String>(empty), isTrue);
expect(new TestValueKey<String>(empty) == new ValueKey<String>(empty), isFalse);
expect(new TestValueKey<String>(empty) == new TestValueKey<String>(empty), isTrue);
expect(new Key(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
expect(new ValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isTrue);
expect(new TestValueKey<String>(nonconst('')) == new ValueKey<String>(nonconst('')), isFalse);
expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<String>(nonconst('')), isTrue);
expect(new ValueKey<String>(empty) == new ValueKey<dynamic>(empty), isFalse);
expect(new TestValueKey<String>(empty) == new TestValueKey<dynamic>(empty), isFalse);
expect(new ValueKey<String>(nonconst('')) == new ValueKey<dynamic>(nonconst('')), isFalse);
expect(new TestValueKey<String>(nonconst('')) == new TestValueKey<dynamic>(nonconst('')), isFalse);
expect(new UniqueKey() == new UniqueKey(), isFalse);
final LocalKey k = new UniqueKey();
......@@ -44,11 +41,11 @@ void main() {
expect(new ValueKey<LocalKey>(k) == new ValueKey<UniqueKey>(k), isFalse);
expect(new ObjectKey(k) == new ObjectKey(k), isTrue);
final NotEquals constNotEquals = const NotEquals(); // we want these instances to be separate instances so that we're not just checking with a single object
final NotEquals constNotEquals = nonconst(const NotEquals());
expect(new ValueKey<NotEquals>(constNotEquals) == new ValueKey<NotEquals>(constNotEquals), isFalse);
expect(new ObjectKey(constNotEquals) == new ObjectKey(constNotEquals), isTrue);
final Object constObject = const Object(); // we want these instances to be separate instances so that we're not just checking with a single object
final Object constObject = nonconst(const Object());
expect(new ObjectKey(constObject) == new ObjectKey(constObject), isTrue);
expect(new ObjectKey(new Object()) == new ObjectKey(new Object()), isFalse);
......
......@@ -6,10 +6,9 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
final Border nullBorder = null; // we want these instances to be separate instances so that we're not just checking with a single object
final BoxDecoration kBoxDecorationA = new BoxDecoration(border: nullBorder);
final BoxDecoration kBoxDecorationB = new BoxDecoration(border: nullBorder);
final BoxDecoration kBoxDecorationC = new BoxDecoration(border: nullBorder);
final BoxDecoration kBoxDecorationA = new BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationB = new BoxDecoration(border: nonconst(null));
final BoxDecoration kBoxDecorationC = new BoxDecoration(border: nonconst(null));
class TestWidget extends StatelessWidget {
const TestWidget({ this.child });
......
......@@ -12,6 +12,7 @@ export 'src/binding.dart';
export 'src/controller.dart';
export 'src/finders.dart';
export 'src/matchers.dart';
export 'src/nonconst.dart';
export 'src/stack_manipulation.dart';
export 'src/test_async_utils.dart';
export 'src/test_pointer.dart';
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/// This function can be used to call a const constructor in such a way as to
/// create a new instance rather than creating the common const instance.
///
/// ```dart
/// class A {
/// const A(this.i);
/// int i;
/// }
///
/// main () {
/// // prevent prefer_const_constructors lint
/// new A(nonconst(null));
///
/// // prevent prefer_const_declarations lint
/// final int $null = nonconst(null);
/// final A a = nonconst(const A(null));
/// }
/// ```
T nonconst<T>(T t) => t;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment