Beispiel #1
0
 def test_all(self):
     assert evalpy('all([1, 2, 3])') == 'true' 
     assert evalpy('all([0, 2, 3])') == 'false'
     assert evalpy('all([])') == 'true'
     
     assert evalpy('all([3, [], 3])') == 'false'
     assert evalpy('all([3, [1], 3])') == 'true'
Beispiel #2
0
 def test_pop(self):
     code = 'a=[1,2,3,4,5];\n'
     assert evalpy(code + 'a.pop(2); a') == '[ 1, 2, 4, 5 ]'
     assert evalpy(code + 'a.pop(0); a') == '[ 2, 3, 4, 5 ]'
     assert evalpy(code + 'a.pop(); a') == '[ 1, 2, 3, 4 ]'
     assert evalpy(code + 'a.pop(-1); a') == '[ 1, 2, 3, 4 ]'
     assert 'IndexError' in evalpy(code + 'try:\n  a.pop(9);\nexcept Exception as e:\n  e')
Beispiel #3
0
 def test_rindex(self):
     # We know that the implementation is basded on find; no need to test all
     assert evalpy('"abcdefghb".rindex("a")') == '0'
     assert evalpy('"abcdefghb".rindex("h")') == '7' 
     assert evalpy('"abcdefghb".rindex("")') == '9'
     assert evalpy('"abcdefghb".rindex("b")') == '8'
     assert 'ValueError' in evalpy('try:\n  "abcdefgh".rindex("z")\nexcept Exception as e:\n  e')
Beispiel #4
0
 def test_assignments(self):
     assert py2js('foo = 3') == 'var foo;\nfoo = 3;'  # with var
     assert py2js('foo.bar = 3') == 'foo.bar = 3;'  # without var
     assert py2js('foo[i] = 3') == 'foo[i] = 3;'  # without var
     
     code = py2js('foo = 3; bar = 4')  # define both
     assert code.count('var') == 1
     code = py2js('foo = 3; foo = 4')  # only define first time
     assert code.count('var') == 1
     
     code = py2js('foo = bar = 3')  # multiple assignment
     assert 'foo = bar = 3' in code
     assert 'var bar, foo' in code  # alphabetic order
     
     # self -> this
     assert py2js('self') == 'this;'
     assert py2js('self.foo') == 'this.foo;'
     
     # Indexing
     assert evalpy('a=[0,0]\na[0]=2\na[1]=3\na', False) == '[2,3]'
     
     # Tuple unpacking
     evalpy('x=[1,2,3]\na, b, c = x\nb', False) == '2'
     evalpy('a,b,c = [1,2,3]\nc,b,a = a,b,c\n[a,b,c]', False) == '[3,2,1]'
     
     # For unpacking, test that variables are declared, but not when attr or index
     assert py2js('xx, yy = 3, 4').count('xx') == 2
     assert py2js('xx[0], yy[0] = 3, 4').count('xx') == 1
     assert py2js('xx.a, yy.a = 3, 4').count('xx') == 1
     
     # Class variables don't get a var
     code = py2js('class Foo:\n  bar=3\n  bar = bar + 1')
     assert code.count('bar') == 3
     assert code.count('Foo.prototype.bar') == 3
Beispiel #5
0
    def test_assignments(self):
        assert py2js("foo = 3") == "var foo;\nfoo = 3;"  # with var
        assert py2js("foo.bar = 3") == "foo.bar = 3;"  # without var

        code = py2js("foo = 3; bar = 4")  # define both
        assert code.count("var") == 1
        code = py2js("foo = 3; foo = 4")  # only define first time
        assert code.count("var") == 1

        code = py2js("foo = bar = 3")  # multiple assignment
        assert "foo = bar = 3" in code
        assert "var bar, foo" in code  # alphabetic order

        # self -> this
        assert py2js("self") == "this;"
        assert py2js("self.foo") == "this.foo;"

        # Indexing
        assert evalpy("a=[0,0]\na[0]=2\na[1]=3\na", False) == "[2,3]"

        # Tuple unpacking
        evalpy("x=[1,2,3]\na, b, c = x\nb", False) == "2"
        evalpy("a,b,c = [1,2,3]\nc,b,a = a,b,c\n[a,b,c]", False) == "[3,2,1]"

        # Class variables don't get a var
        code = py2js("class Foo:\n  bar=3\n  bar = bar + 1")
        assert code.count("bar") == 3
        assert code.count("Foo.prototype.bar") == 3
Beispiel #6
0
 def test_getattr(self):
     code = 'a = {"foo":1, "bar":2};\n'
     assert evalpy(code + 'getattr(a, "foo")') == '1'
     assert evalpy(code + 'getattr(a, "bar")') == '2'
     exc_att = 'except AttributeError: print("err")'
     assert evalpy(code + 'try:\n  getattr(a, "fooo")\n' + exc_att) == 'err'
     assert evalpy(code + 'getattr(a, "fooo", 3)') == '3'
