Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions tests/test_number_system_converter.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
50 changes: 50 additions & 0 deletions utilities/Number-System-Converter/README.md
Original file line number Diff line number Diff line change
@@ -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.