Home | History | Annotate | Download | only in es6
      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
      3 // found in the LICENSE file.
      4 
      5 // Flags: --expose-debug-as debug
      6 // Test the mirror object for promises.
      7 
      8 function MirrorRefCache(json_refs) {
      9   var tmp = eval('(' + json_refs + ')');
     10   this.refs_ = [];
     11   for (var i = 0; i < tmp.length; i++) {
     12     this.refs_[tmp[i].handle] = tmp[i];
     13   }
     14 }
     15 
     16 MirrorRefCache.prototype.lookup = function(handle) {
     17   return this.refs_[handle];
     18 }
     19 
     20 function testPromiseMirror(promise, status, value) {
     21   // Create mirror and JSON representation.
     22   var mirror = debug.MakeMirror(promise);
     23   var serializer = debug.MakeMirrorSerializer();
     24   var json = JSON.stringify(serializer.serializeValue(mirror));
     25   var refs = new MirrorRefCache(
     26       JSON.stringify(serializer.serializeReferencedObjects()));
     27 
     28   // Check the mirror hierachy.
     29   assertTrue(mirror instanceof debug.Mirror);
     30   assertTrue(mirror instanceof debug.ValueMirror);
     31   assertTrue(mirror instanceof debug.ObjectMirror);
     32   assertTrue(mirror instanceof debug.PromiseMirror);
     33 
     34   // Check the mirror properties.
     35   assertEquals(status, mirror.status());
     36   assertTrue(mirror.isPromise());
     37   assertEquals('promise', mirror.type());
     38   assertFalse(mirror.isPrimitive());
     39   assertEquals("Object", mirror.className());
     40   assertEquals("#<Promise>", mirror.toText());
     41   assertSame(promise, mirror.value());
     42   assertTrue(mirror.promiseValue() instanceof debug.Mirror);
     43   assertEquals(value, mirror.promiseValue().value());
     44 
     45   // Parse JSON representation and check.
     46   var fromJSON = eval('(' + json + ')');
     47   assertEquals('promise', fromJSON.type);
     48   assertEquals('Object', fromJSON.className);
     49   assertEquals('function', refs.lookup(fromJSON.constructorFunction.ref).type);
     50   assertEquals('Promise', refs.lookup(fromJSON.constructorFunction.ref).name);
     51   assertEquals(status, fromJSON.status);
     52   assertEquals(value, refs.lookup(fromJSON.promiseValue.ref).value);
     53 }
     54 
     55 // Test a number of different promises.
     56 var resolved = new Promise(function(resolve, reject) { resolve() });
     57 var rejected = new Promise(function(resolve, reject) { reject() });
     58 var pending = new Promise(function(resolve, reject) {});
     59 
     60 testPromiseMirror(resolved, "resolved", undefined);
     61 testPromiseMirror(rejected, "rejected", undefined);
     62 testPromiseMirror(pending, "pending", undefined);
     63 
     64 var resolvedv = new Promise(function(resolve, reject) { resolve('resolve') });
     65 var rejectedv = new Promise(function(resolve, reject) { reject('reject') });
     66 var thrownv = new Promise(function(resolve, reject) { throw 'throw' });
     67 
     68 testPromiseMirror(resolvedv, "resolved", 'resolve');
     69 testPromiseMirror(rejectedv, "rejected", 'reject');
     70 testPromiseMirror(thrownv, "rejected", 'throw');
     71 
     72 // Test internal properties of different promises.
     73 var m1 = debug.MakeMirror(new Promise(
     74     function(resolve, reject) { resolve(1) }));
     75 var ip = m1.internalProperties();
     76 assertEquals(2, ip.length);
     77 assertEquals("[[PromiseStatus]]", ip[0].name());
     78 assertEquals("[[PromiseValue]]", ip[1].name());
     79 assertEquals("resolved", ip[0].value().value());
     80 assertEquals(1, ip[1].value().value());
     81 
     82 var m2 = debug.MakeMirror(new Promise(function(resolve, reject) { reject(2) }));
     83 ip = m2.internalProperties();
     84 assertEquals("rejected", ip[0].value().value());
     85 assertEquals(2, ip[1].value().value());
     86 
     87 var m3 = debug.MakeMirror(new Promise(function(resolve, reject) { }));
     88 ip = m3.internalProperties();
     89 assertEquals("pending", ip[0].value().value());
     90 assertEquals("undefined", typeof(ip[1].value().value()));
     91