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 @Js("ArrayTest.Util") | 5 @Js("ArrayTest.Util") |
6 library js_array_test; | 6 library js_array_test; |
7 | 7 |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 | 9 |
10 import 'dart:js' as js; | 10 import 'dart:js' as js; |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 }, | 123 }, |
124 | 124 |
125 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. | 125 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
126 reduceRightSumDoubledElements: function(array) { | 126 reduceRightSumDoubledElements: function(array) { |
127 return array.reduceRight(function(previousValue, currentValue) { | 127 return array.reduceRight(function(previousValue, currentValue) { |
128 return previousValue + currentValue*2; | 128 return previousValue + currentValue*2; |
129 }, | 129 }, |
130 0); | 130 0); |
131 }, | 131 }, |
132 | 132 |
133 getOwnPropertyDescriptorJson: function(array, property) { | 133 getOwnPropertyDescriptor: function(array, property) { |
134 return JSON.stringify(Object.getOwnPropertyDescriptor(array, property)); | 134 return Object.getOwnPropertyDescriptor(array, property); |
135 }, | 135 }, |
136 | 136 |
137 setLength: function(array, len) { | 137 setLength: function(array, len) { |
138 return array.length = len; | 138 return array.length = len; |
139 }, | 139 }, |
140 | 140 |
141 getValue: function(obj, index) { | 141 getValue: function(obj, index) { |
142 return obj[index]; | 142 return obj[index]; |
143 }, | 143 }, |
144 | 144 |
145 setValue: function(obj, index, value) { | 145 setValue: function(obj, index, value) { |
146 return obj[index] = value; | 146 return obj[index] = value; |
147 }, | 147 }, |
148 | 148 |
149 // Calling a method from Dart List on an arbitrary target object. | 149 // Calling a method from Dart List on an arbitrary target object. |
150 callListMethodOnTarget: function(dartArray, target, methodName, args) { | 150 callListMethodOnTarget: function(dartArray, target, methodName, args) { |
151 return dartArray[methodName].apply(target, args); | 151 return dartArray[methodName].apply(target, args); |
152 }, | 152 }, |
153 | 153 |
154 newArray: function() { return []; }, | 154 newArray: function() { return []; }, |
155 | 155 |
156 newLiteral: function() { return {}; }, | 156 newLiteral: function() { return {}; }, |
157 | 157 |
158 }; | 158 }; |
159 """); | 159 """); |
160 } | 160 } |
161 | 161 |
162 @Js() | 162 @Js() |
| 163 class PropertyDescriptor { |
| 164 external get value; |
| 165 external bool get writable; |
| 166 external bool get enumerable; |
| 167 external bool get configurable; |
| 168 } |
| 169 |
| 170 @Js() |
163 class SimpleJsLiteralClass { | 171 class SimpleJsLiteralClass { |
164 external get foo; | 172 external get foo; |
165 } | 173 } |
166 | 174 |
167 class Foo {} | 175 class Foo {} |
168 | 176 |
169 @Js() | 177 @Js() |
170 external callJsMethod(List array, String methodName, List args); | 178 external callJsMethod(List array, String methodName, List args); |
171 | 179 |
172 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); | 180 callIndexOf(List array, value) => callJsMethod(array, "indexOf", [value]); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 @Js() | 231 @Js() |
224 external mapAddIndexToEachElement(List array); | 232 external mapAddIndexToEachElement(List array); |
225 @Js() | 233 @Js() |
226 external reduceSumDoubledElements(List array); | 234 external reduceSumDoubledElements(List array); |
227 | 235 |
228 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. | 236 // TODO(jacobr): add a test that distinguishes reduce from reduceRight. |
229 @Js() | 237 @Js() |
230 external reduceRightSumDoubledElements(List array); | 238 external reduceRightSumDoubledElements(List array); |
231 | 239 |
232 @Js() | 240 @Js() |
233 external getOwnPropertyDescriptorJson(List array, property); | 241 external PropertyDescriptor getOwnPropertyDescriptor(obj, property); |
234 | 242 |
235 @Js("setLength") | 243 @Js("setLength") |
236 external callSetLength(List array, length); | 244 external callSetLength(List array, length); |
237 | 245 |
238 @Js() | 246 @Js() |
239 external getValue(obj, index); | 247 external getValue(obj, index); |
240 | 248 |
241 @Js() | 249 @Js() |
242 external setValue(obj, index, value); | 250 external setValue(obj, index, value); |
243 | 251 |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 | 500 |
493 test("is array", () { | 501 test("is array", () { |
494 var list = ["a", "b"]; | 502 var list = ["a", "b"]; |
495 expect(checkIsArray(list), isTrue); | 503 expect(checkIsArray(list), isTrue); |
496 }); | 504 }); |
497 | 505 |
498 test("property descriptors", () { | 506 test("property descriptors", () { |
499 // This test matters to make behavior consistent with JS native arrays | 507 // This test matters to make behavior consistent with JS native arrays |
500 // and to make devtools integration work well. | 508 // and to make devtools integration work well. |
501 var list = ["a", "b"]; | 509 var list = ["a", "b"]; |
502 expect(getOwnPropertyDescriptorJson(list, 0), | 510 var descriptor = getOwnPropertyDescriptor(list, 0); |
503 equals('{"value":"a",' | |
504 '"writable":true,' | |
505 '"enumerable":true,' | |
506 '"configurable":true}')); | |
507 | 511 |
508 expect( | 512 expect(descriptor.value, equals("a")); |
509 getOwnPropertyDescriptorJson(list, "length"), | 513 expect(descriptor.writable, isTrue); |
510 equals('{"value":2,' | 514 expect(descriptor.enumerable, isTrue); |
511 '"writable":true,' | 515 expect(descriptor.configurable, isTrue); |
512 '"enumerable":false,' | 516 |
513 '"configurable":false}')); | 517 descriptor = getOwnPropertyDescriptor(list, "length"); |
| 518 expect(descriptor.value, equals(2)); |
| 519 expect(descriptor.writable, isTrue); |
| 520 expect(descriptor.enumerable, isFalse); |
| 521 expect(descriptor.configurable, isFalse); |
514 }); | 522 }); |
515 | 523 |
516 test("concat js arrays", () { | 524 test("concat js arrays", () { |
517 var list = ["1", "2"]; | 525 var list = ["1", "2"]; |
518 // Tests that calling the concat method from JS will flatten out JS arrays | 526 // Tests that calling the concat method from JS will flatten out JS arrays |
519 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} | 527 // We concat the array with "a", "b", ["c", "d"], 42, {foo: 10} |
520 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] | 528 // which should generate ["1", "2", "a", "b", ["c", "d"], 42, {foo: 10}] |
521 var ret = concatValues(list); | 529 var ret = concatValues(list); |
522 expect(list.length, equals(2)); | 530 expect(list.length, equals(2)); |
523 expect(ret.length, equals(8)); | 531 expect(ret.length, equals(8)); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 test('opaque proxy', () { | 681 test('opaque proxy', () { |
674 // Dartium could easily support making LinkedList and all other classes | 682 // Dartium could easily support making LinkedList and all other classes |
675 // implementing List behave like a JavaScript array but that would | 683 // implementing List behave like a JavaScript array but that would |
676 // be challenging to implement in dart2js until browsers support ES6. | 684 // be challenging to implement in dart2js until browsers support ES6. |
677 var list = ["a", "b", "c", "d"]; | 685 var list = ["a", "b", "c", "d"]; |
678 var listView = new UnmodifiableListView(list.getRange(1,3)); | 686 var listView = new UnmodifiableListView(list.getRange(1,3)); |
679 expect(listView is List, isTrue); | 687 expect(listView is List, isTrue); |
680 expect(listView.length, equals(2)); | 688 expect(listView.length, equals(2)); |
681 expect(checkIsArray(listView), isFalse); | 689 expect(checkIsArray(listView), isFalse); |
682 expect(checkIsArray(listView.toList()), isTrue); | 690 expect(checkIsArray(listView.toList()), isTrue); |
683 expect(getOwnPropertyDescriptorJson( | 691 expect(getOwnPropertyDescriptor( |
684 listView, "length"), equals("null")); | 692 listView, "length"), equals(null)); |
685 }); | 693 }); |
686 }); | 694 }); |
687 */ | 695 */ |
688 } | 696 } |
OLD | NEW |