Unverified Commit 7298f9ca authored by Mikkel Nygaard Ravn's avatar Mikkel Nygaard Ravn Committed by GitHub

Expose isMethodCall matcher in flutter_test (#12942)

parent aefc62f8
......@@ -3,9 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:test/test.dart';
import 'message_codecs_utils.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('Haptic feedback control test', () async {
......
// Copyright 2017 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/services.dart' show MethodCall;
import 'package:meta/meta.dart';
import 'package:test/test.dart';
class _IsMethodCall extends Matcher {
const _IsMethodCall(this.name, this.arguments);
final String name;
final dynamic arguments;
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
if (item is! MethodCall)
return false;
if (item.method != name)
return false;
return _deepEquals(item.arguments, arguments);
}
bool _deepEquals(dynamic a, dynamic b) {
if (a == b)
return true;
if (a is List)
return b is List && _deepEqualsList(a, b);
if (a is Map)
return b is Map && _deepEqualsMap(a, b);
return false;
}
bool _deepEqualsList(List<dynamic> a, List<dynamic> b) {
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; i++) {
if (!_deepEquals(a[i], b[i]))
return false;
}
return true;
}
bool _deepEqualsMap(Map<dynamic, dynamic> a, Map<dynamic, dynamic> b) {
if (a.length != b.length)
return false;
for (dynamic key in a.keys) {
if (!b.containsKey(key) || !_deepEquals(a[key], b[key]))
return false;
}
return true;
}
@override
Description describe(Description description) {
return description
.add('has method name: ').addDescriptionOf(name)
.add(' with arguments: ').addDescriptionOf(arguments);
}
}
/// Returns a matcher that matches [MethodCall] instances with the specified
/// method name and arguments.
///
/// Arguments checking implements deep equality for [List] and [Map] types.
Matcher isMethodCall(String name, {@required dynamic arguments}) {
return new _IsMethodCall(name, arguments);
}
......@@ -7,8 +7,6 @@ import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'message_codecs_utils.dart';
void main() {
testWidgets('SystemChrome overlay style test', (WidgetTester tester) async {
// The first call is a cache miss and will queue a microtask
......
......@@ -3,9 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:test/test.dart';
import 'message_codecs_utils.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('System navigator control test', () async {
......
......@@ -3,9 +3,7 @@
// found in the LICENSE file.
import 'package:flutter/services.dart';
import 'package:test/test.dart';
import 'message_codecs_utils.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
test('System sound control test', () async {
......
......@@ -10,7 +10,6 @@ import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/services.dart';
import '../services/message_codecs_utils.dart';
import 'semantics_tester.dart';
void main() {
......
......@@ -6,8 +6,6 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/services.dart';
import '../services/message_codecs_utils.dart';
void main() {
testWidgets('toString control test', (WidgetTester tester) async {
final Widget widget = new Title(
......
......@@ -5,6 +5,7 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:meta/meta.dart';
import 'package:test/test.dart';
......@@ -216,6 +217,14 @@ Matcher equalsIgnoringHashCodes(String value) {
return new _EqualsIgnoringHashCodes(value);
}
/// A matcher for [MethodCall]s, asserting that it has the specified
/// method [name] and [arguments].
///
/// Arguments checking implements deep equality for [List] and [Map] types.
Matcher isMethodCall(String name, {@required dynamic arguments}) {
return new _IsMethodCall(name, arguments);
}
class _FindsWidgetMatcher extends Matcher {
const _FindsWidgetMatcher(this.min, this.max);
......@@ -686,3 +695,57 @@ class _MoreOrLessEquals extends Matcher {
@override
Description describe(Description description) => description.add('$value$epsilon)');
}
class _IsMethodCall extends Matcher {
const _IsMethodCall(this.name, this.arguments);
final String name;
final dynamic arguments;
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
if (item is! MethodCall)
return false;
if (item.method != name)
return false;
return _deepEquals(item.arguments, arguments);
}
bool _deepEquals(dynamic a, dynamic b) {
if (a == b)
return true;
if (a is List)
return b is List && _deepEqualsList(a, b);
if (a is Map)
return b is Map && _deepEqualsMap(a, b);
return false;
}
bool _deepEqualsList(List<dynamic> a, List<dynamic> b) {
if (a.length != b.length)
return false;
for (int i = 0; i < a.length; i++) {
if (!_deepEquals(a[i], b[i]))
return false;
}
return true;
}
bool _deepEqualsMap(Map<dynamic, dynamic> a, Map<dynamic, dynamic> b) {
if (a.length != b.length)
return false;
for (dynamic key in a.keys) {
if (!b.containsKey(key) || !_deepEquals(a[key], b[key]))
return false;
}
return true;
}
@override
Description describe(Description description) {
return description
.add('has method name: ').addDescriptionOf(name)
.add(' with arguments: ').addDescriptionOf(arguments);
}
}
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