Beispiel #7
0
 def test_raw_js_overloading(self):
     # more RawJS tests in test_parser3.py
     s1 = 'a=3; b=4; c=1; a + b - c'
     s2 = 'a=3; b=4; c=1; RawJS("a + b") - c'
     assert evalpy(s1) == '6'
     assert evalpy(s2) == '6'
     assert 'pyfunc' in py2js(s1)
     assert 'pyfunc' not in py2js(s2)
Beispiel #8
0
 def test_remove(self):
     code = 'a=[1,2,3,4,3,5];\n'
     assert evalpy(code + 'a.remove(2); a') == '[ 1, 3, 4, 3, 5 ]'
     assert evalpy(code + 'a.remove(3); a') == '[ 1, 2, 4, 3, 5 ]'
     assert 'ValueError' in evalpy(code + 'try:\n  a.remove(9);\nexcept Exception as e:\n  e')
     assert nowhitespace(evalpy('x = {"a":[2, 3]}; x.a.remove(2); x.a')) == '[3]'
     
     assert evalpy('a=[1,(2,3),4]; a.remove((2,3)); a') == '[ 1, 4 ]'
Beispiel #9
0
 def test_import(self):
     with raises(JSError):
         py2js('import time')
     
     # But we do support special time funcs
     import time
     assert abs(float(evalpy('time()')) - time.time()) < 0.5
     evalpy('t0=perf_counter(); t1=perf_counter(); (t1-t0)').startswith('0.0')
Beispiel #10
0
 def test_import(self):
     # time
     import time
     assert abs(float(evalpy('import time; time.time()')) - time.time()) < 0.5
     assert abs(float(evalpy('from time import time; time()')) - time.time()) < 0.5
     assert evalpy('import time; t0=time.perf_counter(); t1=time.perf_counter(); (t1-t0)').startswith('0.0')
     # sys
     assert 'pyscript' in evalpy('import sys; sys.version').lower()
Beispiel #11
0
    def test_import(self):
        # time
        import time

        assert abs(float(evalpy("import time; time.time()")) - time.time()) < 0.5
        assert abs(float(evalpy("from time import time; time()")) - time.time()) < 0.5
        assert evalpy("import time; t0=time.perf_counter(); t1=time.perf_counter(); (t1-t0)").startswith("0.0")
        # sys
        assert "pyscript" in evalpy("import sys; sys.version").lower()
Beispiel #12
0
 def test_assert(self):
     
     assert 'throw' in py2js('assert True')
     evalpy('assert true; 7') == '7'
     evalpy('assert true, "msg"; 7') == '7'
     
     catcher = 'try { %s } catch(err) { console.log(err); }'
     assert evaljs(catcher % py2js('assert false')).count('AssertionError')
     assert evaljs(catcher % py2js('assert false, "foo"')).count('foo')
Beispiel #13
0
 def test_funcion_call(self):
     jscode = 'var foo = function (x, y) {return x+y;};'
     assert evaljs(jscode + py2js('foo(2,2)')) == '4'
     assert evaljs(jscode + py2js('foo("so ", True)')) == 'so true'
     assert evaljs(jscode + py2js('a=[1,2]; foo(*a)')) == '3'
     
     # Test super (is tested for real in test_parser3.py
     assert evalpy('d={"_base_class": console};d._base_class.log(4)') == '4'
     assert evalpy('d={"_base_class": console};d._base_class.log()') == ''
Beispiel #14
0
    def test_remove(self):
        code = 'a=[1,2,3,4,3,5];\n'
        assert evalpy(code + 'a.remove(2); a') == '[ 1, 3, 4, 3, 5 ]'
        assert evalpy(code + 'a.remove(3); a') == '[ 1, 2, 4, 3, 5 ]'
        assert 'ValueError' in evalpy(
            code + 'try:\n  a.remove(9);\nexcept Exception as e:\n  e')
        assert nowhitespace(
            evalpy('x = {"a":[2, 3]}; x.a.remove(2); x.a')) == '[3]'

        assert evalpy('a=[1,(2,3),4]; a.remove((2,3)); a') == '[ 1, 4 ]'
Beispiel #15
0
    def test_filter(self):
        assert list(filter(lambda x: x > 0, [-1, -2, 1, 2])) == [1, 2]

        code = 'f1 = lambda x: x>0\n'
        assert evalpy(
            code +
            'for x in filter(f1, [-1, -2, 0, 1, 2]): print(x)') == '1\n2'
        assert evalpy(code +
                      'for x in filter(None, [-1, -2, 0, 1, 2]): print(x)'
                      ) == '-1\n-2\n1\n2'
Beispiel #16
0
    def test_indexing_and_slicing(self):
        c = 'a = [1, 2, 3, 4, 5]\n'

        # Indexing
        assert evalpy(c + 'a[2]') == '3'
        assert evalpy(c + 'a[-2]') == '4'

        # Slicing
        assert evalpy(c + 'a[:]') == '[ 1, 2, 3, 4, 5 ]'
        assert evalpy(c + 'a[1:-1]') == '[ 2, 3, 4 ]'
