terminal_test.dart 2 KB
Newer Older
1 2 3 4 5 6 7 8
// 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 'dart:async';

import 'package:flutter_tools/src/base/terminal.dart';

9
import '../src/common.dart';
10
import '../src/context.dart';
11 12 13 14 15 16 17 18 19 20 21 22

void main() {
  group('character input prompt', () {
    AnsiTerminal terminalUnderTest;

    setUp(() {
      terminalUnderTest = new TestTerminal();
    });

    testUsingContext('character prompt', () async {
      mockStdInStream = new Stream<String>.fromFutures(<Future<String>>[
        new Future<String>.value('d'), // Not in accepted list.
23
        new Future<String>.value('\n'), // Not in accepted list
24 25 26 27 28 29 30 31
        new Future<String>.value('b'),
      ]).asBroadcastStream();
      final String choice =
          await terminalUnderTest.promptForCharInput(
            <String>['a', 'b', 'c'],
            prompt: 'Please choose something',
          );
      expect(choice, 'b');
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
      expect(
        testLogger.statusText,
        'Please choose something [a|b|c]: d\n'
        'Please choose something [a|b|c]: \n'
        '\n'
        'Please choose something [a|b|c]: b\n'
      );
    });

    testUsingContext('default character choice without displayAcceptedCharacters', () async {
      mockStdInStream = new Stream<String>.fromFutures(<Future<String>>[
        new Future<String>.value('\n'), // Not in accepted list
      ]).asBroadcastStream();
      final String choice =
          await terminalUnderTest.promptForCharInput(
            <String>['a', 'b', 'c'],
            prompt: 'Please choose something',
            displayAcceptedCharacters: false,
            defaultChoiceIndex: 1, // which is b.
          );
      expect(choice, 'b');
      expect(
        testLogger.statusText,
        'Please choose something: \n'
        '\n'
      );
58 59 60 61 62 63 64 65 66 67 68 69
    });
  });
}

Stream<String> mockStdInStream;

class TestTerminal extends AnsiTerminal {
  @override
  Stream<String> get onCharInput {
    return mockStdInStream;
  }
}