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

Unified Diff: tests/language/assert_lexical_scope_test.dart

Issue 10915083: Change assert implementation to not depend on a top-level function called 'assert'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revert to using lexicalScope Created 8 years, 3 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: tests/language/assert_lexical_scope_test.dart
diff --git a/tests/language/assert_lexical_scope_test.dart b/tests/language/assert_lexical_scope_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..d5a43c29dcb333da128ddedfb3e25e56da8aeb3f
--- /dev/null
+++ b/tests/language/assert_lexical_scope_test.dart
@@ -0,0 +1,221 @@
+// 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.
+
+// A function that accepts 0..2 arguments.
+void poly([a, b]) {
+ polyCount++;
+ polyArg = a;
+}
+
+// First argument of most recent call to poly.
+var polyArg = 0;
+
+// Number of calls to poly.
+var polyCount = 0;
+
+// (Super)classes that declare an "assert" member.
+class SuperGet {
+ get assert => poly;
+
+ void lexicalAssert(x) {
+ assert(x);
+ }
+}
+
+class SuperMethod {
+ assert([a, b]) => poly(a, b);
+
+ void lexicalAssert(x) {
+ assert(x);
+ }
+}
+
+class SuperField {
+ var assert;
+ SuperField() : assert = poly;
+
+ void lexicalAssert(x) {
+ assert(x);
+ }
+}
+
+// Superclass that don't declare "assert", but will handle assert calls.
+class SuperNon {
+ noSuchMethod(x, y) {
+ switch (y.length) {
+ case 0: return poly();
+ case 1: return poly(y[0]);
+ case 2: return poly(y[0], y[1]);
+ }
+ }
+
+ void lexicalAssert(x) {
+ // Hack, since there is no lexically enclosing 'assert' declaration here,
+ // so just act as if there was to avoid special casing it in the test.
+ poly(x);
+ }
+}
+
+// Sub-classes that read/call "assert".
+// In every case except "assert(exp);" this should access the superclass
+// member.
+
+class SubGet extends SuperGet {
+ void getAssert(x) {
+ assert;
+ }
+ void assert0(x) {
+ assert();
+ }
+ void assert1(x) {
+ assert(x);
+ }
+ void assertExp(x) {
+ var z = assert(x);
+ }
+ void assert2(x) {
+ assert(x, x);
+ }
+}
+
+class SubMethod extends SuperMethod {
+ void getAssert(x) {
+ assert;
+ }
+ void assert0(x) {
+ assert();
+ }
+ void assert1(x) {
+ assert(x);
+ }
+ void assertExp(x) {
+ var z = assert(x);
+ }
+ void assert2(x) {
+ assert(x, x);
+ }
+}
+
+class SubField extends SuperField {
+ void getAssert(x) {
+ assert;
+ }
+ void assert0(x) {
+ assert();
+ }
+ void assert1(x) {
+ assert(x);
+ }
+ void assertExp(x) {
+ var z = assert(x);
+ }
+ void assert2(x) {
+ assert(x, x);
+ }
+}
+
+class SubNon extends SuperNon {
+ void getAssert(x) {
+ assert;
+ }
+ assert0(x) {
+ assert();
+ }
+ void assert1(x) {
+ assert(x);
+ }
+ void assertExp(x) {
+ var z = assert(x);
+ }
+ void assert2(x) {
+ assert(x, x);
+ }
+}
+
+
+class SetAssert {
Lasse Reichstein Nielsen 2012/09/07 10:18:57 I'll remove this again until there is agreement on
+ set assert(x) {}
+ void lexicalAssert(x) {
+ // The setter should not inhibit the assert,
+ assert(x);
+ }
+}
+
+
+testAssertDeclared() {
+ var get = new SubGet();
+ var method = new SubMethod();
+ var field = new SubField();
+ var non = new SubNon();
+
+ void expectCallsPoly(code, [bool noArgument = false]) {
+ int oldPolyCount = polyCount;
+ int newPolyArg = polyArg + 1;
+ int expectedPolyArg = noArgument ? null : newPolyArg;
+ code(newPolyArg);
+ Expect.equals(oldPolyCount + 1, polyCount);
+ Expect.equals(expectedPolyArg, polyArg);
+ if (noArgument) polyArg = newPolyArg;
+ }
+
+ void expectAssert(code) {
+ int oldPolyCount = polyCount;
+ // Detect whether asserts are enabled.
+ bool assertsEnabled = false;
+ assert(assertsEnabled = true);
+ try {
+ code(polyArg + 1);
+ // If asserts are enabled, we should not get here.
+ // If they are not, the call does nothing.
+ if (assertsEnabled) {
+ Expect.fail("Didn't call assert with asserts enabled.");
+ }
+ } on AssertionError catch (e) {
+ if (!assertsEnabled) Expect.fail("Called assert with asserts disabled?");
+ }
+ Expect.equals(oldPolyCount, polyCount);
+ }
+
+ // Sanity check.
+ expectCallsPoly(poly);
+
+ // Doesn't fail to read "assert".
+ get.getAssert(0);
+ method.getAssert(0);
+ field.getAssert(0);
+ expectCallsPoly(non.getAssert, true); // Hits 'noSuchMethod'.
+
+ // Check when 'assert' is a superclass member declaration (or, simulated with
+ // noSuchMethod).
+ void testSuperAssert(object) {
+ expectCallsPoly(object.assert0, true);
+ expectAssert(object.assert1);
+ expectCallsPoly(object.assertExp);
+ expectCallsPoly(object.assert2);
+ expectCallsPoly(object.lexicalAssert);
+ }
+
+ testSuperAssert(get);
+ testSuperAssert(method);
+ testSuperAssert(field);
+ testSuperAssert(non);
+
+ // Local declarations
+ expectCallsPoly((x) {
+ var assert = poly;
+ assert(x);
+ });
+
+ expectCallsPoly((x) {
+ void assert(x) => poly(x);
+ assert(x);
+ });
+
+ // Setters don't inhibit assert.
+ expectAssert(new SetAssert().lexicalAssert);
+}
+
+main() {
+ testAssertDeclared();
+}

Powered by Google App Engine
This is Rietveld 408576698