Coverage for src/pytest_vulture/vulture/print_parser.py: 94.74%
19 statements
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-03 10:43 +0200
« prev ^ index » next coverage.py v6.5.0, created at 2022-10-03 10:43 +0200
1"""Manages what functions prints"""
2# mypy: ignore-errors
3import sys
5from typing import List
8class PrintParser:
9 """A class that get the printing of methods
10 Example::
11 >>> log = PrintParser()
12 >>> log.start()
13 >>> print("toto")
14 >>> print("tata")
15 >>> log.stop()
16 >>> print("tutu")
17 tutu
18 >>> log.messages
19 'toto\\ntata\\n'
21 """
22 stdout = sys.stdout
24 _messages: List[str] = []
25 message = ""
27 def start(self):
28 """Starting to save the prints and not show them in the console"""
29 self.stdout = sys.stdout
30 sys.stdout = self
31 self._messages = []
33 def stop(self):
34 """Stop saving the prints"""
35 sys.stdout = self.stdout
37 def getvalue(self):
38 """hacks"""
39 return self.message
41 def write(self, text):
42 """Does not write the text in the console, but saves it"""
43 self._messages.append(text)
45 @property
46 def messages(self) -> str:
47 """Get the printed outputs in a string"""
48 return "".join(self._messages)