|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pathins.coordinates import coordinates_run |
| 8 | + |
| 9 | +TESTFONT_PATH_1 = os.path.join( |
| 10 | + "tests", "testfiles", "fonts", "NotoSans-Regular.subset1.ttf" |
| 11 | +) |
| 12 | + |
| 13 | +COLORED_START = "\033[32mSTART ~~~~~~~~\033[0m" |
| 14 | +COLORED_END = "\033[31m~~~~~~~~~~ END\033[0m" |
| 15 | +UNCOLORED_START = "START ~~~~~~~~" |
| 16 | +UNCOLORED_END = "~~~~~~~~~~ END" |
| 17 | +ON_PATH = "----- on -----" |
| 18 | + |
| 19 | +# instantiate a parser for unit tests in this module |
| 20 | +parser = argparse.ArgumentParser() |
| 21 | +parser.add_argument("--nocolor", action="store_true", help="no ANSI color") |
| 22 | +parser.add_argument("fontpath", type=str, help="font file path") |
| 23 | +parser.add_argument( |
| 24 | + "glyphname", type=str, help="glyph name (optional, default=all)", nargs="?" |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +def test_coordinates_run_error_invalid_path(capsys): |
| 29 | + test_path = os.path.join("bogus", "path.txt") |
| 30 | + args = parser.parse_args([test_path]) |
| 31 | + |
| 32 | + with pytest.raises(SystemExit) as e: |
| 33 | + coordinates_run(args) |
| 34 | + |
| 35 | + captured = capsys.readouterr() |
| 36 | + assert e.type == SystemExit |
| 37 | + assert e.value.code == 1 |
| 38 | + assert "does not appear to be a file" in captured.err |
| 39 | + |
| 40 | + |
| 41 | +def test_coordinates_run_error_non_font_path(capsys): |
| 42 | + test_path = os.path.join("tests", "testfiles", "text", "test.txt") |
| 43 | + args = parser.parse_args([test_path]) |
| 44 | + |
| 45 | + with pytest.raises(SystemExit) as e: |
| 46 | + coordinates_run(args) |
| 47 | + |
| 48 | + captured = capsys.readouterr() |
| 49 | + assert e.type == SystemExit |
| 50 | + assert e.value.code == 1 |
| 51 | + assert "does not appear to be a TTF format font" in captured.err |
| 52 | + |
| 53 | + |
| 54 | +def test_coordinates_run_fail_invalid_glyphname(capsys): |
| 55 | + args = parser.parse_args([TESTFONT_PATH_1, "bogus"]) |
| 56 | + with pytest.raises(SystemExit) as e: |
| 57 | + coordinates_run(args) |
| 58 | + |
| 59 | + captured = capsys.readouterr() |
| 60 | + assert e.type == SystemExit |
| 61 | + assert e.value.code == 1 |
| 62 | + assert "Failed to open glyph" in captured.err |
| 63 | + |
| 64 | + |
| 65 | +def test_coordinates_run_single_glyph_noncomposite_default(capsys, monkeypatch): |
| 66 | + def mock_isatty(): |
| 67 | + return True |
| 68 | + |
| 69 | + # apply the monkeypatch for sys.stdout.isatty() |
| 70 | + monkeypatch.setattr(sys.stdout, "isatty", mock_isatty) |
| 71 | + |
| 72 | + args = parser.parse_args([TESTFONT_PATH_1, "A"]) |
| 73 | + coordinates_run(args) |
| 74 | + |
| 75 | + captured = capsys.readouterr() |
| 76 | + # must be in a tty to get ANSI color output |
| 77 | + # this is mocked above |
| 78 | + assert "\x1b[1;36m'A' coordinates\x1b[0m" in captured.out |
| 79 | + # check first contour start coordinate appropriately labeled |
| 80 | + assert f"(545, 0) {COLORED_START}" in captured.out |
| 81 | + assert f"(459, 221) {ON_PATH}" in captured.out |
| 82 | + # check first contour end coord appropriately labeled |
| 83 | + assert f"(638, 0) {COLORED_END}" in captured.out |
| 84 | + # check second contour start coordinate appropriately labeled |
| 85 | + assert f"(432, 301) {COLORED_START}" in captured.out |
| 86 | + assert "(349, 525)" in captured.out |
| 87 | + # check off-curve coordinate appropriately labeled |
| 88 | + assert f"(349, 525) {ON_PATH}" not in captured.out |
| 89 | + # check second contour end coordinate appropriately labeled |
| 90 | + assert f"(206, 301) {COLORED_END}" in captured.out |
| 91 | + |
| 92 | + |
| 93 | +def test_coordinates_run_single_glyph_noncomposite_nocolor(capsys, monkeypatch): |
| 94 | + def mock_isatty(): |
| 95 | + return True |
| 96 | + |
| 97 | + # apply the monkeypatch for sys.stdout.isatty() |
| 98 | + monkeypatch.setattr(sys.stdout, "isatty", mock_isatty) |
| 99 | + |
| 100 | + args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "A"]) |
| 101 | + coordinates_run(args) |
| 102 | + |
| 103 | + captured = capsys.readouterr() |
| 104 | + # must be in a tty to get ANSI color output |
| 105 | + # this is mocked above |
| 106 | + assert "\x1b[1;36m'A' coordinates\x1b[0m" not in captured.out |
| 107 | + assert "'A' coordinates" in captured.out |
| 108 | + # check first contour start coordinate appropriately labeled |
| 109 | + assert f"(545, 0) {COLORED_START}" not in captured.out |
| 110 | + assert f"(545, 0) {UNCOLORED_START}" in captured.out |
| 111 | + |
| 112 | + assert f"(459, 221) {ON_PATH}" in captured.out |
| 113 | + |
| 114 | + # check first contour end coord appropriately labeled |
| 115 | + assert f"(638, 0) {COLORED_END}" not in captured.out |
| 116 | + assert f"(638, 0) {UNCOLORED_END}" in captured.out |
| 117 | + |
| 118 | + # check second contour start coordinate appropriately labeled |
| 119 | + assert f"(432, 301) {COLORED_START}" not in captured.out |
| 120 | + assert f"(432, 301) {UNCOLORED_START}" in captured.out |
| 121 | + |
| 122 | + # check off-curve coordinate appropriately labeled |
| 123 | + assert "(349, 525)" in captured.out |
| 124 | + assert f"(349, 525) {ON_PATH}" not in captured.out |
| 125 | + |
| 126 | + # check second contour end coordinate appropriately labeled |
| 127 | + assert f"(206, 301) {COLORED_END}" not in captured.out |
| 128 | + assert f"(206, 301) {UNCOLORED_END}" in captured.out |
| 129 | + |
| 130 | + |
| 131 | +def test_coordinates_run_single_glyph_composite_default(capsys, monkeypatch): |
| 132 | + def mock_isatty(): |
| 133 | + return True |
| 134 | + |
| 135 | + # apply the monkeypatch for sys.stdout.isatty() |
| 136 | + monkeypatch.setattr(sys.stdout, "isatty", mock_isatty) |
| 137 | + |
| 138 | + args = parser.parse_args([TESTFONT_PATH_1, "uni2E2E"]) |
| 139 | + coordinates_run(args) |
| 140 | + |
| 141 | + captured = capsys.readouterr() |
| 142 | + # must be in a tty to get ANSI color output |
| 143 | + # this is mocked above |
| 144 | + assert "\x1b[1;36m'uni2E2E' coordinates\x1b[0m" in captured.out |
| 145 | + |
| 146 | + assert f"(303, 201) {COLORED_START}" in captured.out |
| 147 | + assert f"(303, 228) {ON_PATH}" in captured.out |
| 148 | + |
| 149 | + assert f"(303, 266)" in captured.out |
| 150 | + assert f"(303, 266) {ON_PATH}" not in captured.out |
| 151 | + |
| 152 | + assert f"(233, 201) {COLORED_END}" in captured.out |
| 153 | + |
| 154 | + assert f"(326, 54) {COLORED_START}" in captured.out |
| 155 | + |
| 156 | + assert f"(326, 91)" in captured.out |
| 157 | + assert f"(326, 91) {ON_PATH}" not in captured.out |
| 158 | + |
| 159 | + assert f"(264, -14) {ON_PATH}" in captured.out |
| 160 | + |
| 161 | + assert f"(326, 18) {COLORED_END}" in captured.out |
| 162 | + |
| 163 | + |
| 164 | +def test_coordinates_run_single_glyph_composite_nocolor(capsys, monkeypatch): |
| 165 | + def mock_isatty(): |
| 166 | + return True |
| 167 | + |
| 168 | + # apply the monkeypatch for sys.stdout.isatty() |
| 169 | + monkeypatch.setattr(sys.stdout, "isatty", mock_isatty) |
| 170 | + |
| 171 | + args = parser.parse_args(["--nocolor", TESTFONT_PATH_1, "uni2E2E"]) |
| 172 | + coordinates_run(args) |
| 173 | + |
| 174 | + captured = capsys.readouterr() |
| 175 | + # must be in a tty to get ANSI color output |
| 176 | + # this is mocked above |
| 177 | + assert "\x1b[1;36m'uni2E2E' coordinates\x1b[0m" not in captured.out |
| 178 | + assert "'uni2E2E' coordinates" in captured.out |
| 179 | + |
| 180 | + assert f"(303, 201) {UNCOLORED_START}" in captured.out |
| 181 | + assert f"(303, 228) {ON_PATH}" in captured.out |
| 182 | + |
| 183 | + assert f"(303, 266)" in captured.out |
| 184 | + assert f"(303, 266) {ON_PATH}" not in captured.out |
| 185 | + |
| 186 | + assert f"(233, 201) {UNCOLORED_END}" in captured.out |
| 187 | + |
| 188 | + assert f"(326, 54) {UNCOLORED_START}" in captured.out |
| 189 | + |
| 190 | + assert f"(326, 91)" in captured.out |
| 191 | + assert f"(326, 91) {ON_PATH}" not in captured.out |
| 192 | + |
| 193 | + assert f"(264, -14) {ON_PATH}" in captured.out |
| 194 | + |
| 195 | + assert f"(326, 18) {UNCOLORED_END}" in captured.out |
0 commit comments