I want to test some (python 3) code that directly uses the print and input functions. As I understand it, the easiest way to do this is by dependency injection: modifying the code so that it takes input and output streams as arguments, using sys.stdin and sys.stdout by default and passing in mock objects during testing. It's obvious what to do with print calls:
print(text)
#replaced with...
print(text, file=output_stream)
However, input doesn't have arguments for input and output streams. Does the following code correctly reproduce its behaviour?
text = input(prompt)
#replaced with...
print(prompt, file=output_stream, end='')
text = input_stream.readline()[:-1]
I had a look at the implementation of input, and it does quite a lot of magic, calling sys.stdin.fileno and examining sys.stdin.encoding and sys.stdin.errors rather than calling any of the read* methods - I wouldn't know where to start with mocking those.
input, so that makes it easier for me.input's behaviour.