• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'dart:async';
6
7import 'package:flutter_tools/src/base/flags.dart';
8import 'package:flutter_tools/src/cache.dart';
9import 'package:flutter_tools/src/runner/flutter_command.dart';
10
11import '../../src/common.dart';
12import '../../src/context.dart';
13
14typedef _TestMethod = FutureOr<void> Function();
15
16void main() {
17  Cache.disableLocking();
18
19  Future<void> runCommand(Iterable<String> flags, _TestMethod testMethod) async {
20    final List<String> args = <String>['test', ...flags];
21    final _TestCommand command = _TestCommand(testMethod);
22    await createTestCommandRunner(command).run(args);
23  }
24
25  testUsingContext('runCommand works as expected', () async {
26    bool testRan = false;
27    await runCommand(<String>[], () {
28      testRan = true;
29    });
30    expect(testRan, isTrue);
31  });
32
33  group('flags', () {
34    testUsingContext('returns null for undefined flags', () async {
35      await runCommand(<String>[], () {
36        expect(flags['undefined-flag'], isNull);
37      });
38    });
39
40    testUsingContext('picks up default values', () async {
41      await runCommand(<String>[], () {
42        expect(flags['verbose'], isFalse);
43        expect(flags['flag-defaults-to-false'], isFalse);
44        expect(flags['flag-defaults-to-true'], isTrue);
45        expect(flags['option-defaults-to-foo'], 'foo');
46      });
47    });
48
49    testUsingContext('returns null for flags with no default values', () async {
50      await runCommand(<String>[], () {
51        expect(flags['device-id'], isNull);
52        expect(flags['option-no-default'], isNull);
53      });
54    });
55
56    testUsingContext('picks up explicit values', () async {
57      await runCommand(<String>[
58        '--verbose',
59        '--flag-defaults-to-false',
60        '--option-no-default=explicit',
61        '--option-defaults-to-foo=qux',
62      ], () {
63        expect(flags['verbose'], isTrue);
64        expect(flags['flag-defaults-to-false'], isTrue);
65        expect(flags['option-no-default'], 'explicit');
66        expect(flags['option-defaults-to-foo'], 'qux');
67      });
68    });
69  });
70}
71
72class _TestCommand extends FlutterCommand {
73  _TestCommand(this.testMethod) {
74    argParser.addFlag('flag-defaults-to-false', defaultsTo: false);
75    argParser.addFlag('flag-defaults-to-true', defaultsTo: true);
76    argParser.addOption('option-no-default');
77    argParser.addOption('option-defaults-to-foo', defaultsTo: 'foo');
78  }
79
80  final _TestMethod testMethod;
81
82  @override
83  String get name => 'test';
84
85  @override
86  String get description => 'runs a test method';
87
88  @override
89  Future<FlutterCommandResult> runCommand() async {
90    await testMethod();
91    return null;
92  }
93}
94