/ src / scripts / javascript_unit_test.js
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 */
/**
 * All the javascript unit test results collected while a test page runs.
 * The command line test runner reads this list back under node to report
 * each case on its own, so it lives at the top level of the page rather
 * than inside the report function below.
 * @type Array
 */
var unit_test_results = [];
/**
 * Draws a table of javascript unit test results and remembers them so the
 * command line runner can read them back under node. Each result is an
 * object with a name and a pass flag. In a browser the table shows a
 * summary row counting how many of the cases passed followed by one row
 * per case, coloured green when the case passed and red when it did not.
 *
 * @param String container_id id of the element the table is added under
 * @param String test_name name shown for this group of cases
 * @param Array results objects that each have a name string and a pass
 *      boolean
 */
function reportUnitTestResults(container_id, test_name, results)
{
    unit_test_results = unit_test_results.concat(results);
    let passed = 0;
    for (let result of results) {
        if (result.pass) {
            passed++;
        }
    }
    let table = ce("table");
    table.className = "wikitable";
    let summary_row = table.insertRow(-1);
    let name_head = ce("th");
    name_head.innerHTML = test_name;
    summary_row.appendChild(name_head);
    let count_head = ce("th");
    count_head.className = (passed === results.length) ?
        "back-light-green" : "back-red";
    count_head.innerHTML = passed + "/" + results.length + " Test Passed";
    summary_row.appendChild(count_head);
    for (let result of results) {
        let row = table.insertRow(-1);
        let name_cell = row.insertCell(-1);
        name_cell.innerHTML = result.name;
        let status_cell = row.insertCell(-1);
        status_cell.className = result.pass ?
            "back-light-green" : "back-red";
        status_cell.innerHTML = result.pass ? "passed" : "failed";
    }
    let container = elt(container_id);
    if (container) {
        container.appendChild(table);
    }
}
X