Beispiel #17
0
 def test_indexing_and_slicing(self):
     c = 'a = [1, 2, 3, 4, 5]\n'
     
     # Indexing
     assert evalpy(c + 'a[2]') == '3'
     assert evalpy(c + 'a[-2]') == '4'
     
     # Slicing
     assert evalpy(c + 'a[:]') == '[ 1, 2, 3, 4, 5 ]'
     assert evalpy(c + 'a[1:-1]') == '[ 2, 3, 4 ]'
Beispiel #18
0
    def test_indexing_and_slicing(self):
        c = "a = [1, 2, 3, 4, 5]\n"

        # Indexing
        assert evalpy(c + "a[2]") == "3"
        assert evalpy(c + "a[-2]") == "4"

        # Slicing
        assert evalpy(c + "a[:]") == "[ 1, 2, 3, 4, 5 ]"
        assert evalpy(c + "a[1:-1]") == "[ 2, 3, 4 ]"
Beispiel #19
0
 def test_recursion(self=None):
     
     code = 'def f(i): i *= 2; return i if i > 10 else f(i)\n\n'
     assert evalpy(code + 'f(1)') == '16'
     
     clscode = 'class G:\n  def __init__(self): self.i = 1\n\n'
     code = clscode + '  def f(self): self.i *= 2; return self.i if self.i > 10 else self.f()\n\n'
     assert evalpy(code + 'g = G(); g.f()') == '16'
     
     code = clscode + '  def f(self):\n    def h(): self.i *= 2; return self.i if self.i > 10 else h()\n\n'
     assert evalpy(code + '    return h()\n\ng = G(); g.f()') == '16'
Beispiel #20
0
 def test_recursion(self=None):
     
     code = 'def f(i): i *= 2; return i if i > 10 else f(i)\n\n'
     assert evalpy(code + 'f(1)') == '16'
     
     clscode = 'class G:\n  def __init__(self): self.i = 1\n\n'
     code = clscode + '  def f(self): self.i *= 2; return self.i if self.i > 10 else self.f()\n\n'
     assert evalpy(code + 'g = G(); g.f()') == '16'
     
     code = clscode + '  def f(self):\n    def h(): self.i *= 2; return self.i if self.i > 10 else h()\n\n'
     assert evalpy(code + '    return h()\n\ng = G(); g.f()') == '16'
Beispiel #21
0
 def test_sorted(self):
     assert evalpy('for x in sorted([1, 9, 3, 2, 7, 8, 4]): print(x)'
                   ) == '1\n2\n3\n4\n7\n8\n9'
     assert evalpy(
         'for x in reversed(sorted([1, 9, 3, 2, 7, 8, 4])): print(x)'
     ) == '9\n8\n7\n4\n3\n2\n1'
     assert evalpy(
         'for x in sorted([1, 9, 3, 2, 7, 8, 4], key=lambda a: -a): print(x)'
     ) == '9\n8\n7\n4\n3\n2\n1'
     assert evalpy(
         'for x in sorted([1, 9, 3, 2, 7, 8, 4], reverse=True): print(x)'
     ) == '9\n8\n7\n4\n3\n2\n1'
Beispiel #22
0
 def test_import(self):
     # time
     import time
     assert abs(float(evalpy('import time; time.time()')) -
                time.time()) < 0.5
     assert abs(
         float(evalpy('from time import time; time()')) - time.time()) < 0.5
     assert evalpy(
         'import time; t0=time.perf_counter(); t1=time.perf_counter(); (t1-t0)'
     ).startswith('0.0')
     # sys
     assert 'pyscript' in evalpy('import sys; sys.version').lower()
Beispiel #23
0
 def test_count(self):
     assert evalpy('"foo".count("o")') == '2'
     assert evalpy('"foo".count("f")') == '1'
     assert evalpy('"foo".count("x")') == '0'
     assert evalpy('"foo".count("")') == '3'
     
     assert evalpy('"a--a--a".count("a")') == '3'
     assert evalpy('"a--a--a".count("a", 0)') == '3'
     assert evalpy('"a--a--a".count("a", 0, 99)') == '3'
     assert evalpy('"a--a--a".count("a", 1)') == '2'
     assert evalpy('"a--a--a".count("a", 0, 4)') == '2'
     assert evalpy('"a--a--a".count("a", 1, 4)') == '1'
    def test_count(self):
        assert evalpy('"foo".count("o")') == '2'
        assert evalpy('"foo".count("f")') == '1'
        assert evalpy('"foo".count("x")') == '0'
        assert evalpy('"foo".count("")') == '3'

        assert evalpy('"a--a--a".count("a")') == '3'
        assert evalpy('"a--a--a".count("a", 0)') == '3'
        assert evalpy('"a--a--a".count("a", 0, 99)') == '3'
        assert evalpy('"a--a--a".count("a", 1)') == '2'
        assert evalpy('"a--a--a".count("a", 0, 4)') == '2'
        assert evalpy('"a--a--a".count("a", 1, 4)') == '1'
