| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 import "package:expect/expect.dart"; | |
| 6 | |
| 7 main() { | |
| 8 var assertsEnabled = false; | |
| 9 assert((assertsEnabled = true)); | |
| 10 if (!assertsEnabled) return; | |
| 11 | |
| 12 // TODO(rnystrom): Test cases where the first argument to assert() is a | |
| 13 // function. | |
| 14 | |
| 15 testAssertFails(); | |
| 16 testAssertDoesNotFail(); | |
| 17 testNullMessage(); | |
| 18 testDoesNotEvaluateMessageIfAssertSucceeds(); | |
| 19 testMessageExpressionThatThrows(); | |
| 20 testCallsToStringOnMessageLazily(); | |
| 21 } | |
| 22 | |
| 23 /// A class with a custom toString() that tracks when it is called. | |
| 24 class ToString { | |
| 25 bool calledToString = false; | |
| 26 | |
| 27 String toString() { | |
| 28 calledToString = true; | |
| 29 return "toString!"; | |
| 30 } | |
| 31 } | |
| 32 | |
| 33 testAssertFails() { | |
| 34 try { | |
| 35 assert(false, "Oops"); | |
| 36 Expect.fail("Assert should throw."); | |
| 37 } catch (e) { | |
| 38 Expect.isTrue(e.toString().contains("Oops")); | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 testAssertDoesNotFail() { | |
| 43 try { | |
| 44 assert(true, "Oops"); | |
| 45 } catch (e) { | |
| 46 Expect.fail("Assert should not throw."); | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 testNullMessage() { | |
| 51 try { | |
| 52 assert(false, null); | |
| 53 Expect.fail("Assert should throw."); | |
| 54 } catch (e) { | |
| 55 Expect.isTrue(e.toString().contains("is not true")); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 testDoesNotEvaluateMessageIfAssertSucceeds() { | |
| 60 try { | |
| 61 var evaluated = false; | |
| 62 assert(true, evaluated = true); | |
| 63 Expect.isFalse(evaluated); | |
| 64 } catch (e) { | |
| 65 Expect.fail("Assert should not throw."); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 testMessageExpressionThatThrows() { | |
| 70 try { | |
| 71 assert(false, throw "dang"); | |
| 72 Expect.fail("Should throw"); | |
| 73 } catch (e) { | |
| 74 Expect.equals(e, "dang"); | |
| 75 } | |
| 76 } | |
| 77 | |
| 78 testCallsToStringOnMessageLazily() { | |
| 79 var toString = new ToString(); | |
| 80 try { | |
| 81 assert(false, toString); | |
| 82 Expect.fail("Assert should throw."); | |
| 83 } catch (e) { | |
| 84 Expect.isFalse(toString.calledToString); | |
| 85 Expect.isTrue(e.toString().contains("Instance of 'ToString'")); | |
| 86 Expect.isFalse(toString.calledToString); | |
| 87 } | |
| 88 } | |
| OLD | NEW |