Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // VMOptions=--enable_type_checks | 4 // VMOptions=--enable_type_checks |
| 5 // | 5 // |
| 6 // Dart test program testing assert statements. | 6 // Dart test program testing assert statements. |
| 7 | 7 |
| 8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
| 9 | 9 |
| 10 class AssertionTest { | 10 testTrue() { |
|
Jennifer Messerly
2017/07/07 23:43:17
this test also matches VM behavior
| |
| 11 static testTrue() { | 11 int i = 0; |
| 12 int i = 0; | 12 try { |
| 13 try { | 13 assert(true); |
| 14 assert(true); | 14 } on AssertionError catch (error) { |
| 15 } on AssertionError catch (error) { | 15 i = 1; |
| 16 i = 1; | |
| 17 } | |
| 18 return i; | |
| 19 } | 16 } |
| 17 return i; | |
| 18 } | |
| 20 | 19 |
| 21 static testFalse() { | 20 testFalse() { |
| 22 int i = 0; | 21 int i = 0; |
| 23 try { | 22 try { |
| 24 assert(false); | 23 assert(false); |
| 25 } on AssertionError catch (error) { | 24 } on AssertionError catch (error) { |
| 26 i = 1; | 25 i = 1; |
| 27 } | |
| 28 return i; | |
| 29 } | 26 } |
| 27 return i; | |
| 28 } | |
| 30 | 29 |
| 31 static unknown(var a) { | 30 unknown(dynamic a) { |
| 32 return (a) ? true : false; | 31 return a ? true : false; |
| 32 } | |
| 33 | |
| 34 testClosureReturnsFalse() { | |
| 35 int i = 0; | |
| 36 try { | |
| 37 assert(() => false); | |
| 38 } on AssertionError catch (error) { | |
| 39 i = 1; | |
| 33 } | 40 } |
| 41 return i; | |
| 42 } | |
| 34 | 43 |
| 35 static testUnknown() { | 44 testClosure(bool f()) { |
| 36 var x = unknown(false); | 45 int i = 0; |
| 37 int i = 0; | 46 try { |
| 38 try { | 47 assert(f); |
| 39 assert(x); | 48 } on AssertionError catch (error) { |
| 40 } on AssertionError catch (error) { | 49 i = 1; |
| 41 i = 1; | |
| 42 } | |
| 43 return i; | |
| 44 } | 50 } |
| 51 return i; | |
| 52 } | |
| 45 | 53 |
| 46 static testClosure() { | 54 testBoolean(bool value) { |
| 47 int i = 0; | 55 int i = 0; |
| 48 try { | 56 try { |
| 49 assert(() => false); | 57 assert(value); |
| 50 } on AssertionError catch (error) { | 58 } on AssertionError catch (error) { |
| 51 i = 1; | 59 i = 1; |
| 52 } | |
| 53 return i; | |
| 54 } | 60 } |
| 61 return i; | |
| 62 } | |
| 55 | 63 |
| 56 static testClosure2() { | 64 testDynamic(dynamic value) { |
| 57 int i = 0; | 65 int i = 0; |
| 58 try { | 66 try { |
| 59 var x = () => false; | 67 assert(value); |
| 60 assert(x); | 68 } on AssertionError catch (error) { |
| 61 } on AssertionError catch (error) { | 69 i = 1; |
| 62 i = 1; | |
| 63 } | |
| 64 return i; | |
| 65 } | 70 } |
| 71 return i; | |
| 72 } | |
| 66 | 73 |
| 67 static testMain() { | 74 testMessage(value, message) { |
| 68 Expect.equals(0, testTrue()); | 75 try { |
| 69 Expect.equals(1, testFalse()); | 76 assert(value, message); |
| 70 Expect.equals(1, testClosure()); | 77 return null; |
| 71 Expect.equals(1, testClosure2()); | 78 } on AssertionError catch (error) { |
| 79 return error; | |
| 72 } | 80 } |
| 73 } | 81 } |
| 74 | 82 |
| 75 main() { | 83 main() { |
| 76 AssertionTest.testMain(); | 84 Expect.equals(0, testTrue()); |
| 85 Expect.equals(0, testBoolean(true)); | |
| 86 Expect.equals(0, testDynamic(unknown(true))); | |
| 87 Expect.equals(0, testClosure(() => true)); | |
| 88 Expect.equals(0, testDynamic(() => true)); | |
| 89 | |
| 90 Expect.equals(1, testFalse()); | |
| 91 Expect.equals(1, testBoolean(false)); | |
| 92 Expect.equals(1, testClosureReturnsFalse()); | |
| 93 Expect.equals(1, testDynamic(unknown(false))); | |
| 94 Expect.equals(1, testDynamic(() => false)); | |
| 95 | |
| 96 Expect.equals(1, testBoolean(null)); | |
| 97 Expect.equals(1, testDynamic(null)); | |
| 98 Expect.equals(1, testDynamic(42)); | |
| 99 Expect.equals(1, testDynamic(() => 42)); | |
| 100 Expect.equals(1, testDynamic(() => null)); | |
| 101 Expect.equals(1, testClosure(() => null)); | |
| 102 | |
| 103 Expect.equals(1234, testMessage(false, 1234).message); | |
| 104 Expect.equals('hi', testMessage(false, 'hi').message); | |
| 105 Expect.equals(1234, testMessage(() => false, 1234).message); | |
| 106 Expect.equals('hi', testMessage(() => false, 'hi').message); | |
| 107 | |
| 108 // These errors do not have the message because boolean conversion failed. | |
| 109 Expect.equals(null, testMessage(null, 1234).message); | |
| 110 Expect.equals(null, testMessage(null, 'hi').message); | |
| 111 Expect.equals(null, testMessage(() => null, 1234).message); | |
| 112 Expect.equals(null, testMessage(() => null, 'hi').message); | |
| 113 Expect.isTrue("${testMessage(42, 1234)}".contains('boolean expression')); | |
| 114 Expect.isTrue("${testMessage(() => 1, 1234)}".contains('boolean expression')); | |
| 77 } | 115 } |
| OLD | NEW |