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

Unified Diff: pkg/analyzer_experimental/test/utils.dart

Issue 14998006: Add some user-friendly utility functions to pkg/analyzer_experimental. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 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/analyzer_experimental/test/utils.dart
diff --git a/pkg/analyzer_experimental/test/utils.dart b/pkg/analyzer_experimental/test/utils.dart
new file mode 100644
index 0000000000000000000000000000000000000000..62f99095be6943ebc4efeb7e7ca9e433cf12327b
--- /dev/null
+++ b/pkg/analyzer_experimental/test/utils.dart
@@ -0,0 +1,46 @@
+// 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 utils;
+
+import 'dart:io';
+
+import 'package:analyzer_experimental/analyzer.dart';
+import 'package:pathos/path.dart' as pathos;
+
+/// Returns the string representation of the [AnalyzerErrorGroup] thrown when
+/// parsing [contents] as a Dart file. If [contents] doesn't throw any errors,
+/// this will return null.
+///
+/// This replaces the filename in the error string with its basename, since the
+/// full path will vary from machine to machine. It also replaces the exception
+/// message with "..." to decouple these tests from the specific exception
+/// messages.
+String errorsForFile(String contents) {
+ return withTempDir((temp) {
+ var path = pathos.join(temp, 'test.dart');
+ new File(path).writeAsStringSync(contents);
+ try {
+ parseDartFile(path);
+ } on AnalyzerErrorGroup catch (e) {
+ return e.toString().replaceAllMapped(
+ new RegExp(r"^(Error on line \d+ of )([^:]+): .*$", multiLine: true),
+ (match) => match[1] + pathos.basename(match[2]) + ': ...');
+ }
+ return null;
+ });
+}
+
+/// Creates a temporary directory and passes its path to [fn]. Once [fn]
+/// completes, the temporary directory and all its contents will be deleted.
+///
+/// Returns the return value of [fn].
+dynamic withTempDir(fn(String path)) {
+ var tempDir = new Directory('').createTempSync().path;
scheglov 2013/05/09 01:26:46 Just curious - why not store Directory instance in
+ try {
+ return fn(tempDir);
+ } finally {
+ new Directory(tempDir).deleteSync(recursive: true);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698