Add PyPassword Generator project with source code, tests, and
TirSystem/github-action: Sync GitHub mirror metadata / sync-metadata (push) Successful in 7s

documentation
This commit is contained in:
2026-10-04 11:16:42 +08:00
commit 129fd80c67
13 changed files with 673 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
import pytest
from password_generator.constants import LETTERS, NUMBERS, SYMBOLS
from password_generator.generator import generate_easy_password, generate_hard_password
def test_easy_password_is_ordered_letters_symbols_numbers():
password = generate_easy_password(4, 2, 2)
assert len(password) == 8
assert all(c in LETTERS for c in password[:4])
assert all(c in SYMBOLS for c in password[4:6])
assert all(c in NUMBERS for c in password[6:])
def test_hard_password_has_requested_composition():
password = generate_hard_password(5, 3, 4)
assert len(password) == 12
assert sum(c in LETTERS for c in password) == 5
assert sum(c in SYMBOLS for c in password) == 3
assert sum(c in NUMBERS for c in password) == 4
def test_hard_password_is_shuffled():
# 20 letters then 20 numbers in that exact order is astronomically unlikely after a shuffle.
results = {generate_hard_password(20, 0, 20) for _ in range(5)}
assert any(not (p[:20].isalpha() and p[20:].isdigit()) for p in results)
def test_zero_counts_give_empty_password():
assert generate_easy_password(0, 0, 0) == ""
assert generate_hard_password(0, 0, 0) == ""
@pytest.mark.parametrize("func", [generate_easy_password, generate_hard_password])
def test_negative_counts_raise(func):
with pytest.raises(ValueError):
func(-1, 0, 0)
+19
View File
@@ -0,0 +1,19 @@
from password_generator import main as main_module
def test_main_prints_both_passwords(monkeypatch, capsys):
answers = iter(["4", "2", "2"])
monkeypatch.setattr("builtins.input", lambda _prompt="": next(answers))
main_module.main()
out = capsys.readouterr().out
assert "Easy level:" in out
assert "Hard level:" in out
def test_main_reprompts_on_invalid_input(monkeypatch, capsys):
answers = iter(["abc", "-3", "1", "1", "1"])
monkeypatch.setattr("builtins.input", lambda _prompt="": next(answers))
main_module.main()
out = capsys.readouterr().out
assert "whole number" in out
assert "0 or more" in out