jsapi-harness.js 4.21 KB
Newer Older
1 2 3
// 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.
4
//
5 6
// TODO(eholk): Once we have stable test IDs, use those as the key instead.
// See https://github.com/WebAssembly/spec/issues/415
7 8 9
//
// Flags: --expose-wasm --allow-natives-syntax

10
const known_failures = {
11 12 13
  // Enter failing tests like follows:
  // "'WebAssembly.Instance.prototype.exports' accessor property":
  //  'https://bugs.chromium.org/p/v8/issues/detail?id=5507',
14 15 16
};

let failures = [];
17
let unexpected_successes = [];
18 19 20 21 22 23 24 25

let last_promise = new Promise((resolve, reject) => { resolve(); });

function test(func, description) {
  let maybeErr;
  try { func(); }
  catch(e) { maybeErr = e; }
  if (typeof maybeErr !== 'undefined') {
26 27 28 29 30
    var known = "";
    if (known_failures[description]) {
      known = " (known)";
    }
    print(`${description}: FAIL${known}. ${maybeErr}`);
31 32
    failures.push(description);
  } else {
33 34 35
    if (known_failures[description]) {
      unexpected_successes.push(description);
    }
36 37 38 39 40 41
    print(`${description}: PASS.`);
  }
}

function promise_test(func, description) {
  last_promise = last_promise.then(func)
42 43 44 45 46 47
  .then(_ => {
    if (known_failures[description]) {
      unexpected_successes.push(description);
    }
    print(`${description}: PASS.`);
  })
48
  .catch(err => {
49 50 51 52 53
    var known = "";
    if (known_failures[description]) {
      known = " (known)";
    }
    print(`${description}: FAIL${known}. ${err}`);
54 55 56 57 58 59 60
    failures.push(description);
  });
}

let assert_true = assertEquals.bind(null, true);
let assert_false = assertEquals.bind(null, false);

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
function same_value(x, y) {
  if (y !== y) {
    // NaN case
    return x!==x;
  }
  if (x === 0 && y === 0) {
    // Distinguish +0 and -0
    return 1/x === 1/y;
  }
  return x === y;
}

let assert_equals = function(expected, found, description) {
  if (typeof found != typeof expected) {
    assert_true(false, "assert_equals", description,
        "expected (" + typeof expected + ") ${expected} but got (" +
        typeof found + ") ${found}", {expected:expected, found:found});
  }
  assert_true(same_value(found, expected), "assert_equals", description,
      "expected ${expected} but got ${found}",
      {expected:expected, found:found});
}

let assert_not_equals = function(expected, found, description) {
  assert_true(!same_value(found, expected), "assert_not_equals", description,
      "got disallowed value ${found}", {found:found});
}

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
function assert_unreached(description) {
  throw new Error(`unreachable:\n${description}`);
}

function assertErrorMessage(f, ctor, test) {
  try { f(); }
  catch (e) {
    assert_true(e instanceof ctor, "expected exception " + ctor.name + ", got " + e);
    return;
  }
  assert_true(false, "expected exception " + ctor.name + ", no exception thrown");
};

load("test/wasm-js/test/harness/wasm-constants.js");
load("test/wasm-js/test/harness/wasm-module-builder.js");
load("test/wasm-js/test/js-api/jsapi.js");

106 107 108 109 110 111 112 113 114
assertPromiseResult(last_promise, _ => {
  if (failures.length > 0) {
    let unexpected = false;
    print("Some tests FAILED:");
    for (let i in failures) {
      if (known_failures[failures[i]]) {
        print(`  ${failures[i]} [KNOWN: ${known_failures[failures[i]]}]`);
      } else {
        print(`  ${failures[i]}`);
115 116
        unexpected = true;
      }
117 118 119 120 121 122 123
    }
    if (unexpected_successes.length > 0) {
      unexpected = true;
      print("");
      print("Unexpected successes:");
      for(let i in unexpected_successes) {
        print(`  ${unexpected_successes[i]}`);
124
      }
125 126
      print("Some tests SUCCEEDED but were known failures. If you've fixed " +
            "the bug, please remove the test from the known failures list.")
127
    }
128 129 130 131 132 133 134 135 136 137 138 139
    if (unexpected) {
      print("\n");
      print("   #############################################################");
      print("   #                                                           #");
      print("   # Unexpected outcome. Did you forget to run 'gclient sync'? #");
      print("   #                                                           #");
      print("   #############################################################");
      print("\n");
      assertUnreachable("Unexpected outcome");
    }
  }
});