test_build_system.dart 2.14 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 23 24 25 26 27 28
// Copyright 2014 The Flutter 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 'dart:async';

import 'package:flutter_tools/src/build_system/build_system.dart';

class TestBuildSystem implements BuildSystem {
  /// Create a [BuildSystem] instance that returns the provided results in order.
  TestBuildSystem.list(this._results, [this._onRun])
    : _exception = null,
      _singleResult = null;

  /// Create a [BuildSystem] instance that returns the provided result for every build
  /// and buildIncremental request.
  TestBuildSystem.all(this._singleResult, [this._onRun])
    : _exception = null,
      _results = <BuildResult>[];

  /// Create a [BuildSystem] instance that always throws the provided error for every build
  /// and buildIncremental request.
  TestBuildSystem.error(this._exception)
    : _singleResult = null,
      _results = <BuildResult>[],
      _onRun = null;

  final List<BuildResult> _results;
29
  final BuildResult? _singleResult;
30
  final Exception? _exception;
31
  final void Function(Target target, Environment environment)? _onRun;
32 33 34 35 36
  int _nextResult = 0;

  @override
  Future<BuildResult> build(Target target, Environment environment, {BuildSystemConfig buildSystemConfig = const BuildSystemConfig()}) async {
    if (_onRun != null) {
37
      _onRun.call(target, environment);
38 39
    }
    if (_exception != null) {
40
      throw _exception;
41 42
    }
    if (_singleResult != null) {
43
      return _singleResult;
44 45 46 47 48 49 50 51
    }
    if (_nextResult >= _results.length) {
      throw StateError('Unexpected build request of ${target.name}');
    }
    return _results[_nextResult++];
  }

  @override
52
  Future<BuildResult> buildIncremental(Target target, Environment environment, BuildResult? previousBuild) async {
53
    if (_onRun != null) {
54
      _onRun.call(target, environment);
55 56
    }
    if (_exception != null) {
57
      throw _exception;
58 59
    }
    if (_singleResult != null) {
60
      return _singleResult;
61 62 63 64 65 66 67
    }
    if (_nextResult >= _results.length) {
      throw StateError('Unexpected buildIncremental request of ${target.name}');
    }
    return _results[_nextResult++];
  }
}