1+ #!/usr/bin/env python
2+ #-*- coding: utf-8 -*-
3+
4+ import unittest
5+ from unittest .mock import patch
6+ from methods .patternprinting import print_pyramid
7+ from io import StringIO
8+ import sys
9+ import contextlib
10+
11+ class TestPrintPyramid (unittest .TestCase ):
12+ @patch ('builtins.print' )
13+ def test_patternprinting_numrows_zero_call_print_zero_times (self , mock_print ):
14+ print_pyramid (0 , '*' )
15+ self .assertEqual (mock_print .call_count , 0 )
16+
17+ def test_pyramid_pattern_printing_zerorows_asteriks (self ):
18+ expected_output = ''
19+
20+ with StringIO () as buffer , contextlib .redirect_stdout (buffer ):
21+ print_pyramid (0 , "*" )
22+ printed_output = buffer .getvalue ()
23+
24+ self .assertEqual (printed_output , expected_output )
25+
26+ @patch ('builtins.print' )
27+ def test_patternprinting_numrows_three_call_print_twenty_times (self , mock_print ):
28+ print_pyramid (5 , '*' )
29+ self .assertEqual (mock_print .call_count , 20 )
30+
31+ def test_pyramid_pattern_printing_fiverows_asteriks (self ):
32+ expected_output = (
33+ "*\n "
34+ "**\n "
35+ "***\n "
36+ "****\n "
37+ "*****\n "
38+ )
39+
40+ # Assert that the printed output contains the expected parameter
41+ with StringIO () as buffer , contextlib .redirect_stdout (buffer ):
42+ print_pyramid (5 , "*" )
43+ printed_output = buffer .getvalue ()
44+
45+ self .assertEqual (printed_output , expected_output )
46+
47+ @patch ('builtins.print' )
48+ def test_patternprinting_numrows_three_call_print_nine_times (self , mock_print ):
49+ print_pyramid (3 , '&' )
50+ self .assertEqual (mock_print .call_count , 9 )
51+
52+ def test_pyramid_pattern_printing_fiverows_ampersand (self ):
53+ expected_output = (
54+ "&\n "
55+ "&&\n "
56+ "&&&\n "
57+ )
58+
59+ # Assert that the printed output contains the expected parameter
60+ with StringIO () as buffer , contextlib .redirect_stdout (buffer ):
61+ print_pyramid (3 , "&" )
62+ printed_output = buffer .getvalue ()
63+
64+ self .assertEqual (printed_output , expected_output )
65+
66+ if __name__ == '__main__' :
67+ unittest .main ()
0 commit comments