Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Unified Diff: pkg/intl/test/message_extraction/message_extraction_test.dart

Issue 12733003: Adds facilities for extracting Intl.message calls and generating code from translations (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes from review comments, also made tests more robust, removed scheduled_test dependency Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/intl/test/message_extraction/message_extraction_test.dart
diff --git a/pkg/intl/test/message_extraction/message_extraction_test.dart b/pkg/intl/test/message_extraction/message_extraction_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..6c0825a15f56a02a0fdf85faecc6aa81fe64faf5
--- /dev/null
+++ b/pkg/intl/test/message_extraction/message_extraction_test.dart
@@ -0,0 +1,208 @@
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library message_extraction_test;
+
+import 'package:unittest/unittest.dart';
+import 'dart:io';
+import 'dart:async';
+import 'package:pathos/path.dart' as path;
+import '../data_directory.dart';
+
+final dart = new Options().executable;
+
+// TODO(alanknight): We have no way of knowing what the package-root is,
+// so when we're running under the test framework, which sets the
+// package-root, we use a horrible hack and infer it from the executable.
+final packageDir = _findPackageDir(dart);
+
+/**
+ * Find our package directory from the executable. If we seem to be running
+ * from out/Release<arch>/dart or the equivalent Debug, then use the packages
+ * directory under Release<arch>. Otherwise return null, indicating to use
+ * the normal pub packages directory.
+ */
+String _findPackageDir(executable) {
+ var oneUp = path.dirname(executable);
+ var tail = path.basename(oneUp);
+ // If we're running from test.dart, we want e.g. out/ReleaseIA32/packages
+ if (tail.contains('Release') || tail.contains('Debug')) {
+ return path.join(oneUp, 'packages/');
+ }
+ // Otherwise we will rely on the normal packages directory.
+ return null;
+}
+
+/** If our package root directory is set, return it as a VM argument. */
+final vmArgs = (packageDir == null) ? [] : ['--package-root=$packageDir'];
+
+/**
+ * Translate a file path into this test directory, regardless of the
+ * working directory.
+ */
+String dir([String s]) =>
+ path.join(intlDirectory, 'test', 'message_extraction', s);
+
+main() {
+ test("Test round trip message generation, printing, and ", () {
+ deleteGeneratedFiles();
+ extractMessages(null)
+ .then(expectAsync1((result) => generateTranslationFiles(result)
Emily Fortuna 2013/03/14 19:45:00 a handy new feature for our unittesting library we
Alan Knight 2013/03/14 20:50:04 Nice.
+ .then(expectAsync1((result) => generateCodeFromTranslation(result)
+ .then(expectAsync1((result) => runGeneratedCode(result)
+ .then(expectAsync1(verifyResult),
+ onError: print)),
+ onError: print)),
+ onError: print)),
+ onError: print);
+ });
+}
+
+void deleteGeneratedFiles() {
+ var files = [dir('intl_messages.json'), dir('translation_fr.json'),
+ dir('messages_fr.dart'), dir('message_de_DE.dart'),
+ dir('messages_all.dart')];
+ files.map((name) => new File(name)).forEach((x) {
+ if (x.existsSync()) x.deleteSync();});
+}
+
+/**
+ * Run the process with the given list of filenames, which we assume
+ * are in dir() and need to be qualified in case that's not our working
+ * directory.
+ */
+Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames)
+{
+ // If there's a failure in one of the sub-programs, print its output.
+ if (previousResult != null) {
+ if (previousResult.exitCode != 0) {
+ print("Error running sub-program:");
+ }
+ print(previousResult.stdout);
+ print(previousResult.stderr);
+ print("exitCode=${previousResult.exitCode}");
+ }
+ var filesInTheRightDirectory = filenames.map((x) => dir(x)).toList();
+ // Inject the script argument --output-dir in between the script and its
+ // arguments.
+ var args = []
+ ..addAll(vmArgs)
+ ..add(filesInTheRightDirectory.first)
+ ..addAll(["--output-dir=${dir()}"])
+ ..addAll(filesInTheRightDirectory.skip(1));
+ var result = Process.run(dart, args);
+ return result;
+}
+
+Future<ProcessResult> extractMessages(ProcessResult previousResult) => run(
+ previousResult,
+ ['extract_to_json.dart', 'sample_with_messages.dart',
+ 'part_of_sample_with_messages.dart']);
+
+Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) =>
+ run(
+ previousResult,
+ ['make_hardcoded_translation.dart', 'intl_messages.json']);
+
+Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult)
+ => run(
+ previousResult,
+ ['generate_from_json.dart', 'sample_with_messages.dart',
+ 'part_of_sample_with_messages.dart', 'translation_fr.json',
+ 'translation_de_DE.json' ]);
+
+Future<ProcessResult> runGeneratedCode(ProcessResult previousResult) =>
+ run(previousResult, ['sample_with_messages.dart']);
+
+void verifyResult(results) {
+ var lineIterator;
+ verify(String s) {
+ lineIterator.moveNext();
+ var value = lineIterator.current;
+ expect(value, s);
+ }
+
+ var output = results.stdout;
+ var lines = output.split("\n");
+ lineIterator = lines.iterator..moveNext();
+ verify("Printing messages for en_US");
+ verify("This is a message");
+ verify("Another message with parameter hello");
+ verify("Characters that need escaping, e.g slashes \\ dollars \${ "
+ "(curly braces are ok) and xml reserved characters <& and "
+ "quotes \" parameters 1, 2, and 3");
+ verify("This string extends across multiple lines.");
+ verify("1, b, [c, d]");
+ verify('"So-called"');
+ verify("Cette chaîne est toujours traduit");
+ verify("Interpolation is tricky when it ends a sentence like this.");
+ verify("This comes from a method");
+ verify("This method is not a lambda");
+ verify("This comes from a static method");
+ verify("This is missing some translations");
+ verify("Ancient Greek hangman characters: 𐅆𐅇.");
+// verify("The thing is, well");
+// verify("One of the tricky things is the plural form");
+// verify("One of the tricky things is plural forms");
+ verify("Escapable characters here: ");
+
+ var fr_lines = lines.skip(1).skipWhile(
+ (line) => !line.contains('----')).toList();
+ lineIterator = fr_lines.iterator..moveNext();
+ verify("Printing messages for fr");
+ verify("Il s'agit d'un message");
+ verify("Un autre message avec un seul paramètre hello");
+ verify(
+ "Caractères qui doivent être échapper, par exemple barres \\ "
+ "dollars \${ (les accolades sont ok), et xml/html réservés <& et "
+ "des citations \" "
+ "avec quelques paramètres ainsi 1, 2, et 3");
+ verify("Cette message prend plusiers lignes.");
+ verify("1, b, [c, d]");
+ verify('"Soi-disant"');
+ verify("Cette chaîne est toujours traduit");
+ verify(
+ "L'interpolation est délicate quand elle se termine une "
+ "phrase comme this.");
+ verify("Cela vient d'une méthode");
+ verify("Cette méthode n'est pas un lambda");
+ verify("Cela vient d'une méthode statique");
+ verify("Ce manque certaines traductions");
+ verify("Anciens caractères grecs jeux du pendu: 𐅆𐅇.");
+// verify("La chose est, well");
+// verify("Une des choses difficiles est la forme plurielle");
+// verify("Une des choses difficiles est les formes plurielles");
+ verify("Escapes: ");
+ verify("\r\f\b\t\v.");
+
+
+ var de_lines = fr_lines.skip(1).skipWhile(
+ (line) => !line.contains('----')).toList();
+ lineIterator = de_lines.iterator..moveNext();
+ verify("Printing messages for de_DE");
+ verify("Dies ist eine Nachricht");
+ verify("Eine weitere Meldung mit dem Parameter hello");
+ verify(
+ "Zeichen, die Flucht benötigen, zB Schrägstriche \\ Dollar "
+ "\${ (geschweiften Klammern sind ok) und xml reservierte Zeichen <& und "
+ "Zitate \" Parameter 1, 2 und 3");
+ verify("Dieser String erstreckt sich über mehrere "
+ "Zeilen erstrecken.");
+ verify("1, b, [c, d]");
+ verify('"Sogenannt"');
+ // This is correct, the message is forced to French, even in a German locale.
+ verify("Cette chaîne est toujours traduit");
+ verify(
+ "Interpolation ist schwierig, wenn es einen Satz wie dieser endet this.");
+ verify("Dies ergibt sich aus einer Methode");
+ verify("Diese Methode ist nicht eine Lambda");
+ verify("Dies ergibt sich aus einer statischen Methode");
+ verify("This is missing some translations");
+ verify("Antike griechische Galgenmännchen Zeichen: 𐅆𐅇");
+// verify("Die Sache ist, well");
+// expect("Einer der knifflige Dinge ist der Plural");
+// expect("Zu den kniffligen Dinge Pluralformen");
+ verify("Escapes: ");
+ verify("\r\f\b\t\v.");
+}

Powered by Google App Engine
This is Rietveld 408576698