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

Unified Diff: lib/unittest/description.dart

Issue 10441104: New expectation functions plus convert old tests to use these. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 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: lib/unittest/description.dart
===================================================================
--- lib/unittest/description.dart (revision 0)
+++ lib/unittest/description.dart (revision 0)
@@ -0,0 +1,105 @@
+// Copyright (c) 2012, 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.
+
+/**
+ * The default implementation of IDescription. This should rarely need
+ * substitution, although conceivably it is a place where other languages
+ * could be supported.
+ */
+
Bob Nystrom 2012/05/30 23:23:51 Remove blank line.
gram 2012/06/01 17:33:15 Done.
+class StringDescription implements IDescription {
+ var _out = '';
+
+ /**
+ * Get the description as a string.
+ */
+ String toString() => _out;
+
+ /**
+ * Append some plain [text] to the description.
+ */
+
Bob Nystrom 2012/05/30 23:23:51 Remove blank line, here and elsewhere.
gram 2012/06/01 17:33:15 Done.
+ IDescription append(text) {
Bob Nystrom 2012/05/30 23:23:51 "add" to be consistent with List and StringBuffer.
gram 2012/06/01 17:33:15 Done.
+ _out = '${_out}${text}';
+ return this;
+ }
+
+ /**
+ * Append a description of given [value]. If it is a string just
Bob Nystrom 2012/05/30 23:23:51 "Append" -> "appends". Remove "given".
gram 2012/06/01 17:33:15 Done.
+ * append it with some control characters escaped. Else try
Bob Nystrom 2012/05/30 23:23:51 This sentence needs some grammar work.
+ * call describe on the value itself, which will work for
+ * IMatchers. If that fails, just quote its toString() value.
+ */
+
+ IDescription appendDescriptionOf(value) {
+ try {
+ value.describe(this);
Bob Nystrom 2012/05/30 23:23:51 Instead of just trying to call describe(), why not
gram 2012/06/01 17:33:15 Done.
+ } catch(var e) {
+ if (value is String) {
+ _appendEscapedString(value);
+ } else {
+ String description = value.toString();
+ if (description.startsWith('<') && description.endsWith('>')) {
+ append(description);
+ } else {
+ append('<');
+ append(description);
+ append('>');
+ }
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Append a [list] of objects to the description, using the
+ * specified [separator] and framing the list with [start]
+ * and [end].
+ */
+
+ IDescription appendList(start, separator, end, list) {
+ var separate = false;
+ append(start);
+ for (var item in list) {
+ if (separate) {
+ append(separator);
+ }
+ appendDescriptionOf(item);
+ separate = true;
+ }
+ append(end);
+ return this;
+ }
+
+ /**
+ * Escape the control characters in [string] so that they are visible.
+ */
+ _appendEscapedString(String string) {
+ append("'");
+ for (var i = 0; i < string.length; i++) {
+ append(_escape(string[i]));
+ }
+ append("'");
+ }
+
+ /**
+ * Return the escaped form of a character [ch].
+ */
+ _escape(ch) {
+ if (ch == "'")
+ return "\'";
+ else if (ch == '\n')
+ return '\\n';
+ else if (ch == '\r')
+ return '\\r';
+ else if (ch == '\t')
+ return '\\t';
+ else
+ return ch;
+ }
+}
+
+
+
+

Powered by Google App Engine
This is Rietveld 408576698