-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Only show_default on
--*/--no-*
pair bool arguments for the ac…
…tual default.
- Loading branch information
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
import pytest | ||
from typing_extensions import Annotated | ||
|
||
import cappa | ||
from tests.utils import TestOutput, backends, parse | ||
|
||
|
||
@backends | ||
def test_show_default_default_true(backend, capsys): | ||
@dataclass | ||
class Command: | ||
foo: bool = False | ||
|
||
assert parse(Command, backend=backend).foo is False | ||
|
||
with pytest.raises(cappa.HelpExit): | ||
parse(Command, "--help", backend=backend) | ||
|
||
output = TestOutput.from_capsys(capsys) | ||
assert "(Default: False)" in output.stdout | ||
|
||
|
||
@backends | ||
def test_show_default_set_false(backend, capsys): | ||
@dataclass | ||
class Command: | ||
foo: Annotated[bool, cappa.Arg(show_default=False)] = False | ||
|
||
assert parse(Command, backend=backend).foo is False | ||
|
||
with pytest.raises(cappa.HelpExit): | ||
parse(Command, "--help", backend=backend) | ||
|
||
output = TestOutput.from_capsys(capsys) | ||
assert "(Default: False)" not in output.stdout | ||
|
||
|
||
@backends | ||
def test_show_default_no_option_shows_for_false_default(backend, capsys): | ||
@dataclass | ||
class Command: | ||
foo: Annotated[bool, cappa.Arg(long="--foo/--no-foo")] = False | ||
|
||
assert parse(Command, backend=backend).foo is False | ||
|
||
with pytest.raises(cappa.HelpExit): | ||
parse(Command, "--help", backend=backend) | ||
|
||
stdout = TestOutput.from_capsys(capsys).stdout.replace(" ", "") | ||
assert "[--foo](Default:True)" not in stdout | ||
assert "[--no-foo](Default:False)" in stdout | ||
|
||
|
||
@backends | ||
def test_show_default_no_option_shows_for_true_default(backend, capsys): | ||
@dataclass | ||
class Command: | ||
foo: Annotated[bool, cappa.Arg(long="--foo/--no-foo")] = True | ||
|
||
assert parse(Command, backend=backend).foo is True | ||
|
||
with pytest.raises(cappa.HelpExit): | ||
parse(Command, "--help", backend=backend) | ||
|
||
stdout = TestOutput.from_capsys(capsys).stdout.replace(" ", "") | ||
assert "[--foo](Default:True)" in stdout | ||
assert "[--no-foo](Default:False)" not in stdout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters