Skip to content

Commit b3e7212

Browse files
authored
Merge pull request #134 from RelativelyFine/feature/js-test-coverage
feature(tests): add js py2js and js2py error conversion test
2 parents 25b8ea6 + 0e9d9fc commit b3e7212

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

tests/js/js2py/error.simple

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file js2py/string.simple
3+
* Ensures that an error constructed in PY and binded to JS retains error message.
4+
* Error properties are not retained.
5+
*
6+
* @author David Courtis, [email protected]
7+
* @date July 2023
8+
*/
9+
'use strict';
10+
11+
const throughJS = x => x;
12+
let errorFlag = false;
13+
14+
function tester(exceptionpy, exceptionjs) {
15+
if (!exceptionpy.toString() === exceptionjs.toString())
16+
{
17+
console.error('Expected\n', exceptionpy.toString(), '\nbut got\n', exceptionjs.toString());
18+
errorFlag = true;
19+
} else {
20+
console.log('pass -', exceptionpy.toString());
21+
}
22+
}
23+
24+
function inbuiltError() {
25+
const exceptionpy = python.eval(`Exception('I know Python!')`);
26+
const exceptionjs = throughJS(exceptionpy);
27+
tester(exceptionpy, exceptionjs);
28+
}
29+
30+
function customError() {
31+
python.exec(
32+
`class IAmAnError(Exception):
33+
def __init__(self, message):
34+
super().__init__(message)
35+
`
36+
);
37+
const exceptionpy = python.eval(`IAmAnError('I know Python!')`);
38+
const exceptionjs = throughJS(exceptionpy);
39+
tester(exceptionpy, exceptionjs);
40+
}
41+
42+
inbuiltError();
43+
customError();
44+
45+
if (errorFlag) {
46+
throw new Error('test failed');
47+
}
48+

tests/js/py2js/error.simple

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @file py2js/string.simple
3+
* Ensures that an error constructed in JS and passed through python retains error message.
4+
* There will be a lossful conversion of error properties from JS to Py.
5+
*
6+
* @author David Courtis, [email protected]
7+
* @date July 2023
8+
*/
9+
'use strict';
10+
11+
class RandomError extends Error {
12+
constructor(message) {
13+
super(message);
14+
}
15+
}
16+
17+
const exceptionjs = new RandomError("I was created!");
18+
const throughPython = python.eval('(lambda x: x)');
19+
const exceptionpy = throughPython(exceptionjs);
20+
21+
if (!exceptionpy.toString().includes(exceptionjs.toString())) {
22+
console.error('Expected\n', exceptionjs.toString(), '\nbut got\n', exceptionpy.toString());
23+
throw new Error('test failed');
24+
}
25+
26+
console.log('pass -', exceptionjs);

0 commit comments

Comments
 (0)