mjsunit-assertion-error.js 2.2 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
// Copyright 2017 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.

let fileName = 'mjsunit-assertion-error.js';

function addDefaultFrames(frameExpectations) {
  // The last frame contains the error instantiation.
  frameExpectations.unshift('assertTrue.*mjsunit.js');
  // Frist frame is the top-level script.
  frameExpectations.push(fileName);
}

function assertErrorMessage(frameExpectations, error) {
  let stack = error.stack.split("\n");
  let title = stack.shift();
  assertContains('MjsUnitAssertionError', title);
  addDefaultFrames(frameExpectations);
  // Add default frames to the expectations.
  assertEquals(frameExpectations.length, stack.length);
  for (let i = 0; i < stack.length; i++) {
    let frame = stack[i];
    let expectation = frameExpectations[i];
    assertTrue(frame.search(expectation) != -1,
        `Frame ${i}: Did not find '${expectation}' in '${frame}'`);
  }
}

// Toplevel
try {
  assertTrue(false);
} catch(e) {
  assertErrorMessage([], e);
}

// Single function.
function throwError() {
  assertTrue(false);
}
try {
  throwError();
  assertUnreachable();
} catch(e) {
  assertErrorMessage(['throwError'], e);
}

// Nested function.
function outer() {
  throwError();
}
try {
  outer();
  assertUnreachable();
} catch(e) {
  assertErrorMessage(['throwError', 'outer'], e);
}

// Test Array helper nesting
try {
  [1].map(throwError);
  assertUnreachable();
} catch(e) {
  assertErrorMessage(['throwError', 'Array.map'], e);
}
try {
  Array.prototype.map.call([1], throwError);
  assertUnreachable();
} catch(e) {
  assertErrorMessage(['throwError', 'Array.map'], e);
}

// Test eval
try {
  eval("assertTrue(false);");
  assertUnreachable();
} catch(e) {
  assertErrorMessage(['eval'], e);
}

(function testNestedEval() {
  try {
    eval("assertTrue(false);");
    assertUnreachable();
  } catch(e) {
    assertErrorMessage(['eval', 'testNestedEval'], e);
  }
})();


(function testConstructor() {
  class Failer {
    constructor() {
      assertTrue(false);
    }
  }
  try {
    new Failer();
    assertUnreachable();
  } catch(e) {
    assertErrorMessage(['new Failer', 'testConstructor'], e);
  }
})();