1+ import importlib
2+ import io
3+ import os
4+ import sys
5+ from pathlib import Path
6+ from unittest import TestCase , skipIf
7+ from unittest .mock import patch
8+
9+ file_path = Path (__file__ ).absolute ().parent / 'main.py'
10+ is_file_empty = os .stat (file_path ).st_size == 0
11+
12+
13+ @skipIf (is_file_empty , 'Empty file. Test 012 Skipped' )
14+ class TestWhatColorIsThatSquare (TestCase ):
15+
16+ def setUp (self ) -> None :
17+ self .module_name = 'projects.m1.012-what-color-is-that-square.python.main'
18+
19+ @patch ('builtins.input' )
20+ def test_ok_white (self , mock_input ):
21+ """
22+ Check if return 'white' in the output
23+ """
24+
25+ squares = ['a2' , 'a4' , 'a6' , 'a8' ,
26+ 'b1' , 'b3' , 'b5' , 'b7' ,
27+ 'c2' , 'c4' , 'c6' , 'c8' ,
28+ 'd1' , 'd3' , 'd5' , 'd7' ,
29+ 'e2' , 'e4' , 'e6' , 'e8' ,
30+ 'f1' , 'f3' , 'f5' , 'f7' ,
31+ 'g2' , 'g4' , 'g6' , 'g8' ,
32+ 'h1' , 'h3' , 'h5' , 'h7' ]
33+
34+ for square in squares :
35+ mock_input .return_value = square
36+
37+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
38+ sys .modules .pop (self .module_name , None )
39+ importlib .import_module (name = self .module_name , package = 'files' )
40+
41+ result = mock_print .getvalue ().lower ()
42+ self .assertIn ('white' , result , f'The { square } is white.' )
43+
44+ @patch ('builtins.input' )
45+ def test_ok_black (self , mock_input ):
46+ """
47+ Check if return 'black' in the output
48+ """
49+
50+ squares = ['a1' , 'a3' , 'a5' , 'a7' ,
51+ 'b2' , 'b4' , 'b6' , 'b8' ,
52+ 'c1' , 'c3' , 'c5' , 'c7' ,
53+ 'd2' , 'd4' , 'd6' , 'd8' ,
54+ 'e1' , 'e3' , 'e5' , 'e7' ,
55+ 'f2' , 'f4' , 'f6' , 'f8' ,
56+ 'g1' , 'g3' , 'g5' , 'g7' ,
57+ 'h2' , 'h4' , 'h6' , 'h8' ]
58+
59+ for square in squares :
60+ mock_input .return_value = square
61+
62+ with patch ('sys.stdout' , new_callable = io .StringIO ) as mock_print :
63+ sys .modules .pop (self .module_name , None )
64+ importlib .import_module (name = self .module_name , package = 'files' )
65+
66+ result = mock_print .getvalue ().lower ()
67+ self .assertIn ('black' , result , f'The { square } is black.' )
0 commit comments