From 9b9e25d1d042e315b745aba73f30501e6ab07edb Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Wed, 28 May 2025 23:49:09 +0900 Subject: Test CLI --- tst/test_help.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tst/test_help.py (limited to 'tst/test_help.py') diff --git a/tst/test_help.py b/tst/test_help.py new file mode 100644 index 0000000..429d7c4 --- /dev/null +++ b/tst/test_help.py @@ -0,0 +1,69 @@ +# Tests for CLI help formatting +# Copyright (C) 2025 Nguyễn Gia Phong +# +# This file is part of scadere. +# +# Scadere is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Scadere is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with scadere. If not, see . + +from contextlib import redirect_stdout, suppress +from io import StringIO + +from hypothesis import given +from pytest import fixture, mark, raises + +from scadere import NetLoc +from scadere.check import main as check +from scadere.listen import main as listen + + +@fixture(scope='session') +def help_string(request): + string = StringIO() + with suppress(SystemExit), redirect_stdout(string): + request.param(['--help']) + return string.getvalue() + + +@mark.parametrize('help_string', [check, listen], indirect=True) +def test_usage_prefix(help_string): + assert help_string.startswith('Usage: ') + + +@mark.parametrize('help_string', [check, listen], indirect=True) +def test_options_heading(help_string): + assert '\n\nOptions:\n' in help_string + + +@mark.parametrize('help_string', [check], indirect=True) +def test_one_or_more(help_string): + assert ' HOST[:PORT]...\n' in help_string + + +@mark.parametrize('help_string', [check], indirect=True) +@mark.parametrize('short,long,metavar', [('-d', '--days', 'DAYS'), + ('-o', '--output', 'PATH')]) +def test_long_option(help_string, short, long, metavar): + assert f'{short} {metavar}, {long}={metavar}' in help_string + + +@given(...) +def test_netloc(hostname: str, port: int | None, default_port: int | None): + netloc = NetLoc(default_port) + if port is not None: + assert netloc(f'{hostname}:{port}') == (hostname, port) + elif default_port is None: + with raises(ValueError): + netloc(f'{hostname}:{port}') + else: + assert netloc(hostname) == (hostname, default_port) -- cgit 1.4.1