command_help_test.dart 15 KB
Newer Older
1 2 3 4 5
// 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 'package:flutter_tools/src/base/command_help.dart';
6
import 'package:flutter_tools/src/base/logger.dart';
7
import 'package:flutter_tools/src/base/platform.dart';
8
import 'package:flutter_tools/src/base/terminal.dart' show AnsiTerminal, OutputPreferences;
9 10

import '../../src/common.dart';
11
import '../../src/fakes.dart';
12 13

CommandHelp _createCommandHelp({
14 15
  required bool ansi,
  required int wrapColumn,
16
}) {
17 18 19
  final Platform platform = FakePlatform(
    stdoutSupportsAnsi: ansi,
  );
20
  return CommandHelp(
21
    logger: BufferLogger.test(),
22
    terminal: AnsiTerminal(
23
      stdio:  FakeStdio(),
24
      platform: platform,
25
    ),
26
    platform: platform,
27 28 29 30 31 32
    outputPreferences: OutputPreferences.test(
      showColor: ansi,
      wrapColumn: wrapColumn,
    ),
  );
}
33 34

// Used to use the message length in different scenarios in a DRY way
35
void _testMessageLength({
36 37 38
  required bool stdoutSupportsAnsi,
  required int maxTestLineLength,
  required int wrapColumn,
39 40 41 42 43
}) {
  final CommandHelp commandHelp = _createCommandHelp(
    ansi: stdoutSupportsAnsi,
    wrapColumn: wrapColumn,
  );
44 45 46 47 48 49 50 51

  int expectedWidth = maxTestLineLength;

  if (stdoutSupportsAnsi) {
    const int ansiMetaCharactersLength = 33;
    expectedWidth += ansiMetaCharactersLength;
  }

52
  expect(commandHelp.I.toString().length, lessThanOrEqualTo(expectedWidth));
53
  expect(commandHelp.L.toString().length, lessThanOrEqualTo(expectedWidth));
54
  expect(commandHelp.M.toString().length, lessThanOrEqualTo(expectedWidth));
55 56 57 58 59
  expect(commandHelp.P.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.R.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.S.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.U.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.a.toString().length, lessThanOrEqualTo(expectedWidth));
60 61
  expect(commandHelp.b.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.c.toString().length, lessThanOrEqualTo(expectedWidth));
62
  expect(commandHelp.d.toString().length, lessThanOrEqualTo(expectedWidth));
63
  expect(commandHelp.f.toString().length, lessThanOrEqualTo(expectedWidth));
64
  expect(commandHelp.g.toString().length, lessThanOrEqualTo(expectedWidth));
65 66
  expect(commandHelp.hWithDetails.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.hWithoutDetails.toString().length, lessThanOrEqualTo(expectedWidth));
67
  expect(commandHelp.i.toString().length, lessThanOrEqualTo(expectedWidth));
68
  expect(commandHelp.k.toString().length, lessThanOrEqualTo(expectedWidth));
69 70 71 72 73 74 75 76
  expect(commandHelp.o.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.p.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.q.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.r.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.s.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.t.toString().length, lessThanOrEqualTo(expectedWidth));
  expect(commandHelp.w.toString().length, lessThanOrEqualTo(expectedWidth));
}
77 78 79 80

