diff --git a/tests/js/js2py/object-mutation.simple b/tests/js/js2py/object-mutation.simple new file mode 100644 index 00000000..2952efdb --- /dev/null +++ b/tests/js/js2py/object-mutation.simple @@ -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, + * @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'); \ No newline at end of file diff --git a/tests/js/js2py/object.simple b/tests/js/js2py/object.simple new file mode 100644 index 00000000..b4f3d6be --- /dev/null +++ b/tests/js/js2py/object.simple @@ -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, + * @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'); \ No newline at end of file diff --git a/tests/js/py2js/object-methods.simple b/tests/js/py2js/object-methods.simple new file mode 100644 index 00000000..8219d227 --- /dev/null +++ b/tests/js/py2js/object-methods.simple @@ -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, + * @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'); \ No newline at end of file diff --git a/tests/js/py2js/object.simple b/tests/js/py2js/object.simple new file mode 100644 index 00000000..2bebadad --- /dev/null +++ b/tests/js/py2js/object.simple @@ -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, + * @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'); \ No newline at end of file