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 import "package:expect/expect.dart"; | |
6 import 'dart:_foreign_helper' show JS_INTERCEPTOR_CONSTANT, JS; | |
7 import 'dart:_js_helper' show Native, Creates; | |
8 import 'dart:_interceptors' show | |
9 Interceptor, | |
10 JavaScriptObject, | |
11 PlainJavaScriptObject, | |
12 UnknownJavaScriptObject; | |
13 | |
14 // Test for safe formatting of JavaScript objects by Error.safeToString. | |
15 | |
16 @Native('PP') | |
17 class Purple {} | |
18 | |
19 @Native('QQ') | |
20 class Q {} | |
21 | |
22 @Native('RR') | |
23 class Rascal { | |
24 toString() => 'RRRR'; | |
25 } | |
26 | |
27 makeA() native; | |
28 makeB() native; | |
29 makeC() native; | |
30 makeD() native; | |
31 makeE() native; | |
32 makeP() native; | |
33 makeQ() native; | |
34 makeR() native; | |
35 | |
36 void setup() native r""" | |
37 makeA = function(){return {hello: 123};}; | |
38 | |
39 function BB(){} | |
40 makeB = function(){return new BB();}; | |
41 | |
42 function CC(){} | |
43 makeC = function(){ | |
44 var x = new CC(); | |
45 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
| |
46 return x; | |
47 }; | |
48 | |
49 function DD(){} | |
50 makeD = function(){ | |
51 var x = new DD(); | |
52 x.constructor = {name: 'DDxxx'}; // Foils constructor lookup. | |
53 return x; | |
54 }; | |
55 | |
56 function EE(){} | |
57 makeE = function(){ | |
58 var x = new EE(); | |
59 x.constructor = function Liar(){}; // Foils constructor lookup. | |
60 return x; | |
61 }; | |
62 | |
63 function PP(){} | |
64 makeP = function(){return new PP();}; | |
65 | |
66 function QQ(){} | |
67 makeQ = function(){return new QQ();}; | |
68 | |
69 function RR(){} | |
70 makeR = function(){return new RR();}; | |
71 | |
72 """; | |
73 | |
74 | |
75 expectTypeName(expectedName, s) { | |
76 var m = new RegExp(r"Instance of '(.*)'").firstMatch(s); | |
77 Expect.isNotNull(m); | |
78 var name = m.group(1); | |
79 Expect.isTrue(expectedName == name || name.length <= 3, | |
80 "Is '$expectedName' or minified: '$name'"); | |
81 } | |
82 | |
83 final plainJsString = | |
84 Error.safeToString(JS_INTERCEPTOR_CONSTANT(PlainJavaScriptObject)); | |
85 | |
86 final unknownJsString = | |
87 Error.safeToString(JS_INTERCEPTOR_CONSTANT(UnknownJavaScriptObject)); | |
88 | |
89 final interceptorString = | |
90 Error.safeToString(JS_INTERCEPTOR_CONSTANT(Interceptor)); | |
91 | |
92 | |
93 testDistinctInterceptors() { | |
94 // Test invariants needed for the other tests. | |
95 | |
96 Expect.notEquals(plainJsString, unknownJsString); | |
97 Expect.notEquals(plainJsString, interceptorString); | |
98 Expect.notEquals(unknownJsString, interceptorString); | |
99 | |
100 expectTypeName('PlainJavaScriptObject', plainJsString); | |
101 expectTypeName('UnknownJavaScriptObject', unknownJsString); | |
102 expectTypeName('Interceptor', interceptorString); | |
103 | |
104 // Sometimes interceptor *objects* are used instead of the prototypes. Check | |
105 // these work too. | |
106 var plain2 = Error.safeToString(const PlainJavaScriptObject()); | |
107 Expect.equals(plainJsString, plain2); | |
108 | |
109 var unk2 = Error.safeToString(const UnknownJavaScriptObject()); | |
110 Expect.equals(unknownJsString, unk2); | |
111 } | |
112 | |
113 | |
114 testExternal() { | |
115 var x = makeA(); | |
116 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.
| |
117 Expect.equals(plainJsString, Error.safeToString(x)); | |
118 | |
119 x = makeB(); | |
120 print('B $x ${Error.safeToString(x)}'); | |
121 // Gets name from constructor, regardless of minification. | |
122 Expect.equals("Instance of 'BB'", Error.safeToString(x)); | |
123 | |
124 x = makeC(); | |
125 print('C $x ${Error.safeToString(x)}'); | |
126 Expect.equals(unknownJsString, Error.safeToString(x)); | |
127 | |
128 x = makeD(); | |
129 print('D $x ${Error.safeToString(x)}'); | |
130 Expect.equals(unknownJsString, Error.safeToString(x)); | |
131 | |
132 x = makeE(); | |
133 print('D $x ${Error.safeToString(x)}'); | |
134 Expect.equals(unknownJsString, Error.safeToString(x)); | |
135 } | |
136 | |
137 testNative() { | |
138 var x = makeP(); | |
139 Expect.isTrue(x is Purple); // This test forces Purple to be distinguished. | |
140 print('P $x ${Error.safeToString(x)}'); | |
141 Expect.notEquals(plainJsString, Error.safeToString(x)); | |
142 Expect.notEquals(unknownJsString, Error.safeToString(x)); | |
143 Expect.notEquals(interceptorString, Error.safeToString(x)); | |
144 // And not the native class constructor. | |
145 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.
| |
146 expectTypeName('Purple', Error.safeToString(x)); | |
147 | |
148 x = makeQ(); | |
149 print('Q $x ${Error.safeToString(x)}'); | |
150 // We are going to get either the general interceptor | |
151 Expect.isTrue( | |
152 "Instance of 'QQ'" == Error.safeToString(x) || | |
153 interceptorString == Error.safeToString(x)); | |
154 | |
155 x = makeR(); | |
156 // 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.
| |
157 print('R $x ${Error.safeToString(x)}'); | |
158 Expect.notEquals(plainJsString, Error.safeToString(x)); | |
159 Expect.notEquals(unknownJsString, Error.safeToString(x)); | |
160 Expect.notEquals(interceptorString, Error.safeToString(x)); | |
161 // And not the native class constructor. | |
162 Expect.notEquals("Instance of 'RR'", Error.safeToString(x)); | |
163 expectTypeName('Rascal', Error.safeToString(x)); | |
164 } | |
165 | |
166 main() { | |
167 setup(); | |
168 | |
169 testDistinctInterceptors(); | |
170 testExternal(); | |
171 testNative(); | |
172 } | |
OLD | NEW |