Beispiel #25
0
 def test_ops(self):
     # Test code
     assert py2js('2+3') == '2 + 3;'  # Binary
     assert py2js('2/3') == '2 / 3;'
     assert py2js('not 2') == '!2;'  # Unary
     assert py2js('-(2+3)') == '-(2 + 3);'
     assert py2js('True and False') == 'true && false;'  # Boolean
     assert py2js('4 > 3') == '4 > 3;'  # Comparisons
     assert py2js('4 is 3') == '4 === 3;'
     
     # No parentices around names, numbers and strings
     assert py2js('foo + bar') == "foo + bar;"
     assert py2js('_foo3 + _bar4') == "_foo3 + _bar4;"
     assert py2js('3 + 4') == "3 + 4;"
     assert py2js('"abc" + "def"') == "'abc' + 'def';"
     assert py2js("'abc' + 'def'") == "'abc' + 'def';"
     assert py2js("'abc' + \"'def\"") == "'abc' + \"'def\";"
     
     # But they should be if it gets more complex
     assert py2js('foo + bar == 3') == "(foo + bar) == 3;"
     
     # Test outcome
     assert evalpy('2+3') == '5'  # Binary
     assert evalpy('6/3') == '2'
     assert evalpy('4//3') == '1'
     assert evalpy('2**8') == '256'
     assert evalpy('not True') == 'false'  # Unary
     assert evalpy('- 3') == '-3'
     assert evalpy('True and False') == 'false'  # Boolean
     assert evalpy('True or False') == 'true'
Beispiel #26
0
 def test_while(self):
     
     # Test code output
     line = nowhitespace(py2js('while(True): pass'))
     assert line == 'while(true){}'
     line = nowhitespace(py2js('while(not ok): pass'))
     assert 'while' in line
     
     # Test break and continue
     for9 = 'i=-1\nwhile(i<8):\n  i+=1\n  '
     assert evalpy(for9 + 'if i==4:break\n  print(i)\n0') == '0\n1\n2\n3\n0'
     assert evalpy(for9 + 'if i<6:continue\n  print(i)\n0') == '6\n7\n8\n0'
     # Test else
     assert evalpy(for9 + 'if i==3:break\nelse: print(99)\n0') == '0'
     assert evalpy(for9 + 'if i==30:break\nelse: print(99)\n0') == '99\n0'
Beispiel #27
0
 def test_funcion_call(self):
     jscode = 'var foo = function (x, y) {return x+y;};'
     assert evaljs(jscode + py2js('foo(2,2)')) == '4'
     assert evaljs(jscode + py2js('foo("so ", True)')) == 'so true'
     assert evaljs(jscode + py2js('a=[1,2]; foo(*a)')) == '3'
     assert evaljs(jscode + py2js('a=[1,2]; foo(7, *a)')) == '8'
     
     # Test super (is tested for real in test_parser3.py
     assert evalpy('d={"_base_class": console};d._base_class.log(4)') == '4'
     assert evalpy('d={"_base_class": console};d._base_class.log()') == ''
     
     jscode = 'var foo = function () {return this.val};'
     jscode += 'var d = {"foo": foo, "val": 7};\n'
     assert evaljs(jscode + py2js('d["foo"]()')) == '7'
     assert evaljs(jscode + py2js('d["foo"](*[3, 4])')) == '7'
Beispiel #28
0
 def test_while(self):
     
     # Test code output
     line = nowhitespace(py2js('while(True): pass'))
     assert line == 'while(true){}'
     line = nowhitespace(py2js('while(not ok): pass'))
     assert 'while' in line
     
     # Test break and continue
     for9 = 'i=-1\nwhile(i<8):\n  i+=1\n  '
     assert evalpy(for9 + 'if i==4:break\n  print(i)\n0') == '0\n1\n2\n3\n0'
     assert evalpy(for9 + 'if i<6:continue\n  print(i)\n0') == '6\n7\n8\n0'
     # Test else
     assert evalpy(for9 + 'if i==3:break\nelse: print(99)\n0') == '0'
     assert evalpy(for9 + 'if i==30:break\nelse: print(99)\n0') == '99\n0'
Beispiel #29
0
    def test_funcion_call(self):
        jscode = "var foo = function (x, y) {return x+y;};"
        assert evaljs(jscode + py2js("foo(2,2)")) == "4"
        assert evaljs(jscode + py2js('foo("so ", True)')) == "so true"
        assert evaljs(jscode + py2js("a=[1,2]; foo(*a)")) == "3"
        assert evaljs(jscode + py2js("a=[1,2]; foo(7, *a)")) == "8"

        # Test super (is tested for real in test_parser3.py
        assert evalpy('d={"_base_class": console};d._base_class.log(4)') == "4"
        assert evalpy('d={"_base_class": console};d._base_class.log()') == ""

        jscode = "var foo = function () {return this.val};"
        jscode += 'var d = {"foo": foo, "val": 7};\n'
        assert evaljs(jscode + py2js('d["foo"]()')) == "7"
        assert evaljs(jscode + py2js('d["foo"](*[3, 4])')) == "7"
