parser/htmlparser/tests/mochitest/parser_web_testrunner.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/htmlparser/tests/mochitest/parser_web_testrunner.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 + 
     1.9 +/**
    1.10 + * Runs html5lib-formatted test cases in the browser. Requires SimpleTest.
    1.11 + *
    1.12 + * Define an array named parserDatFiles before loading this script,
    1.13 + * and it will load each of those dat files into an array, then run
    1.14 + * the test parser on each and run the tests by assigning the input
    1.15 + * data to an iframe's url.
    1.16 + *
    1.17 + * Your test document should have an element with id "display" and
    1.18 + * an iframe with id "testframe".
    1.19 + */
    1.20 +
    1.21 +var functionsToRunAsync = [];
    1.22 +
    1.23 +window.addEventListener("message", function(event) {
    1.24 +  if (event.source == window && event.data == "async-run") {
    1.25 +    event.stopPropagation();
    1.26 +    var fn = functionsToRunAsync.shift();
    1.27 +    fn();
    1.28 +  }
    1.29 +}, true);
    1.30 +
    1.31 +function asyncRun(fn) {
    1.32 +  functionsToRunAsync.push(fn);
    1.33 +  window.postMessage("async-run", "*");
    1.34 +}
    1.35 +
    1.36 +function writeErrorSummary(input, expected, got, isTodo) {
    1.37 +  if (isTodo) {
    1.38 +    $("display").appendChild(createEl('h2', null, "Unexpected Success:"));
    1.39 +  } else {
    1.40 +    $("display").appendChild(createEl('h2', null, "Unexpected Failure:"));
    1.41 +  }
    1.42 +  $("display").appendChild(createEl('br'));
    1.43 +  $("display").appendChild(createEl('span', null, "Matched: "));
    1.44 +  $("display").appendChild(document.createTextNode("" + (expected == got)));
    1.45 +  var pre = createEl('pre');
    1.46 +  pre.appendChild(document.createTextNode("Input: \n" + input, "\n-\n"));
    1.47 +  pre.appendChild(document.createTextNode("Expected:\n" + expected, "\n-\n"));
    1.48 +  pre.appendChild(document.createTextNode("Output:\n" + got + "\n-\n"));
    1.49 +  $("display").appendChild(pre);
    1.50 +  $("display").appendChild(createEl('hr'));
    1.51 +}
    1.52 +
    1.53 +/**
    1.54 + * Control will bounce back and forth between nextTest() and the
    1.55 + * event handler returned by makeTestChecker() or the callback returned by
    1.56 + * makeFragmentTestChecker() until the 'testcases' iterator is spent.
    1.57 + */
    1.58 +function makeTestChecker(input, expected, errors) {
    1.59 +  return function (e) {
    1.60 +    var domAsString = docToTestOutput(e.target.contentDocument);
    1.61 +    if (html5Exceptions[input]) {
    1.62 +      todo_is(domAsString, expected, "HTML5 expected success.");
    1.63 +      if (domAsString == expected) {
    1.64 +        writeErrorSummary(input, expected, domAsString, true);
    1.65 +      }
    1.66 +    } else {
    1.67 +      is(domAsString, expected, "HTML5 expected success.");
    1.68 +      if (domAsString != expected) {
    1.69 +        writeErrorSummary(input, expected, domAsString, false);
    1.70 +      }
    1.71 +    }
    1.72 +    nextTest(e.target);
    1.73 +  } 
    1.74 +}
    1.75 +
    1.76 +function makeFragmentTestChecker(input, 
    1.77 +                                 expected, 
    1.78 +                                 errors, 
    1.79 +                                 fragment, 
    1.80 +                                 testframe) {
    1.81 +  return function () {
    1.82 +    var context = document.createElementNS("http://www.w3.org/1999/xhtml",
    1.83 +                                           fragment);
    1.84 +    context.innerHTML = input;
    1.85 +    var domAsString = fragmentToTestOutput(context);
    1.86 +    is(domAsString, expected, "HTML5 expected success. " + new Date());
    1.87 +    if (domAsString != expected) {
    1.88 +      writeErrorSummary(input, expected, domAsString, false);
    1.89 +    }
    1.90 +    nextTest(testframe);
    1.91 +  } 
    1.92 +}
    1.93 +
    1.94 +var testcases;
    1.95 +function nextTest(testframe) {
    1.96 +  var test = 0;
    1.97 +  try {
    1.98 +    var [input, output, errors, fragment] = testcases.next();
    1.99 +    if (fragment) {
   1.100 +      asyncRun(makeFragmentTestChecker(input, 
   1.101 +                                       output, 
   1.102 +                                       errors, 
   1.103 +                                       fragment, 
   1.104 +                                       testframe));
   1.105 +    } else {
   1.106 +      dataURL = "data:text/html;charset=utf-8," + encodeURIComponent(input);
   1.107 +      testframe.onload = makeTestChecker(input, output, errors);
   1.108 +      testframe.src = dataURL;
   1.109 +    }
   1.110 +  } catch (err if err instanceof StopIteration) {
   1.111 +    SimpleTest.finish();
   1.112 +  }
   1.113 +}
   1.114 +
   1.115 +var testFileContents = [];
   1.116 +function loadNextTestFile() {
   1.117 +  var datFile = parserDatFiles.shift();
   1.118 +  if (datFile) {
   1.119 +    var xhr = new XMLHttpRequest();
   1.120 +    xhr.onreadystatechange = function () {
   1.121 +      if (this.readyState == 4) {
   1.122 +        testFileContents.push(this.responseText);
   1.123 +        loadNextTestFile();
   1.124 +      }
   1.125 +    };
   1.126 +    xhr.open("GET", "html5lib_tree_construction/" + datFile);
   1.127 +    xhr.send();
   1.128 +  } else {
   1.129 +    testcases = test_parser(testFileContents);
   1.130 +    nextTest($("testframe"));
   1.131 +  }
   1.132 +}
   1.133 +
   1.134 +addLoadEvent(loadNextTestFile);
   1.135 +SimpleTest.waitForExplicitFinish();

mercurial