Commit 62609669 authored by Adam Barth's avatar Adam Barth

Merge pull request #2141 from abarth/text_span_equals

Fix TextSpan's operator==
parents 941d69db 25219277
......@@ -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<Object> a, List<Object> 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));
}
......
// 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: <TextSpan>[ a1 ]);
TextSpan b2 = new TextSpan(children: <TextSpan>[ 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);
});
}
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