Beispiel #30
0
 def test_funcion_call(self):
     jscode = 'var foo = function (x, y) {return x+y;};'
     assert evaljs(jscode + py2js('foo(2,2)')) == '4'
     assert evaljs(jscode + py2js('foo("so ", True)')) == 'so true'
     assert evaljs(jscode + py2js('a=[1,2]; foo(*a)')) == '3'
     assert evaljs(jscode + py2js('a=[1,2]; foo(7, *a)')) == '8'
     
     # Test super (is tested for real in test_parser3.py
     assert evalpy('d={"_base_class": console};d._base_class.log(4)') == '4'
     assert evalpy('d={"_base_class": console};d._base_class.log()') == ''
     
     jscode = 'var foo = function () {return this.val};'
     jscode += 'var d = {"foo": foo, "val": 7};\n'
     assert evaljs(jscode + py2js('d["foo"]()')) == '7'
     assert evaljs(jscode + py2js('d["foo"](*[3, 4])')) == '7'
    def test_rfind(self):
        assert evalpy('"abcdefgh".rfind("a")') == '0'
        assert evalpy('"abcdefgh".rfind("h")') == '7'
        assert evalpy('"abcdefgh".rfind("z")') == '-1'
        assert evalpy('"abcdefgh".rfind("")') == '8'

        assert evalpy('"abcdefgh".rfind("cd")') == '2'
        assert evalpy('"abcdefgh".rfind("def")') == '3'

        assert evalpy('"ab ab ab".rfind("ab", 0, -2)') == '3'
        assert evalpy('"ab ab ab".rfind("ab", 0, 3)') == '0'
        assert evalpy('"ab      ".rfind("ab", 3)') == '-1'
Beispiel #32
0
 def test_rfind(self):
     assert evalpy('"abcdefgh".rfind("a")') == '0'
     assert evalpy('"abcdefgh".rfind("h")') == '7' 
     assert evalpy('"abcdefgh".rfind("z")') == '-1'
     assert evalpy('"abcdefgh".rfind("")') == '8'
     
     assert evalpy('"abcdefgh".rfind("cd")') == '2'
     assert evalpy('"abcdefgh".rfind("def")') == '3'
     
     assert evalpy('"ab ab ab".rfind("ab", 0, -2)') == '3'
     assert evalpy('"ab ab ab".rfind("ab", 0, 3)') == '0'
     assert evalpy('"ab      ".rfind("ab", 3)') == '-1'
 def test_isidentifier(self):
     assert evalpy('"".isidentifier()') == 'false'
     assert evalpy('"012".isidentifier()') == 'false'
     assert evalpy('"abc".isidentifier()') == 'true'
     assert evalpy('"0a1b2c".isidentifier()') == 'false'
     assert evalpy('"a0a1b2c".isidentifier()') == 'true'
     assert evalpy('"0a_".isidentifier()') == 'false'
     assert evalpy('"_a".isidentifier()') == 'true'
     assert evalpy('"_0".isidentifier()') == 'true'
Beispiel #34
0
 def test_isidentifier(self):
     assert evalpy('"".isidentifier()') == 'false'
     assert evalpy('"012".isidentifier()') == 'false'
     assert evalpy('"abc".isidentifier()') == 'true'
     assert evalpy('"0a1b2c".isidentifier()') == 'false'
     assert evalpy('"a0a1b2c".isidentifier()') == 'true'
     assert evalpy('"0a_".isidentifier()') == 'false'
     assert evalpy('"_a".isidentifier()') == 'true'
     assert evalpy('"_0".isidentifier()') == 'true'
Beispiel #35
0
 def xx_test_list_comprehension_speed(self):
     # https://developers.google.com/speed/articles/optimizing-javascript
     # ~ 0.029 when comprehension transpile to closures
     # ~ 0.023 when comprehensions transpile to inner function s, plus smaller chance on closures
     # ~ 0.017 when comprehensions transpile to inner function s, plus no chance on closures
     code = 't0 = perf_counter()\nfor i in range(100000): a = [j for j in range(10)]\n\nperf_counter()-t0'
     t1 = evalpy(code)
     print(t1)
Beispiel #36
0
    def test_basic_types(self):
        assert py2js('True') == 'true;'
        assert py2js('False') == 'false;'
        assert py2js('None') == 'null;'

        assert py2js('"bla\\"bla"') == '"bla\\"bla";'
        assert py2js('3') == '3;'
        assert py2js('3.1415') == '3.1415;'

        assert py2js('[1,2,3]') == '[1, 2, 3];'
        assert py2js('(1,2,3)') == '[1, 2, 3];'

        assert py2js('{"foo": 3, "bar": 4}') == '({foo: 3, bar: 4});'
        assert evalpy('a={"foo": 3, "bar": 4};a') == '{ foo: 3, bar: 4 }'
        with raises(JSError):
            assert evalpy(
                'bla="foo";a={bla: 3, bar: 4};a') == '{ foo: 3, bar: 4 }'
Beispiel #37
0
 def xx_test_list_comprehension_speed(self):
     # https://developers.google.com/speed/articles/optimizing-javascript
     # ~ 0.029 when comprehension transpile to closures
     # ~ 0.023 when comprehensions transpile to inner function s, plus smaller chance on closures
     # ~ 0.017 when comprehensions transpile to inner function s, plus no chance on closures
     code = 't0 = perf_counter()\nfor i in range(100000): a = [j for j in range(10)]\n\nperf_counter()-t0'
     t1 = evalpy(code)
     print(t1)
