scientific-programming-exer.../test/test_io.py

42 lines
894 B
Python

from io import StringIO
from util.io import readvalue
def test_io_readvalue_int():
fin = StringIO("10\n")
fout = StringIO()
result = readvalue("Enter an Integer ", int, fin=fin, fout=fout)
assert result == 10
def test_io_readvalue_msg():
fin = StringIO("10\n")
fout = StringIO()
readvalue("Enter an Integer ", int, fin=fin, fout=fout)
fout.seek(0, 0)
result = fout.read()
assert result == "Enter an Integer "
def test_io_readvalue_float():
fin = StringIO("10.1\n")
fout = StringIO()
result = readvalue("Enter a Float ", float, fin=fin, fout=fout)
assert result == 10.1
def test_io_readvalue_int_fail():
fin = StringIO("10.1\n10\n")
fout = StringIO()
readvalue("Enter an Integer ", int, fin=fin, fout=fout)
fout.seek(0, 0)
result = fout.read()
assert result == ("Enter an Integer "
r"invalid literal for int() with base 10: '10.1'"
"\nEnter an Integer ")