utils_test.dart 11.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter_tools/src/base/terminal.dart';
6
import 'package:flutter_tools/src/base/utils.dart';
7
import 'package:flutter_tools/src/base/version.dart';
8

9
import '../src/common.dart';
10 11 12

void main() {
  group('SettingsFile', () {
13
    testWithoutContext('parse', () {
14
      final SettingsFile file = SettingsFile.parse('''
15 16 17 18 19 20 21 22 23
# ignore comment
foo=bar
baz=qux
''');
      expect(file.values['foo'], 'bar');
      expect(file.values['baz'], 'qux');
      expect(file.values, hasLength(2));
    });
  });
24

25
  group('Version', () {
26
    testWithoutContext('can parse and compare', () {
27
      expect(Version.unknown.toString(), equals('unknown'));
28
      expect(Version(null, null, null).toString(), equals('0'));
29
      expect(const Version.withText(1, 2, 3, 'versionText').toString(), 'versionText');
30

31
      final Version v1 = Version.parse('1')!;
32 33 34 35 36 37
      expect(v1.major, equals(1));
      expect(v1.minor, equals(0));
      expect(v1.patch, equals(0));

      expect(v1, greaterThan(Version.unknown));

38
      final Version v2 = Version.parse('1.2')!;
39 40 41 42
      expect(v2.major, equals(1));
      expect(v2.minor, equals(2));
      expect(v2.patch, equals(0));

43
      final Version v3 = Version.parse('1.2.3')!;
44 45 46 47
      expect(v3.major, equals(1));
      expect(v3.minor, equals(2));
      expect(v3.patch, equals(3));

48
      final Version v4 = Version.parse('1.12')!;
49 50 51 52 53
      expect(v4, greaterThan(v2));

      expect(v3, greaterThan(v2));
      expect(v2, greaterThan(v1));

54
      final Version v5 = Version(1, 2, 0, text: 'foo');
55
      expect(v5, equals(v2));
56

57
      expect(Version.parse('Preview2.2'), isNull);
58 59
    });
  });
60

61
  group('Misc', () {
62
    testWithoutContext('snakeCase', () async {
63 64 65 66 67 68 69 70 71 72
      expect(snakeCase('abc'), equals('abc'));
      expect(snakeCase('abC'), equals('ab_c'));
      expect(snakeCase('aBc'), equals('a_bc'));
      expect(snakeCase('aBC'), equals('a_b_c'));
      expect(snakeCase('Abc'), equals('abc'));
      expect(snakeCase('AbC'), equals('ab_c'));
      expect(snakeCase('ABc'), equals('a_bc'));
      expect(snakeCase('ABC'), equals('a_b_c'));
    });
  });
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

  group('text wrapping', () {
    const int _lineLength = 40;
    const String _longLine = 'This is a long line that needs to be wrapped.';
    final String _longLineWithNewlines = 'This is a long line with newlines that\n'
        'needs to be wrapped.\n\n' +
        '0123456789' * 5;
    final String _longAnsiLineWithNewlines = '${AnsiTerminal.red}This${AnsiTerminal.resetAll} is a long line with newlines that\n'
        'needs to be wrapped.\n\n'
        '${AnsiTerminal.green}0123456789${AnsiTerminal.resetAll}' +
        '0123456789' * 3 +
        '${AnsiTerminal.green}0123456789${AnsiTerminal.resetAll}';
    const String _onlyAnsiSequences = '${AnsiTerminal.red}${AnsiTerminal.resetAll}';
    final String _indentedLongLineWithNewlines = '    This is an indented long line with newlines that\n'
        'needs to be wrapped.\n\tAnd preserves tabs.\n      \n  ' +
        '0123456789' * 5;
    const String _shortLine = 'Short line.';
    const String _indentedLongLine = '    This is an indented long line that needs to be '
        'wrapped and indentation preserved.';
92 93
    testWithoutContext('does not wrap by default in tests', () {
      expect(wrapText(_longLine, columnWidth: 80, shouldWrap: true), equals(_longLine));
94
    });
95 96

    testWithoutContext('can override wrap preference if preference is off', () {
97 98 99 100
      expect(wrapText(_longLine, columnWidth: _lineLength, shouldWrap: true), equals('''
This is a long line that needs to be
wrapped.'''));
    });
101 102 103

    testWithoutContext('can override wrap preference if preference is on', () {
      expect(wrapText(_longLine, shouldWrap: false, columnWidth: 80), equals(_longLine));
104
    });
105 106 107

    testWithoutContext('does not wrap at all if not told to wrap', () {
      expect(wrapText(_longLine, columnWidth: 80, shouldWrap: false), equals(_longLine));
108
    });
109 110 111

    testWithoutContext('does not wrap short lines.', () {
      expect(wrapText(_shortLine, columnWidth: _lineLength, shouldWrap: true), equals(_shortLine));
112
    });
113 114 115

    testWithoutContext('able to wrap long lines', () {
      expect(wrapText(_longLine, columnWidth: _lineLength, shouldWrap: true), equals('''
116 117 118
This is a long line that needs to be
wrapped.'''));
    });
119 120 121

    testWithoutContext('able to handle dynamically changing terminal column size', () {
      expect(wrapText(_longLine, columnWidth: 20, shouldWrap: true), equals('''
122 123 124
This is a long line
that needs to be
wrapped.'''));
125 126

      expect(wrapText(_longLine, columnWidth: _lineLength, shouldWrap: true), equals('''
127 128 129
This is a long line that needs to be
wrapped.'''));
    });
130 131 132

    testWithoutContext('wrap long lines with no whitespace', () {
      expect(wrapText('0123456789' * 5, columnWidth: _lineLength, shouldWrap: true), equals('''
133 134 135
0123456789012345678901234567890123456789
0123456789'''));
    });
136 137 138

    testWithoutContext('refuses to wrap to a column smaller than 10 characters', () {
      expect(wrapText('$_longLine ' + '0123456789' * 4, columnWidth: 1, shouldWrap: true), equals('''
139 140 141 142 143 144 145 146 147 148
This is a
long line
that needs
to be
wrapped.
0123456789
0123456789
0123456789
0123456789'''));
    });
149 150
    testWithoutContext('preserves indentation', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, shouldWrap: true), equals('''
151 152 153 154
    This is an indented long line that
    needs to be wrapped and indentation
    preserved.'''));
    });
