OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 // Test that closures have a useful string that identifies the function by name |
| 6 // in error messages. |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 |
| 10 @NoInline |
| 11 @AssumeDynamic |
| 12 confuse(x) => x; |
| 13 |
| 14 class CCCC { |
| 15 instanceMethod([a, b]) => '[$a, $b]'; |
| 16 static staticMethod() => 'hi'; |
| 17 // Default `toString` method returns "Instance of 'CCCC'" or similar with a |
| 18 // shorter name if minified. |
| 19 } |
| 20 |
| 21 main() { |
| 22 var c = confuse(new CCCC()); |
| 23 |
| 24 var instanceString = confuse(c).toString(); |
| 25 bool isMinified = instanceString.contains(new RegExp("Instance of '..?.?'")); |
| 26 if (!isMinified) { |
| 27 Expect.equals("Instance of 'CCCC'", instanceString); |
| 28 } |
| 29 |
| 30 checkContains(String message, String tag) { |
| 31 if (!message.contains(tag)) { |
| 32 if (!isMinified) { |
| 33 Expect.fail('"$message" should contain "$tag"'); |
| 34 } |
| 35 // When minified we will accept quoted names up to 3 characters. |
| 36 Expect.isTrue( |
| 37 message.contains(new RegExp("'..?.?'")), |
| 38 '"$message" should contain minified name'); |
| 39 } |
| 40 } |
| 41 |
| 42 // We use ArgumentError.value since it prints the value using |
| 43 // Error.safeToString. |
| 44 var e1 = new ArgumentError.value(c); |
| 45 var s1 = '$e1'; |
| 46 Expect.isTrue(s1.contains(instanceString), |
| 47 'Error message "$s1" should contain "$instanceString"'); |
| 48 |
| 49 // Instance method tear-off. |
| 50 var e2 = new ArgumentError.value(confuse(c).instanceMethod); |
| 51 var s2 = '$e2'; |
| 52 // Instance method tear-off should contain instance string. |
| 53 Expect.isTrue(s2.contains(instanceString), |
| 54 'Error message "$s2" should contain "$instanceString"'); |
| 55 // Instance method tear-off should also name the method. |
| 56 checkContains(s2.replaceAll(instanceString, '*'), "instanceMethod"); |
| 57 |
| 58 // Top level tear-off. |
| 59 var e3 = new ArgumentError.value(confuse); |
| 60 var s3 = '$e3'; |
| 61 checkContains(s3, "confuse"); |
| 62 checkContains('$confuse', "confuse"); |
| 63 |
| 64 // Static method tear-off. |
| 65 var e4 = new ArgumentError.value(CCCC.staticMethod); |
| 66 var s4 = '$e4'; |
| 67 checkContains(s4, "staticMethod"); |
| 68 checkContains('${CCCC.staticMethod}', "staticMethod"); |
| 69 |
| 70 // Local anonymous closure. |
| 71 var anon = () => c; |
| 72 var e5 = new ArgumentError.value(anon); |
| 73 var s5 = '$e5'; |
| 74 checkContains(s5, "main_closure"); |
| 75 checkContains('$anon', "main_closure"); |
| 76 |
| 77 // Local named closure. |
| 78 localFunction() => c; |
| 79 var e6 = new ArgumentError.value(localFunction); |
| 80 var s6 = '$e6'; |
| 81 checkContains(s6, "localFunction"); |
| 82 checkContains('$localFunction', "localFunction"); |
| 83 } |
OLD | NEW |