|
| 1 | +""" |
| 2 | +Tests platform comparisons, literal containment, and prefix checks. |
| 3 | +""" |
| 4 | + |
| 5 | +# Specification: https://typing.python.org/en/latest/spec/directives.html#version-and-platform-checks |
| 6 | + |
| 7 | +import sys |
| 8 | +from typing import assert_type |
| 9 | + |
| 10 | + |
| 11 | +def test_equality(a: int, b: str) -> None: |
| 12 | + error: int = 0 |
| 13 | + equal: int | str |
| 14 | + if sys.platform == "bogus_platform": |
| 15 | + error = "unreachable" |
| 16 | + equal = b |
| 17 | + else: |
| 18 | + error = "reachable" # E |
| 19 | + equal = a |
| 20 | + assert_type(equal, int) |
| 21 | + |
| 22 | + unequal: int | str |
| 23 | + if sys.platform != "bogus_platform": |
| 24 | + error = "reachable" # E |
| 25 | + unequal = a |
| 26 | + else: |
| 27 | + error = "unreachable" |
| 28 | + unequal = b |
| 29 | + assert_type(unequal, int) |
| 30 | + |
| 31 | + |
| 32 | +def test_startswith(a: int, b: str) -> None: |
| 33 | + error: int = 0 |
| 34 | + empty_prefix: int | str |
| 35 | + if sys.platform.startswith(""): |
| 36 | + error = "reachable" # E: Every platform starts with the empty prefix. |
| 37 | + empty_prefix = a |
| 38 | + else: |
| 39 | + error = "unreachable" |
| 40 | + empty_prefix = b |
| 41 | + assert_type(empty_prefix, int) |
| 42 | + |
| 43 | + unknown_prefix: int | str |
| 44 | + if sys.platform.startswith("bogus_platform"): |
| 45 | + error = "unreachable" |
| 46 | + unknown_prefix = b |
| 47 | + else: |
| 48 | + error = "reachable" # E |
| 49 | + unknown_prefix = a |
| 50 | + assert_type(unknown_prefix, int) |
| 51 | + |
| 52 | + |
| 53 | +def test_versioned_platform_prefix() -> None: |
| 54 | + error: int = 0 |
| 55 | + if sys.platform == "freebsd13": |
| 56 | + if sys.platform.startswith("freebsd"): |
| 57 | + error = "reachable" # E[freebsd_match] |
| 58 | + else: |
| 59 | + error = "unreachable" |
| 60 | + if sys.platform.startswith("linux"): |
| 61 | + error = "unreachable" |
| 62 | + else: |
| 63 | + error = "reachable" # E[freebsd_nonmatch] |
| 64 | + else: |
| 65 | + error = "reachable" # E[freebsd_match] |
| 66 | + error = "reachable" # E[freebsd_nonmatch] |
| 67 | + |
| 68 | + |
| 69 | +def test_tuple_membership(a: int, b: str) -> None: |
| 70 | + # > The membership checks ``in`` and ``not in`` only support simple containment |
| 71 | + # > testing with a literal tuple, literal set, or literal list of literal strings. |
| 72 | + error: int = 0 |
| 73 | + contained: int | str |
| 74 | + if sys.platform in ("bogus_platform",): |
| 75 | + error = "unreachable" |
| 76 | + contained = b |
| 77 | + else: |
| 78 | + error = "reachable" # E |
| 79 | + contained = a |
| 80 | + assert_type(contained, int) |
| 81 | + |
| 82 | + excluded: int | str |
| 83 | + if sys.platform not in ("bogus_platform", "another_bogus_platform"): |
| 84 | + error = "reachable" # E |
| 85 | + excluded = a |
| 86 | + else: |
| 87 | + error = "unreachable" |
| 88 | + excluded = b |
| 89 | + assert_type(excluded, int) |
| 90 | + |
| 91 | + # Positive matches are guarded so this file also works on non-Linux targets. |
| 92 | + if sys.platform == "linux": |
| 93 | + if sys.platform in ("linux", "darwin"): |
| 94 | + error = "reachable" # E[tuple_match] |
| 95 | + else: |
| 96 | + error = "unreachable" |
| 97 | + if sys.platform not in ("linux",): |
| 98 | + error = "unreachable" |
| 99 | + else: |
| 100 | + error = "reachable" # E[tuple_nonmatch] |
| 101 | + else: |
| 102 | + error = "reachable" # E[tuple_match] |
| 103 | + error = "reachable" # E[tuple_nonmatch] |
| 104 | + |
| 105 | + |
| 106 | +def test_set_membership(a: int, b: str) -> None: |
| 107 | + error: int = 0 |
| 108 | + contained: int | str |
| 109 | + if sys.platform in {"bogus_platform"}: |
| 110 | + error = "unreachable" |
| 111 | + contained = b |
| 112 | + else: |
| 113 | + error = "reachable" # E |
| 114 | + contained = a |
| 115 | + assert_type(contained, int) |
| 116 | + |
| 117 | + excluded: int | str |
| 118 | + if sys.platform not in {"bogus_platform", "another_bogus_platform"}: |
| 119 | + error = "reachable" # E |
| 120 | + excluded = a |
| 121 | + else: |
| 122 | + error = "unreachable" |
| 123 | + excluded = b |
| 124 | + assert_type(excluded, int) |
| 125 | + |
| 126 | + if sys.platform == "linux": |
| 127 | + if sys.platform in {"linux", "darwin"}: |
| 128 | + error = "reachable" # E[set_match] |
| 129 | + else: |
| 130 | + error = "unreachable" |
| 131 | + if sys.platform not in {"linux"}: |
| 132 | + error = "unreachable" |
| 133 | + else: |
| 134 | + error = "reachable" # E[set_nonmatch] |
| 135 | + else: |
| 136 | + error = "reachable" # E[set_match] |
| 137 | + error = "reachable" # E[set_nonmatch] |
| 138 | + |
| 139 | + |
| 140 | +def test_list_membership(a: int, b: str) -> None: |
| 141 | + error: int = 0 |
| 142 | + contained: int | str |
| 143 | + if sys.platform in ["bogus_platform"]: |
| 144 | + error = "unreachable" |
| 145 | + contained = b |
| 146 | + else: |
| 147 | + error = "reachable" # E |
| 148 | + contained = a |
| 149 | + assert_type(contained, int) |
| 150 | + |
| 151 | + excluded: int | str |
| 152 | + if sys.platform not in ["bogus_platform", "another_bogus_platform"]: |
| 153 | + error = "reachable" # E |
| 154 | + excluded = a |
| 155 | + else: |
| 156 | + error = "unreachable" |
| 157 | + excluded = b |
| 158 | + assert_type(excluded, int) |
| 159 | + |
| 160 | + if sys.platform == "linux": |
| 161 | + if sys.platform in ["linux", "darwin"]: |
| 162 | + error = "reachable" # E[list_match] |
| 163 | + else: |
| 164 | + error = "unreachable" |
| 165 | + if sys.platform not in ["linux"]: |
| 166 | + error = "unreachable" |
| 167 | + else: |
| 168 | + error = "reachable" # E[list_nonmatch] |
| 169 | + else: |
| 170 | + error = "reachable" # E[list_match] |
| 171 | + error = "reachable" # E[list_nonmatch] |
0 commit comments