155 156 157

    testWithoutContext('preserves indentation and stripping trailing whitespace', () {
      expect(wrapText('$_indentedLongLine   ', columnWidth: _lineLength, shouldWrap: true), equals('''
158 159 160 161
    This is an indented long line that
    needs to be wrapped and indentation
    preserved.'''));
    });
162 163 164

    testWithoutContext('wraps text with newlines', () {
      expect(wrapText(_longLineWithNewlines, columnWidth: _lineLength, shouldWrap: true), equals('''
165 166 167 168 169 170
This is a long line with newlines that
needs to be wrapped.

0123456789012345678901234567890123456789
0123456789'''));
    });
171 172 173

    testWithoutContext('wraps text with ANSI sequences embedded', () {
      expect(wrapText(_longAnsiLineWithNewlines, columnWidth: _lineLength, shouldWrap: true), equals('''
174 175 176 177 178 179
${AnsiTerminal.red}This${AnsiTerminal.resetAll} is a long line with newlines that
needs to be wrapped.

${AnsiTerminal.green}0123456789${AnsiTerminal.resetAll}012345678901234567890123456789
${AnsiTerminal.green}0123456789${AnsiTerminal.resetAll}'''));
    });
180 181 182

    testWithoutContext('wraps text with only ANSI sequences', () {
      expect(wrapText(_onlyAnsiSequences, columnWidth: _lineLength, shouldWrap: true),
183 184
          equals('${AnsiTerminal.red}${AnsiTerminal.resetAll}'));
    });
