58
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dartのmacher一覧

Last updated at Posted at 2019-09-08

Dartのmatcherを一覧にしてみました。

Core

matcher
isEmpty item.isEmpty
isNotEmpty item.isNotEmpty
isNull item == null
isNotNull item != null
isTrue item == true
isFalse item == false
isNaN double.nan.compareTo(item) == 0
isNotNaN double.nan.compareTo(item) != 0
same(expected) identical(item, _expected)
anything true
returnsNormally 例外が発生ぜずに関数としてコールできればpass
isMap item is Map
isList item is List
hasLength(matcher) item.length >= 0 && matcher
contains(expected) itemの中にexpectedが含まれていればpass
isIn(expected) itemがexpectedの中に含まれていればpass(containsの逆)
predicate<T>(bool f(T value)) boolを返す任意の関数を指定できる

Operator

matcher
equals(expected) item == expected
isNot(matcher) !matcher
allOf(arg0, [arg1, arg2, arg3, arg4, arg5, arg6]) 全てにmatchすればpass
anyOf(arg0, [arg1, arg2, arg3, arg4, arg5, arg6]) いずれかにmatchすればpass
greaterThan(value) item > value
greaterThanOrEqualTo(value) item >= value
lessThan(value) item < value
lessThanOrEqualTo(value) item <= value

Type

matcher
isA<T> item is T

Numeric

matcher
closeTo(value, delta) abs(item - value) <= delta
inInclusiveRange(low, high) low <= item && item <= high
inExclusiveRange(low, high) low < item && item < high
inOpenClosedRange(low, high) low < item && item <= high
inClosedOpenRange(low, high) low <= item && item < high
isZero item == 0
isNonZero item != 0
isPositive item > 0
isNonPositive item > 0 == false
isNegative item < 0
isNonNegative item < 0 == false

String

matcher
equalsIgnoringCase(value) 大文字、小文字を無視して比較
equalsIgnoringWhitespace(value) 空白文字を無視して比較
startsWith(prefixString) 前方一致で比較
endsWith(suffixString) 後方一致で比較
stringContainsInOrder(substrings) 各文字列が順序通りに含まれていればpass
matches(re) 正規表現にマッチしていればpass

Iterable

matcher
everyElement(matcher) 全ての要素がmatcherとマッチすればpass
anyElement(matcher) どれかの要素がmatcherとマッチすればpass
orderedEquals(expected) 要素数と要素の順番が一致していればpass
unorderedEquals(expected) 順番が異なっていても要素数と各要素が一致していればpass
unorderedMatches(expected) unorderedEqualsと同様だが、expectedにmatcherを指定する
pairwiseCompare<S, T>(expected, bool comparator(S a, T b) 任意の比較関数を指定できる
containsAll(expected) 全ての値が含まれていればpass
containsAllInOrder(expected) 全ての値が順番通り含まれていればpass

Map

matcher
containsValue(value) item.containsValue(value)
containsPair(key, value) item.containsKey(key) && item[key]がvalueのmatcherをpass

Feature

matcher
completes 終了すればpass
completion(matcher) 終了してmatcherにマッチする値を返せばpass
doesNotComplete 終了しなければpass

Stream

matcher
emitsDone 次にdoneイベントが発行されればpass
emits(matcher) 次にmatcherに一致する値が発行されればpass
emitsError(matcher) 次にmatcherに一致するエラーが発行されればpass
mayEmit(matcher) 常にpass。matcherに一致した値が発行されたら終了する
emitsAnyOf(matchers) matchersのどれかにマッチする値が発行されればpass
emitsInOrder(matchers) matchersにマッチする値が順番通りに発行されればpass
emitsThrough(matcher) matcherに一致する値が一回以上発行されればpass
mayEmitMultiple(matcher) 常にpass。mayEmitと異なり、複数回マッチする
neverEmits(matcher) matcherに一致する値が発行されなければpass
emitsInAnyOrder(matchers) matchersにマッチする値が発行されればpass

Throws

matcher
throwsA(matcher) 関数やFeatureが例外をthrowすればpass
throwsArgumentError throw ArgumentError
throwsConcurrentModificationError throw ConcurrentModificationError
throwsCyclicInitializationError throw CyclicInitializationError
throwsException throw Exception
throwsFormatException throw FormatException
throwsNoSuchMethodError throw NoSuchMethodError
throwsNullThrownError throw NullThrownError
throwsRangeError throw RangeError
throwsStateError throw StateError
throwsUnimplementedError throw UnimplementedError
throwsUnsupportedError throw UnsupportedError

Error

matcher
isArgumentError item is ArgumentError
isCastError item is CastError
isConcurrentModificationError item is ConcurrentModificationError
isCyclicInitializationError item is CyclicInitializationError
isException item is Exception
isFormatException item is FormatException
isNoSuchMethodError item is NoSuchMethodError
isNullThrownError item is NullThrownError
isRangeError item is RangeError
isStateError item is StateError
isUnimplementedError item is UnimplementedError
isUnsupportedError item is UnsupportedError

参考リンク

dart-lang/test: A library for writing unit tests in Dart.
dart-lang/matcher: A declarative API for specifying expectations.
matcher library - Dart API

58
32
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
58
32

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?