From d05f5fe4ba3a6e1a6bf74c0ff14b7ea5a791c05e Mon Sep 17 00:00:00 2001 From: netra_038 Date: Wed, 18 Nov 2026 13:19:53 +0530 Subject: [PATCH] test: document and verify number system converter Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/test_number_system_converter.py | 104 ++++++++++++++++++ .../Number-System-Converter.py | 1 + utilities/Number-System-Converter/README.md | 50 +++++++++ 3 files changed, 155 insertions(+) create mode 100644 tests/test_number_system_converter.py create mode 100644 utilities/Number-System-Converter/README.md diff --git a/tests/test_number_system_converter.py b/tests/test_number_system_converter.py new file mode 100644 index 00000000..0f2d9ce9 --- /dev/null +++ b/tests/test_number_system_converter.py @@ -0,0 +1,104 @@ +import importlib.util +import json +import os +from pathlib import Path + +import pytest + + +MODULE_PATH = ( + Path(__file__).resolve().parents[1] + / "utilities" + / "Number-System-Converter" + / "Number-System-Converter.py" +) +SPEC = importlib.util.spec_from_file_location("number_system_converter", MODULE_PATH) + +if SPEC is None or SPEC.loader is None: + raise RuntimeError(f"Unable to load converter module at {MODULE_PATH}") + +CONVERTER = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(CONVERTER) + + +def record_case(name, inputs, expected, actual): + results_path = os.environ.get("R26_T027_CASE_RESULTS") + if results_path is not None: + with Path(results_path).open("a", encoding="utf-8") as results_file: + results_file.write( + json.dumps( + { + "check": name, + "inputs": inputs, + "expected": expected, + "actual": actual, + } + ) + + "\n" + ) + + +@pytest.mark.parametrize( + ("choice", "value", "expected"), + [ + ("1", "42", ("101010", "52", "2A")), + ("2", "101010", ("42", "52", "2A")), + ("3", "52", ("42", "101010", "2A")), + ("4", "2a", ("42", "101010", "52")), + ], +) +def test_main_converts_each_supported_base( + monkeypatch, capsys, choice, value, expected +): + responses = iter((choice, value, "5")) + monkeypatch.setattr("builtins.input", lambda _prompt="": next(responses)) + + result = CONVERTER.main() + + output = capsys.readouterr().out + expected_result = { + "return": None, + "output_contains": [*expected, "Goodbye!"], + } + actual_result = { + "return": result, + "output_contains": [ + text for text in expected_result["output_contains"] if text in output + ], + "output": output, + } + record_case( + "test_main_converts_each_supported_base", + {"choice": choice, "value": value, "exit_choice": "5"}, + expected_result, + actual_result, + ) + assert result is None + assert all(value in output for value in expected) + assert "Goodbye!" in output + + +def test_main_reports_invalid_choice_and_number(monkeypatch, capsys): + responses = iter(("9", "1", "not-a-number", "5")) + monkeypatch.setattr("builtins.input", lambda _prompt="": next(responses)) + + CONVERTER.main() + + output = capsys.readouterr().out + expected = { + "output_contains": ["Invalid choice!", "valid decimal integer"], + } + actual = { + "output_contains": [ + text for text in expected["output_contains"] if text in output + ], + "output": output, + } + record_case( + "test_main_reports_invalid_choice_and_number", + {"choices_and_values": ["9", "1", "not-a-number", "5"]}, + expected, + actual, + ) + assert "Invalid choice!" in output + assert "valid decimal integer" in output diff --git a/utilities/Number-System-Converter/Number-System-Converter.py b/utilities/Number-System-Converter/Number-System-Converter.py index 4696f38a..5c59d293 100644 --- a/utilities/Number-System-Converter/Number-System-Converter.py +++ b/utilities/Number-System-Converter/Number-System-Converter.py @@ -1,4 +1,5 @@ def main(): + """Run the interactive number-system converter in the terminal.""" print("🔢 Number System Converter 🔢") print("Convert between Decimal, Binary, Octal, and Hexadecimal formats interactively!\n") diff --git a/utilities/Number-System-Converter/README.md b/utilities/Number-System-Converter/README.md new file mode 100644 index 00000000..3c48b407 --- /dev/null +++ b/utilities/Number-System-Converter/README.md @@ -0,0 +1,50 @@ +# 🔢 Number System Converter + +A beginner-friendly command-line utility for converting between decimal, +binary, octal, and hexadecimal numbers. + +## Requirements + +- Python 3.10 or newer +- No external libraries + +## Run + +From the repository root: + +```bash +python utilities/Number-System-Converter/Number-System-Converter.py +``` + +You can also run it from the project directory: + +```bash +cd utilities/Number-System-Converter +python Number-System-Converter.py +``` + +## Public API + +The module exposes `main()`. It returns `None`, prints prompts and conversion +results to standard output, and reads menu choices and values from standard +input. Running the file directly calls `main()` automatically. + +Because the filename contains hyphens, use `importlib` rather than a normal +Python import statement: + +```python +import importlib.util +from pathlib import Path + +path = Path("utilities/Number-System-Converter/Number-System-Converter.py") +spec = importlib.util.spec_from_file_location("number_system_converter", path) + +if spec is None or spec.loader is None: + raise RuntimeError("Unable to load Number-System-Converter.py") + +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) +module.main() +``` + +In normal use, `main()` is interactive and does not accept a numeric argument.