

def test_for(self):
    line = nowhitespace(py2js('for i in range(9): pass', inline_stdlib=False))
    assert (line == 'vari;for(i=0;i<9;i+=1){}')
    line = nowhitespace(py2js('for i in range(2, 99): pass', inline_stdlib=False))
    assert (line == 'vari;for(i=2;i<99;i+=1){}')
    line = nowhitespace(py2js('for i in range(100, 0, -1): pass', inline_stdlib=False))
    assert (line == 'vari;for(i=100;i>0;i+=-1){}')
    assert (' in ' not in py2js('for i in [1, 2, 3]: pass'))
    assert (' in ' not in py2js('for i in {1:2, 2:3}: pass'))
    assert ('var aa' in py2js('for aa in x: pass'))
    assert ('var aa' in py2js('aa=""\nfor aa in x: pass'))
    assert ('var aa' in py2js('j=aa=""\nfor aa in x: pass'))
    assert (evalpy('for i in range(3):\n  print(i)') == '0\n1\n2')
    assert (evalpy('for i in range(1,6,2):\n  print(i)') == '1\n3\n5')
    assert (evalpy('for i in range(sum([2, 3])): print(i)') == '0\n1\n2\n3\n4')
    code = py2js('a=[7,8]\nfor i in range(len(a)):\n  print(a[i])')
    assert ((' in ' not in code) and (evaljs(code) == '7\n8'))
    code = py2js('for k in [7, 8]:\n  print(k)')
    assert ((' in ' not in code) and (evaljs(code) == '7\n8'))
    raises(JSError, py2js, 'for i, j in range(10): pass')
    code = py2js('d = {3:7, 4:8}\nfor k in d:\n  print(k)')
    assert ((' in ' not in code) and (evaljs(code) == '3\n4'))
    code = py2js('d = {3:7, 4:8}\nfor k in d:\n  print(d[k])')
    assert ((' in ' not in code) and (evaljs(code) == '7\n8'))
    code = py2js('d = {3:7, 4:8}\nfor k in d.keys():\n  print(d[k])')
    assert (evaljs(code) == '7\n8')
    code = py2js('d = {3:7, 4:8}\nfor v in d.values():\n  print(v)')
    assert ((' in ' in code) and (evaljs(code) == '7\n8'))
    code = py2js('d = {3:7, 4:8}\nfor k,v in d.items():\n  print(k)')
    assert ((' in ' in code) and (evaljs(code) == '3\n4'))
    code = py2js('d = {3:7, 4:8}\nfor k,v in d.items():\n  print(v)')
    assert ((' in ' in code) and (evaljs(code) == '7\n8'))
    raises(JSError, py2js, 'for i, j in x.keys(): pass')
    raises(JSError, py2js, 'for i, j in x.values(): pass')
    raises(JSError, py2js, 'for i in x.items(): pass')
    raises(JSError, py2js, 'for i, j, k in x.items(): pass')
    code = py2js('for c in "foo":\n  print(c)')
    assert (evaljs(code) == 'f\no\no')
    for9 = 'for i in range(9):\n  '
    assert (evalpy((for9 + 'if i==4:break\n  print(i)')) == '0\n1\n2\n3')
    assert (evalpy((for9 + 'if i<5:continue\n  print(i)')) == '5\n6\n7\n8')
    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')
    code = py2js(self.method_for)
    assert (evaljs(('%s method_for()' % code)) == 'ok\nok\nnull')
    assert (evalpy('for i, j in [[1, 2], [3, 4]]: print(i+j)') == '3\n7')
    assert (evalpy('for i, j, k in [[1, 2, 3], [3, 4, 5]]: print(i+j+k)') == '6\n12')

dataset/ETHPy150Open zoofIO/flexx/flexx/pyscript/tests/test_parser2.py TestConrolFlow.test_for/original