Coverage for src/pytest_vulture/vulture/print_parser.py: 94.74%

1"""Manages what functions prints"""

2# mypy: ignore-errors

3 import sys

4

5 from typing import List

6

7

8 class 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'

20

21 """

22 stdout = sys.stdout

23

24 _messages: List[str] = []

25 message = ""

26

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 = []

32

33 def stop(self):

34 """Stop saving the prints"""

35 sys.stdout = self.stdout

36

37 def getvalue(self):

38 """hacks"""

39 return self.message

40

41 def write(self, text):

42 """Does not write the text in the console, but saves it"""

43 self._messages.append(text)

44

45 @property

46 def messages(self) -> str:

47 """Get the printed outputs in a string"""

48 return "".join(self._messages)