def test_option_json(assert_print, capfd): wrapper(b"print(j[0]['a'])", b'[{"a": 1}]', parse_json=True) assert_print("1") # x is not available with pytest.raises(NameError): wrapper(b"print(x)", b'[{"a": 1}]', parse_json=True) # you can't mix json parsing with iterable, full or split with pytest.raises(ValueError): wrapper(b"print(j[0]['a'])", b'[{"a": 1}]', parse_json=True, full=True) with pytest.raises(ValueError): wrapper(b"print(j[0]['a'])", b'[{"a": 1}]', parse_json=True, split="-") with pytest.raises(ValueError): wrapper(b"print(j[0]['a'])", b'[{"a": 1}]', parse_json=True, iterable=True) # works with charset wrapper(b"print(j[0]['a'])", '[{"a": "é"}]'.encode('cp1252'), parse_json=True, stdin_charset='cp1252') assert_print("é") # works with autoprint wrapper(b"j[0]['a']", b'[{"a": "1"}]', parse_json=True, autoprint="True") assert_print("1") # rstrip should have no effect execute_statements([b"print(j[0]['a'])"], BytesIO(b'[{"a": 1}]'), parse_json=True, rstrip="") resout, reserr = capfd.readouterr() assert resout == "1\n"
def test_option_rstrip(capfd): execute_statements([b"print(x)"], BytesIO(b"1\n2\n")) resout, reserr = capfd.readouterr() assert resout == "1\n2\n" execute_statements([b"print(x)"], BytesIO(b"1\n2\n"), rstrip="") resout, reserr = capfd.readouterr() assert resout == "1\n\n2\n\n" execute_statements([b"print(x)"], BytesIO(b"1.\n2.\n"), rstrip=".\n") resout, reserr = capfd.readouterr() assert resout == "1\n2\n"
def wrapper(statements, stdin=None, *args, **kwargs): """ A wrapper around pyped.execute_statements() to make testing easier. Convert scalar arguments in lists when required, and strings to file like objects if necessary. Add an empty list, named "lst", to the context so it can be filled with values """ if isinstance(statements, six.binary_type): statements = [statements] if stdin: if not isinstance(stdin, six.binary_type): stdin = b"\n".join(stdin) stdin = BytesIO(stdin) return execute_statements(statements, stdin, *args, **kwargs)
def test_option_full(capfd): execute_statements([b"print(stdin)"], BytesIO(b"1\n2\n"), full=True) resout, reserr = capfd.readouterr() assert resout == "1\n2\n\n" # x should not exists with pytest.raises(NameError): execute_statements([b"print(x)"], BytesIO(b"1\n2\n"), full=True) # split should work execute_statements([b"print(f[0])"], BytesIO(b"1-\n" + b"2\n"), full=True, split=b"-") resout, reserr = capfd.readouterr() assert resout == "1\n" # rstrip should have no effect execute_statements([b"print(stdin)"], BytesIO(b"1\n2\n"), full=True, rstrip=b"") resout, reserr = capfd.readouterr() assert resout == "1\n2\n\n" # you should not be able to set full and iterable together with pytest.raises(ValueError): execute_statements([b"print(x)"], BytesIO(b"1\n2\n"), full=True, iterable=True) # decode works with full execute_statements([b"print(stdin)"], BytesIO("é".encode('cp1252')), full=True, stdin_charset="cp1252") resout, reserr = capfd.readouterr() assert resout == "é\n" # autoprint works with full execute_statements([b"stdin"], BytesIO(b"1"), autoprint=True, full=True) resout, reserr = capfd.readouterr() assert resout == "1\n"