Beispiel #38
0
 def test_istitle(self):
     assert evalpy('"".istitle()') == 'false'
     assert evalpy('" ".istitle()') == 'false'
     assert evalpy('"AbC".istitle()') == 'false'
     assert evalpy('"Foo bar".istitle()') == 'false'
     assert evalpy('"AbC 01_".istitle()') == 'false'
     
     assert evalpy('"Foo".istitle()') == 'true'
     assert evalpy('"Foo Bar".istitle()') == 'true'
     assert evalpy('"Foo 01_".istitle()') == 'true'
Beispiel #39
0
 def test_index(self):
     assert evalpy('[1,2,3,4,5,3].index(2)') == '1'
     assert evalpy('[1,2,3,4,5,3].index(3)') == '2'
     assert 'ValueError' in evalpy('try:\n  [1,2,3,4,5,3].index(9);\nexcept Exception as e:\n  e')
     assert evalpy('[1,2,3,4,5,3].index(3, 4)') == '5'
     assert evalpy('[1,2,3,4,5,3].index(3, -2)') == '5'
     assert evalpy('[1,2,3,4,5,3].index(3, 0, -2)') == '2'
     
     assert evalpy('a=[1,(2,3),4, (2,3), 5]; a.index((2,3))') == '1'
     assert evalpy('a=[1,(2,3),4, (2,3), 5]; a.index((2,3),2)') == '3'
    def test_replace(self):
        assert evalpy("'abcABC'.replace('a', 'x')") == "xbcABC"
        assert evalpy("'abcABC'.replace('C', 'x')") == "abcABx"
        assert evalpy("'abcABC'.replace('cA', 'x')") == "abxBC"

        assert evalpy("'abababab'.replace('a', 'x', 0)") == "abababab"
        assert evalpy("'abababab'.replace('a', 'x', 1)") == "xbababab"
        assert evalpy("'abababab'.replace('a', 'x', 3)") == "xbxbxbab"
        assert evalpy("'abababab'.replace('a', 'x', 99)") == "xbxbxbxb"
        assert evalpy("'abababab'.replace('b', 'x', 2)") == "axaxabab"
Beispiel #41
0
 def test_replace(self):
     assert evalpy("'abcABC'.replace('a', 'x')") == "xbcABC"
     assert evalpy("'abcABC'.replace('C', 'x')") == "abcABx"
     assert evalpy("'abcABC'.replace('cA', 'x')") == "abxBC"
     
     assert evalpy("'abababab'.replace('a', 'x', 0)") == "abababab"
     assert evalpy("'abababab'.replace('a', 'x', 1)") == "xbababab"
     assert evalpy("'abababab'.replace('a', 'x', 3)") == "xbxbxbab"
     assert evalpy("'abababab'.replace('a', 'x', 99)") == "xbxbxbxb"
     assert evalpy("'abababab'.replace('b', 'x', 2)") == "axaxabab"
    def test_istitle(self):
        assert evalpy('"".istitle()') == 'false'
        assert evalpy('" ".istitle()') == 'false'
        assert evalpy('"AbC".istitle()') == 'false'
        assert evalpy('"Foo bar".istitle()') == 'false'
        assert evalpy('"AbC 01_".istitle()') == 'false'

        assert evalpy('"Foo".istitle()') == 'true'
        assert evalpy('"Foo Bar".istitle()') == 'true'
        assert evalpy('"Foo 01_".istitle()') == 'true'
 def test_bool(self):
     assert evalpy('bool(5)') == 'true'
     assert evalpy('bool("xx")') == 'true'
     assert evalpy('bool(0)') == 'false'
     assert evalpy('bool("")') == 'false'
     #
     assert evalpy('bool([1])') == 'true'
     assert evalpy('bool({1:2})') == 'true'
     assert evalpy('bool([])') == 'false'
     assert evalpy('bool({})') == 'false'
Beispiel #44
0
 def test_bool(self):
     assert evalpy('bool(5)') == 'true'
     assert evalpy('bool("xx")') == 'true'
     assert evalpy('bool(0)') == 'false'
     assert evalpy('bool("")') == 'false'
     #
     assert evalpy('bool([1])') == 'true'
     assert evalpy('bool({1:2})') == 'true'
     assert evalpy('bool([])') == 'false'
     assert evalpy('bool({})') == 'false'
Beispiel #45
0
    def test_list_comprehensions(self):

        # Simple
        code = '[i for i in [-1, -2, 1, 2, 3]]'
        assert str(eval(code)) == normallist(evalpy(code))

        # With ifs
        code = '[i for i in [-1, -2, 1, 2, 3] if i > 0 and i < 3]'
        assert str(eval(code)) == normallist(evalpy(code))
        code = '[i for i in [-1, -2, 1, 2, 3] if i > 0 if i < 3]'
        assert str(eval(code)) == normallist(evalpy(code))

        # Double
        code = '[i*j for i in [-1, -2, 1, 2, 3] if i > 0 for j in [1, 10, 100] if j<100]'
        assert str(eval(code)) == normallist(evalpy(code))

        # Triple
        code = '[i*j*k for i in [1, 2, 3] for j in [1, 10] for k in [5, 7]]'
        assert str(eval(code)) == normallist(evalpy(code))

        # Double args
        code = '[(i, j) for i in [1, 2, 3] for j in [1, 10]]'
        assert str(eval(code)).replace('(', '[').replace(')',
                                                         ']') == normallist(
                                                             evalpy(code))

        # Double iters
        code = '[(i, j) for i, j in [[1, 2], [3, 4], [5, 6]]]'
        assert str(eval(code)).replace('(', '[').replace(')',
                                                         ']') == normallist(
                                                             evalpy(code))
