test_python_startup.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import sys
  2. import unittest
  3. from python_startup import PromptCounter
  4. class RecordStdout(object):
  5. def __init__(self):
  6. self.value = None
  7. def write(self, value):
  8. self.value = value
  9. class TestPromptCounter(unittest.TestCase):
  10. def setUp(self):
  11. sys.stdout = RecordStdout()
  12. self.prompt = PromptCounter('in {} ', 'out {} ')
  13. self.assertEqual(1, self.prompt.count)
  14. def tearDown(self):
  15. sys.stdout = sys.__stdout__
  16. def test_call_does_not_increment_count(self):
  17. for x in range(10):
  18. self.prompt('test')
  19. self.assertEqual("out 0 'test'\n", sys.stdout.value)
  20. self.assertEqual(1, self.prompt.count)
  21. def test_str_does_increment_count(self):
  22. for x in range(10):
  23. out = str(self.prompt)
  24. self.assertEqual('in {} '.format(x + 1), out)
  25. self.assertEqual(11, self.prompt.count)
  26. def test_call_and_str(self):
  27. for x in range(10):
  28. out = str(self.prompt)
  29. self.prompt(x)
  30. self.assertEqual('in {} '.format(x + 1), out)
  31. self.assertEqual("out {} {}\n".format(x + 1, x), sys.stdout.value)
  32. def test_call_with_none(self):
  33. self.prompt(None)
  34. self.assertIsNone(sys.stdout.value)
  35. if __name__ == '__main__':
  36. unittest.main()