Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.10 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import io
  3. import grep
  4.  
  5.  
  6. def test_integrate_stdin_grep(monkeypatch, capsys):
  7. monkeypatch.setattr('sys.stdin', io.StringIO(
  8. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  9. grep.main(['needle?'])
  10. out, err = capsys.readouterr()
  11. assert err == ''
  12. assert out == 'pref needle?\nneedle? suf\npref needle? suf\n'
  13.  
  14.  
  15. def test_integrate_stdin_regex_grep(monkeypatch, capsys):
  16. monkeypatch.setattr('sys.stdin', io.StringIO(
  17. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  18. grep.main(['-E', 'needle?'])
  19. out, err = capsys.readouterr()
  20. assert err == ''
  21. assert out == 'pref needle?\nneedle? suf\nthe needl\npref needle? suf\n'
  22.  
  23.  
  24. def test_integrate_stdin_grep_count(monkeypatch, capsys):
  25. monkeypatch.setattr('sys.stdin', io.StringIO(
  26. 'pref needle\nneedle suf\nthe needl\npref needle suf'))
  27. grep.main(['-c', 'needle'])
  28. out, err = capsys.readouterr()
  29. assert err == ''
  30. assert out == '3\n'
  31.  
  32.  
  33. def test_integrate_file_grep(tmp_path, monkeypatch, capsys):
  34. (tmp_path / 'a.txt').write_text('the needl\npref needle suf')
  35. monkeypatch.chdir(tmp_path)
  36. grep.main(['needle', 'a.txt'])
  37. out, err = capsys.readouterr()
  38. assert err == ''
  39. assert out == 'pref needle suf\n'
  40.  
  41.  
  42. def test_integrate_files_grep(tmp_path, monkeypatch, capsys):
  43. (tmp_path / 'a.txt').write_text('pref needle\nneedle suf\n')
  44. (tmp_path / 'b.txt').write_text('the needl\npref needle suf')
  45. monkeypatch.chdir(tmp_path)
  46. grep.main(['needle', 'b.txt', 'a.txt'])
  47. out, err = capsys.readouterr()
  48. assert err == ''
  49. assert out == 'b.txt:pref needle suf\na.txt:pref needle\na.txt:needle suf\n'
  50.  
  51.  
  52. def test_integrate_files_grep_count(tmp_path, monkeypatch, capsys):
  53. (tmp_path / 'a.txt').write_text('pref needle\nneedle suf\n')
  54. (tmp_path / 'b.txt').write_text('the needl\npref needle suf')
  55. monkeypatch.chdir(tmp_path)
  56. grep.main(['-c', 'needle', 'b.txt', 'a.txt'])
  57. out, err = capsys.readouterr()
  58. assert err == ''
  59. assert out == 'b.txt:1\na.txt:2\n'
  60.  
  61. # UNIT TESTS START
  62.  
  63.  
  64. def test_print_in_files_one(capsys): # FILES = 1
  65. file = 'a.txt'
  66. line = 'found'
  67. files = 1
  68. grep.print_in_files(files, file, line)
  69. out, err = capsys.readouterr()
  70. assert err == ''
  71. assert out == 'a.txt:found\n'
  72.  
  73.  
  74. def test_print_in_files_zero(capsys): # FILES = 0
  75. file = 'a.txt'
  76. line = 'found'
  77. files = 0
  78. grep.print_in_files(files, file, line)
  79. out, err = capsys.readouterr()
  80. assert err == ''
  81. assert out == 'found\n'
  82.  
  83.  
  84. def test_working_with_one_file(tmp_path, monkeypatch, capsys): # NOT (COUNTER AND REGEX)
  85. pattern = 'needle'
  86. (tmp_path / 'a.txt').write_text('the needl\npref needle suf')
  87. monkeypatch.chdir(tmp_path)
  88. files = ['a.txt']
  89. regex = 0
  90. count = 0
  91. grep.working_with_files(files, pattern, regex, count)
  92. out, err = capsys.readouterr()
  93. assert err == ''
  94. assert out == 'pref needle suf\n'
  95.  
  96.  
  97. def test_working_with_one_file_count(tmp_path, monkeypatch, capsys): # COUNTER
  98. pattern = 'needle'
  99. (tmp_path / 'a.txt').write_text('the needl\npref needle suf')
  100. monkeypatch.chdir(tmp_path)
  101. files = ['a.txt']
  102. regex = 0
  103. count = 1
  104. grep.working_with_files(files, pattern, regex, count)
  105. out, err = capsys.readouterr()
  106. assert err == ''
  107. assert out == '1\n'
  108.  
  109.  
  110. def test_working_with_one_file_regex(tmp_path, monkeypatch, capsys): # REGEX
  111. pattern = 'needle'
  112. (tmp_path / 'a.txt').write_text('pref needle?\nneedle? suf\nthe needl\npref needle? suf')
  113. monkeypatch.chdir(tmp_path)
  114. files = ['a.txt']
  115. regex = 1
  116. count = 0
  117. grep.working_with_files(files, pattern, regex, count)
  118. out, err = capsys.readouterr()
  119. assert err == ''
  120. assert out == 'pref needle?\nneedle? suf\nthe needl\npref needle? suf\n'
  121.  
  122.  
  123. def test_working_with_one_file_count_regex(tmp_path, monkeypatch, capsys): # REGEX AND COUNT
  124. pattern = 'needle'
  125. (tmp_path / 'a.txt').write_text('pref needle?\nneedle? suf\nthe needl\npref needle? suf')
  126. monkeypatch.chdir(tmp_path)
  127. files = ['a.txt']
  128. regex = 1
  129. count = 1
  130. grep.working_with_files(files, pattern, regex, count)
  131. out, err = capsys.readouterr()
  132. assert err == ''
  133. assert out == 'pref needle?\nneedle? suf\nthe needl\npref needle? suf\n1\n'
  134.  
  135.  
  136. def test_working_with_many_files(tmp_path, monkeypatch, capsys): # NOT (COUNTER AND REDEX)
  137. pattern = 'needle'
  138. (tmp_path / 'a.txt').write_text('pref needle\nneedle suf\n')
  139. (tmp_path / 'b.txt').write_text('the needl\npref needle suf')
  140. monkeypatch.chdir(tmp_path)
  141. files = ['a.txt', 'b.txt']
  142. regex = 0
  143. count = 0
  144. grep.working_with_files(files, pattern, regex, count)
  145. out, err = capsys.readouterr()
  146. assert err == ''
  147. assert out == 'a.txt:pref needle\na.txt:needle suf\nb.txt:pref needle suf\n'
  148.  
  149.  
  150. def test_working_with_many_files_count(tmp_path, monkeypatch, capsys): # COUNTER
  151. needle = 'needle'
  152. (tmp_path / 'a.txt').write_text('pref needle\nneedle suf\n')
  153. (tmp_path / 'b.txt').write_text('the needl\npref needle suf')
  154. monkeypatch.chdir(tmp_path)
  155. files = ['a.txt', 'b.txt']
  156. regex = 0
  157. count = 1
  158. grep.working_with_files(files, needle, regex, count)
  159. out, err = capsys.readouterr()
  160. assert err == ''
  161. assert out == 'a.txt:2\nb.txt:1\n'
  162.  
  163.  
  164. def test_working_with_many_files_regex(tmp_path, monkeypatch, capsys): # REGEX
  165. needle = 'needle'
  166. (tmp_path / 'a.txt').write_text('pref needle?\nneedle? suf')
  167. (tmp_path / 'b.txt').write_text('pref needle?')
  168. monkeypatch.chdir(tmp_path)
  169. files = ['a.txt', 'b.txt']
  170. regex = 1
  171. count = 0
  172. grep.working_with_files(files, needle, regex, count)
  173. out, err = capsys.readouterr()
  174. assert err == ''
  175. assert out == 'a.txt:pref needle?\nneedle? suf\nb.txt:pref needle?\nneedle? suf\n'
  176.  
  177.  
  178. def test_working_with_many_files_regex_count(tmp_path, monkeypatch, capsys): # REGEX AND COUNT
  179. needle = 'needle'
  180. (tmp_path / 'a.txt').write_text('pref needle?\nneedle?')
  181. (tmp_path / 'b.txt').write_text('pref needle?\nneedle?')
  182. monkeypatch.chdir(tmp_path)
  183. files = ['a.txt', 'b.txt']
  184. regex = 1
  185. count = 1
  186. grep.working_with_files(files, needle, regex, count)
  187. out, err = capsys.readouterr()
  188. assert err == ''
  189. assert out == 'a.txt:pref needle?\nneedle?\n2\nb.txt: pref needle?\n1\n'
  190.  
  191.  
  192. def test_working_with_stdin(monkeypatch, capsys): # NOT (REGEX AND COUNT)
  193. needle = 'needle'
  194. monkeypatch.setattr('sys.stdin', io.StringIO(
  195. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  196. count = 0
  197. regex = 0
  198. grep.working_with_stdin(needle, regex, count)
  199. out, err = capsys.readouterr()
  200. assert err == ''
  201. assert out == 'pref needle?\nneedle? suf\npref needle? suf\n'
  202.  
  203.  
  204. def test_working_with_stdin_count(monkeypatch, capsys): # COUNT
  205. needle = 'needle'
  206. monkeypatch.setattr('sys.stdin', io.StringIO(
  207. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  208. count = 1
  209. regex = 0
  210. grep.working_with_stdin(needle, regex, count)
  211. out, err = capsys.readouterr()
  212. assert err == ''
  213. assert out == '3\n'
  214.  
  215.  
  216. def test_working_with_stdin_regex(monkeypatch, capsys): # REGEX
  217. needle = 'needle'
  218. monkeypatch.setattr('sys.stdin', io.StringIO(
  219. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  220. count = 0
  221. regex = 1
  222. grep.working_with_stdin(needle, regex, count)
  223. out, err = capsys.readouterr()
  224. assert err == ''
  225. assert out == 'pref needle?\nneedle? suf\nthe needl\npref needle? suf\n'
  226.  
  227.  
  228. def test_working_with_stdin_regex_count(monkeypatch, capsys): # REGEX AND COUNT
  229. needle = 'needle'
  230. monkeypatch.setattr('sys.stdin', io.StringIO(
  231. 'pref needle?\nneedle? suf\nthe needl\npref needle? suf'))
  232. count = 1
  233. regex = 1
  234. grep.working_with_stdin(needle, regex, count)
  235. out, err = capsys.readouterr()
  236. assert err == ''
  237. assert out == 'pref needle?\nneedle? suf\nthe needl\npref needle? suf\n3\n'
  238.  
  239.  
  240. # UNIT TESTS END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement