|
| 1 | +import pytest |
| 2 | +from rotate_group_words.rotate_group_words import ( |
| 3 | + is_similar, rotate_word, rotate_word_once, iterating_through_input, rotate_group_words |
| 4 | +) |
| 5 | + |
| 6 | +# --- Unit tests --- |
| 7 | +def test_is_similar_true(): |
| 8 | + assert is_similar("abcd", "cdab") |
| 9 | + assert is_similar("abcd", "dabc") |
| 10 | + assert is_similar("abcd", "bcda") |
| 11 | + assert is_similar("a", "a") |
| 12 | + assert is_similar("", "") |
| 13 | + |
| 14 | +def test_is_similar_false(): |
| 15 | + assert not is_similar("abcd", "abce") |
| 16 | + assert not is_similar("abcd", "abc") |
| 17 | + assert not is_similar("abcd", "efgh") |
| 18 | + assert not is_similar("abcd", "") |
| 19 | + assert not is_similar("", "abcd") |
| 20 | + |
| 21 | +def test_rotate_word_once(): |
| 22 | + assert rotate_word_once("abcd") == "dabc" |
| 23 | + assert rotate_word_once("a") == "a" |
| 24 | + assert rotate_word_once("") == "" |
| 25 | + |
| 26 | +def test_rotate_word(): |
| 27 | + assert rotate_word("abcd", 1) == "dabc" |
| 28 | + assert rotate_word("abcd", 2) == "cdab" |
| 29 | + assert rotate_word("abcd", 4) == "abcd" |
| 30 | + assert rotate_word("a", 3) == "a" |
| 31 | + assert rotate_word("", 5) == "" |
| 32 | + |
| 33 | +def test_iterating_through_input_basic(): |
| 34 | + words = ["abcd", "cdab", "dabc", "bcda", "efgh", "ghfe"] |
| 35 | + result = iterating_through_input(words) |
| 36 | + assert set(result.keys()) == {"abcd", "efgh", "ghfe"} |
| 37 | + assert set(result["abcd"]) == {"abcd", "cdab", "dabc", "bcda"} |
| 38 | + assert result["efgh"] == ["efgh"] |
| 39 | + assert result["ghfe"] == ["ghfe"] |
| 40 | + |
| 41 | +def test_iterating_through_input_empty(): |
| 42 | + assert iterating_through_input([]) == {} |
| 43 | + |
| 44 | +def test_iterating_through_input_single(): |
| 45 | + assert iterating_through_input(["a"]) == {"a": ["a"]} |
| 46 | + |
| 47 | +# --- Integration test for rotate_group_words (captures print output) --- |
| 48 | +def test_rotate_group_words_print(monkeypatch): |
| 49 | + words = ["abcd", "cdab", "dabc", "bcda", "efgh", "ghfe"] |
| 50 | + output = [] |
| 51 | + monkeypatch.setattr("builtins.print", lambda x: output.append(x)) |
| 52 | + rotate_group_words(words) |
| 53 | + assert any("abcd: [abcd, cdab, dabc, bcda]" in line for line in output) |
| 54 | + assert any("efgh: [efgh]" in line for line in output) |
| 55 | + assert any("ghfe: [ghfe]" in line for line in output) |
| 56 | + |
| 57 | +# --- CLI integration test --- |
| 58 | +import subprocess |
| 59 | +import sys |
| 60 | +import os |
| 61 | + |
| 62 | +def test_cli_entry(tmp_path): |
| 63 | + script = os.path.join(os.path.dirname(__file__), "../src/rotate_group_words/entry.py") |
| 64 | + result = subprocess.run( |
| 65 | + [sys.executable, script, "-w", "abcd,cdab,dabc,bcda,efgh,ghfe"], |
| 66 | + capture_output=True, text=True |
| 67 | + ) |
| 68 | + assert "abcd: [abcd, cdab, dabc, bcda]" in result.stdout |
| 69 | + assert "efgh: [efgh]" in result.stdout |
| 70 | + assert "ghfe: [ghfe]" in result.stdout |
| 71 | + assert result.returncode == 0 |
0 commit comments