gen_l10n_test.dart 7.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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 'dart:async';

import 'package:file/file.dart';

import '../src/common.dart';
import 'test_data/gen_l10n_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';

14 15
final GenL10nProject project = GenL10nProject();

16
// Verify that the code generated by gen_l10n executes correctly.
17 18
// It can fail if gen_l10n produces a lib/l10n/app_localizations.dart that
// does not analyze cleanly.
19
void main() {
20 21
  late Directory tempDir;
  late FlutterRunTestDriver flutter;
22 23 24 25 26 27

  setUp(() async {
    tempDir = createResolvedTempDirectorySync('gen_l10n_test.');
  });

  tearDown(() async {
28
    await flutter.stop();
29 30 31
    tryToDelete(tempDir);
  });

32
  Future<StringBuffer> runApp() async {
33 34 35 36
    // Run the app defined in GenL10nProject.main and wait for it to
    // send '#l10n END' to its stdout.
    final Completer<void> l10nEnd = Completer<void>();
    final StringBuffer stdout = StringBuffer();
37
    final StreamSubscription<String> subscription = flutter.stdout.listen((String line) {
38 39 40 41 42 43 44
      if (line.contains('#l10n')) {
        stdout.writeln(line.substring(line.indexOf('#l10n')));
      }
      if (line.contains('#l10n END')) {
        l10nEnd.complete();
      }
    });
45
    await flutter.run();
46 47
    await l10nEnd.future;
    await subscription.cancel();
48 49 50 51
    return stdout;
  }

  void expectOutput(StringBuffer stdout) {
52
    expect(stdout.toString(),
53 54 55 56
      '#l10n 0 (--- supportedLocales tests ---)\n'
      '#l10n 1 (supportedLocales[0]: languageCode: en, countryCode: null, scriptCode: null)\n'
      '#l10n 2 (supportedLocales[1]: languageCode: en, countryCode: CA, scriptCode: null)\n'
      '#l10n 3 (supportedLocales[2]: languageCode: en, countryCode: GB, scriptCode: null)\n'
57 58 59 60 61 62 63 64 65 66 67 68 69
      '#l10n 4 (supportedLocales[3]: languageCode: es, countryCode: null, scriptCode: null)\n'
      '#l10n 5 (supportedLocales[4]: languageCode: es, countryCode: 419, scriptCode: null)\n'
      '#l10n 6 (supportedLocales[5]: languageCode: zh, countryCode: null, scriptCode: null)\n'
      '#l10n 7 (supportedLocales[6]: languageCode: zh, countryCode: null, scriptCode: Hans)\n'
      '#l10n 8 (supportedLocales[7]: languageCode: zh, countryCode: null, scriptCode: Hant)\n'
      '#l10n 9 (supportedLocales[8]: languageCode: zh, countryCode: TW, scriptCode: Hant)\n'
      '#l10n 10 (--- countryCode (en_CA) tests ---)\n'
      '#l10n 11 (CA Hello World)\n'
      '#l10n 12 (Hello CA fallback World)\n'
      '#l10n 13 (--- countryCode (en_GB) tests ---)\n'
      '#l10n 14 (GB Hello World)\n'
      '#l10n 15 (Hello GB fallback World)\n'
      '#l10n 16 (--- zh ---)\n'
70
      '#l10n 17 (你好世界)\n'
71 72 73 74
      '#l10n 18 (你好)\n'
      '#l10n 19 (你好世界)\n'
      '#l10n 20 (你好2个其他世界)\n'
      '#l10n 21 (Hello 世界)\n'
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
      '#l10n 22 (zh - Hello for 价钱 CNY123.00)\n'
      '#l10n 23 (--- scriptCode: zh_Hans ---)\n'
      '#l10n 24 (简体你好世界)\n'
      '#l10n 25 (--- scriptCode - zh_Hant ---)\n'
      '#l10n 26 (繁體你好世界)\n'
      '#l10n 27 (--- scriptCode - zh_Hant_TW ---)\n'
      '#l10n 28 (台灣繁體你好世界)\n'
      '#l10n 29 (--- General formatting tests ---)\n'
      '#l10n 30 (Hello World)\n'
      '#l10n 31 (Hello _NEWLINE_ World)\n'
      '#l10n 32 (Hello \$ World)\n'
      '#l10n 33 (Hello World)\n'
      '#l10n 34 (Hello World)\n'
      '#l10n 35 (Hello World on Friday, January 1, 1960)\n'
      '#l10n 36 (Hello world argument on 1/1/1960 at 00:00)\n'
      '#l10n 37 (Hello World from 1960 to 2020)\n'
      '#l10n 38 (Hello for 123)\n'
      '#l10n 39 (Hello for price USD123.00)\n'
93
      '#l10n 40 (Hello for price BTC0.50 (with optional param))\n'
94
      "#l10n 41 (Hello for price BTC'0.50 (with special character))\n"
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      '#l10n 42 (Hello for price BTC"0.50 (with special character))\n'
      '#l10n 43 (Hello for price BTC"\'0.50 (with special character))\n'
      '#l10n 44 (Hello for Decimal Pattern 1,200,000)\n'
      '#l10n 45 (Hello for Percent Pattern 120,000,000%)\n'
      '#l10n 46 (Hello for Scientific Pattern 1E6)\n'
      '#l10n 47 (Hello)\n'
      '#l10n 48 (Hello World)\n'
      '#l10n 49 (Hello two worlds)\n'
      '#l10n 50 (Hello)\n'
      '#l10n 51 (Hello new World)\n'
      '#l10n 52 (Hello two new worlds)\n'
      '#l10n 53 (Hello on Friday, January 1, 1960)\n'
      '#l10n 54 (Hello World, on Friday, January 1, 1960)\n'
      '#l10n 55 (Hello two worlds, on Friday, January 1, 1960)\n'
      '#l10n 56 (Hello other 0 worlds, with a total of 100 citizens)\n'
      '#l10n 57 (Hello World of 101 citizens)\n'
      '#l10n 58 (Hello two worlds with 102 total citizens)\n'
      '#l10n 59 ([Hello] -World- #123#)\n'
      '#l10n 60 (\$!)\n'
      '#l10n 61 (One \$)\n'
115 116
      "#l10n 62 (Flutter's amazing!)\n"
      "#l10n 63 (Flutter's amazing, times 2!)\n"
117 118
      '#l10n 64 (Flutter is "amazing"!)\n'
      '#l10n 65 (Flutter is "amazing", times 2!)\n'
119 120 121 122 123
      '#l10n 66 (16 wheel truck)\n'
      "#l10n 67 (Sedan's elegance)\n"
      '#l10n 68 (Cabriolet has "acceleration")\n'
      '#l10n 69 (Oh, she found 1 item!)\n'
      '#l10n 70 (Indeed, they like Flutter!)\n'
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
      '#l10n 71 (Indeed, he likes ice cream!)\n'
      '#l10n 72 (Indeed, she likes chocolate!)\n'
      '#l10n 73 (--- es ---)\n'
      '#l10n 74 (ES - Hello world)\n'
      '#l10n 75 (ES - Hello _NEWLINE_ World)\n'
      '#l10n 76 (ES - Hola \$ Mundo)\n'
      '#l10n 77 (ES - Hello Mundo)\n'
      '#l10n 78 (ES - Hola Mundo)\n'
      '#l10n 79 (ES - Hello World on viernes, 1 de enero de 1960)\n'
      '#l10n 80 (ES - Hello world argument on 1/1/1960 at 0:00)\n'
      '#l10n 81 (ES - Hello World from 1960 to 2020)\n'
      '#l10n 82 (ES - Hello for 123)\n'
      '#l10n 83 (ES - Hello)\n'
      '#l10n 84 (ES - Hello World)\n'
      '#l10n 85 (ES - Hello two worlds)\n'
      '#l10n 86 (ES - Hello)\n'
      '#l10n 87 (ES - Hello nuevo World)\n'
      '#l10n 88 (ES - Hello two nuevo worlds)\n'
      '#l10n 89 (ES - Hello on viernes, 1 de enero de 1960)\n'
      '#l10n 90 (ES - Hello World, on viernes, 1 de enero de 1960)\n'
      '#l10n 91 (ES - Hello two worlds, on viernes, 1 de enero de 1960)\n'
      '#l10n 92 (ES - Hello other 0 worlds, with a total of 100 citizens)\n'
      '#l10n 93 (ES - Hello World of 101 citizens)\n'
      '#l10n 94 (ES - Hello two worlds with 102 total citizens)\n'
      '#l10n 95 (ES - [Hola] -Mundo- #123#)\n'
      '#l10n 96 (ES - \$!)\n'
      '#l10n 97 (ES - One \$)\n'
      "#l10n 98 (ES - Flutter's amazing!)\n"
      "#l10n 99 (ES - Flutter's amazing, times 2!)\n"
      '#l10n 100 (ES - Flutter is "amazing"!)\n'
      '#l10n 101 (ES - Flutter is "amazing", times 2!)\n'
      '#l10n 102 (ES - 16 wheel truck)\n'
      "#l10n 103 (ES - Sedan's elegance)\n"
      '#l10n 104 (ES - Cabriolet has "acceleration")\n'
      '#l10n 105 (ES - Oh, she found ES - 1 itemES - !)\n'
      '#l10n 106 (ES - Indeed, ES - they like ES - Flutter!)\n'
      '#l10n 107 (--- es_419 ---)\n'
161
      '#l10n 108 (ES 419 - Hello World)\n'
162 163 164
      '#l10n 109 (ES 419 - Hello)\n'
      '#l10n 110 (ES 419 - Hello World)\n'
      '#l10n 111 (ES 419 - Hello two worlds)\n'
165
      '#l10n END\n'
166
    );
167 168
  }

169 170
  // TODO(jsimmons): need a localization test that uses deferred loading
  // (see https://github.com/flutter/flutter/issues/61911)
171
  testWithoutContext('generated l10n classes produce expected localized strings', () async {
172 173
    await project.setUpIn(tempDir);
    flutter = FlutterRunTestDriver(tempDir);
174 175 176
    final StringBuffer stdout = await runApp();
    expectOutput(stdout);
  });
177
}