1- import importlib .util
2- import io
3- import os
4- from contextlib import redirect_stdout
5- from unittest .mock import patch
6-
7-
8- def _load_converter_module ():
9- file_path = os .path .join (
10- os .path .dirname (__file__ ), ".." ,
11- "utilities" , "Number-System-Converter" , "Number-System-Converter.py"
12- )
13- file_path = os .path .abspath (file_path )
14-
15- spec = importlib .util .spec_from_file_location ("number_system_converter" , file_path )
16- module = importlib .util .module_from_spec (spec )
17-
18- with patch ("builtins.input" , side_effect = ["5" ]):
19- with patch ("builtins.print" ):
20- spec .loader .exec_module (module )
21-
22- return module
23-
24-
25- _mod = _load_converter_module ()
26-
27-
28- def _capture_output (func , * args ):
29- buffer = io .StringIO ()
30- with redirect_stdout (buffer ):
31- func (* args )
32- return buffer .getvalue ()
33-
34-
35- class TestNumberSystemConverter :
36- def test_decimal_to_others (self ):
37- output = _capture_output (_mod .decimal_to_others , "10" )
38- assert "Binary (Base 2) : 1010" in output
39- assert "Octal (Base 8)" in output
40- assert "Hex (Base 16)" in output
41-
42- def test_binary_to_others (self ):
43- output = _capture_output (_mod .binary_to_others , "1010" )
44- assert "Decimal (Base 10) : 10" in output
45- assert "Octal (Base 8)" in output
46- assert "Hex (Base 16)" in output
47-
48- def test_octal_to_others (self ):
49- output = _capture_output (_mod .octal_to_others , "17" )
50- assert "Decimal (Base 10) : 15" in output
51- assert "Binary (Base 2)" in output
52- assert "Hex (Base 16)" in output
53-
54- def test_hex_to_others (self ):
55- output = _capture_output (_mod .hex_to_others , "1a" )
56- assert "Conversions for Hexadecimal: 1A" in output
57- assert "Decimal (Base 10) : 26" in output
58- assert "Binary (Base 2)" in output
59-
60- def test_invalid_input_is_handled (self ):
61- output = _capture_output (_mod .decimal_to_others , "not-a-number" )
62- assert "Invalid input! Please enter a valid decimal integer." in output
1+ import os
2+ import subprocess
3+ import sys
4+ import unittest
5+
6+ class TestNumberSystemConverter (unittest .TestCase ):
7+ def setUp (self ):
8+ self .script_path = os .path .abspath (os .path .join (
9+ os .path .dirname (__file__ ), ".." ,
10+ "utilities" , "Number-System-Converter" , "Number-System-Converter.py"
11+ ))
12+
13+ def run_script_with_inputs (self , inputs ):
14+ # Join inputs with newlines
15+ input_data = "\n " .join (inputs ) + "\n "
16+ env = os .environ .copy ()
17+ env ["PYTHONIOENCODING" ] = "utf-8"
18+ process = subprocess .Popen (
19+ [sys .executable , self .script_path ],
20+ stdin = subprocess .PIPE ,
21+ stdout = subprocess .PIPE ,
22+ stderr = subprocess .PIPE ,
23+ env = env ,
24+ encoding = "utf-8"
25+ )
26+ stdout , stderr = process .communicate (input = input_data , timeout = 5 )
27+ return stdout , stderr
28+
29+ def test_decimal_to_others (self ):
30+ # Input choice 1 (Decimal), then value 10, then choice 5 (Exit)
31+ stdout , stderr = self .run_script_with_inputs (["1" , "10" , "5" ])
32+ self .assertIn ("Binary (Base 2) : 1010" , stdout )
33+ self .assertIn ("Octal (Base 8)" , stdout )
34+ self .assertIn ("Hex (Base 16)" , stdout )
35+
36+ def test_binary_to_others (self ):
37+ # Input choice 2 (Binary), then value 1010, then choice 5 (Exit)
38+ stdout , stderr = self .run_script_with_inputs (["2" , "1010" , "5" ])
39+ self .assertIn ("Decimal (Base 10) : 10" , stdout )
40+ self .assertIn ("Octal (Base 8)" , stdout )
41+ self .assertIn ("Hex (Base 16)" , stdout )
42+
43+ def test_octal_to_others (self ):
44+ # Input choice 3 (Octal), then value 17, then choice 5 (Exit)
45+ stdout , stderr = self .run_script_with_inputs (["3" , "17" , "5" ])
46+ self .assertIn ("Decimal (Base 10) : 15" , stdout )
47+ self .assertIn ("Binary (Base 2)" , stdout )
48+ self .assertIn ("Hex (Base 16)" , stdout )
49+
50+ def test_hex_to_others (self ):
51+ # Input choice 4 (Hex), then value 1a, then choice 5 (Exit)
52+ stdout , stderr = self .run_script_with_inputs (["4" , "1a" , "5" ])
53+ self .assertIn ("Conversions for Hexadecimal: 1A" , stdout )
54+ self .assertIn ("Decimal (Base 10) : 26" , stdout )
55+ self .assertIn ("Binary (Base 2)" , stdout )
56+
57+ def test_invalid_input_is_handled (self ):
58+ # Input choice 1 (Decimal), then value "not-a-number", then choice 5 (Exit)
59+ stdout , stderr = self .run_script_with_inputs (["1" , "not-a-number" , "5" ])
60+ self .assertIn ("Invalid input! Please enter a valid decimal integer." , stdout )
0 commit comments