From 25219277f68d18e8200f8df283f48fcb18144dea Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 24 Feb 2016 16:46:58 -0800 Subject: [PATCH] Fix TextSpan's operator== We forgot to compare the lengths of the lists. --- .../lib/src/painting/text_painter.dart | 29 ++++++++++-------- .../flutter/test/painting/text_span_test.dart | 30 +++++++++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 packages/flutter/test/painting/text_span_test.dart diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index c91f21dfd97..7808926e915 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -8,6 +8,19 @@ import 'basic_types.dart'; import 'text_editing.dart'; import 'text_style.dart'; +// TODO(abarth): Should this be somewhere more general? +bool _deepEquals(List a, List b) { + if (a == null) + return b == null; + if (b == null || a.length != b.length) + return false; + for (int i = 0; i < a.length; ++i) { + if (a[i] != b[i]) + return false; + } + return true; +} + /// An immutable span of text. class TextSpan { const TextSpan({ @@ -74,19 +87,9 @@ class TextSpan { if (other is! TextSpan) return false; final TextSpan typedOther = other; - if (typedOther.text != text) - return false; - if (typedOther.style != style) - return false; - if ((typedOther.children == null) != (children == null)) - return false; - if (children != null) { - for (int i = 0; i < children.length; ++i) { - if (typedOther.children[i] != children[i]) - return false; - } - } - return true; + return typedOther.text == text + && typedOther.style == style + && _deepEquals(typedOther.children, children); } int get hashCode => hashValues(style, text, hashList(children)); } diff --git a/packages/flutter/test/painting/text_span_test.dart b/packages/flutter/test/painting/text_span_test.dart new file mode 100644 index 00000000000..9a358380efb --- /dev/null +++ b/packages/flutter/test/painting/text_span_test.dart @@ -0,0 +1,30 @@ +// Copyright 2016 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. + +import 'package:flutter/painting.dart'; + +import 'package:test/test.dart'; + +void main() { + test("TextSpan equals", () { + TextSpan a1 = new TextSpan(text: 'a'); + TextSpan a2 = new TextSpan(text: 'a'); + TextSpan b1 = new TextSpan(children: [ a1 ]); + TextSpan b2 = new TextSpan(children: [ a2 ]); + TextSpan c1 = new TextSpan(); + TextSpan c2 = new TextSpan(); + + expect(a1 == a2, isTrue); + expect(b1 == b2, isTrue); + expect(c1 == c2, isTrue); + + expect(a1 == b2, isFalse); + expect(b1 == c2, isFalse); + expect(c1 == a2, isFalse); + + expect(a1 == c2, isFalse); + expect(b1 == a2, isFalse); + expect(c1 == b2, isFalse); + }); +}