Beispiel #46
0
 def test_list_comprehensions(self):
     
     # Simple
     code = '[i for i in [-1, -2, 1, 2, 3]]'
     assert str(eval(code)) == normallist(evalpy(code))
     
     # With ifs
     code = '[i for i in [-1, -2, 1, 2, 3] if i > 0 and i < 3]'
     assert str(eval(code)) == normallist(evalpy(code))
     code = '[i for i in [-1, -2, 1, 2, 3] if i > 0 if i < 3]'
     assert str(eval(code)) == normallist(evalpy(code))
     
     # Double
     code = '[i*j for i in [-1, -2, 1, 2, 3] if i > 0 for j in [1, 10, 100] if j<100]'
     assert str(eval(code)) == normallist(evalpy(code))
     
     # Triple
     code = '[i*j*k for i in [1, 2, 3] for j in [1, 10] for k in [5, 7]]'
     assert str(eval(code)) == normallist(evalpy(code))
     
     # Double args
     code = '[(i, j) for i in [1, 2, 3] for j in [1, 10]]'
     assert str(eval(code)).replace('(', '[').replace(')', ']') == normallist(evalpy(code))
     
     # Double iters
     code = '[(i, j) for i, j in [[1, 2], [3, 4], [5, 6]]]'
     assert str(eval(code)).replace('(', '[').replace(')', ']') == normallist(evalpy(code))
Beispiel #47
0
 def test_function_call_keyword_only_args_and_kwargs(self):
     code = "def foo(*, a=3, b=4, **x): return repr([a, b]) + repr(x);\nd = {'foo':foo}\n"
     assert evalpy(code + 'foo(1)') == '[3,4]{}'
     assert evalpy(code + 'foo(a=1, b=2)') == '[1,2]{}'
     assert evalpy(code + 'foo(a=1, b=2, c=5)') == '[1,2]{"c":5}'
     #
     assert evalpy(code + 'd.foo(1)') == '[3,4]{}'
     assert evalpy(code + 'd.foo(a=1, b=2, c=5)') == '[1,2]{"c":5}'
     
     # All the args
     code = "def foo(a, b=2, *, c=3, **x): return repr([a, b, c]) + repr(x);\n"
     assert evalpy(code + 'foo(1)') == '[1,2,3]{}'
     assert evalpy(code + 'foo(1, b=8)') == '[1,2,3]{"b":8}'  # this one might be surprising
     assert evalpy(code + 'foo(1, c=8)') == '[1,2,8]{}'
     assert evalpy(code + 'foo(1, d=8)') == '[1,2,3]{"d":8}'
