| 1 | """Test command module and argument handling.""" |
| 2 | import pytest |
| 3 | from bracex.__main__ import main |
| 4 | from bracex import __version__ |
| 5 | |
| 6 | |
| 7 | def test_expand_with_default_terminator(capsys): |
| 8 | """Test that by default one expansion is printed per line.""" |
| 9 | with pytest.raises(SystemExit) as exinfo: |
| 10 | main(['{a..c}']) |
| 11 | capture = capsys.readouterr() |
| 12 | assert capture.out == "a\nb\nc\n" |
| 13 | assert capture.err == "" |
| 14 | assert exinfo.value.code == 0 |
| 15 | |
| 16 | |
| 17 | def test_expand_with_spaces(capsys): |
| 18 | """Test that expansions can be space terminated.""" |
| 19 | with pytest.raises(SystemExit) as exinfo: |
| 20 | main(['-t', ' ', '{a..c}']) |
| 21 | capture = capsys.readouterr() |
| 22 | assert capture.out == "a b c " |
| 23 | assert capture.err == "" |
| 24 | assert exinfo.value.code == 0 |
| 25 | |
| 26 | |
| 27 | def test_expand_with_empty_terminators(capsys): |
| 28 | """Test that expansions can be terminated by an empty string.""" |
| 29 | with pytest.raises(SystemExit) as exinfo: |
| 30 | main(['-t', '', '{a..c}']) |
| 31 | capture = capsys.readouterr() |
| 32 | assert capture.out == "abc" |
| 33 | assert capture.err == "" |
| 34 | assert exinfo.value.code == 0 |
| 35 | |
| 36 | |
| 37 | def test_expand_with_nul_terminators(capsys): |
| 38 | """Test that expansions can be terminated by a NUL character.""" |
| 39 | with pytest.raises(SystemExit) as exinfo: |
| 40 | main(['-0', '{a..c}']) |
| 41 | capture = capsys.readouterr() |
| 42 | assert capture.out == "a\x00b\x00c\x00" |
| 43 | assert capture.err == "" |
| 44 | assert exinfo.value.code == 0 |
| 45 | |
| 46 | |
| 47 | def test_terminator_arguments_are_mutually_exclusive(capsys): |
| 48 | """Test that contradicting terminators raise an error.""" |
| 49 | with pytest.raises(SystemExit) as exinfo: |
| 50 | main(['-0', '--terminator', ' ', '{a..c}']) |
| 51 | |
| 52 | capture = capsys.readouterr() |
| 53 | assert capture.out == "" |
| 54 | assert capture.err.find("error: argument --terminator/-t: not allowed with argument -0") > 0 |
| 55 | assert exinfo.value.code > 0 |
| 56 | |
| 57 | |
| 58 | def test_help(capsys): |
| 59 | """Test that help is available.""" |
| 60 | with pytest.raises(SystemExit) as exinfo: |
| 61 | main(['--help']) |
| 62 | capture = capsys.readouterr() |
| 63 | assert capture.out.startswith("usage:") |
| 64 | assert capture.err == "" |
| 65 | assert exinfo.value.code == 0 |
| 66 | |
| 67 | |
| 68 | def test_version(capsys): |
| 69 | """Test that the version is available.""" |
| 70 | with pytest.raises(SystemExit) as exinfo: |
| 71 | main(['--version']) |
| 72 | capture = capsys.readouterr() |
| 73 | assert capture.out == f"{__version__}\n" |
| 74 | assert capture.err == "" |
| 75 | assert exinfo.value.code == 0 |
| 76 | |
| 77 | |
| 78 | def test_no_args_is_considered_an_error(capsys): |
| 79 | """Test that an error is reported when no expression is provided.""" |
| 80 | with pytest.raises(SystemExit) as exinfo: |
| 81 | main([]) |
| 82 | capture = capsys.readouterr() |
| 83 | assert capture.out == "" |
| 84 | assert capture.err.endswith("error: the following arguments are required: expression\n") |
| 85 | assert exinfo.value.code > 0 |
| 86 | |
| 87 | |
| 88 | def test_excess_args_is_considered_an_error(capsys): |
| 89 | """Test that an error is reported when too many arguments are provided.""" |
| 90 | with pytest.raises(SystemExit) as exinfo: |
| 91 | main(['{a,b,c}', '{1..3}']) |
| 92 | capture = capsys.readouterr() |
| 93 | assert capture.out == "" |
| 94 | assert capture.err.find("error: unrecognized arguments") > 0 |
| 95 | assert exinfo.value.code > 0 |