diff --git a/dev/tools/lib/archive_publisher.dart b/dev/tools/lib/archive_publisher.dart index 55481e3080..afad198cb1 100644 --- a/dev/tools/lib/archive_publisher.dart +++ b/dev/tools/lib/archive_publisher.dart @@ -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) { diff --git a/packages/flutter/test/material/tabs_test.dart b/packages/flutter/test/material/tabs_test.dart index 06de02a670..ae03a01c5b 100644 --- a/packages/flutter/test/material/tabs_test.dart +++ b/packages/flutter/test/material/tabs_test.dart @@ -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); }); diff --git a/packages/flutter/test/painting/alignment_test.dart b/packages/flutter/test/painting/alignment_test.dart index 31d03d461a..fc634dce08 100644 --- a/packages/flutter/test/painting/alignment_test.dart +++ b/packages/flutter/test/painting/alignment_test.dart @@ -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)); diff --git a/packages/flutter/test/painting/border_rtl_test.dart b/packages/flutter/test/painting/border_rtl_test.dart index 300d3faee2..df69c3b24b 100644 --- a/packages/flutter/test/painting/border_rtl_test.dart +++ b/packages/flutter/test/painting/border_rtl_test.dart @@ -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)); }); diff --git a/packages/flutter/test/painting/border_side_test.dart b/packages/flutter/test/painting/border_side_test.dart index d5b403f952..e47e46caac 100644 --- a/packages/flutter/test/painting/border_side_test.dart +++ b/packages/flutter/test/painting/border_side_test.dart @@ -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( diff --git a/packages/flutter/test/painting/border_test.dart b/packages/flutter/test/painting/border_test.dart index bd28a44f7f..4db3b12ef3 100644 --- a/packages/flutter/test/painting/border_test.dart +++ b/packages/flutter/test/painting/border_test.dart @@ -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', () { diff --git a/packages/flutter/test/painting/colors_test.dart b/packages/flutter/test/painting/colors_test.dart index 213b8ed7da..c0fcbab41c 100644 --- a/packages/flutter/test/painting/colors_test.dart +++ b/packages/flutter/test/painting/colors_test.dart @@ -40,7 +40,7 @@ void main() { }); test('ColorSwatch test', () { - final int color = 0xFF027223; + final int color = nonconst(0xFF027223); final ColorSwatch greens1 = new ColorSwatch( color, const { '2259 C': const Color(0xFF027223), diff --git a/packages/flutter/test/painting/edge_insets_test.dart b/packages/flutter/test/painting/edge_insets_test.dart index 813d617a3b..ec275d35b4 100644 --- a/packages/flutter/test/painting/edge_insets_test.dart +++ b/packages/flutter/test/painting/edge_insets_test.dart @@ -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)); diff --git a/packages/flutter/test/painting/image_provider_test.dart b/packages/flutter/test/painting/image_provider_test.dart index 1513da093f..7e3957fd7e 100644 --- a/packages/flutter/test/painting/image_provider_test.dart +++ b/packages/flutter/test/painting/image_provider_test.dart @@ -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); }); } diff --git a/packages/flutter/test/painting/text_span_test.dart b/packages/flutter/test/painting/text_span_test.dart index 75ec26e57f..a8f88ede6c 100644 --- a/packages/flutter/test/painting/text_span_test.dart +++ b/packages/flutter/test/painting/text_span_test.dart @@ -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: [ a1 ]); final TextSpan b2 = new TextSpan(children: [ 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); diff --git a/packages/flutter/test/widgets/directionality_test.dart b/packages/flutter/test/widgets/directionality_test.dart index cde6811b7c..98960607d2 100644 --- a/packages/flutter/test/widgets/directionality_test.dart +++ b/packages/flutter/test/widgets/directionality_test.dart @@ -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); }); } diff --git a/packages/flutter/test/widgets/key_test.dart b/packages/flutter/test/widgets/key_test.dart index 8b953e909a..7355ee50ce 100644 --- a/packages/flutter/test/widgets/key_test.dart +++ b/packages/flutter/test/widgets/key_test.dart @@ -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(int3) == new ValueKey(int3), isTrue); - expect(new ValueKey(int3) == new ValueKey(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(int3) == new ValueKey(int2), isFalse); + expect(new ValueKey(nonconst(3)) == new ValueKey(nonconst(3)), isTrue); + expect(new ValueKey(nonconst(3)) == new ValueKey(nonconst(3)), isFalse); + expect(new ValueKey(nonconst(3)) == new ValueKey(nonconst(2)), isFalse); expect(const ValueKey(double.NAN) == const ValueKey(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(empty), isTrue); - expect(new ValueKey(empty) == new ValueKey(empty), isTrue); - expect(new TestValueKey(empty) == new ValueKey(empty), isFalse); - expect(new TestValueKey(empty) == new TestValueKey(empty), isTrue); + expect(new Key(nonconst('')) == new ValueKey(nonconst('')), isTrue); + expect(new ValueKey(nonconst('')) == new ValueKey(nonconst('')), isTrue); + expect(new TestValueKey(nonconst('')) == new ValueKey(nonconst('')), isFalse); + expect(new TestValueKey(nonconst('')) == new TestValueKey(nonconst('')), isTrue); - expect(new ValueKey(empty) == new ValueKey(empty), isFalse); - expect(new TestValueKey(empty) == new TestValueKey(empty), isFalse); + expect(new ValueKey(nonconst('')) == new ValueKey(nonconst('')), isFalse); + expect(new TestValueKey(nonconst('')) == new TestValueKey(nonconst('')), isFalse); expect(new UniqueKey() == new UniqueKey(), isFalse); final LocalKey k = new UniqueKey(); @@ -44,11 +41,11 @@ void main() { expect(new ValueKey(k) == new ValueKey(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(constNotEquals) == new ValueKey(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); diff --git a/packages/flutter/test/widgets/render_object_widget_test.dart b/packages/flutter/test/widgets/render_object_widget_test.dart index 2cc32bd4d0..c72e0b3348 100644 --- a/packages/flutter/test/widgets/render_object_widget_test.dart +++ b/packages/flutter/test/widgets/render_object_widget_test.dart @@ -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 }); diff --git a/packages/flutter_test/lib/flutter_test.dart b/packages/flutter_test/lib/flutter_test.dart index 55c0adae81..e94f911b8b 100644 --- a/packages/flutter_test/lib/flutter_test.dart +++ b/packages/flutter_test/lib/flutter_test.dart @@ -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'; diff --git a/packages/flutter_test/lib/src/nonconst.dart b/packages/flutter_test/lib/src/nonconst.dart new file mode 100644 index 0000000000..cf364ee278 --- /dev/null +++ b/packages/flutter_test/lib/src/nonconst.dart @@ -0,0 +1,23 @@ +// 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; \ No newline at end of file