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