Skip to content

Commit

Permalink
add web test runners (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 21, 2024
1 parent 07dfe35 commit 626b8ff
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
3 changes: 2 additions & 1 deletion c/src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "macros.h"
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
Expand Down Expand Up @@ -131,7 +132,7 @@ typedef struct {
AnswerType type;
} Answer;

Answer get_answer(uint16_t id) {
Answer EMSCRIPTEN_KEEPALIVE get_answer(uint16_t id) {
Answer ret = {
.id = id,
};
Expand Down
3 changes: 2 additions & 1 deletion cplusplus/src/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <sstream>
#include <fstream>
#include "macros.h"

#ifdef _WIN32
#include <windows.h>
Expand Down Expand Up @@ -100,7 +101,7 @@ typedef struct {
AnswerType type;
} Answer;

Answer get_answer(const uint16_t id) {
Answer EMSCRIPTEN_KEEPALIVE get_answer(const uint16_t id) {
Answer answer;
char c_id[6];
snprintf(c_id, sizeof(c_id), "%" PRIu16, id);
Expand Down
64 changes: 64 additions & 0 deletions docs/_static/test-c.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>C Tests (Packaged with emscripten)</title>
<link href="https://npmcdn.com/[email protected]/mocha.css" rel="stylesheet" />
<script src="https://npmcdn.com/[email protected]/mocha.js"></script>
</head>
<body>
<!-- A container element for the visual Mocha results -->
<div id="mocha"></div>
<!-- Mocha setup and initiation code -->
<script type="module">
import * as wasm from './dist/c-lib.js';
mocha.setup('bdd');
window.onload = function() {
init().then(() => {
for (let p = 1; p < 10000; p++) {
const formattedQuestion = `${p}`.padStart(4, '0');
const func = wasm[`_p${formattedQuestion}`];
if (func === undefined) continue;
const expected = wasm._get_answer(p);
describe(`run test ${p}`, function() {
this.timeout(Infinity);
it(`should return ${expected}`, async () => {
const answer = func();
console.log(p, answer, expected);
if (answer !== expected) {
throw new Error();
}
});
});
}
const runner = mocha.run();
let failedTests = [];

runner.on('end', function() {
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});

runner.on('fail', function(test, err){
const flattenTitles = function(test){
let titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};

failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
});
});
});
};
</script>
</body>
</html>
64 changes: 64 additions & 0 deletions docs/_static/test-cp.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>C++ Tests (Packaged with emscripten)</title>
<link href="https://npmcdn.com/[email protected]/mocha.css" rel="stylesheet" />
<script src="https://npmcdn.com/[email protected]/mocha.js"></script>
</head>
<body>
<!-- A container element for the visual Mocha results -->
<div id="mocha"></div>
<!-- Mocha setup and initiation code -->
<script type="module">
import * as wasm from './dist/cp-lib.js';
mocha.setup('bdd');
window.onload = function() {
init().then(() => {
for (let p = 1; p < 10000; p++) {
const formattedQuestion = `${p}`.padStart(4, '0');
const func = wasm[`_p${formattedQuestion}`];
if (func === undefined) continue;
const expected = wasm._get_answer(p);
describe(`run test ${p}`, function() {
this.timeout(Infinity);
it(`should return ${expected}`, async () => {
const answer = func();
console.log(p, answer, expected);
if (answer !== expected) {
throw new Error();
}
});
});
}
const runner = mocha.run();
let failedTests = [];

runner.on('end', function() {
window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests;
});

runner.on('fail', function(test, err){
const flattenTitles = function(test){
let titles = [];
while (test.parent.title){
titles.push(test.parent.title);
test = test.parent;
}
return titles.reverse();
};

failedTests.push({
name: test.title,
result: false,
message: err.message,
stack: err.stack,
titles: flattenTitles(test)
});
});
});
};
</script>
</body>
</html>

0 comments on commit 626b8ff

Please sign in to comment.