flexxui / pscript

Python to JavaScript compiler
http://pscript.readthedocs.io
BSD 2-Clause "Simplified" License
256 stars 25 forks source link

Transpilation of endswith() gives wrong results #66

Closed edridgedsouza closed 2 years ago

edridgedsouza commented 2 years ago

I transpiled a python script containing str.endswith() to js and realized that the new code gives inaccurate results. Here is the resulting js:

var _pymeth_endswith = function (x) { // nargs: 1
    if (this.constructor !== String) return this.endswith.apply(this, arguments);
    return this.lastIndexOf(x) == this.length - x.length;
};

When this gets called, it looks like: _pymeth_endswith.call(string1, string2). This gives false positives when string2 is one character longer than string1; for example, _pymeth_endswith.call('a', 'bb') gives true.

almarklein commented 2 years ago

Thanks! I can indeed reproduce this. A fix is ready in #67

almarklein commented 2 years ago

And a new release has been pushed with the fix.

edridgedsouza commented 2 years ago

Thanks! Does pscript include a method to check that transpiled js passes tests written pre-transpilation in pytest or another python based testing suite?

almarklein commented 2 years ago

Does pscript include a method to check that transpiled js passes tests written pre-transpilation in pytest or another python based testing suite?

I don't think so, but I'm also not sure if I understand the question :)

edridgedsouza commented 2 years ago

I was essentially just trying to see if there's an easy way to see if transpiling a python script with preexisting tests written could also apply the same tests to the resulting js code. Could perhaps be useful for catching edge cases like this if I use pscript again

almarklein commented 2 years ago

Ah I see. In pscript most tests are applied in JS. The evalpy() function used in many tests evaluates Python code in Node by first transpiling and then running it.

edridgedsouza commented 2 years ago

Thanks. As I understand it, this means that a good future workflow would be to create test_foo.py and for every def test_foo() in python, there should also be a def test_foo_js() calling evaljs() on the same code.