Skip to content

Commit 30a9dcd

Browse files
authored
Merge pull request #118 from j0ashm/feat/object-datatype-tests
Add tests for passing Objects between JS <=> Python
2 parents 61c718c + 7409480 commit 30a9dcd

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* @file js2py/object-mutation.simple
3+
* Simple test which shows that converting objects from JS => Python uses
4+
* shared memory, and changes made in either language will affect the object
5+
* in the other.
6+
* @author Joash Mathew, <[email protected]>
7+
* @date July 2023
8+
*/
9+
'use strict';
10+
11+
const obj = { a: 1 };
12+
const pcode = `
13+
def change_and_return(obj):
14+
obj["a"] = 5;
15+
return obj;
16+
`;
17+
18+
python.exec(pcode);
19+
20+
const fun = python.eval("change_and_return");
21+
const obj2 = fun(obj);
22+
23+
if (obj.a !== 5 || obj2["a"] !== 5)
24+
{
25+
console.error('Object isn\'t sharing memory.');
26+
throw new Error('Test failed');
27+
}
28+
29+
obj.a = 1000;
30+
31+
if (obj.a !== 1000 || obj2["a"] !== 1000)
32+
{
33+
console.error('Object isn\'t sharing memory.');
34+
throw new Error('Test failed');
35+
}
36+
37+
console.log('Test passed');

tests/js/js2py/object.simple

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file js2py/object.simple
3+
* Simple test which shows that sending objects to Python and getting them back into JS
4+
* works as expected.
5+
* @author Joash Mathew, <[email protected]>
6+
* @date July 2023
7+
*/
8+
'use strict';
9+
10+
const objJs = { a: 1, b: 2, c: 3 };
11+
const throughPython = python.eval('(lambda x: x)');
12+
const objPy = throughPython(objJs);
13+
14+
if (objJs !== objPy)
15+
{
16+
console.error(`Expected ${objJs} but got ${objPy}`);
17+
throw new Error('Test failed');
18+
}
19+
20+
console.log('Test passed');
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file py2js/object.simple
3+
* Simple test which shows that sending objects from Python to JS still retains
4+
* the ability to perform basic Object methods like Object.keys(), Object.values() and
5+
* Object.entries() on them.
6+
* @author Joash Mathew, <[email protected]>
7+
* @date July 2023
8+
*/
9+
'use strict';
10+
11+
const obj = python.eval('{ "a": 1, "b": 2, "c": 3 }');
12+
const throughJS = (x) => x;
13+
const jsObj = throughJS(obj);
14+
const standardJSObj = { a: 1, b: 2, c: 3 };
15+
16+
if (JSON.stringify(Object.keys(jsObj)) !== JSON.stringify(Object.keys(standardJSObj)))
17+
{
18+
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
19+
throw new Error('Test failed');
20+
}
21+
22+
if (JSON.stringify(Object.values(jsObj)) !== JSON.stringify(Object.values(standardJSObj)))
23+
{
24+
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
25+
throw new Error('Test failed');
26+
}
27+
28+
if (JSON.stringify(Object.entries(jsObj)) !== JSON.stringify(Object.entries(standardJSObj)))
29+
{
30+
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
31+
throw new Error('Test failed');
32+
}
33+
34+
console.log('Test passed');

tests/js/py2js/object.simple

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @file py2js/object.simple
3+
* Simple test which shows that sending objects to JS and getting them back into Python
4+
* works as expected.
5+
* @author Joash Mathew, <[email protected]>
6+
* @date July 2023
7+
*/
8+
'use strict';
9+
10+
const obj = python.eval('{"a": 1, "b": 2, "c": 3}');
11+
const throughJS = (x) => x;
12+
const jsObj = throughJS(obj);
13+
14+
if (jsObj !== obj)
15+
{
16+
console.error('expected ', obj, ' but got ', jsObj);
17+
throw new Error('Test failed');
18+
}
19+
20+
console.log('Test passed');

0 commit comments

Comments
 (0)