185 186 187

    testWithoutContext('preserves indentation in the presence of newlines', () {
      expect(wrapText(_indentedLongLineWithNewlines, columnWidth: _lineLength, shouldWrap: true), equals('''
188 189 190 191 192 193 194 195
    This is an indented long line with
    newlines that
needs to be wrapped.
\tAnd preserves tabs.

  01234567890123456789012345678901234567
  890123456789'''));
    });
196 197 198

    testWithoutContext('removes trailing whitespace when wrapping', () {
      expect(wrapText('$_longLine     \t', columnWidth: _lineLength, shouldWrap: true), equals('''
199 200 201
This is a long line that needs to be
wrapped.'''));
    });
202 203 204

    testWithoutContext('honors hangingIndent parameter', () {
      expect(wrapText(_longLine, columnWidth: _lineLength, hangingIndent: 6, shouldWrap: true), equals('''
205 206 207
This is a long line that needs to be
      wrapped.'''));
    });
208 209 210

    testWithoutContext('handles hangingIndent with a single unwrapped line.', () {
      expect(wrapText(_shortLine, columnWidth: _lineLength, hangingIndent: 6, shouldWrap: true), equals('''
211 212
Short line.'''));
    });
213 214 215

    testWithoutContext('handles hangingIndent with two unwrapped lines and the second is empty.', () {
      expect(wrapText('$_shortLine\n', columnWidth: _lineLength, hangingIndent: 6, shouldWrap: true), equals('''
216 217 218
Short line.
'''));
    });
219 220 221

    testWithoutContext('honors hangingIndent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, hangingIndent: 6, shouldWrap: true), equals('''
222 223 224 225
    This is an indented long line that
          needs to be wrapped and
          indentation preserved.'''));
    });
226 227 228

    testWithoutContext('honors hangingIndent and indent parameters at the same time.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, indent: 6, hangingIndent: 6, shouldWrap: true), equals('''
229 230 231 232 233
          This is an indented long line
                that needs to be wrapped
                and indentation
                preserved.'''));
    });
234 235 236

    testWithoutContext('honors indent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, indent: 6, shouldWrap: true), equals('''
237 238 239 240
          This is an indented long line
          that needs to be wrapped and
          indentation preserved.'''));
    });
241 242 243

    testWithoutContext('honors hangingIndent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLineWithNewlines, columnWidth: _lineLength, hangingIndent: 6, shouldWrap: true), equals('''
244 245 246 247 248 249 250 251
    This is an indented long line with
          newlines that
needs to be wrapped.
	And preserves tabs.

  01234567890123456789012345678901234567
        890123456789'''));
    });
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

    testWithoutContext('', () {
      expect(wrapText(
        ' ' * 7 + 'abc def ghi', columnWidth: 20, hangingIndent: 5, indent: 3, shouldWrap: true),
        equals(
          '          abc def\n'
          '          ghi'
        ),
      );
      expect(wrapText(
        'abc def ghi', columnWidth: 0, hangingIndent: 5, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        'abc def ghi', columnWidth: 0, indent: 5, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        '     abc def ghi', columnWidth: 0, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        'abc def ghi', columnWidth: kMinColumnWidth - 2, hangingIndent: 5, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        'abc def ghi', columnWidth: kMinColumnWidth - 2, indent: 5, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        '     abc def ghi', columnWidth: kMinColumnWidth - 2, shouldWrap: true),
        equals(
          'abc def\n'
          'ghi'
        ),
      );
      expect(wrapText(
        'abc def ghi jkl', columnWidth: kMinColumnWidth + 2, hangingIndent: 5, shouldWrap: true),
        equals(
          'abc def ghi\n'
          '  jkl'
        ),
      );
      expect(wrapText(
        'abc def ghi', columnWidth: kMinColumnWidth + 2, indent: 5, shouldWrap: true),
        equals(
          '  abc def\n'
          '  ghi'
        ),
      );
      expect(wrapText(
        '     abc def ghi', columnWidth: kMinColumnWidth + 2, shouldWrap: true),
        equals(
          '  abc def\n'
          '  ghi'
        ),
      );
    });
325
  });
326
}