flutter/packages/flutter_driver/lib/src/matcher_util.dart
Ian Hickson 8f56f6fdd1 Add documentation and clean up code. (#11330)
Mainly, this adds documentation to members that were previously
lacking documentation.

It also adds a big block of documentation about improving performance
of widgets.

This also removes some references to package:collection and adds
global setEquals and listEquals methods in foundation that we can use.
(setEquals in particular should be much faster than the
package:collection equivalent, though both should be faster as they
avoid allocating new objects.) All remaining references now qualify
the import so we know what our remaining dependencies are.

Also lots of code reordering in Flutter driver to make the code
consistent and apply the style guide more thoroughly.
2017-07-21 16:39:04 -07:00

83 lines
2.0 KiB
Dart

// 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:matcher/matcher.dart';
/// Matches [value] against the [matcher].
MatchResult match(dynamic value, Matcher matcher) {
final Map<dynamic, dynamic> matchState = <dynamic, dynamic>{};
if (matcher.matches(value, matchState)) {
return new MatchResult._matched();
} else {
final Description description = matcher.describeMismatch(
value, new _TextDescription(), matchState, false,
);
return new MatchResult._mismatched(description.toString());
}
}
/// Result of matching a value against a matcher.
class MatchResult {
MatchResult._matched()
: hasMatched = true,
mismatchDescription = null;
MatchResult._mismatched(this.mismatchDescription)
: hasMatched = false;
/// Whether the match succeeded.
final bool hasMatched;
/// If the match did not succeed, this field contains the explanation.
final String mismatchDescription;
}
/// Writes description into a string.
class _TextDescription implements Description {
final StringBuffer _text = new StringBuffer();
@override
int get length => _text.length;
@override
Description add(String text) {
_text.write(text);
return this;
}
@override
Description replace(String text) {
_text.clear();
_text.write(text);
return this;
}
@override
Description addDescriptionOf(dynamic value) {
if (value is Matcher) {
value.describe(this);
return this;
} else {
return add('$value');
}
}
@override
Description addAll(String start, String separator, String end, Iterable<dynamic> list) {
add(start);
if (list.isNotEmpty) {
addDescriptionOf(list.first);
for (dynamic item in list.skip(1)) {
add(separator);
addDescriptionOf(item);
}
}
add(end);
return this;
}
@override
String toString() => '$_text';
}