http://www.mediawiki.org/wiki/Special:Code/pywikipedia/11630
Revision: 11630 Author: valhallasw Date: 2013-06-08 22:25:52 +0000 (Sat, 08 Jun 2013) Log Message: ----------- + UI input tests
Modified Paths: -------------- branches/rewrite/tests/ui_tests.py
Modified: branches/rewrite/tests/ui_tests.py =================================================================== --- branches/rewrite/tests/ui_tests.py 2013-06-08 22:20:26 UTC (rev 11629) +++ branches/rewrite/tests/ui_tests.py 2013-06-08 22:25:52 UTC (rev 11630) @@ -50,7 +50,7 @@ 'caller_line': 0, 'newline': "\n"}
- class TestTerminalUI(unittest.TestCase): + class TestTerminalOutput(unittest.TestCase): def setUp(self): patch() newstdout.truncate(0) @@ -161,7 +161,79 @@
self.assertNotEqual(stderrlines[-1], "\n")
+ class TestTerminalInput(unittest.TestCase): + def setUp(self): + patch() + newstdout.truncate(0) + newstderr.truncate(0) + newstdin.truncate(0)
+ def tearDown(self): + unpatch() + + def testInput(self): + newstdin.write("input to read\n") + newstdin.seek(0) + + returned = pywikibot.input("question") + + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "question ") + + self.assertIsInstance(returned, unicode) + self.assertEqual(returned, u"input to read") + + @unittest.expectedFailure + def testInputChoiceDefault(self): + newstdin.write("\n") + newstdin.seek(0) + + returned = pywikibot.inputChoice("question", ["answer 1", "answer 2", "answer 3"], ["A", "N", "S"], "A") + + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "question ([A]nswer 1, a[N]swer 2, an[S]wer 3) ") + + self.assertIsInstance(returned, unicode) + self.assertEqual(returned, "a") + + def testInputChoiceCapital(self): + newstdin.write("N\n") + newstdin.seek(0) + + returned = pywikibot.inputChoice("question", ["answer 1", "answer 2", "answer 3"], ["A", "N", "S"], "A") + + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "question ([A]nswer 1, a[N]swer 2, an[S]wer 3) ") + + self.assertIsInstance(returned, unicode) + self.assertEqual(returned, "n") + + def testInputChoiceNonCapital(self): + newstdin.write("n\n") + newstdin.seek(0) + + returned = pywikibot.inputChoice("question", ["answer 1", "answer 2", "answer 3"], ["A", "N", "S"], "A") + + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "question ([A]nswer 1, a[N]swer 2, an[S]wer 3) ") + + self.assertIsInstance(returned, unicode) + self.assertEqual(returned, "n") + + def testInputChoiceIncorrectAnswer(self): + newstdin.write("X\nN\n") + newstdin.seek(0) + + returned = pywikibot.inputChoice("question", ["answer 1", "answer 2", "answer 3"], ["A", "N", "S"], "A") + + self.assertEqual(newstdout.getvalue(), "") + self.assertEqual(newstderr.getvalue(), "question ([A]nswer 1, a[N]swer 2, an[S]wer 3) "*2) + + self.assertIsInstance(returned, unicode) + self.assertEqual(returned, "n") + + + try: try: unittest.main()
pywikipedia-svn@lists.wikimedia.org