method to set FakeInput package-wide
This commit is contained in:
parent
d109d93341
commit
62a4527a37
@ -9,7 +9,7 @@ import fake_filesystem_glob
|
|||||||
|
|
||||||
from papers import papers_cmd
|
from papers import papers_cmd
|
||||||
from papers import color
|
from papers import color
|
||||||
from papers.p3 import io
|
from papers.p3 import io, input
|
||||||
|
|
||||||
|
|
||||||
# code for fake fs
|
# code for fake fs
|
||||||
@ -84,11 +84,15 @@ def redirect(f):
|
|||||||
|
|
||||||
# automating input
|
# automating input
|
||||||
|
|
||||||
|
real_input = input
|
||||||
|
|
||||||
class FakeInput():
|
class FakeInput():
|
||||||
""" Replace the input() command, and mock user input during tests
|
""" Replace the input() command, and mock user input during tests
|
||||||
|
|
||||||
Instanciate as :
|
Instanciate as :
|
||||||
input = FakeInput(['yes', 'no'])
|
input = FakeInput(['yes', 'no'])
|
||||||
|
then replace the input command in every module of the package :
|
||||||
|
input.as_global()
|
||||||
Then :
|
Then :
|
||||||
input() returns 'yes'
|
input() returns 'yes'
|
||||||
input() returns 'no'
|
input() returns 'no'
|
||||||
@ -99,6 +103,10 @@ class FakeInput():
|
|||||||
self.inputs = list(inputs) or []
|
self.inputs = list(inputs) or []
|
||||||
self._cursor = 0
|
self._cursor = 0
|
||||||
|
|
||||||
|
def as_global(self):
|
||||||
|
for md in mod_list:
|
||||||
|
md.input = self
|
||||||
|
|
||||||
def add_input(self, inp):
|
def add_input(self, inp):
|
||||||
self.inputs.append(inp)
|
self.inputs.append(inp)
|
||||||
|
|
||||||
@ -123,6 +131,8 @@ def _execute_cmds(cmds, fs = None):
|
|||||||
return outs
|
return outs
|
||||||
|
|
||||||
|
|
||||||
|
# actual tests
|
||||||
|
|
||||||
class TestInit(unittest.TestCase):
|
class TestInit(unittest.TestCase):
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
@ -173,6 +183,15 @@ class TestInput(unittest.TestCase):
|
|||||||
with self.assertRaises(IndexError):
|
with self.assertRaises(IndexError):
|
||||||
input()
|
input()
|
||||||
|
|
||||||
|
def test_input(self):
|
||||||
|
other_input = FakeInput(['yes', 'no'])
|
||||||
|
other_input.as_global()
|
||||||
|
self.assertEqual(color.input(), 'yes')
|
||||||
|
self.assertEqual(color.input(), 'no')
|
||||||
|
with self.assertRaises(IndexError):
|
||||||
|
color.input()
|
||||||
|
|
||||||
|
|
||||||
class TestUsecase(unittest.TestCase):
|
class TestUsecase(unittest.TestCase):
|
||||||
|
|
||||||
def test_first(self):
|
def test_first(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user