Beispiel #48
0
 def test_if(self):
     # Normal if
     assert evalpy('if True: 4\nelse: 5') == '4'
     assert evalpy('if False: 4\nelse: 5') == '5'
     assert evalpy('x=4\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '13'
     assert evalpy('x=3\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '12'
     assert evalpy('x=1\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '10'
     
     # One-line if
     line = py2js('3 if True else 4').replace(')', '').replace('(', '')
     assert line == 'true? 3 : 4;'
     #
     assert evalpy('4 if True else 5') == '4'
     assert evalpy('4 if False else 5') == '5'
     assert evalpy('3+1 if 0+2/1 else 4+1') == '4'
     assert evalpy('3+1 if 4/2-2 else 4+1') == '5'
Beispiel #49
0
 def test_if(self):
     # Normal if
     assert evalpy('if True: 4\nelse: 5') == '4'
     assert evalpy('if False: 4\nelse: 5') == '5'
     assert evalpy('x=4\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '13'
     assert evalpy('x=3\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '12'
     assert evalpy('x=1\nif x>3: 13\nelif x > 2: 12\nelse: 10') == '10'
     
     # One-line if
     line = py2js('3 if True else 4').replace(')', '').replace('(', '')
     assert line == 'true? 3 : 4;'
     #
     assert evalpy('4 if True else 5') == '4'
     assert evalpy('4 if False else 5') == '5'
     assert evalpy('3+1 if 0+2/1 else 4+1') == '4'
     assert evalpy('3+1 if 4/2-2 else 4+1') == '5'
Beispiel #50
0
 def test_partition(self):
     assert evalpy('"".partition("-")') == "[ '', '', '' ]"
     assert evalpy('"abc".partition("-")') == "[ 'abc', '', '' ]"
     assert evalpy('"-".partition("-")') == "[ '', '-', '' ]"
     assert evalpy('"abc-".partition("-")') == "[ 'abc', '-', '' ]"
     assert evalpy('"-def".partition("-")') == "[ '', '-', 'def' ]"
     assert evalpy('"abc-def".partition("-")') == "[ 'abc', '-', 'def' ]"
     
     assert 'ValueError' in evalpy('try:\n  "aa".partition("")\nexcept Exception as e:\n  e')
Beispiel #51
0
 def test_function_call_default_args(self):
     code = "def foo(a=2, b=3, c=4): return a+b+c;\nd = {'foo':foo}\n"
     assert evalpy(code + 'foo(1, 2, 3)') == '6'
     assert evalpy(code + 'd.foo(1, 2, 3)') == '6'
     #
     assert evalpy(code + 'foo(1, 2)') == '7'
     assert evalpy(code + 'd.foo(1, 2)') == '7'
     #
     assert evalpy(code + 'foo(1)') == '8'
     assert evalpy(code + 'd.foo(1)') == '8'
     #
     assert evalpy(code + 'foo()') == '9'
     assert evalpy(code + 'd.foo()') == '9'
 def test_int(self):
     assert evalpy('int(3.4)') == '3'
     assert evalpy('int(3.6)') == '3'
     assert evalpy('int(-3.4)') == '-3'
     assert evalpy('int(-3.6)') == '-3'
     assert evalpy('"5" + 2') == '52'
     assert evalpy('int("5") + 2') == '7'  # -> evaluate to number
 def test_isspace(self):
     assert evalpy('"".isspace()') == 'false'
     assert evalpy('" ".isspace()') == 'true'
     assert evalpy('" \\t\\n".isspace()') == 'true'
     assert evalpy('" _".isspace()') == 'false'
     assert evalpy('" a".isspace()') == 'false'
     assert evalpy('" 1".isspace()') == 'false'
 def test_strip(self):
     assert evalpy('"".strip() + "."') == '.'
     assert evalpy('" \\t\\r\\n".strip() + "."') == '.'
     assert evalpy('"  ab x cd  ".strip() + "."') == 'ab x cd.'
     assert evalpy('"  ab x cd  ".strip("x") + "."') == '  ab x cd  .'
     assert evalpy('"  ab x cd  ".strip(" x") + "."') == 'ab x cd.'
     assert evalpy('"x x ab x cd x x".strip(" x") + "."') == 'ab x cd.'
 def test_rpartition(self):
     assert evalpy('"".rpartition("-")') == "[ '', '', '' ]"
     assert evalpy('"abc".rpartition("-")') == "[ '', '', 'abc' ]"
     assert evalpy('"-".rpartition("-")') == "[ '', '-', '' ]"
     assert evalpy('"abc-".rpartition("-")') == "[ 'abc', '-', '' ]"
     assert evalpy('"-def".rpartition("-")') == "[ '', '-', 'def' ]"
     assert evalpy('"abc-def".rpartition("-")') == "[ 'abc', '-', 'def' ]"
Beispiel #56
0
 def test_function_call_args_and_kwargs(self):
     # Func returns args
     code = "def foo(*args, **kwargs): return args;\n"
     assert evalpy(code + 'foo(1, 2, 3)') == '[ 1, 2, 3 ]'
     assert evalpy(code + 'foo(1, 2, 3, a=3)') == '[ 1, 2, 3 ]'
     assert evalpy(code + 'foo(1, 2, 3, **{"b":4})') == '[ 1, 2, 3 ]'
     assert evalpy(code + 'foo(a=3, **{"b":4})') == '[]'
     
     # Func return kwargs
     code = "def foo(*args, **kwargs): return kwargs;\n"
     assert evalpy(code + 'foo(1, 2, 3)') == '{}'
     assert evalpy(code + 'foo(1, 2, 3, a=3)') == '{ a: 3 }'
     assert evalpy(code + 'foo(1, 2, 3, **{"b":4})') == '{ b: 4 }'
     assert evalpy(code + 'foo(a=3, **{"b":4})') == '{ a: 3, b: 4 }'
Beispiel #57
0
    def test_get(self):
        assert evalpy('a = {"foo":3}; a.get("foo")') == '3'
        assert evalpy('a = {"foo":3}; a.get("foo", 0)') == '3'
        assert evalpy('a = {"foo":3}; a.get("bar")') == 'null'
        assert evalpy('a = {"foo":3}; a.get("bar", 0)') == '0'
        assert evalpy('{"foo":3}.get("foo")') == '3'
        assert evalpy('{"foo":3}.get("bar", 0)') == '0'

        # Test that if a get exists, that one is used
        fun = 'def fun(x): return 42\n'
        assert evalpy(fun + 'a = {"get": fun}; a.get("bar")') == '42'
    def test_isupper(self):
        assert evalpy('"".isupper()') == 'false'
        assert evalpy('" ".isupper()') == 'false'
        assert evalpy('"AbC".isupper()') == 'false'
        assert evalpy('"AbC 01_".isupper()') == 'false'

        assert evalpy('"ABC".isupper()') == 'true'
        assert evalpy('"ABC 01_".isupper()') == 'true'