void main() {
  group('CommandHelp', () {
    group('toString', () {
81 82
      testWithoutContext('ends with a resetBold when it has parenthetical text', () {
        final Platform platform = FakePlatform(stdoutSupportsAnsi: true);
83
        final AnsiTerminal terminal = AnsiTerminal(stdio: FakeStdio(), platform: platform);
84 85 86 87 88 89 90 91 92 93 94 95

        final CommandHelpOption commandHelpOption = CommandHelpOption(
          'tester',
          'for testing',
          platform: platform,
          outputPreferences: OutputPreferences.test(showColor: true),
          terminal: terminal,
          logger: BufferLogger.test(),
          inParenthesis: 'Parenthetical',
        );
        expect(commandHelpOption.toString(), endsWith(AnsiTerminal.resetBold));
      });
96

97 98 99 100 101 102
      testWithoutContext('should have a bold command key', () {
        final CommandHelp commandHelp = _createCommandHelp(
          ansi: true,
          wrapColumn: maxLineWidth,
        );

103
        expect(commandHelp.I.toString(), startsWith('\x1B[1mI\x1B[22m'));
104
        expect(commandHelp.L.toString(), startsWith('\x1B[1mL\x1B[22m'));
105
        expect(commandHelp.M.toString(), startsWith('\x1B[1mM\x1B[22m'));
106 107 108 109 110
        expect(commandHelp.P.toString(), startsWith('\x1B[1mP\x1B[22m'));
        expect(commandHelp.R.toString(), startsWith('\x1B[1mR\x1B[22m'));
        expect(commandHelp.S.toString(), startsWith('\x1B[1mS\x1B[22m'));
        expect(commandHelp.U.toString(), startsWith('\x1B[1mU\x1B[22m'));
        expect(commandHelp.a.toString(), startsWith('\x1B[1ma\x1B[22m'));
111 112
        expect(commandHelp.b.toString(), startsWith('\x1B[1mb\x1B[22m'));
        expect(commandHelp.c.toString(), startsWith('\x1B[1mc\x1B[22m'));
113
        expect(commandHelp.d.toString(), startsWith('\x1B[1md\x1B[22m'));
114
        expect(commandHelp.g.toString(), startsWith('\x1B[1mg\x1B[22m'));
115 116
        expect(commandHelp.hWithDetails.toString(), startsWith('\x1B[1mh\x1B[22m'));
        expect(commandHelp.hWithoutDetails.toString(), startsWith('\x1B[1mh\x1B[22m'));
117
        expect(commandHelp.i.toString(), startsWith('\x1B[1mi\x1B[22m'));
118
        expect(commandHelp.k.toString(), startsWith('\x1B[1mk\x1B[22m'));
119 120 121 122 123 124 125
        expect(commandHelp.o.toString(), startsWith('\x1B[1mo\x1B[22m'));
        expect(commandHelp.p.toString(), startsWith('\x1B[1mp\x1B[22m'));
        expect(commandHelp.q.toString(), startsWith('\x1B[1mq\x1B[22m'));
        expect(commandHelp.r.toString(), startsWith('\x1B[1mr\x1B[22m'));
        expect(commandHelp.s.toString(), startsWith('\x1B[1ms\x1B[22m'));
        expect(commandHelp.t.toString(), startsWith('\x1B[1mt\x1B[22m'));
        expect(commandHelp.w.toString(), startsWith('\x1B[1mw\x1B[22m'));
126 127
      });

128
      testWithoutContext('commands that should have a grey bolden parenthetical text', () {
129 130 131 132 133
        final CommandHelp commandHelp = _createCommandHelp(
          ansi: true,
          wrapColumn: maxLineWidth,
        );

134 135 136 137 138 139 140
        expect(commandHelp.I.toString(), endsWith('\x1B[90m(debugInvertOversizedImages)\x1B[39m\x1B[22m'));
        expect(commandHelp.L.toString(), endsWith('\x1B[90m(debugDumpLayerTree)\x1B[39m\x1B[22m'));
        expect(commandHelp.P.toString(), endsWith('\x1B[90m(WidgetsApp.showPerformanceOverlay)\x1B[39m\x1B[22m'));
        expect(commandHelp.S.toString(), endsWith('\x1B[90m(debugDumpSemantics)\x1B[39m\x1B[22m'));
        expect(commandHelp.U.toString(), endsWith('\x1B[90m(debugDumpSemantics)\x1B[39m\x1B[22m'));
        expect(commandHelp.a.toString(), endsWith('\x1B[90m(debugProfileWidgetBuilds)\x1B[39m\x1B[22m'));
        expect(commandHelp.b.toString(), endsWith('\x1B[90m(debugBrightnessOverride)\x1B[39m\x1B[22m'));
141
        expect(commandHelp.f.toString(), endsWith('\x1B[90m(debugDumpFocusTree)\x1B[39m\x1B[22m'));
142 143 144 145 146
        expect(commandHelp.i.toString(), endsWith('\x1B[90m(WidgetsApp.showWidgetInspectorOverride)\x1B[39m\x1B[22m'));
        expect(commandHelp.o.toString(), endsWith('\x1B[90m(defaultTargetPlatform)\x1B[39m\x1B[22m'));
        expect(commandHelp.p.toString(), endsWith('\x1B[90m(debugPaintSizeEnabled)\x1B[39m\x1B[22m'));
        expect(commandHelp.t.toString(), endsWith('\x1B[90m(debugDumpRenderTree)\x1B[39m\x1B[22m'));
        expect(commandHelp.w.toString(), endsWith('\x1B[90m(debugDumpApp)\x1B[39m\x1B[22m'));
147 148
      });

149 150 151 152 153 154
      testWithoutContext('should not create a help text longer than maxLineWidth without ansi support', () {
        _testMessageLength(
          stdoutSupportsAnsi: false,
          wrapColumn: 0,
          maxTestLineLength: maxLineWidth,
        );
155 156
      });

157 158 159 160 161 162
      testWithoutContext('should not create a help text longer than maxLineWidth with ansi support', () {
        _testMessageLength(
          stdoutSupportsAnsi: true,
          wrapColumn: 0,
          maxTestLineLength: maxLineWidth,
        );
163 164
      });

165 166 167 168 169 170
      testWithoutContext('should not create a help text longer than outputPreferences.wrapColumn without ansi support', () {
        _testMessageLength(
          stdoutSupportsAnsi: false,
          wrapColumn: OutputPreferences.kDefaultTerminalColumns,
          maxTestLineLength: OutputPreferences.kDefaultTerminalColumns,
        );
171 172
      });

173 174 175 176 177 178
      testWithoutContext('should not create a help text longer than outputPreferences.wrapColumn with ansi support', () {
        _testMessageLength(
          stdoutSupportsAnsi: true,
          wrapColumn: OutputPreferences.kDefaultTerminalColumns,
          maxTestLineLength: OutputPreferences.kDefaultTerminalColumns,
        );
179 180
      });

181 182 183 184 185 186
      testWithoutContext('should create the correct help text with ansi support', () {
        final CommandHelp commandHelp = _createCommandHelp(
          ansi: true,
          wrapColumn: maxLineWidth,
        );

187 188 189 190
        expect(commandHelp.I.toString(), equals('\x1B[1mI\x1B[22m Toggle oversized image inversion.                     \x1B[90m(debugInvertOversizedImages)\x1B[39m\x1B[22m'));
        expect(commandHelp.L.toString(), equals('\x1B[1mL\x1B[22m Dump layer tree to the console.                               \x1B[90m(debugDumpLayerTree)\x1B[39m\x1B[22m'));
        expect(commandHelp.M.toString(), equals('\x1B[1mM\x1B[22m Write SkSL shaders to a unique file in the project directory.'));
        expect(commandHelp.P.toString(), equals('\x1B[1mP\x1B[22m Toggle performance overlay.                    \x1B[90m(WidgetsApp.showPerformanceOverlay)\x1B[39m\x1B[22m'));
191
        expect(commandHelp.R.toString(), equals('\x1B[1mR\x1B[22m Hot restart.'));
192 193 194 195 196
        expect(commandHelp.S.toString(), equals('\x1B[1mS\x1B[22m Dump accessibility tree in traversal order.                   \x1B[90m(debugDumpSemantics)\x1B[39m\x1B[22m'));
        expect(commandHelp.U.toString(), equals('\x1B[1mU\x1B[22m Dump accessibility tree in inverse hit test order.            \x1B[90m(debugDumpSemantics)\x1B[39m\x1B[22m'));
        expect(commandHelp.a.toString(), equals('\x1B[1ma\x1B[22m Toggle timeline events for all widget build methods.    \x1B[90m(debugProfileWidgetBuilds)\x1B[39m\x1B[22m'));
        expect(commandHelp.b.toString(), equals('\x1B[1mb\x1B[22m Toggle platform brightness (dark and light mode).        \x1B[90m(debugBrightnessOverride)\x1B[39m\x1B[22m'));
        expect(commandHelp.c.toString(), equals('\x1B[1mc\x1B[22m Clear the screen'));
197
        expect(commandHelp.d.toString(), equals('\x1B[1md\x1B[22m Detach (terminate "flutter run" but leave application running).'));
198
        expect(commandHelp.f.toString(), equals('\x1B[1mf\x1B[22m Dump focus tree to the console.                               \x1B[90m(debugDumpFocusTree)\x1B[39m\x1B[22m'));
199
        expect(commandHelp.g.toString(), equals('\x1B[1mg\x1B[22m Run source code generators.'));
200 201 202 203 204
        expect(commandHelp.hWithDetails.toString(), equals('\x1B[1mh\x1B[22m Repeat this help message.'));
        expect(commandHelp.hWithoutDetails.toString(), equals('\x1B[1mh\x1B[22m List all available interactive commands.'));
        expect(commandHelp.i.toString(), equals('\x1B[1mi\x1B[22m Toggle widget inspector.                  \x1B[90m(WidgetsApp.showWidgetInspectorOverride)\x1B[39m\x1B[22m'));
        expect(commandHelp.o.toString(), equals('\x1B[1mo\x1B[22m Simulate different operating systems.                      \x1B[90m(defaultTargetPlatform)\x1B[39m\x1B[22m'));
        expect(commandHelp.p.toString(), equals('\x1B[1mp\x1B[22m Toggle the display of construction lines.                  \x1B[90m(debugPaintSizeEnabled)\x1B[39m\x1B[22m'));
205 206 207
        expect(commandHelp.q.toString(), equals('\x1B[1mq\x1B[22m Quit (terminate the application on the device).'));
        expect(commandHelp.r.toString(), equals('\x1B[1mr\x1B[22m Hot reload. $fire$fire$fire'));
        expect(commandHelp.s.toString(), equals('\x1B[1ms\x1B[22m Save a screenshot to flutter.png.'));
208
        expect(commandHelp.t.toString(), equals('\x1B[1mt\x1B[22m Dump rendering tree to the console.                          \x1B[90m(debugDumpRenderTree)\x1B[39m\x1B[22m'));
209
        expect(commandHelp.v.toString(), equals('\x1B[1mv\x1B[22m Open Flutter DevTools.'));
210
        expect(commandHelp.w.toString(), equals('\x1B[1mw\x1B[22m Dump widget hierarchy to the console.                               \x1B[90m(debugDumpApp)\x1B[39m\x1B[22m'));
211 212
      });

213 214 215 216 217 218
      testWithoutContext('should create the correct help text without ansi support', () {
        final CommandHelp commandHelp = _createCommandHelp(
          ansi: false,
          wrapColumn: maxLineWidth,
        );

219 220
        expect(commandHelp.I.toString(), equals('I Toggle oversized image inversion.                     (debugInvertOversizedImages)'));
        expect(commandHelp.M.toString(), equals('M Write SkSL shaders to a unique file in the project directory.'));
221 222 223 224 225 226
        expect(commandHelp.L.toString(), equals('L Dump layer tree to the console.                               (debugDumpLayerTree)'));
        expect(commandHelp.P.toString(), equals('P Toggle performance overlay.                    (WidgetsApp.showPerformanceOverlay)'));
        expect(commandHelp.R.toString(), equals('R Hot restart.'));
        expect(commandHelp.S.toString(), equals('S Dump accessibility tree in traversal order.                   (debugDumpSemantics)'));
        expect(commandHelp.U.toString(), equals('U Dump accessibility tree in inverse hit test order.            (debugDumpSemantics)'));
        expect(commandHelp.a.toString(), equals('a Toggle timeline events for all widget build methods.    (debugProfileWidgetBuilds)'));
227 228
        expect(commandHelp.b.toString(), equals('b Toggle platform brightness (dark and light mode).        (debugBrightnessOverride)'));
        expect(commandHelp.c.toString(), equals('c Clear the screen'));
229
        expect(commandHelp.d.toString(), equals('d Detach (terminate "flutter run" but leave application running).'));
230
        expect(commandHelp.g.toString(), equals('g Run source code generators.'));
231 232
        expect(commandHelp.hWithDetails.toString(), equals('h Repeat this help message.'));
        expect(commandHelp.hWithoutDetails.toString(), equals('h List all available interactive commands.'));
233 234 235 236 237 238 239
        expect(commandHelp.i.toString(), equals('i Toggle widget inspector.                  (WidgetsApp.showWidgetInspectorOverride)'));
        expect(commandHelp.o.toString(), equals('o Simulate different operating systems.                      (defaultTargetPlatform)'));
        expect(commandHelp.p.toString(), equals('p Toggle the display of construction lines.                  (debugPaintSizeEnabled)'));
        expect(commandHelp.q.toString(), equals('q Quit (terminate the application on the device).'));
        expect(commandHelp.r.toString(), equals('r Hot reload. $fire$fire$fire'));
        expect(commandHelp.s.toString(), equals('s Save a screenshot to flutter.png.'));
        expect(commandHelp.t.toString(), equals('t Dump rendering tree to the console.                          (debugDumpRenderTree)'));
240
        expect(commandHelp.v.toString(), equals('v Open Flutter DevTools.'));
241 242
        expect(commandHelp.w.toString(), equals('w Dump widget hierarchy to the console.                               (debugDumpApp)'));
      });
243 244 245
    });
  });
}