test_matchers.dart 1.77 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Copyright 2015 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/material.dart';
import 'package:test/test.dart';

bool _hasAncestorOfType(Element element, Type targetType) {
  expect(element, isNotNull);
  bool result = false;
  element.visitAncestorElements((Element ancestor) {
    if (ancestor.widget.runtimeType == targetType) {
      result = true;
      return false;
    }
    return true;
  });
  return result;
}

class _IsOnStage extends Matcher {
  const _IsOnStage();
23 24

  @override
25
  bool matches(Element item, Map<dynamic, dynamic> matchState) => !_hasAncestorOfType(item, OffStage);
26 27

  @override
28 29 30 31 32
  Description describe(Description description) => description.add('onstage');
}

class _IsOffStage extends Matcher {
  const _IsOffStage();
33 34

  @override
35
  bool matches(Element item, Map<dynamic, dynamic> matchState) => _hasAncestorOfType(item, OffStage);
36 37

  @override
38 39 40 41 42
  Description describe(Description description) => description.add('offstage');
}

class _IsInCard extends Matcher {
  const _IsInCard();
43 44

  @override
45
  bool matches(Element item, Map<dynamic, dynamic> matchState) => _hasAncestorOfType(item, Card);
46 47

  @override
48 49 50 51 52
  Description describe(Description description) => description.add('in card');
}

class _IsNotInCard extends Matcher {
  const _IsNotInCard();
53 54

  @override
55
  bool matches(Element item, Map<dynamic, dynamic> matchState) => !_hasAncestorOfType(item, Card);
56 57

  @override
58 59 60 61 62 63 64
  Description describe(Description description) => description.add('not in card');
}

const Matcher isOnStage = const _IsOnStage();
const Matcher isOffStage = const _IsOffStage();
const Matcher isInCard = const _IsInCard();
const Matcher isNotInCard = const _IsNotInCard();