| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
|
| 4 |
class AppTestRawInput(): |
| 5 |
|
| 6 |
def test_input_and_raw_input(self): |
| 7 |
import sys, io |
| 8 |
flushed = [False] |
| 9 |
class CheckFlushed(io.StringIO): |
| 10 |
def flush(self): |
| 11 |
flushed[0] = True |
| 12 |
super().flush() |
| 13 |
for prompt, expected in [("def:", "abc/def:/ghi\n"), |
| 14 |
("", "abc//ghi\n"), |
| 15 |
(42, "abc/42/ghi\n"), |
| 16 |
(None, "abc/None/ghi\n"), |
| 17 |
(Ellipsis, "abc//ghi\n")]: |
| 18 |
for inputfn, inputtext, gottext in [ |
| 19 |
(input, "foo\nbar\n", "foo")]: |
| 20 |
save = sys.stdin, sys.stdout, sys.stderr |
| 21 |
try: |
| 22 |
sys.stdin = io.StringIO(inputtext) |
| 23 |
out = sys.stdout = io.StringIO() |
| 24 |
|
| 25 |
flushed = [False] |
| 26 |
err = sys.stderr = CheckFlushed() |
| 27 |
sys.stderr.write('foo') |
| 28 |
print("abc", end='') |
| 29 |
out.write('/') |
| 30 |
if prompt is Ellipsis: |
| 31 |
got = inputfn() |
| 32 |
else: |
| 33 |
got = inputfn(prompt) |
| 34 |
out.write('/') |
| 35 |
print("ghi") |
| 36 |
finally: |
| 37 |
sys.stdin, sys.stdout, sys.stderr = save |
| 38 |
assert out.getvalue() == expected |
| 39 |
assert flushed[0] |
| 40 |
assert got == gottext |
| 41 |
|
| 42 |
def test_bad_fileno(self): |
| 43 |
import io |
| 44 |
import sys |
| 45 |
class BadFileno(io.StringIO): |
| 46 |
def fileno(self): |
| 47 |
1 / 0 |
| 48 |
stdin, sys.stdin = sys.stdin, BadFileno('foo') |
| 49 |
try: |
| 50 |
result = input() |
| 51 |
finally: |
| 52 |
sys.stdin = stdin |
| 53 |
assert result == 'foo' |