test.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/usr/bin/env python3
  2. import scroller
  3. import sys
  4. from unittest import TestCase, main
  5. from unittest.mock import patch, call
  6. class ScrollerTest(TestCase):
  7. _TEST_STRING = 'argument clinic'
  8. _TEST_STRING_PERMUTED = 'rgument clinica'
  9. def setUp(self):
  10. self.test_str = self.__class__._TEST_STRING
  11. self.test_str_permuted = self.__class__._TEST_STRING_PERMUTED
  12. def test_permute(self):
  13. string = scroller.permute(self.test_str)
  14. self.assertEqual(string, self.test_str_permuted)
  15. string = scroller.permute(string, rev=True)
  16. self.assertEqual(string, self.test_str)
  17. def test_scroll(self):
  18. scrolled = scroller.scroll(self.test_str)
  19. string = self.test_str
  20. for _ in range(len(self.test_str)):
  21. string = scroller.permute(string)
  22. self.assertEqual(next(scrolled), string)
  23. def test_scroll_reverse(self):
  24. scrolled = scroller.scroll(self.test_str)
  25. test_length = len(self.test_str) - 1
  26. for _ in range(test_length):
  27. string = next(scrolled)
  28. scrolled = scroller.scroll(string, rev=True)
  29. for _ in range(test_length):
  30. string = next(scrolled)
  31. self.assertEqual(string, self.test_str)
  32. def test_scroll_separator(self):
  33. sep = ' '
  34. scrolled = scroller.scroll(self.test_str, sep=sep)
  35. string = self.test_str + sep
  36. for _ in range(len(self.test_str)):
  37. string = scroller.permute(string)
  38. self.assertEqual(next(scrolled), string)
  39. def test_scroll_separator_when_reversed(self):
  40. sep = ' '
  41. scrolled = scroller.scroll(self.test_str, sep=sep, rev=True)
  42. string = self.test_str + sep
  43. for i in range(len(self.test_str)):
  44. string = scroller.permute(string, rev=True)
  45. self.assertEqual(next(scrolled), string)
  46. def test_scroll_static(self):
  47. scrolled = scroller.scroll(self.test_str, static=True)
  48. for i in range(len(self.test_str)):
  49. self.assertEqual(next(scrolled), self.test_str)
  50. def test_scroll_static_when_reversed(self):
  51. scrolled = scroller.scroll(self.test_str, static=True, rev=True)
  52. for i in range(len(self.test_str)):
  53. self.assertEqual(next(scrolled), self.test_str)
  54. def test_scroller_count(self):
  55. s = scroller.scroller(self.test_str, count=len(self.test_str))
  56. string = self.test_str
  57. for x in s:
  58. string = x
  59. self.assertEqual(string, self.test_str)
  60. def test_scroller(self):
  61. s = scroller.scroller(self.test_str, count=len(self.test_str))
  62. e = self.test_str
  63. for a in s:
  64. e = scroller.permute(e)
  65. self.assertEqual(a, e)
  66. def test_scroller_when_reversed(self):
  67. s = scroller.scroller(
  68. self.test_str,
  69. count=len(self.test_str),
  70. rev=True
  71. )
  72. e = self.test_str
  73. for a in s:
  74. e = scroller.permute(e, rev=True)
  75. self.assertEqual(a, e)
  76. def test_scroller_static(self):
  77. s = scroller.scroller(
  78. self.test_str,
  79. count=len(self.test_str),
  80. static=True
  81. )
  82. e = self.test_str
  83. for a in s:
  84. self.assertEqual(a, e)
  85. def test_scroller_static_when_reversed(self):
  86. s = scroller.scroller(self.test_str, count=3, static=True, rev=True)
  87. e = self.test_str
  88. for a in s:
  89. self.assertEqual(a, e)
  90. class ScrollerCLITest(TestCase):
  91. _TEST_STRING = 'gumby brain specialist'
  92. def setUp(self):
  93. self.test_str = self.__class__._TEST_STRING
  94. @patch('time.sleep', autospec=True)
  95. @patch('builtins.print', autospec=True)
  96. def test_count(self, mock_print, mock_sleep):
  97. c = 5
  98. args = scroller.parser.parse_args(['-c', str(c)])
  99. scroller.main(self.test_str, args)
  100. self.assertEqual(mock_print.call_count, c)
  101. @patch('time.sleep', autospec=True)
  102. @patch('builtins.print', autospec=True)
  103. def test_length_when_should_not_be_static(self, mock_print, mock_sleep):
  104. l = len(self.test_str)
  105. args = scroller.parser.parse_args(['-l', str(l), '-c', '5'])
  106. scroller.main(self.test_str, args)
  107. self.assertNotEqual(mock_print.call_args, self.test_str)
  108. @patch('time.sleep', autospec=True)
  109. @patch('builtins.print', autospec=True)
  110. def test_length_when_should_be_static(self, mock_print, mock_sleep):
  111. l = len(self.test_str) + 2
  112. c = 2
  113. args = scroller.parser.parse_args([
  114. '-l', str(l),
  115. '-c', str(c),
  116. '-s', ''
  117. ])
  118. scroller.main(self.test_str, args)
  119. expected_call = call(self.test_str, end='\n')
  120. mock_print.assert_has_calls([expected_call for i in range(c)])
  121. @patch('time.sleep', autospec=True)
  122. @patch('builtins.print', autospec=True)
  123. def test_interval(self, mock_print, mock_sleep):
  124. i = 2
  125. args = scroller.parser.parse_args(['-c', '1', '-i', str(i)])
  126. scroller.main(self.test_str, args)
  127. mock_sleep.assert_called_with(i)
  128. @patch('time.sleep', autospec=True)
  129. @patch('builtins.print', autospec=True)
  130. def test_reverse(self, mock_print, mock_sleep):
  131. args = scroller.parser.parse_args(['-c', '1', '-s', '', '-r'])
  132. scroller.main(self.test_str, args)
  133. (calling_args,), _ = mock_print.call_args
  134. p = scroller.permute(self.test_str, rev=True)
  135. self.assertEqual(calling_args, p)
  136. @patch('time.sleep', autospec=True)
  137. @patch('builtins.print', autospec=True)
  138. def test_separator(self, mock_print, mock_sleep):
  139. s = '_'
  140. args = scroller.parser.parse_args(['-c', '1', '-s', s, '-r'])
  141. scroller.main(self.test_str, args)
  142. (calling_args,), _ = mock_print.call_args
  143. self.assertEqual(calling_args, s + self.test_str)
  144. @patch('time.sleep', autospec=True)
  145. @patch('builtins.print', autospec=True)
  146. def test_newline(self, mock_print, mock_sleep):
  147. args = scroller.parser.parse_args(['-c', '1'])
  148. scroller.main(' ', args)
  149. _, calling_kwargs = mock_print.call_args
  150. self.assertEqual(calling_kwargs['end'], '\n')
  151. @patch('time.sleep', autospec=True)
  152. @patch('builtins.print', autospec=True)
  153. @patch('sys.stdout.write', autospe=True)
  154. @patch('builtins.input', autospec=True)
  155. @patch('select.select', autospec=True)
  156. def test_open(self, mock_select, mock_input, mock_write,
  157. mock_print, mock_sleep):
  158. mock_input.side_effect = ['life of brian', 'flying circus']
  159. def gen_side_effect():
  160. yield ([sys.stdin], [], [])
  161. while True:
  162. yield ([], [], [])
  163. mock_select.side_effect = gen_side_effect()
  164. args = scroller.parser.parse_args(['-c', '2', '-o', '-m'])
  165. scroller.main(args=args)
  166. self.assertEqual(mock_input.call_count, 2)
  167. self.assertTrue(mock_write.called)
  168. self.assertTrue(mock_sleep.called)
  169. @patch('time.sleep', autospec=True)
  170. @patch('builtins.print', autospec=True)
  171. def test_prefix(self, mock_print, mock_sleep):
  172. args = scroller.parser.parse_args([
  173. '-c', '1',
  174. '-b', 'prefix',
  175. '-s', ''
  176. ])
  177. scroller.main(' ', args)
  178. calling_args, _ = mock_print.call_args
  179. self.assertEqual(calling_args[0], 'prefix ')
  180. @patch('time.sleep', autospec=True)
  181. @patch('builtins.print', autospec=True)
  182. def test_postfix(self, mock_print, mock_sleep):
  183. args = scroller.parser.parse_args([
  184. '-c', '1',
  185. '-a', 'postfix',
  186. '-s', ''
  187. ])
  188. scroller.main(' ', args)
  189. calling_args, _ = mock_print.call_args
  190. self.assertEqual(calling_args[0], ' postfix')
  191. if __name__ == '__main__':
  192. main()