OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --expose-debug-as debug --harmony-promises | 5 // Flags: --expose-debug-as debug --harmony-promises |
6 // Test the mirror object for promises. | 6 // Test the mirror object for promises. |
7 | 7 |
8 function MirrorRefCache(json_refs) { | 8 function MirrorRefCache(json_refs) { |
9 var tmp = eval('(' + json_refs + ')'); | 9 var tmp = eval('(' + json_refs + ')'); |
10 this.refs_ = []; | 10 this.refs_ = []; |
11 for (var i = 0; i < tmp.length; i++) { | 11 for (var i = 0; i < tmp.length; i++) { |
12 this.refs_[tmp[i].handle] = tmp[i]; | 12 this.refs_[tmp[i].handle] = tmp[i]; |
13 } | 13 } |
14 } | 14 } |
15 | 15 |
16 MirrorRefCache.prototype.lookup = function(handle) { | 16 MirrorRefCache.prototype.lookup = function(handle) { |
17 return this.refs_[handle]; | 17 return this.refs_[handle]; |
18 } | 18 } |
19 | 19 |
20 function testPromiseMirror(promise, status) { | 20 function testPromiseMirror(promise, status, value) { |
21 // Create mirror and JSON representation. | 21 // Create mirror and JSON representation. |
22 var mirror = debug.MakeMirror(promise); | 22 var mirror = debug.MakeMirror(promise); |
23 var serializer = debug.MakeMirrorSerializer(); | 23 var serializer = debug.MakeMirrorSerializer(); |
24 var json = JSON.stringify(serializer.serializeValue(mirror)); | 24 var json = JSON.stringify(serializer.serializeValue(mirror)); |
25 var refs = new MirrorRefCache( | 25 var refs = new MirrorRefCache( |
26 JSON.stringify(serializer.serializeReferencedObjects())); | 26 JSON.stringify(serializer.serializeReferencedObjects())); |
27 | 27 |
28 // Check the mirror hierachy. | 28 // Check the mirror hierachy. |
29 assertTrue(mirror instanceof debug.Mirror); | 29 assertTrue(mirror instanceof debug.Mirror); |
30 assertTrue(mirror instanceof debug.ValueMirror); | 30 assertTrue(mirror instanceof debug.ValueMirror); |
31 assertTrue(mirror instanceof debug.ObjectMirror); | 31 assertTrue(mirror instanceof debug.ObjectMirror); |
32 assertTrue(mirror instanceof debug.PromiseMirror); | 32 assertTrue(mirror instanceof debug.PromiseMirror); |
33 | 33 |
34 // Check the mirror properties. | 34 // Check the mirror properties. |
35 assertEquals(status, mirror.status()); | 35 assertEquals(status, mirror.status()); |
36 assertTrue(mirror.isPromise()); | 36 assertTrue(mirror.isPromise()); |
37 assertEquals('promise', mirror.type()); | 37 assertEquals('promise', mirror.type()); |
38 assertFalse(mirror.isPrimitive()); | 38 assertFalse(mirror.isPrimitive()); |
39 assertEquals("Object", mirror.className()); | 39 assertEquals("Object", mirror.className()); |
40 assertEquals("#<Promise>", mirror.toText()); | 40 assertEquals("#<Promise>", mirror.toText()); |
41 assertSame(promise, mirror.value()); | 41 assertSame(promise, mirror.value()); |
| 42 assertEquals(value, mirror.promiseValue()); |
42 | 43 |
43 // Parse JSON representation and check. | 44 // Parse JSON representation and check. |
44 var fromJSON = eval('(' + json + ')'); | 45 var fromJSON = eval('(' + json + ')'); |
45 assertEquals('promise', fromJSON.type); | 46 assertEquals('promise', fromJSON.type); |
46 assertEquals('Object', fromJSON.className); | 47 assertEquals('Object', fromJSON.className); |
47 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type); | 48 assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type); |
48 assertEquals('Promise', refs.lookup(fromJSON.constructorFunction.ref).name); | 49 assertEquals('Promise', refs.lookup(fromJSON.constructorFunction.ref).name); |
49 assertEquals(status, fromJSON.status); | 50 assertEquals(status, fromJSON.status); |
50 | 51 assertEquals(value, fromJSON.promiseValue); |
51 } | 52 } |
52 | 53 |
53 // Test a number of different promises. | 54 // Test a number of different promises. |
54 var resolved = new Promise(function(resolve, reject) { resolve() }); | 55 var resolved = new Promise(function(resolve, reject) { resolve() }); |
55 var rejected = new Promise(function(resolve, reject) { reject() }); | 56 var rejected = new Promise(function(resolve, reject) { reject() }); |
56 var pending = new Promise(function(resolve, reject) {}); | 57 var pending = new Promise(function(resolve, reject) {}); |
57 | 58 |
58 testPromiseMirror(resolved, "resolved"); | 59 testPromiseMirror(resolved, "resolved", undefined); |
59 testPromiseMirror(rejected, "rejected"); | 60 testPromiseMirror(rejected, "rejected", undefined); |
60 testPromiseMirror(pending, "pending"); | 61 testPromiseMirror(pending, "pending", undefined); |
| 62 |
| 63 var resolvedv = new Promise(function(resolve, reject) { resolve('resolve') }); |
| 64 var rejectedv = new Promise(function(resolve, reject) { reject('reject') }); |
| 65 var thrownv = new Promise(function(resolve, reject) { throw 'throw' }); |
| 66 |
| 67 testPromiseMirror(resolvedv, "resolved", 'resolve'); |
| 68 testPromiseMirror(rejectedv, "rejected", 'reject'); |
| 69 testPromiseMirror(thrownv, "rejected", 'throw'); |
OLD | NEW |