aggregate-error.js 5.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function TestNoParameters() {
    // Can't omit the "errors" parameter; there's nothing to iterate.
    assertThrows(() => { new AggregateError(); });
})();

(function TestNoParameters_NoNew() {
    // Can't omit the "errors" parameter; there's nothing to iterate.
    assertThrows(() => { AggregateError(); });
})();

(function TestOneParameterErrorsIsArray() {
    let error = new AggregateError([1, 20, 4]);
    assertEquals('', error.message);
    assertEquals([1, 20, 4], error.errors);
})();

(function TestOneParameterErrorsIsArray_NoNew() {
    let error = AggregateError([1, 20, 4]);
    assertEquals('', error.message);
    assertEquals([1, 20, 4], error.errors);
})();

(function TestOneParameterErrosIsAnEmptyArray() {
    let error = new AggregateError([]);
    assertEquals('', error.message);
    assertEquals([], error.errors);
})();

(function TestOneParameterErrorsIsASet() {
    let set = new Set();
    set.add(5);
    set.add(100);
    let error = new AggregateError(set);
    assertEquals('', error.message);
    assertEquals(2, error.errors.length);
    assertTrue(error.errors[0] == 5 || error.errors[1] == 5);
    assertTrue(error.errors[0] == 100 || error.errors[1] == 100);
})();

(function TestOneParameterErrorsNotIterable() {
    assertThrows(() => { new AggregateError(5); });
})();

(function TestOneParameterErrorsNotIterable_NoNew() {
    assertThrows(() => { AggregateError(5); });
})();

(function TestTwoParameters() {
    let error = new AggregateError([1, 20, 4], 'custom message');
    assertEquals('custom message', error.message);
    assertEquals([1, 20, 4], error.errors);
})();

(function TestTwoParameters_NoNew() {
    let error = AggregateError([1, 20, 4], 'custom message');
    assertEquals('custom message', error.message);
    assertEquals([1, 20, 4], error.errors);
})();

(function TestTwoParametersMessageNotString() {
    let custom = { toString() { return 'hello'; } };
    let error = new AggregateError([], custom);
    assertEquals('hello', error.message);
})();

70 71 72 73 74
(function TestTwoParametersMessageIsSMI() {
    let error = new AggregateError([], 44);
    assertEquals('44', error.message);
})();

75 76 77 78 79
(function TestTwoParametersMessageUndefined() {
    let error = new AggregateError([], undefined);
    assertFalse(Object.prototype.hasOwnProperty.call(error, 'message'));
})();

80
(function SetErrors() {
81 82
  let e = new AggregateError([1]);
  e.errors = [4, 5, 6];
83
  assertEquals([4, 5, 6], e.errors);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
})();

(function SubClassProto() {
    class A extends AggregateError {
        constructor() {
            super([]);
        }
    }

    let o = new A();
    assertEquals(o.__proto__, A.prototype);
})();

(function ErrorsWithHoles() {
    let errors = [0];
    errors[2] = 2;
    let a = new AggregateError(errors);
    assertEquals([0, undefined, 2], a.errors);
})();

(function ErrorsIsANewArray(){
  let array = [8, 9];
  let e = new AggregateError(array);
  array.push(1);
  assertEquals([8, 9], e.errors);
})();

111
(function ErrorsIsTheSameArray(){
112 113 114
    let e = new AggregateError([9, 6, 3]);
    const errors1 = e.errors;
    const errors2 = e.errors;
115
    assertSame(errors1, errors2);
116 117
})();

118 119 120 121 122 123
(function ErrorsModified(){
    let e = new AggregateError([9, 6, 3]);
    const errors1 = e.errors;
    errors1[0] = 50;
    const errors2 = e.errors;
    assertEquals([50, 6, 3], errors1);
124
    assertEquals([50, 6, 3], errors2);
125 126 127 128 129 130 131 132
})();

(function EmptyErrorsModified1(){
    let e = new AggregateError([]);
    const errors1 = e.errors;
    errors1[0] = 50;
    const errors2 = e.errors;
    assertEquals([50], errors1);
133
    assertEquals([50], errors2);
134 135 136 137 138 139 140 141
})();

(function EmptyErrorsModified2(){
    let e = new AggregateError([]);
    const errors1 = e.errors;
    errors1.push(50);
    const errors2 = e.errors;
    assertEquals([50], errors1);
142
    assertEquals([50], errors2);
143 144
})();

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
(function AggregateErrorCreation() {
    // Verify that we match the spec wrt getting the prototype from the
    // newTarget, iterating the errors array and calling toString on the
    // message.
    let counter = 1;
    let prototype_got = 0;
    let errors_iterated = 0;
    let to_string_called = 0;

    // For observing Get(new target, "prototype")
    function target() {}
    let handler = {
        get: (target, prop, receiver) => {
          if (prop == 'prototype') {
            prototype_got = counter++;
            return target.prototype;
          }
        }
    };
    let p = new Proxy(target, handler);

    // For observing IterableToList(errors)
    var errors = {
      [Symbol.iterator]() {
        return {
          next() {
            errors_iterated = counter++;
            return { done: true };
          }
        };
      }
    };

    // For observing ToString(message)
    let message = { toString: () => { to_string_called = counter++;}}

    let o = Reflect.construct(AggregateError, [errors, message], p);

    assertEquals(1, prototype_got);
    assertEquals(2, to_string_called);
    assertEquals(3, errors_iterated);
 })();
187 188 189 190 191 192 193 194

(function TestErrorsProperties() {
    let error = new AggregateError([1, 20, 4]);
    let desc = Object.getOwnPropertyDescriptor(error, 'errors');
    assertEquals(true, desc.configurable);
    assertEquals(false, desc.enumerable);
    assertEquals(true, desc.writable);
})();