From 62a4527a374b636c7565c924205836c80ceec4ed Mon Sep 17 00:00:00 2001 From: Fabien Benureau Date: Thu, 4 Jul 2013 10:34:00 +0100 Subject: [PATCH] method to set FakeInput package-wide --- tests/test_usecase.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/test_usecase.py b/tests/test_usecase.py index cccadb7..8f7ddbf 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -9,7 +9,7 @@ import fake_filesystem_glob from papers import papers_cmd from papers import color -from papers.p3 import io +from papers.p3 import io, input # code for fake fs @@ -84,11 +84,15 @@ def redirect(f): # automating input +real_input = input + class FakeInput(): """ Replace the input() command, and mock user input during tests Instanciate as : input = FakeInput(['yes', 'no']) + then replace the input command in every module of the package : + input.as_global() Then : input() returns 'yes' input() returns 'no' @@ -99,6 +103,10 @@ class FakeInput(): self.inputs = list(inputs) or [] self._cursor = 0 + def as_global(self): + for md in mod_list: + md.input = self + def add_input(self, inp): self.inputs.append(inp) @@ -123,6 +131,8 @@ def _execute_cmds(cmds, fs = None): return outs + # actual tests + class TestInit(unittest.TestCase): def test_init(self): @@ -173,6 +183,15 @@ class TestInput(unittest.TestCase): with self.assertRaises(IndexError): 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): def test_first(self):