Chromium Code Reviews| Index: tests/compiler/dart2js_native/error_safeToString_test.dart |
| diff --git a/tests/compiler/dart2js_native/error_safeToString_test.dart b/tests/compiler/dart2js_native/error_safeToString_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b2072d48ec119e6b16fc9ed3ed5bf49f8d0118cd |
| --- /dev/null |
| +++ b/tests/compiler/dart2js_native/error_safeToString_test.dart |
| @@ -0,0 +1,172 @@ |
| +// Copyright (c) 2015, 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. |
| + |
| +import "package:expect/expect.dart"; |
| +import 'dart:_foreign_helper' show JS_INTERCEPTOR_CONSTANT, JS; |
| +import 'dart:_js_helper' show Native, Creates; |
| +import 'dart:_interceptors' show |
| + Interceptor, |
| + JavaScriptObject, |
| + PlainJavaScriptObject, |
| + UnknownJavaScriptObject; |
| + |
| +// Test for safe formatting of JavaScript objects by Error.safeToString. |
| + |
| +@Native('PP') |
| +class Purple {} |
| + |
| +@Native('QQ') |
| +class Q {} |
| + |
| +@Native('RR') |
| +class Rascal { |
| + toString() => 'RRRR'; |
| +} |
| + |
| +makeA() native; |
| +makeB() native; |
| +makeC() native; |
| +makeD() native; |
| +makeE() native; |
| +makeP() native; |
| +makeQ() native; |
| +makeR() native; |
| + |
| +void setup() native r""" |
| +makeA = function(){return {hello: 123};}; |
| + |
| +function BB(){} |
| +makeB = function(){return new BB();}; |
| + |
| +function CC(){} |
| +makeC = function(){ |
| + var x = new CC(); |
| + x.constructor = null; // Foils constructor lookup. |
|
Siggi Cherem (dart-lang)
2015/09/22 16:51:23
Foils => Fails? (same below)
sra1
2015/09/23 03:58:21
To foil is to prevent from succeeding, "a brave po
|
| + return x; |
| +}; |
| + |
| +function DD(){} |
| +makeD = function(){ |
| + var x = new DD(); |
| + x.constructor = {name: 'DDxxx'}; // Foils constructor lookup. |
| + return x; |
| +}; |
| + |
| +function EE(){} |
| +makeE = function(){ |
| + var x = new EE(); |
| + x.constructor = function Liar(){}; // Foils constructor lookup. |
| + return x; |
| +}; |
| + |
| +function PP(){} |
| +makeP = function(){return new PP();}; |
| + |
| +function QQ(){} |
| +makeQ = function(){return new QQ();}; |
| + |
| +function RR(){} |
| +makeR = function(){return new RR();}; |
| + |
| +"""; |
| + |
| + |
| +expectTypeName(expectedName, s) { |
| + var m = new RegExp(r"Instance of '(.*)'").firstMatch(s); |
| + Expect.isNotNull(m); |
| + var name = m.group(1); |
| + Expect.isTrue(expectedName == name || name.length <= 3, |
| + "Is '$expectedName' or minified: '$name'"); |
| +} |
| + |
| +final plainJsString = |
| + Error.safeToString(JS_INTERCEPTOR_CONSTANT(PlainJavaScriptObject)); |
| + |
| +final unknownJsString = |
| + Error.safeToString(JS_INTERCEPTOR_CONSTANT(UnknownJavaScriptObject)); |
| + |
| +final interceptorString = |
| + Error.safeToString(JS_INTERCEPTOR_CONSTANT(Interceptor)); |
| + |
| + |
| +testDistinctInterceptors() { |
| + // Test invariants needed for the other tests. |
| + |
| + Expect.notEquals(plainJsString, unknownJsString); |
| + Expect.notEquals(plainJsString, interceptorString); |
| + Expect.notEquals(unknownJsString, interceptorString); |
| + |
| + expectTypeName('PlainJavaScriptObject', plainJsString); |
| + expectTypeName('UnknownJavaScriptObject', unknownJsString); |
| + expectTypeName('Interceptor', interceptorString); |
| + |
| + // Sometimes interceptor *objects* are used instead of the prototypes. Check |
| + // these work too. |
| + var plain2 = Error.safeToString(const PlainJavaScriptObject()); |
| + Expect.equals(plainJsString, plain2); |
| + |
| + var unk2 = Error.safeToString(const UnknownJavaScriptObject()); |
| + Expect.equals(unknownJsString, unk2); |
| +} |
| + |
| + |
| +testExternal() { |
| + var x = makeA(); |
| + print('A $x ${Error.safeToString(x)}'); |
|
Siggi Cherem (dart-lang)
2015/09/22 16:51:23
do you need to keep the prints in the test? or whe
sra1
2015/09/23 03:58:21
I'll remove them.
|
| + Expect.equals(plainJsString, Error.safeToString(x)); |
| + |
| + x = makeB(); |
| + print('B $x ${Error.safeToString(x)}'); |
| + // Gets name from constructor, regardless of minification. |
| + Expect.equals("Instance of 'BB'", Error.safeToString(x)); |
| + |
| + x = makeC(); |
| + print('C $x ${Error.safeToString(x)}'); |
| + Expect.equals(unknownJsString, Error.safeToString(x)); |
| + |
| + x = makeD(); |
| + print('D $x ${Error.safeToString(x)}'); |
| + Expect.equals(unknownJsString, Error.safeToString(x)); |
| + |
| + x = makeE(); |
| + print('D $x ${Error.safeToString(x)}'); |
| + Expect.equals(unknownJsString, Error.safeToString(x)); |
| +} |
| + |
| +testNative() { |
| + var x = makeP(); |
| + Expect.isTrue(x is Purple); // This test forces Purple to be distinguished. |
| + print('P $x ${Error.safeToString(x)}'); |
| + Expect.notEquals(plainJsString, Error.safeToString(x)); |
| + Expect.notEquals(unknownJsString, Error.safeToString(x)); |
| + Expect.notEquals(interceptorString, Error.safeToString(x)); |
| + // And not the native class constructor. |
| + Expect.notEquals("Instance of 'PP'", Error.safeToString(x)); |
|
Siggi Cherem (dart-lang)
2015/09/22 16:51:23
Do we prevent minification from picking names we'v
sra1
2015/09/23 03:58:21
Good point. Changed to PPPP.
|
| + expectTypeName('Purple', Error.safeToString(x)); |
| + |
| + x = makeQ(); |
| + print('Q $x ${Error.safeToString(x)}'); |
| + // We are going to get either the general interceptor |
| + Expect.isTrue( |
| + "Instance of 'QQ'" == Error.safeToString(x) || |
| + interceptorString == Error.safeToString(x)); |
| + |
| + x = makeR(); |
| + // The toString calls (interpolation) force Rascal to be distinguished. |
|
Siggi Cherem (dart-lang)
2015/09/22 16:51:23
maybe also make a note here that this is because R
sra1
2015/09/23 03:58:21
Done.
|
| + print('R $x ${Error.safeToString(x)}'); |
| + Expect.notEquals(plainJsString, Error.safeToString(x)); |
| + Expect.notEquals(unknownJsString, Error.safeToString(x)); |
| + Expect.notEquals(interceptorString, Error.safeToString(x)); |
| + // And not the native class constructor. |
| + Expect.notEquals("Instance of 'RR'", Error.safeToString(x)); |
| + expectTypeName('Rascal', Error.safeToString(x)); |
| +} |
| + |
| +main() { |
| + setup(); |
| + |
| + testDistinctInterceptors(); |
| + testExternal(); |
| + testNative(); |
| +} |