Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/js/js2py/object-mutation.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @file js2py/object-mutation.simple
* Simple test which shows that converting objects from JS => Python uses
* shared memory, and changes made in either language will affect the object
* in the other.
* @author Joash Mathew, <joash@distributive.network>
* @date July 2023
*/
'use strict';

const obj = { a: 1 };
const pcode = `
def change_and_return(obj):
obj["a"] = 5;
return obj;
`;

python.exec(pcode);

const fun = python.eval("change_and_return");
const obj2 = fun(obj);

if (obj.a !== 5 || obj2["a"] !== 5)
{
console.error('Object isn\'t sharing memory.');
throw new Error('Test failed');
}

obj.a = 1000;

if (obj.a !== 1000 || obj2["a"] !== 1000)
{
console.error('Object isn\'t sharing memory.');
throw new Error('Test failed');
}

console.log('Test passed');
20 changes: 20 additions & 0 deletions tests/js/js2py/object.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file js2py/object.simple
* Simple test which shows that sending objects to Python and getting them back into JS
* works as expected.
* @author Joash Mathew, <joash@distributive.network>
* @date July 2023
*/
'use strict';

const objJs = { a: 1, b: 2, c: 3 };
const throughPython = python.eval('(lambda x: x)');
const objPy = throughPython(objJs);

if (objJs !== objPy)
{
console.error(`Expected ${objJs} but got ${objPy}`);
throw new Error('Test failed');
}

console.log('Test passed');
34 changes: 34 additions & 0 deletions tests/js/py2js/object-methods.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @file py2js/object.simple
* Simple test which shows that sending objects from Python to JS still retains
* the ability to perform basic Object methods like Object.keys(), Object.values() and
* Object.entries() on them.
* @author Joash Mathew, <joash@distributive.network>
* @date July 2023
*/
'use strict';

const obj = python.eval('{ "a": 1, "b": 2, "c": 3 }');
const throughJS = (x) => x;
const jsObj = throughJS(obj);
const standardJSObj = { a: 1, b: 2, c: 3 };

if (JSON.stringify(Object.keys(jsObj)) !== JSON.stringify(Object.keys(standardJSObj)))
{
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
throw new Error('Test failed');
}

if (JSON.stringify(Object.values(jsObj)) !== JSON.stringify(Object.values(standardJSObj)))
{
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
throw new Error('Test failed');
}

if (JSON.stringify(Object.entries(jsObj)) !== JSON.stringify(Object.entries(standardJSObj)))
{
console.error('The output of the PythonMonkey JS object does not match the output of a standard JS Object.');
throw new Error('Test failed');
}

console.log('Test passed');
20 changes: 20 additions & 0 deletions tests/js/py2js/object.simple
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file py2js/object.simple
* Simple test which shows that sending objects to JS and getting them back into Python
* works as expected.
* @author Joash Mathew, <joash@distributive.network>
* @date July 2023
*/
'use strict';

const obj = python.eval('{"a": 1, "b": 2, "c": 3}');
const throughJS = (x) => x;
const jsObj = throughJS(obj);

if (jsObj !== obj)
{
console.error('expected ', obj, ' but got ', jsObj);
throw new Error('Test failed');
}

console.log('Test passed');