Skip to content

Commit

Permalink
Tweak setup for test-py.html, linting
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 7, 2024
1 parent 8fb8d5e commit 939ce32
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
15 changes: 7 additions & 8 deletions docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,35 @@ _static/test-c.html:
cd ../c && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../c/dist/bundle-c.js _static/dist/bundle-c.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-c.js > _static/test-c.html
cat _static/test-js.html | sed s#_static/bundle-js.js#_static/bundle-c.js > _static/test-c.html

_static/test-cp.html:
cd ../cplusplus && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../cplusplus/dist/bundle-cp.js _static/dist/bundle-cp.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-cp.js > _static/test-cp.html
cat _static/test-js.html | sed s#_static/bundle-js.js#_static/bundle-cp.js > _static/test-cp.html

_static/test-cs.html:
cd ../csharp && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../csharp/dist/bundle-cs.js _static/dist/bundle-cs.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-cs.js > _static/test-cs.html
cat _static/dist/test-js.html | sed s#_static/bundle-js.js#_static/bundle-cs.js > _static/test-cs.html

_static/test-js.html:
_static/dist/bundle.js:
cd ../javascript && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../javascript/dist/bundle-js.js _static/dist/bundle-js.js
cp ../javascript/index.html _static/test-js.html

_static/dist/pyodide.js:
_static/dist/python.tar.gz:
cd ../python && $(MAKE) webpack $(MFLAGS)

_static/test-rs.html:
cd ../rust && $(MAKE) webpack $(MFLAGS)
mkdir -p _static/dist
cp ../rust/dist/bundle-rs.js _static/dist/bundle-rs.js
cat ../javascript/index.html | sed s#_static/bundle-js.js#_static/bundle-rs.js > _static/test-rs.html
cat _static/dist/test-js.html | sed s#_static/bundle-js.js#_static/bundle-rs.js > _static/test-rs.html

html: _static/test-js.html _static/dist/pyodide.js
html: _static/dist/bundle.js _static/dist/python.tar.gz
$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)

# Catch-all target: route all unknown targets to Sphinx using the new
Expand Down
File renamed without changes.
74 changes: 46 additions & 28 deletions docs/_static/test-py.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<script src="https://cdn.jsdelivr.net/pyodide/v0.26.2/debug/pyodide.js"></script>
<link href="https://npmcdn.com/mocha@10.6.0/mocha.css" rel="stylesheet" />
<script src="https://npmcdn.com/mocha@10.6.0/mocha.js"></script>
</head>

<body>
<p>
You can execute any Python code. Just enter something in the box below and
click the button.
</p>
<input id="code" value="sum([1, 2, 3, 4, 5])" />
<button onclick="evaluatePython()">Run</button>
<br />
<br />
<div>Output:</div>
<textarea id="output" style="width: 100%;" rows="6" disabled></textarea>
<!-- A container element for the visual Mocha results -->
<div id="mocha"></div>

<!-- Mocha setup and initiation code -->
<script>
const output = document.getElementById("output");
const code = document.getElementById("code");

function addToOutput(s) {
output.value += ">>>" + code.value + "\n" + s + "\n";
}

output.value = "Initializing...\n";
// init Pyodide
mocha.setup('bdd');
window.onload = function() {
async function main() {
let pyodide = await loadPyodide();
output.value += "Ready!\n";
Expand All @@ -43,16 +33,44 @@
await response.unpack_archive() # by default, unpacks to the current dir
`);
});
// describe tests here, or include another file

async function evaluatePython() {
let pyodide = await pyodideReadyPromise;
try {
let output = pyodide.runPython(code.value);
addToOutput(output);
} catch (err) {
addToOutput(err);
}
}
for (let i = 1; i < 26; i += 1) {
describe(`run test ${i}`, () => {
it('should return a value', async () => {
const pyodide = await pyodideReadyPromise;
expect(await pyodide.runPythonAsync(`
from src import p{q}
p{q}.main()
`)).to.be.finite;
});
});
}
var runner = mocha.run();
var failedTests = []; runner.on('end', function() { window.mochaResults = runner.stats;
window.mochaResults.reports = failedTests; });
runner.on('fail', logFailure);

function logFailure(test, err){
var flattenTitles = function(test){
var 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>

6 changes: 3 additions & 3 deletions python/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

for i in range(1, 10000):
try:
name = f'p{i:04}'
name = f'p{i:04}' # noqa
module = import_module(f'.{name}', __name__)
setattr(modules[__name__], name, module)
__all__.append(name)
Expand All @@ -19,9 +19,9 @@
pass


def run_problems(): # pragma: no cover
def run_problems() -> None: # pragma: no cover
for i, p in problems.items():
start = perf_counter_ns()
answer = p()
stop = perf_counter_ns()
print(f'The answer to problem {i:04} is {answer!r} (found in {(stop - start) / 1000:,}μs)')
print(f'The answer to problem {i:04} is {answer!r} (found in {(stop - start) / 1000:,}μs)') # noqa

0 comments on commit 939ce32

Please sign in to comment.