Skip to content

Commit 678bad9

Browse files
Merge pull request #3213 from isabel-lombardi/add-test-m1-012
add test m1 012
2 parents 6f9de2a + e4618c7 commit 678bad9

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
# What Color Is That Square?
22

3-
Positions on a chess board are identified by a letter and a number.
3+
Positions on a chess board are identified by a letter and a number.
44
The letter identifies the column, while the number identifies the row, as shown below:
55

66
<img src="https://upload.wikimedia.org/wikipedia/commons/5/5b/Chess-board-with-letters_nevit_111.svg" />
77

8-
Write a program that reads a position from the user.
9-
Use an if statement to determine if the column begins with a black square or a white square.
8+
Write a program that reads a position from the user.
9+
Use an if statement to determine **if the column begins with a black square or a white square**.
1010
Then use modular arithmetic to report the color of the square in that row.
11-
For example, if the user enters a1 then your program should report that the square is black.
12-
If the user enters d5 then your program should report that the square is white.
1311
Your program may assume that a valid position will always be entered.
1412
It does not need to perform any error checking.
1513

14+
Example:
15+
Input = a1
16+
Output = black
17+
18+
Input = d5
19+
Output = white
20+
21+
1622
# Documentation
1723

1824
For this project solution you may use:
@@ -22,6 +28,13 @@ For this project solution you may use:
2228
- Iteration
2329
- Strings
2430

31+
32+
# Test
33+
Execute the test to validate your solution.
34+
35+
- **Python**: `python -m unittest python/test_what_color_is_that_square.py`
36+
37+
2538
# Deadline
2639

2740
This project requires to be completed in a maximum of **4 hours**

projects/m1/012-what-color-is-that-square/__init__.py

Whitespace-only changes.

projects/m1/012-what-color-is-that-square/python/__init__.py

Whitespace-only changes.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)