utils_test.dart 12.7 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
import 'dart:async';

7
import 'package:flutter_tools/src/base/io.dart';
8
import 'package:flutter_tools/src/base/utils.dart';
9
import 'package:flutter_tools/src/base/version.dart';
10
import 'package:flutter_tools/src/base/terminal.dart';
11

12 13
import '../src/common.dart';
import '../src/context.dart';
14 15 16 17

void main() {
  group('SettingsFile', () {
    test('parse', () {
18
      final SettingsFile file = SettingsFile.parse('''
19 20 21 22 23 24 25 26 27
# ignore comment
foo=bar
baz=qux
''');
      expect(file.values['foo'], 'bar');
      expect(file.values['baz'], 'qux');
      expect(file.values, hasLength(2));
    });
  });
28 29 30 31

  group('uuid', () {
    // xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
    test('simple', () {
32
      final Uuid uuid = Uuid();
33
      final String result = uuid.generateV4();
34 35 36 37 38 39 40 41
      expect(result.length, 36);
      expect(result[8], '-');
      expect(result[13], '-');
      expect(result[18], '-');
      expect(result[23], '-');
    });

    test('can parse', () {
42
      final Uuid uuid = Uuid();
43
      final String result = uuid.generateV4();
44 45 46 47 48 49 50 51
      expect(int.parse(result.substring(0, 8), radix: 16), isNotNull);
      expect(int.parse(result.substring(9, 13), radix: 16), isNotNull);
      expect(int.parse(result.substring(14, 18), radix: 16), isNotNull);
      expect(int.parse(result.substring(19, 23), radix: 16), isNotNull);
      expect(int.parse(result.substring(24, 36), radix: 16), isNotNull);
    });

    test('special bits', () {
52
      final Uuid uuid = Uuid();
53 54 55 56 57 58 59 60 61 62 63 64
      String result = uuid.generateV4();
      expect(result[14], '4');
      expect(result[19].toLowerCase(), isIn('89ab'));

      result = uuid.generateV4();
      expect(result[19].toLowerCase(), isIn('89ab'));

      result = uuid.generateV4();
      expect(result[19].toLowerCase(), isIn('89ab'));
    });

    test('is pretty random', () {
65
      final Set<String> set = <String>{};
66

67
      Uuid uuid = Uuid();
68
      for (int i = 0; i < 64; i++) {
69
        final String val = uuid.generateV4();
70 71 72 73
        expect(set, isNot(contains(val)));
        set.add(val);
      }

74
      uuid = Uuid();
75
      for (int i = 0; i < 64; i++) {
76
        final String val = uuid.generateV4();
77 78 79 80
        expect(set, isNot(contains(val)));
        set.add(val);
      }

81
      uuid = Uuid();
82
      for (int i = 0; i < 64; i++) {
83
        final String val = uuid.generateV4();
84 85 86 87 88
        expect(set, isNot(contains(val)));
        set.add(val);
      }
    });
  });
89 90 91 92

  group('Version', () {
    test('can parse and compare', () {
      expect(Version.unknown.toString(), equals('unknown'));
93
      expect(Version(null, null, null).toString(), equals('0'));
94

95
      final Version v1 = Version.parse('1');
96 97 98 99 100 101
      expect(v1.major, equals(1));
      expect(v1.minor, equals(0));
      expect(v1.patch, equals(0));

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

102
      final Version v2 = Version.parse('1.2');
103 104 105 106
      expect(v2.major, equals(1));
      expect(v2.minor, equals(2));
      expect(v2.patch, equals(0));

107
      final Version v3 = Version.parse('1.2.3');
108 109 110 111
      expect(v3.major, equals(1));
      expect(v3.minor, equals(2));
      expect(v3.patch, equals(3));

112
      final Version v4 = Version.parse('1.12');
113 114 115 116 117
      expect(v4, greaterThan(v2));

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

118
      final Version v5 = Version(1, 2, 0, text: 'foo');
119
      expect(v5, equals(v2));
120

121
      expect(Version.parse('Preview2.2'), isNull);
122 123
    });
  });
124 125

  group('Poller', () {
126
    const Duration kShortDelay = Duration(milliseconds: 100);
127 128 129 130 131 132 133 134 135

    Poller poller;

    tearDown(() {
      poller?.cancel();
    });

    test('fires at start', () async {
      bool called = false;
136
      poller = Poller(() async {
137 138 139
        called = true;
      }, const Duration(seconds: 1));
      expect(called, false);
140
      await Future<void>.delayed(kShortDelay);
141 142 143 144 145 146
      expect(called, true);
    });

    test('runs periodically', () async {
      // Ensure we get the first (no-delay) callback, and one of the periodic callbacks.
      int callCount = 0;
147
      poller = Poller(() async {
148
        callCount++;
149
      }, Duration(milliseconds: kShortDelay.inMilliseconds ~/ 2));
150
      expect(callCount, 0);
151
      await Future<void>.delayed(kShortDelay);
152 153 154 155 156
      expect(callCount, greaterThanOrEqualTo(2));
    });

    test('no quicker then the periodic delay', () async {
      // Make sure that the poller polls at delay + the time it took to run the callback.
157
      final Completer<Duration> completer = Completer<Duration>();
158
      DateTime firstTime;
159
      poller = Poller(() async {
160
        if (firstTime == null) {
161
          firstTime = DateTime.now();
162
        } else {
163
          completer.complete(DateTime.now().difference(firstTime));
164
        }
165 166

        // introduce a delay
167
        await Future<void>.delayed(kShortDelay);
168 169
      }, kShortDelay);
      final Duration duration = await completer.future;
170 171
      expect(
          duration, greaterThanOrEqualTo(Duration(milliseconds: kShortDelay.inMilliseconds * 2)));
172 173
    });
  });
174 175 176 177 178 179 180 181 182 183 184 185 186

  group('Misc', () {
    test('snakeCase', () async {
      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'));
    });
  });
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

  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.';
206
    final FakeStdio fakeStdio = FakeStdio();
207

208 209
    void testWrap(String description, Function body) {
      testUsingContext(description, body, overrides: <Type, Generator>{
210 211 212
        OutputPreferences: () => OutputPreferences(wrapText: true, wrapColumn: _lineLength),
      });
    }
213 214 215

    void testNoWrap(String description, Function body) {
      testUsingContext(description, body, overrides: <Type, Generator>{
216 217 218 219 220 221 222
        OutputPreferences: () => OutputPreferences(wrapText: false),
      });
    }

    test('does not wrap by default in tests', () {
      expect(wrapText(_longLine), equals(_longLine));
    });
223 224 225 226 227 228 229 230
    testNoWrap('can override wrap preference if preference is off', () {
      expect(wrapText(_longLine, columnWidth: _lineLength, shouldWrap: true), equals('''
This is a long line that needs to be
wrapped.'''));
    });
    testWrap('can override wrap preference if preference is on', () {
      expect(wrapText(_longLine, shouldWrap: false), equals(_longLine));
    });
231 232 233 234 235 236 237 238 239 240 241
    testNoWrap('does not wrap at all if not told to wrap', () {
      expect(wrapText(_longLine), equals(_longLine));
    });
    testWrap('does not wrap short lines.', () {
      expect(wrapText(_shortLine, columnWidth: _lineLength), equals(_shortLine));
    });
    testWrap('able to wrap long lines', () {
      expect(wrapText(_longLine, columnWidth: _lineLength), equals('''
This is a long line that needs to be
wrapped.'''));
    });
242 243 244 245 246 247 248 249 250 251 252 253 254 255
    testUsingContext('able to handle dynamically changing terminal column size', () {
      fakeStdio.currentColumnSize = 20;
      expect(wrapText(_longLine), equals('''
This is a long line
that needs to be
wrapped.'''));
      fakeStdio.currentColumnSize = _lineLength;
      expect(wrapText(_longLine), equals('''
This is a long line that needs to be
wrapped.'''));
    }, overrides: <Type, Generator>{
      OutputPreferences: () => OutputPreferences(wrapText: true),
      Stdio: () => fakeStdio,
    });
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    testWrap('wrap long lines with no whitespace', () {
      expect(wrapText('0123456789' * 5, columnWidth: _lineLength), equals('''
0123456789012345678901234567890123456789
0123456789'''));
    });
    testWrap('refuses to wrap to a column smaller than 10 characters', () {
      expect(wrapText('$_longLine ' + '0123456789' * 4, columnWidth: 1), equals('''
This is a
long line
that needs
to be
wrapped.
0123456789
0123456789
0123456789
0123456789'''));
    });
    testWrap('preserves indentation', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength), equals('''
    This is an indented long line that
    needs to be wrapped and indentation
    preserved.'''));
    });
    testWrap('preserves indentation and stripping trailing whitespace', () {
      expect(wrapText('$_indentedLongLine   ', columnWidth: _lineLength), equals('''
    This is an indented long line that
    needs to be wrapped and indentation
    preserved.'''));
    });
    testWrap('wraps text with newlines', () {
      expect(wrapText(_longLineWithNewlines, columnWidth: _lineLength), equals('''
This is a long line with newlines that
needs to be wrapped.

0123456789012345678901234567890123456789
0123456789'''));
    });
    testWrap('wraps text with ANSI sequences embedded', () {
      expect(wrapText(_longAnsiLineWithNewlines, columnWidth: _lineLength), equals('''
${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}'''));
    });
    testWrap('wraps text with only ANSI sequences', () {
      expect(wrapText(_onlyAnsiSequences, columnWidth: _lineLength),
          equals('${AnsiTerminal.red}${AnsiTerminal.resetAll}'));
    });
    testWrap('preserves indentation in the presence of newlines', () {
      expect(wrapText(_indentedLongLineWithNewlines, columnWidth: _lineLength), equals('''
    This is an indented long line with
    newlines that
needs to be wrapped.
\tAnd preserves tabs.

  01234567890123456789012345678901234567
  890123456789'''));
    });
    testWrap('removes trailing whitespace when wrapping', () {
      expect(wrapText('$_longLine     \t', columnWidth: _lineLength), equals('''
This is a long line that needs to be
wrapped.'''));
    });
    testWrap('honors hangingIndent parameter', () {
      expect(wrapText(_longLine, columnWidth: _lineLength, hangingIndent: 6), equals('''
This is a long line that needs to be
      wrapped.'''));
    });
    testWrap('handles hangingIndent with a single unwrapped line.', () {
      expect(wrapText(_shortLine, columnWidth: _lineLength, hangingIndent: 6), equals('''
Short line.'''));
    });
    testWrap('handles hangingIndent with two unwrapped lines and the second is empty.', () {
      expect(wrapText('$_shortLine\n', columnWidth: _lineLength, hangingIndent: 6), equals('''
Short line.
'''));
    });
    testWrap('honors hangingIndent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, hangingIndent: 6), equals('''
    This is an indented long line that
          needs to be wrapped and
          indentation preserved.'''));
    });
    testWrap('honors hangingIndent and indent parameters at the same time.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, indent: 6, hangingIndent: 6), equals('''
          This is an indented long line
                that needs to be wrapped
                and indentation
                preserved.'''));
    });
    testWrap('honors indent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLine, columnWidth: _lineLength, indent: 6), equals('''
          This is an indented long line
          that needs to be wrapped and
          indentation preserved.'''));
    });
    testWrap('honors hangingIndent parameter on already indented line.', () {
      expect(wrapText(_indentedLongLineWithNewlines, columnWidth: _lineLength, hangingIndent: 6), equals('''
    This is an indented long line with
          newlines that
needs to be wrapped.
	And preserves tabs.

  01234567890123456789012345678901234567
        890123456789'''));
    });
  });
364
}
365 366 367 368 369 370 371 372 373

class FakeStdio extends Stdio {
  FakeStdio();

  int currentColumnSize = 20;

  @override
  int get terminalColumns => currentColumnSize;
}