Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1 | import array |
| 2 | import collections |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3 | import dataclasses |
| 4 | import enum |
| 5 | import inspect |
| 6 | import unittest |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 7 | |
| 8 | |
| 9 | @dataclasses.dataclass |
| 10 | class Point: |
| 11 | x: int |
| 12 | y: int |
| 13 | |
| 14 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 15 | class TestCompiler(unittest.TestCase): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 16 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 17 | def test_refleaks(self): |
| 18 | # Hunting for leaks using -R doesn't catch leaks in the compiler itself, |
| 19 | # just the code under test. This test ensures that if there are leaks in |
| 20 | # the pattern compiler, those runs will fail: |
| 21 | with open(__file__) as file: |
| 22 | compile(file.read(), __file__, "exec") |
| 23 | |
| 24 | |
| 25 | class TestInheritance(unittest.TestCase): |
| 26 | |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 27 | @staticmethod |
| 28 | def check_sequence_then_mapping(x): |
| 29 | match x: |
| 30 | case [*_]: |
| 31 | return "seq" |
| 32 | case {}: |
| 33 | return "map" |
| 34 | |
| 35 | @staticmethod |
| 36 | def check_mapping_then_sequence(x): |
| 37 | match x: |
| 38 | case {}: |
| 39 | return "map" |
| 40 | case [*_]: |
| 41 | return "seq" |
| 42 | |
| 43 | def test_multiple_inheritance_mapping(self): |
| 44 | class C: |
| 45 | pass |
| 46 | class M1(collections.UserDict, collections.abc.Sequence): |
| 47 | pass |
| 48 | class M2(C, collections.UserDict, collections.abc.Sequence): |
| 49 | pass |
| 50 | class M3(collections.UserDict, C, list): |
| 51 | pass |
| 52 | class M4(dict, collections.abc.Sequence, C): |
| 53 | pass |
| 54 | self.assertEqual(self.check_sequence_then_mapping(M1()), "map") |
| 55 | self.assertEqual(self.check_sequence_then_mapping(M2()), "map") |
| 56 | self.assertEqual(self.check_sequence_then_mapping(M3()), "map") |
| 57 | self.assertEqual(self.check_sequence_then_mapping(M4()), "map") |
| 58 | self.assertEqual(self.check_mapping_then_sequence(M1()), "map") |
| 59 | self.assertEqual(self.check_mapping_then_sequence(M2()), "map") |
| 60 | self.assertEqual(self.check_mapping_then_sequence(M3()), "map") |
| 61 | self.assertEqual(self.check_mapping_then_sequence(M4()), "map") |
| 62 | |
| 63 | def test_multiple_inheritance_sequence(self): |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 64 | class C: |
| 65 | pass |
| 66 | class S1(collections.UserList, collections.abc.Mapping): |
| 67 | pass |
| 68 | class S2(C, collections.UserList, collections.abc.Mapping): |
| 69 | pass |
| 70 | class S3(list, C, collections.abc.Mapping): |
| 71 | pass |
| 72 | class S4(collections.UserList, dict, C): |
| 73 | pass |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 74 | self.assertEqual(self.check_sequence_then_mapping(S1()), "seq") |
| 75 | self.assertEqual(self.check_sequence_then_mapping(S2()), "seq") |
| 76 | self.assertEqual(self.check_sequence_then_mapping(S3()), "seq") |
| 77 | self.assertEqual(self.check_sequence_then_mapping(S4()), "seq") |
| 78 | self.assertEqual(self.check_mapping_then_sequence(S1()), "seq") |
| 79 | self.assertEqual(self.check_mapping_then_sequence(S2()), "seq") |
| 80 | self.assertEqual(self.check_mapping_then_sequence(S3()), "seq") |
| 81 | self.assertEqual(self.check_mapping_then_sequence(S4()), "seq") |
| 82 | |
| 83 | def test_late_registration_mapping(self): |
| 84 | class Parent: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 85 | pass |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 86 | class ChildPre(Parent): |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 87 | pass |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 88 | class GrandchildPre(ChildPre): |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 89 | pass |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 90 | collections.abc.Mapping.register(Parent) |
| 91 | class ChildPost(Parent): |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 92 | pass |
Miss Islington (bot) | 8897012 | 2021-06-25 08:46:23 -0700 | [diff] [blame] | 93 | class GrandchildPost(ChildPost): |
| 94 | pass |
| 95 | self.assertEqual(self.check_sequence_then_mapping(Parent()), "map") |
| 96 | self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "map") |
| 97 | self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "map") |
| 98 | self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "map") |
| 99 | self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "map") |
| 100 | self.assertEqual(self.check_mapping_then_sequence(Parent()), "map") |
| 101 | self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "map") |
| 102 | self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "map") |
| 103 | self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "map") |
| 104 | self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "map") |
| 105 | |
| 106 | def test_late_registration_sequence(self): |
| 107 | class Parent: |
| 108 | pass |
| 109 | class ChildPre(Parent): |
| 110 | pass |
| 111 | class GrandchildPre(ChildPre): |
| 112 | pass |
| 113 | collections.abc.Sequence.register(Parent) |
| 114 | class ChildPost(Parent): |
| 115 | pass |
| 116 | class GrandchildPost(ChildPost): |
| 117 | pass |
| 118 | self.assertEqual(self.check_sequence_then_mapping(Parent()), "seq") |
| 119 | self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "seq") |
| 120 | self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "seq") |
| 121 | self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "seq") |
| 122 | self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "seq") |
| 123 | self.assertEqual(self.check_mapping_then_sequence(Parent()), "seq") |
| 124 | self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "seq") |
| 125 | self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "seq") |
| 126 | self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "seq") |
| 127 | self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "seq") |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 128 | |
| 129 | |
| 130 | class TestPatma(unittest.TestCase): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 131 | |
| 132 | def test_patma_000(self): |
| 133 | match 0: |
| 134 | case 0: |
| 135 | x = True |
| 136 | self.assertIs(x, True) |
| 137 | |
| 138 | def test_patma_001(self): |
| 139 | match 0: |
| 140 | case 0 if False: |
| 141 | x = False |
| 142 | case 0 if True: |
| 143 | x = True |
| 144 | self.assertIs(x, True) |
| 145 | |
| 146 | def test_patma_002(self): |
| 147 | match 0: |
| 148 | case 0: |
| 149 | x = True |
| 150 | case 0: |
| 151 | x = False |
| 152 | self.assertIs(x, True) |
| 153 | |
| 154 | def test_patma_003(self): |
| 155 | x = False |
| 156 | match 0: |
| 157 | case 0 | 1 | 2 | 3: |
| 158 | x = True |
| 159 | self.assertIs(x, True) |
| 160 | |
| 161 | def test_patma_004(self): |
| 162 | x = False |
| 163 | match 1: |
| 164 | case 0 | 1 | 2 | 3: |
| 165 | x = True |
| 166 | self.assertIs(x, True) |
| 167 | |
| 168 | def test_patma_005(self): |
| 169 | x = False |
| 170 | match 2: |
| 171 | case 0 | 1 | 2 | 3: |
| 172 | x = True |
| 173 | self.assertIs(x, True) |
| 174 | |
| 175 | def test_patma_006(self): |
| 176 | x = False |
| 177 | match 3: |
| 178 | case 0 | 1 | 2 | 3: |
| 179 | x = True |
| 180 | self.assertIs(x, True) |
| 181 | |
| 182 | def test_patma_007(self): |
| 183 | x = False |
| 184 | match 4: |
| 185 | case 0 | 1 | 2 | 3: |
| 186 | x = True |
| 187 | self.assertIs(x, False) |
| 188 | |
| 189 | def test_patma_008(self): |
| 190 | x = 0 |
| 191 | class A: |
| 192 | y = 1 |
| 193 | match x: |
| 194 | case A.y as z: |
| 195 | pass |
| 196 | self.assertEqual(x, 0) |
| 197 | self.assertEqual(A.y, 1) |
| 198 | |
| 199 | def test_patma_009(self): |
| 200 | class A: |
| 201 | B = 0 |
| 202 | match 0: |
| 203 | case x if x: |
| 204 | z = 0 |
| 205 | case _ as y if y == x and y: |
| 206 | z = 1 |
| 207 | case A.B: |
| 208 | z = 2 |
| 209 | self.assertEqual(A.B, 0) |
| 210 | self.assertEqual(x, 0) |
| 211 | self.assertEqual(y, 0) |
| 212 | self.assertEqual(z, 2) |
| 213 | |
| 214 | def test_patma_010(self): |
| 215 | match (): |
| 216 | case []: |
| 217 | x = 0 |
| 218 | self.assertEqual(x, 0) |
| 219 | |
| 220 | def test_patma_011(self): |
| 221 | match (0, 1, 2): |
| 222 | case [*x]: |
| 223 | y = 0 |
| 224 | self.assertEqual(x, [0, 1, 2]) |
| 225 | self.assertEqual(y, 0) |
| 226 | |
| 227 | def test_patma_012(self): |
| 228 | match (0, 1, 2): |
| 229 | case [0, *x]: |
| 230 | y = 0 |
| 231 | self.assertEqual(x, [1, 2]) |
| 232 | self.assertEqual(y, 0) |
| 233 | |
| 234 | def test_patma_013(self): |
| 235 | match (0, 1, 2): |
| 236 | case [0, 1, *x,]: |
| 237 | y = 0 |
| 238 | self.assertEqual(x, [2]) |
| 239 | self.assertEqual(y, 0) |
| 240 | |
| 241 | def test_patma_014(self): |
| 242 | match (0, 1, 2): |
| 243 | case [0, 1, 2, *x]: |
| 244 | y = 0 |
| 245 | self.assertEqual(x, []) |
| 246 | self.assertEqual(y, 0) |
| 247 | |
| 248 | def test_patma_015(self): |
| 249 | match (0, 1, 2): |
| 250 | case [*x, 2,]: |
| 251 | y = 0 |
| 252 | self.assertEqual(x, [0, 1]) |
| 253 | self.assertEqual(y, 0) |
| 254 | |
| 255 | def test_patma_016(self): |
| 256 | match (0, 1, 2): |
| 257 | case [*x, 1, 2]: |
| 258 | y = 0 |
| 259 | self.assertEqual(x, [0]) |
| 260 | self.assertEqual(y, 0) |
| 261 | |
| 262 | def test_patma_017(self): |
| 263 | match (0, 1, 2): |
| 264 | case [*x, 0, 1, 2,]: |
| 265 | y = 0 |
| 266 | self.assertEqual(x, []) |
| 267 | self.assertEqual(y, 0) |
| 268 | |
| 269 | def test_patma_018(self): |
| 270 | match (0, 1, 2): |
| 271 | case [0, *x, 2]: |
| 272 | y = 0 |
| 273 | self.assertEqual(x, [1]) |
| 274 | self.assertEqual(y, 0) |
| 275 | |
| 276 | def test_patma_019(self): |
| 277 | match (0, 1, 2): |
| 278 | case [0, 1, *x, 2,]: |
| 279 | y = 0 |
| 280 | self.assertEqual(x, []) |
| 281 | self.assertEqual(y, 0) |
| 282 | |
| 283 | def test_patma_020(self): |
| 284 | match (0, 1, 2): |
| 285 | case [0, *x, 1, 2]: |
| 286 | y = 0 |
| 287 | self.assertEqual(x, []) |
| 288 | self.assertEqual(y, 0) |
| 289 | |
| 290 | def test_patma_021(self): |
| 291 | match (0, 1, 2): |
| 292 | case [*x,]: |
| 293 | y = 0 |
| 294 | self.assertEqual(x, [0, 1, 2]) |
| 295 | self.assertEqual(y, 0) |
| 296 | |
| 297 | def test_patma_022(self): |
| 298 | x = {} |
| 299 | match x: |
| 300 | case {}: |
| 301 | y = 0 |
| 302 | self.assertEqual(x, {}) |
| 303 | self.assertEqual(y, 0) |
| 304 | |
| 305 | def test_patma_023(self): |
| 306 | x = {0: 0} |
| 307 | match x: |
| 308 | case {}: |
| 309 | y = 0 |
| 310 | self.assertEqual(x, {0: 0}) |
| 311 | self.assertEqual(y, 0) |
| 312 | |
| 313 | def test_patma_024(self): |
| 314 | x = {} |
| 315 | y = None |
| 316 | match x: |
| 317 | case {0: 0}: |
| 318 | y = 0 |
| 319 | self.assertEqual(x, {}) |
| 320 | self.assertIs(y, None) |
| 321 | |
| 322 | def test_patma_025(self): |
| 323 | x = {0: 0} |
| 324 | match x: |
| 325 | case {0: (0 | 1 | 2 as z)}: |
| 326 | y = 0 |
| 327 | self.assertEqual(x, {0: 0}) |
| 328 | self.assertEqual(y, 0) |
| 329 | self.assertEqual(z, 0) |
| 330 | |
| 331 | def test_patma_026(self): |
| 332 | x = {0: 1} |
| 333 | match x: |
| 334 | case {0: (0 | 1 | 2 as z)}: |
| 335 | y = 0 |
| 336 | self.assertEqual(x, {0: 1}) |
| 337 | self.assertEqual(y, 0) |
| 338 | self.assertEqual(z, 1) |
| 339 | |
| 340 | def test_patma_027(self): |
| 341 | x = {0: 2} |
| 342 | match x: |
| 343 | case {0: (0 | 1 | 2 as z)}: |
| 344 | y = 0 |
| 345 | self.assertEqual(x, {0: 2}) |
| 346 | self.assertEqual(y, 0) |
| 347 | self.assertEqual(z, 2) |
| 348 | |
| 349 | def test_patma_028(self): |
| 350 | x = {0: 3} |
| 351 | y = None |
| 352 | match x: |
| 353 | case {0: (0 | 1 | 2 as z)}: |
| 354 | y = 0 |
| 355 | self.assertEqual(x, {0: 3}) |
| 356 | self.assertIs(y, None) |
| 357 | |
| 358 | def test_patma_029(self): |
| 359 | x = {} |
| 360 | y = None |
| 361 | match x: |
| 362 | case {0: [1, 2, {}]}: |
| 363 | y = 0 |
| 364 | case {0: [1, 2, {}], 1: [[]]}: |
| 365 | y = 1 |
| 366 | case []: |
| 367 | y = 2 |
| 368 | self.assertEqual(x, {}) |
| 369 | self.assertIs(y, None) |
| 370 | |
| 371 | def test_patma_030(self): |
| 372 | x = {False: (True, 2.0, {})} |
| 373 | match x: |
| 374 | case {0: [1, 2, {}]}: |
| 375 | y = 0 |
| 376 | case {0: [1, 2, {}], 1: [[]]}: |
| 377 | y = 1 |
| 378 | case []: |
| 379 | y = 2 |
| 380 | self.assertEqual(x, {False: (True, 2.0, {})}) |
| 381 | self.assertEqual(y, 0) |
| 382 | |
| 383 | def test_patma_031(self): |
| 384 | x = {False: (True, 2.0, {}), 1: [[]], 2: 0} |
| 385 | match x: |
| 386 | case {0: [1, 2, {}]}: |
| 387 | y = 0 |
| 388 | case {0: [1, 2, {}], 1: [[]]}: |
| 389 | y = 1 |
| 390 | case []: |
| 391 | y = 2 |
| 392 | self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0}) |
| 393 | self.assertEqual(y, 0) |
| 394 | |
| 395 | def test_patma_032(self): |
| 396 | x = {False: (True, 2.0, {}), 1: [[]], 2: 0} |
| 397 | match x: |
| 398 | case {0: [1, 2]}: |
| 399 | y = 0 |
| 400 | case {0: [1, 2, {}], 1: [[]]}: |
| 401 | y = 1 |
| 402 | case []: |
| 403 | y = 2 |
| 404 | self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0}) |
| 405 | self.assertEqual(y, 1) |
| 406 | |
| 407 | def test_patma_033(self): |
| 408 | x = [] |
| 409 | match x: |
| 410 | case {0: [1, 2, {}]}: |
| 411 | y = 0 |
| 412 | case {0: [1, 2, {}], 1: [[]]}: |
| 413 | y = 1 |
| 414 | case []: |
| 415 | y = 2 |
| 416 | self.assertEqual(x, []) |
| 417 | self.assertEqual(y, 2) |
| 418 | |
| 419 | def test_patma_034(self): |
| 420 | x = {0: 0} |
| 421 | match x: |
| 422 | case {0: [1, 2, {}]}: |
| 423 | y = 0 |
| 424 | case {0: ([1, 2, {}] | False)} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}: |
| 425 | y = 1 |
| 426 | case []: |
| 427 | y = 2 |
| 428 | self.assertEqual(x, {0: 0}) |
| 429 | self.assertEqual(y, 1) |
| 430 | |
| 431 | def test_patma_035(self): |
| 432 | x = {0: 0} |
| 433 | match x: |
| 434 | case {0: [1, 2, {}]}: |
| 435 | y = 0 |
| 436 | case {0: [1, 2, {}] | True} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}: |
| 437 | y = 1 |
| 438 | case []: |
| 439 | y = 2 |
| 440 | self.assertEqual(x, {0: 0}) |
| 441 | self.assertEqual(y, 1) |
| 442 | |
| 443 | def test_patma_036(self): |
| 444 | x = 0 |
| 445 | match x: |
| 446 | case 0 | 1 | 2: |
| 447 | y = 0 |
| 448 | self.assertEqual(x, 0) |
| 449 | self.assertEqual(y, 0) |
| 450 | |
| 451 | def test_patma_037(self): |
| 452 | x = 1 |
| 453 | match x: |
| 454 | case 0 | 1 | 2: |
| 455 | y = 0 |
| 456 | self.assertEqual(x, 1) |
| 457 | self.assertEqual(y, 0) |
| 458 | |
| 459 | def test_patma_038(self): |
| 460 | x = 2 |
| 461 | match x: |
| 462 | case 0 | 1 | 2: |
| 463 | y = 0 |
| 464 | self.assertEqual(x, 2) |
| 465 | self.assertEqual(y, 0) |
| 466 | |
| 467 | def test_patma_039(self): |
| 468 | x = 3 |
| 469 | y = None |
| 470 | match x: |
| 471 | case 0 | 1 | 2: |
| 472 | y = 0 |
| 473 | self.assertEqual(x, 3) |
| 474 | self.assertIs(y, None) |
| 475 | |
| 476 | def test_patma_040(self): |
| 477 | x = 0 |
| 478 | match x: |
| 479 | case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 480 | y = 0 |
| 481 | self.assertEqual(x, 0) |
| 482 | self.assertEqual(y, 0) |
| 483 | self.assertEqual(z, 0) |
| 484 | |
| 485 | def test_patma_041(self): |
| 486 | x = 1 |
| 487 | match x: |
| 488 | case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 489 | y = 0 |
| 490 | self.assertEqual(x, 1) |
| 491 | self.assertEqual(y, 0) |
| 492 | self.assertEqual(z, 1) |
| 493 | |
| 494 | def test_patma_042(self): |
| 495 | x = 2 |
| 496 | y = None |
| 497 | match x: |
| 498 | case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 499 | y = 0 |
| 500 | self.assertEqual(x, 2) |
| 501 | self.assertIs(y, None) |
| 502 | self.assertEqual(z, 2) |
| 503 | |
| 504 | def test_patma_043(self): |
| 505 | x = 3 |
| 506 | y = None |
| 507 | match x: |
| 508 | case (0 as z) | (1 as z) | (2 as z) if z == x % 2: |
| 509 | y = 0 |
| 510 | self.assertEqual(x, 3) |
| 511 | self.assertIs(y, None) |
| 512 | |
| 513 | def test_patma_044(self): |
| 514 | x = () |
| 515 | match x: |
| 516 | case []: |
| 517 | y = 0 |
| 518 | self.assertEqual(x, ()) |
| 519 | self.assertEqual(y, 0) |
| 520 | |
| 521 | def test_patma_045(self): |
| 522 | x = () |
| 523 | match x: |
| 524 | case (): |
| 525 | y = 0 |
| 526 | self.assertEqual(x, ()) |
| 527 | self.assertEqual(y, 0) |
| 528 | |
| 529 | def test_patma_046(self): |
| 530 | x = (0,) |
| 531 | match x: |
| 532 | case [0]: |
| 533 | y = 0 |
| 534 | self.assertEqual(x, (0,)) |
| 535 | self.assertEqual(y, 0) |
| 536 | |
| 537 | def test_patma_047(self): |
| 538 | x = ((),) |
| 539 | match x: |
| 540 | case [[]]: |
| 541 | y = 0 |
| 542 | self.assertEqual(x, ((),)) |
| 543 | self.assertEqual(y, 0) |
| 544 | |
| 545 | def test_patma_048(self): |
| 546 | x = [0, 1] |
| 547 | match x: |
| 548 | case [0, 1] | [1, 0]: |
| 549 | y = 0 |
| 550 | self.assertEqual(x, [0, 1]) |
| 551 | self.assertEqual(y, 0) |
| 552 | |
| 553 | def test_patma_049(self): |
| 554 | x = [1, 0] |
| 555 | match x: |
| 556 | case [0, 1] | [1, 0]: |
| 557 | y = 0 |
| 558 | self.assertEqual(x, [1, 0]) |
| 559 | self.assertEqual(y, 0) |
| 560 | |
| 561 | def test_patma_050(self): |
| 562 | x = [0, 0] |
| 563 | y = None |
| 564 | match x: |
| 565 | case [0, 1] | [1, 0]: |
| 566 | y = 0 |
| 567 | self.assertEqual(x, [0, 0]) |
| 568 | self.assertIs(y, None) |
| 569 | |
| 570 | def test_patma_051(self): |
| 571 | w = None |
| 572 | x = [1, 0] |
| 573 | match x: |
| 574 | case [(0 as w)]: |
| 575 | y = 0 |
| 576 | case [z] | [1, (0 | 1 as z)] | [z]: |
| 577 | y = 1 |
| 578 | self.assertIs(w, None) |
| 579 | self.assertEqual(x, [1, 0]) |
| 580 | self.assertEqual(y, 1) |
| 581 | self.assertEqual(z, 0) |
| 582 | |
| 583 | def test_patma_052(self): |
| 584 | x = [1, 0] |
| 585 | match x: |
| 586 | case [0]: |
| 587 | y = 0 |
| 588 | case [1, 0] if (x := x[:0]): |
| 589 | y = 1 |
| 590 | case [1, 0]: |
| 591 | y = 2 |
| 592 | self.assertEqual(x, []) |
| 593 | self.assertEqual(y, 2) |
| 594 | |
| 595 | def test_patma_053(self): |
| 596 | x = {0} |
| 597 | y = None |
| 598 | match x: |
| 599 | case [0]: |
| 600 | y = 0 |
| 601 | self.assertEqual(x, {0}) |
| 602 | self.assertIs(y, None) |
| 603 | |
| 604 | def test_patma_054(self): |
| 605 | x = set() |
| 606 | y = None |
| 607 | match x: |
| 608 | case []: |
| 609 | y = 0 |
| 610 | self.assertEqual(x, set()) |
| 611 | self.assertIs(y, None) |
| 612 | |
| 613 | def test_patma_055(self): |
| 614 | x = iter([1, 2, 3]) |
| 615 | y = None |
| 616 | match x: |
| 617 | case []: |
| 618 | y = 0 |
| 619 | self.assertEqual([*x], [1, 2, 3]) |
| 620 | self.assertIs(y, None) |
| 621 | |
| 622 | def test_patma_056(self): |
| 623 | x = {} |
| 624 | y = None |
| 625 | match x: |
| 626 | case []: |
| 627 | y = 0 |
| 628 | self.assertEqual(x, {}) |
| 629 | self.assertIs(y, None) |
| 630 | |
| 631 | def test_patma_057(self): |
| 632 | x = {0: False, 1: True} |
| 633 | y = None |
| 634 | match x: |
| 635 | case [0, 1]: |
| 636 | y = 0 |
| 637 | self.assertEqual(x, {0: False, 1: True}) |
| 638 | self.assertIs(y, None) |
| 639 | |
| 640 | def test_patma_058(self): |
| 641 | x = 0 |
| 642 | match x: |
| 643 | case 0: |
| 644 | y = 0 |
| 645 | self.assertEqual(x, 0) |
| 646 | self.assertEqual(y, 0) |
| 647 | |
| 648 | def test_patma_059(self): |
| 649 | x = 0 |
| 650 | y = None |
| 651 | match x: |
| 652 | case False: |
| 653 | y = 0 |
| 654 | self.assertEqual(x, 0) |
| 655 | self.assertEqual(y, None) |
| 656 | |
| 657 | def test_patma_060(self): |
| 658 | x = 0 |
| 659 | y = None |
| 660 | match x: |
| 661 | case 1: |
| 662 | y = 0 |
| 663 | self.assertEqual(x, 0) |
| 664 | self.assertIs(y, None) |
| 665 | |
| 666 | def test_patma_061(self): |
| 667 | x = 0 |
| 668 | y = None |
| 669 | match x: |
| 670 | case None: |
| 671 | y = 0 |
| 672 | self.assertEqual(x, 0) |
| 673 | self.assertIs(y, None) |
| 674 | |
| 675 | def test_patma_062(self): |
| 676 | x = 0 |
| 677 | match x: |
| 678 | case 0: |
| 679 | y = 0 |
| 680 | case 0: |
| 681 | y = 1 |
| 682 | self.assertEqual(x, 0) |
| 683 | self.assertEqual(y, 0) |
| 684 | |
| 685 | def test_patma_063(self): |
| 686 | x = 0 |
| 687 | y = None |
| 688 | match x: |
| 689 | case 1: |
| 690 | y = 0 |
| 691 | case 1: |
| 692 | y = 1 |
| 693 | self.assertEqual(x, 0) |
| 694 | self.assertIs(y, None) |
| 695 | |
| 696 | def test_patma_064(self): |
| 697 | x = "x" |
| 698 | match x: |
| 699 | case "x": |
| 700 | y = 0 |
| 701 | case "y": |
| 702 | y = 1 |
| 703 | self.assertEqual(x, "x") |
| 704 | self.assertEqual(y, 0) |
| 705 | |
| 706 | def test_patma_065(self): |
| 707 | x = "x" |
| 708 | match x: |
| 709 | case "y": |
| 710 | y = 0 |
| 711 | case "x": |
| 712 | y = 1 |
| 713 | self.assertEqual(x, "x") |
| 714 | self.assertEqual(y, 1) |
| 715 | |
| 716 | def test_patma_066(self): |
| 717 | x = "x" |
| 718 | match x: |
| 719 | case "": |
| 720 | y = 0 |
| 721 | case "x": |
| 722 | y = 1 |
| 723 | self.assertEqual(x, "x") |
| 724 | self.assertEqual(y, 1) |
| 725 | |
| 726 | def test_patma_067(self): |
| 727 | x = b"x" |
| 728 | match x: |
| 729 | case b"y": |
| 730 | y = 0 |
| 731 | case b"x": |
| 732 | y = 1 |
| 733 | self.assertEqual(x, b"x") |
| 734 | self.assertEqual(y, 1) |
| 735 | |
| 736 | def test_patma_068(self): |
| 737 | x = 0 |
| 738 | match x: |
| 739 | case 0 if False: |
| 740 | y = 0 |
| 741 | case 0: |
| 742 | y = 1 |
| 743 | self.assertEqual(x, 0) |
| 744 | self.assertEqual(y, 1) |
| 745 | |
| 746 | def test_patma_069(self): |
| 747 | x = 0 |
| 748 | y = None |
| 749 | match x: |
| 750 | case 0 if 0: |
| 751 | y = 0 |
| 752 | case 0 if 0: |
| 753 | y = 1 |
| 754 | self.assertEqual(x, 0) |
| 755 | self.assertIs(y, None) |
| 756 | |
| 757 | def test_patma_070(self): |
| 758 | x = 0 |
| 759 | match x: |
| 760 | case 0 if True: |
| 761 | y = 0 |
| 762 | case 0 if True: |
| 763 | y = 1 |
| 764 | self.assertEqual(x, 0) |
| 765 | self.assertEqual(y, 0) |
| 766 | |
| 767 | def test_patma_071(self): |
| 768 | x = 0 |
| 769 | match x: |
| 770 | case 0 if 1: |
| 771 | y = 0 |
| 772 | case 0 if 1: |
| 773 | y = 1 |
| 774 | self.assertEqual(x, 0) |
| 775 | self.assertEqual(y, 0) |
| 776 | |
| 777 | def test_patma_072(self): |
| 778 | x = 0 |
| 779 | match x: |
| 780 | case 0 if True: |
| 781 | y = 0 |
| 782 | case 0 if True: |
| 783 | y = 1 |
| 784 | y = 2 |
| 785 | self.assertEqual(x, 0) |
| 786 | self.assertEqual(y, 2) |
| 787 | |
| 788 | def test_patma_073(self): |
| 789 | x = 0 |
| 790 | match x: |
| 791 | case 0 if 0: |
| 792 | y = 0 |
| 793 | case 0 if 1: |
| 794 | y = 1 |
| 795 | y = 2 |
| 796 | self.assertEqual(x, 0) |
| 797 | self.assertEqual(y, 2) |
| 798 | |
| 799 | def test_patma_074(self): |
| 800 | x = 0 |
| 801 | y = None |
| 802 | match x: |
| 803 | case 0 if not (x := 1): |
| 804 | y = 0 |
| 805 | case 1: |
| 806 | y = 1 |
| 807 | self.assertEqual(x, 1) |
| 808 | self.assertIs(y, None) |
| 809 | |
| 810 | def test_patma_075(self): |
| 811 | x = "x" |
| 812 | match x: |
| 813 | case ["x"]: |
| 814 | y = 0 |
| 815 | case "x": |
| 816 | y = 1 |
| 817 | self.assertEqual(x, "x") |
| 818 | self.assertEqual(y, 1) |
| 819 | |
| 820 | def test_patma_076(self): |
| 821 | x = b"x" |
| 822 | match x: |
| 823 | case [b"x"]: |
| 824 | y = 0 |
| 825 | case ["x"]: |
| 826 | y = 1 |
| 827 | case [120]: |
| 828 | y = 2 |
| 829 | case b"x": |
| 830 | y = 4 |
| 831 | self.assertEqual(x, b"x") |
| 832 | self.assertEqual(y, 4) |
| 833 | |
| 834 | def test_patma_077(self): |
| 835 | x = bytearray(b"x") |
| 836 | y = None |
| 837 | match x: |
| 838 | case [120]: |
| 839 | y = 0 |
| 840 | case 120: |
| 841 | y = 1 |
| 842 | self.assertEqual(x, b"x") |
| 843 | self.assertIs(y, None) |
| 844 | |
| 845 | def test_patma_078(self): |
| 846 | x = "" |
| 847 | match x: |
| 848 | case []: |
| 849 | y = 0 |
| 850 | case [""]: |
| 851 | y = 1 |
| 852 | case "": |
| 853 | y = 2 |
| 854 | self.assertEqual(x, "") |
| 855 | self.assertEqual(y, 2) |
| 856 | |
| 857 | def test_patma_079(self): |
| 858 | x = "xxx" |
| 859 | match x: |
| 860 | case ["x", "x", "x"]: |
| 861 | y = 0 |
| 862 | case ["xxx"]: |
| 863 | y = 1 |
| 864 | case "xxx": |
| 865 | y = 2 |
| 866 | self.assertEqual(x, "xxx") |
| 867 | self.assertEqual(y, 2) |
| 868 | |
| 869 | def test_patma_080(self): |
| 870 | x = b"xxx" |
| 871 | match x: |
| 872 | case [120, 120, 120]: |
| 873 | y = 0 |
| 874 | case [b"xxx"]: |
| 875 | y = 1 |
| 876 | case b"xxx": |
| 877 | y = 2 |
| 878 | self.assertEqual(x, b"xxx") |
| 879 | self.assertEqual(y, 2) |
| 880 | |
| 881 | def test_patma_081(self): |
| 882 | x = 0 |
| 883 | match x: |
| 884 | case 0 if not (x := 1): |
| 885 | y = 0 |
| 886 | case (0 as z): |
| 887 | y = 1 |
| 888 | self.assertEqual(x, 1) |
| 889 | self.assertEqual(y, 1) |
| 890 | self.assertEqual(z, 0) |
| 891 | |
| 892 | def test_patma_082(self): |
| 893 | x = 0 |
| 894 | match x: |
| 895 | case (1 as z) if not (x := 1): |
| 896 | y = 0 |
| 897 | case 0: |
| 898 | y = 1 |
| 899 | self.assertEqual(x, 0) |
| 900 | self.assertEqual(y, 1) |
| 901 | |
| 902 | def test_patma_083(self): |
| 903 | x = 0 |
| 904 | match x: |
| 905 | case (0 as z): |
| 906 | y = 0 |
| 907 | self.assertEqual(x, 0) |
| 908 | self.assertEqual(y, 0) |
| 909 | self.assertEqual(z, 0) |
| 910 | |
| 911 | def test_patma_084(self): |
| 912 | x = 0 |
| 913 | y = None |
| 914 | match x: |
| 915 | case (1 as z): |
| 916 | y = 0 |
| 917 | self.assertEqual(x, 0) |
| 918 | self.assertIs(y, None) |
| 919 | |
| 920 | def test_patma_085(self): |
| 921 | x = 0 |
| 922 | y = None |
| 923 | match x: |
| 924 | case (0 as z) if (w := 0): |
| 925 | y = 0 |
| 926 | self.assertEqual(w, 0) |
| 927 | self.assertEqual(x, 0) |
| 928 | self.assertIs(y, None) |
| 929 | self.assertEqual(z, 0) |
| 930 | |
| 931 | def test_patma_086(self): |
| 932 | x = 0 |
| 933 | match x: |
| 934 | case ((0 as w) as z): |
| 935 | y = 0 |
| 936 | self.assertEqual(w, 0) |
| 937 | self.assertEqual(x, 0) |
| 938 | self.assertEqual(y, 0) |
| 939 | self.assertEqual(z, 0) |
| 940 | |
| 941 | def test_patma_087(self): |
| 942 | x = 0 |
| 943 | match x: |
| 944 | case (0 | 1) | 2: |
| 945 | y = 0 |
| 946 | self.assertEqual(x, 0) |
| 947 | self.assertEqual(y, 0) |
| 948 | |
| 949 | def test_patma_088(self): |
| 950 | x = 1 |
| 951 | match x: |
| 952 | case (0 | 1) | 2: |
| 953 | y = 0 |
| 954 | self.assertEqual(x, 1) |
| 955 | self.assertEqual(y, 0) |
| 956 | |
| 957 | def test_patma_089(self): |
| 958 | x = 2 |
| 959 | match x: |
| 960 | case (0 | 1) | 2: |
| 961 | y = 0 |
| 962 | self.assertEqual(x, 2) |
| 963 | self.assertEqual(y, 0) |
| 964 | |
| 965 | def test_patma_090(self): |
| 966 | x = 3 |
| 967 | y = None |
| 968 | match x: |
| 969 | case (0 | 1) | 2: |
| 970 | y = 0 |
| 971 | self.assertEqual(x, 3) |
| 972 | self.assertIs(y, None) |
| 973 | |
| 974 | def test_patma_091(self): |
| 975 | x = 0 |
| 976 | match x: |
| 977 | case 0 | (1 | 2): |
| 978 | y = 0 |
| 979 | self.assertEqual(x, 0) |
| 980 | self.assertEqual(y, 0) |
| 981 | |
| 982 | def test_patma_092(self): |
| 983 | x = 1 |
| 984 | match x: |
| 985 | case 0 | (1 | 2): |
| 986 | y = 0 |
| 987 | self.assertEqual(x, 1) |
| 988 | self.assertEqual(y, 0) |
| 989 | |
| 990 | def test_patma_093(self): |
| 991 | x = 2 |
| 992 | match x: |
| 993 | case 0 | (1 | 2): |
| 994 | y = 0 |
| 995 | self.assertEqual(x, 2) |
| 996 | self.assertEqual(y, 0) |
| 997 | |
| 998 | def test_patma_094(self): |
| 999 | x = 3 |
| 1000 | y = None |
| 1001 | match x: |
| 1002 | case 0 | (1 | 2): |
| 1003 | y = 0 |
| 1004 | self.assertEqual(x, 3) |
| 1005 | self.assertIs(y, None) |
| 1006 | |
| 1007 | def test_patma_095(self): |
| 1008 | x = 0 |
| 1009 | match x: |
| 1010 | case -0: |
| 1011 | y = 0 |
| 1012 | self.assertEqual(x, 0) |
| 1013 | self.assertEqual(y, 0) |
| 1014 | |
| 1015 | def test_patma_096(self): |
| 1016 | x = 0 |
| 1017 | match x: |
| 1018 | case -0.0: |
| 1019 | y = 0 |
| 1020 | self.assertEqual(x, 0) |
| 1021 | self.assertEqual(y, 0) |
| 1022 | |
| 1023 | def test_patma_097(self): |
| 1024 | x = 0 |
| 1025 | match x: |
| 1026 | case -0j: |
| 1027 | y = 0 |
| 1028 | self.assertEqual(x, 0) |
| 1029 | self.assertEqual(y, 0) |
| 1030 | |
| 1031 | def test_patma_098(self): |
| 1032 | x = 0 |
| 1033 | match x: |
| 1034 | case -0.0j: |
| 1035 | y = 0 |
| 1036 | self.assertEqual(x, 0) |
| 1037 | self.assertEqual(y, 0) |
| 1038 | |
| 1039 | def test_patma_099(self): |
| 1040 | x = -1 |
| 1041 | match x: |
| 1042 | case -1: |
| 1043 | y = 0 |
| 1044 | self.assertEqual(x, -1) |
| 1045 | self.assertEqual(y, 0) |
| 1046 | |
| 1047 | def test_patma_100(self): |
| 1048 | x = -1.5 |
| 1049 | match x: |
| 1050 | case -1.5: |
| 1051 | y = 0 |
| 1052 | self.assertEqual(x, -1.5) |
| 1053 | self.assertEqual(y, 0) |
| 1054 | |
| 1055 | def test_patma_101(self): |
| 1056 | x = -1j |
| 1057 | match x: |
| 1058 | case -1j: |
| 1059 | y = 0 |
| 1060 | self.assertEqual(x, -1j) |
| 1061 | self.assertEqual(y, 0) |
| 1062 | |
| 1063 | def test_patma_102(self): |
| 1064 | x = -1.5j |
| 1065 | match x: |
| 1066 | case -1.5j: |
| 1067 | y = 0 |
| 1068 | self.assertEqual(x, -1.5j) |
| 1069 | self.assertEqual(y, 0) |
| 1070 | |
| 1071 | def test_patma_103(self): |
| 1072 | x = 0 |
| 1073 | match x: |
| 1074 | case 0 + 0j: |
| 1075 | y = 0 |
| 1076 | self.assertEqual(x, 0) |
| 1077 | self.assertEqual(y, 0) |
| 1078 | |
| 1079 | def test_patma_104(self): |
| 1080 | x = 0 |
| 1081 | match x: |
| 1082 | case 0 - 0j: |
| 1083 | y = 0 |
| 1084 | self.assertEqual(x, 0) |
| 1085 | self.assertEqual(y, 0) |
| 1086 | |
| 1087 | def test_patma_105(self): |
| 1088 | x = 0 |
| 1089 | match x: |
| 1090 | case -0 + 0j: |
| 1091 | y = 0 |
| 1092 | self.assertEqual(x, 0) |
| 1093 | self.assertEqual(y, 0) |
| 1094 | |
| 1095 | def test_patma_106(self): |
| 1096 | x = 0 |
| 1097 | match x: |
| 1098 | case -0 - 0j: |
| 1099 | y = 0 |
| 1100 | self.assertEqual(x, 0) |
| 1101 | self.assertEqual(y, 0) |
| 1102 | |
| 1103 | def test_patma_107(self): |
| 1104 | x = 0.25 + 1.75j |
| 1105 | match x: |
| 1106 | case 0.25 + 1.75j: |
| 1107 | y = 0 |
| 1108 | self.assertEqual(x, 0.25 + 1.75j) |
| 1109 | self.assertEqual(y, 0) |
| 1110 | |
| 1111 | def test_patma_108(self): |
| 1112 | x = 0.25 - 1.75j |
| 1113 | match x: |
| 1114 | case 0.25 - 1.75j: |
| 1115 | y = 0 |
| 1116 | self.assertEqual(x, 0.25 - 1.75j) |
| 1117 | self.assertEqual(y, 0) |
| 1118 | |
| 1119 | def test_patma_109(self): |
| 1120 | x = -0.25 + 1.75j |
| 1121 | match x: |
| 1122 | case -0.25 + 1.75j: |
| 1123 | y = 0 |
| 1124 | self.assertEqual(x, -0.25 + 1.75j) |
| 1125 | self.assertEqual(y, 0) |
| 1126 | |
| 1127 | def test_patma_110(self): |
| 1128 | x = -0.25 - 1.75j |
| 1129 | match x: |
| 1130 | case -0.25 - 1.75j: |
| 1131 | y = 0 |
| 1132 | self.assertEqual(x, -0.25 - 1.75j) |
| 1133 | self.assertEqual(y, 0) |
| 1134 | |
| 1135 | def test_patma_111(self): |
| 1136 | class A: |
| 1137 | B = 0 |
| 1138 | x = 0 |
| 1139 | match x: |
| 1140 | case A.B: |
| 1141 | y = 0 |
| 1142 | self.assertEqual(A.B, 0) |
| 1143 | self.assertEqual(x, 0) |
| 1144 | self.assertEqual(y, 0) |
| 1145 | |
| 1146 | def test_patma_112(self): |
| 1147 | class A: |
| 1148 | class B: |
| 1149 | C = 0 |
| 1150 | x = 0 |
| 1151 | match x: |
| 1152 | case A.B.C: |
| 1153 | y = 0 |
| 1154 | self.assertEqual(A.B.C, 0) |
| 1155 | self.assertEqual(x, 0) |
| 1156 | self.assertEqual(y, 0) |
| 1157 | |
| 1158 | def test_patma_113(self): |
| 1159 | class A: |
| 1160 | class B: |
| 1161 | C = 0 |
| 1162 | D = 1 |
| 1163 | x = 1 |
| 1164 | match x: |
| 1165 | case A.B.C: |
| 1166 | y = 0 |
| 1167 | case A.B.D: |
| 1168 | y = 1 |
| 1169 | self.assertEqual(A.B.C, 0) |
| 1170 | self.assertEqual(A.B.D, 1) |
| 1171 | self.assertEqual(x, 1) |
| 1172 | self.assertEqual(y, 1) |
| 1173 | |
| 1174 | def test_patma_114(self): |
| 1175 | class A: |
| 1176 | class B: |
| 1177 | class C: |
| 1178 | D = 0 |
| 1179 | x = 0 |
| 1180 | match x: |
| 1181 | case A.B.C.D: |
| 1182 | y = 0 |
| 1183 | self.assertEqual(A.B.C.D, 0) |
| 1184 | self.assertEqual(x, 0) |
| 1185 | self.assertEqual(y, 0) |
| 1186 | |
| 1187 | def test_patma_115(self): |
| 1188 | class A: |
| 1189 | class B: |
| 1190 | class C: |
| 1191 | D = 0 |
| 1192 | E = 1 |
| 1193 | x = 1 |
| 1194 | match x: |
| 1195 | case A.B.C.D: |
| 1196 | y = 0 |
| 1197 | case A.B.C.E: |
| 1198 | y = 1 |
| 1199 | self.assertEqual(A.B.C.D, 0) |
| 1200 | self.assertEqual(A.B.C.E, 1) |
| 1201 | self.assertEqual(x, 1) |
| 1202 | self.assertEqual(y, 1) |
| 1203 | |
| 1204 | def test_patma_116(self): |
| 1205 | match = case = 0 |
| 1206 | match match: |
| 1207 | case case: |
| 1208 | x = 0 |
| 1209 | self.assertEqual(match, 0) |
| 1210 | self.assertEqual(case, 0) |
| 1211 | self.assertEqual(x, 0) |
| 1212 | |
| 1213 | def test_patma_117(self): |
| 1214 | match = case = 0 |
| 1215 | match case: |
| 1216 | case match: |
| 1217 | x = 0 |
| 1218 | self.assertEqual(match, 0) |
| 1219 | self.assertEqual(case, 0) |
| 1220 | self.assertEqual(x, 0) |
| 1221 | |
| 1222 | def test_patma_118(self): |
| 1223 | x = [] |
| 1224 | match x: |
| 1225 | case [*_, _]: |
| 1226 | y = 0 |
| 1227 | case []: |
| 1228 | y = 1 |
| 1229 | self.assertEqual(x, []) |
| 1230 | self.assertEqual(y, 1) |
| 1231 | |
| 1232 | def test_patma_119(self): |
| 1233 | x = collections.defaultdict(int) |
| 1234 | match x: |
| 1235 | case {0: 0}: |
| 1236 | y = 0 |
| 1237 | case {}: |
| 1238 | y = 1 |
| 1239 | self.assertEqual(x, {}) |
| 1240 | self.assertEqual(y, 1) |
| 1241 | |
| 1242 | def test_patma_120(self): |
| 1243 | x = collections.defaultdict(int) |
| 1244 | match x: |
| 1245 | case {0: 0}: |
| 1246 | y = 0 |
| 1247 | case {**z}: |
| 1248 | y = 1 |
| 1249 | self.assertEqual(x, {}) |
| 1250 | self.assertEqual(y, 1) |
| 1251 | self.assertEqual(z, {}) |
| 1252 | |
| 1253 | def test_patma_121(self): |
| 1254 | match (): |
| 1255 | case (): |
| 1256 | x = 0 |
| 1257 | self.assertEqual(x, 0) |
| 1258 | |
| 1259 | def test_patma_122(self): |
| 1260 | match (0, 1, 2): |
| 1261 | case (*x,): |
| 1262 | y = 0 |
| 1263 | self.assertEqual(x, [0, 1, 2]) |
| 1264 | self.assertEqual(y, 0) |
| 1265 | |
| 1266 | def test_patma_123(self): |
| 1267 | match (0, 1, 2): |
| 1268 | case 0, *x: |
| 1269 | y = 0 |
| 1270 | self.assertEqual(x, [1, 2]) |
| 1271 | self.assertEqual(y, 0) |
| 1272 | |
| 1273 | def test_patma_124(self): |
| 1274 | match (0, 1, 2): |
| 1275 | case (0, 1, *x,): |
| 1276 | y = 0 |
| 1277 | self.assertEqual(x, [2]) |
| 1278 | self.assertEqual(y, 0) |
| 1279 | |
| 1280 | def test_patma_125(self): |
| 1281 | match (0, 1, 2): |
| 1282 | case 0, 1, 2, *x: |
| 1283 | y = 0 |
| 1284 | self.assertEqual(x, []) |
| 1285 | self.assertEqual(y, 0) |
| 1286 | |
| 1287 | def test_patma_126(self): |
| 1288 | match (0, 1, 2): |
| 1289 | case *x, 2,: |
| 1290 | y = 0 |
| 1291 | self.assertEqual(x, [0, 1]) |
| 1292 | self.assertEqual(y, 0) |
| 1293 | |
| 1294 | def test_patma_127(self): |
| 1295 | match (0, 1, 2): |
| 1296 | case (*x, 1, 2): |
| 1297 | y = 0 |
| 1298 | self.assertEqual(x, [0]) |
| 1299 | self.assertEqual(y, 0) |
| 1300 | |
| 1301 | def test_patma_128(self): |
| 1302 | match (0, 1, 2): |
| 1303 | case *x, 0, 1, 2,: |
| 1304 | y = 0 |
| 1305 | self.assertEqual(x, []) |
| 1306 | self.assertEqual(y, 0) |
| 1307 | |
| 1308 | def test_patma_129(self): |
| 1309 | match (0, 1, 2): |
| 1310 | case (0, *x, 2): |
| 1311 | y = 0 |
| 1312 | self.assertEqual(x, [1]) |
| 1313 | self.assertEqual(y, 0) |
| 1314 | |
| 1315 | def test_patma_130(self): |
| 1316 | match (0, 1, 2): |
| 1317 | case 0, 1, *x, 2,: |
| 1318 | y = 0 |
| 1319 | self.assertEqual(x, []) |
| 1320 | self.assertEqual(y, 0) |
| 1321 | |
| 1322 | def test_patma_131(self): |
| 1323 | match (0, 1, 2): |
| 1324 | case (0, *x, 1, 2): |
| 1325 | y = 0 |
| 1326 | self.assertEqual(x, []) |
| 1327 | self.assertEqual(y, 0) |
| 1328 | |
| 1329 | def test_patma_132(self): |
| 1330 | match (0, 1, 2): |
| 1331 | case *x,: |
| 1332 | y = 0 |
| 1333 | self.assertEqual(x, [0, 1, 2]) |
| 1334 | self.assertEqual(y, 0) |
| 1335 | |
| 1336 | def test_patma_133(self): |
| 1337 | x = collections.defaultdict(int, {0: 1}) |
| 1338 | match x: |
| 1339 | case {1: 0}: |
| 1340 | y = 0 |
| 1341 | case {0: 0}: |
| 1342 | y = 1 |
| 1343 | case {}: |
| 1344 | y = 2 |
| 1345 | self.assertEqual(x, {0: 1}) |
| 1346 | self.assertEqual(y, 2) |
| 1347 | |
| 1348 | def test_patma_134(self): |
| 1349 | x = collections.defaultdict(int, {0: 1}) |
| 1350 | match x: |
| 1351 | case {1: 0}: |
| 1352 | y = 0 |
| 1353 | case {0: 0}: |
| 1354 | y = 1 |
| 1355 | case {**z}: |
| 1356 | y = 2 |
| 1357 | self.assertEqual(x, {0: 1}) |
| 1358 | self.assertEqual(y, 2) |
| 1359 | self.assertEqual(z, {0: 1}) |
| 1360 | |
| 1361 | def test_patma_135(self): |
| 1362 | x = collections.defaultdict(int, {0: 1}) |
| 1363 | match x: |
| 1364 | case {1: 0}: |
| 1365 | y = 0 |
| 1366 | case {0: 0}: |
| 1367 | y = 1 |
| 1368 | case {0: _, **z}: |
| 1369 | y = 2 |
| 1370 | self.assertEqual(x, {0: 1}) |
| 1371 | self.assertEqual(y, 2) |
| 1372 | self.assertEqual(z, {}) |
| 1373 | |
| 1374 | def test_patma_136(self): |
| 1375 | x = {0: 1} |
| 1376 | match x: |
| 1377 | case {1: 0}: |
| 1378 | y = 0 |
| 1379 | case {0: 0}: |
| 1380 | y = 0 |
| 1381 | case {}: |
| 1382 | y = 1 |
| 1383 | self.assertEqual(x, {0: 1}) |
| 1384 | self.assertEqual(y, 1) |
| 1385 | |
| 1386 | def test_patma_137(self): |
| 1387 | x = {0: 1} |
| 1388 | match x: |
| 1389 | case {1: 0}: |
| 1390 | y = 0 |
| 1391 | case {0: 0}: |
| 1392 | y = 0 |
| 1393 | case {**z}: |
| 1394 | y = 1 |
| 1395 | self.assertEqual(x, {0: 1}) |
| 1396 | self.assertEqual(y, 1) |
| 1397 | self.assertEqual(z, {0: 1}) |
| 1398 | |
| 1399 | def test_patma_138(self): |
| 1400 | x = {0: 1} |
| 1401 | match x: |
| 1402 | case {1: 0}: |
| 1403 | y = 0 |
| 1404 | case {0: 0}: |
| 1405 | y = 0 |
| 1406 | case {0: _, **z}: |
| 1407 | y = 1 |
| 1408 | self.assertEqual(x, {0: 1}) |
| 1409 | self.assertEqual(y, 1) |
| 1410 | self.assertEqual(z, {}) |
| 1411 | |
| 1412 | def test_patma_139(self): |
| 1413 | x = False |
| 1414 | match x: |
| 1415 | case bool(z): |
| 1416 | y = 0 |
| 1417 | self.assertIs(x, False) |
| 1418 | self.assertEqual(y, 0) |
| 1419 | self.assertIs(z, x) |
| 1420 | |
| 1421 | def test_patma_140(self): |
| 1422 | x = True |
| 1423 | match x: |
| 1424 | case bool(z): |
| 1425 | y = 0 |
| 1426 | self.assertIs(x, True) |
| 1427 | self.assertEqual(y, 0) |
| 1428 | self.assertIs(z, x) |
| 1429 | |
| 1430 | def test_patma_141(self): |
| 1431 | x = bytearray() |
| 1432 | match x: |
| 1433 | case bytearray(z): |
| 1434 | y = 0 |
| 1435 | self.assertEqual(x, bytearray()) |
| 1436 | self.assertEqual(y, 0) |
| 1437 | self.assertIs(z, x) |
| 1438 | |
| 1439 | def test_patma_142(self): |
| 1440 | x = b"" |
| 1441 | match x: |
| 1442 | case bytes(z): |
| 1443 | y = 0 |
| 1444 | self.assertEqual(x, b"") |
| 1445 | self.assertEqual(y, 0) |
| 1446 | self.assertIs(z, x) |
| 1447 | |
| 1448 | def test_patma_143(self): |
| 1449 | x = {} |
| 1450 | match x: |
| 1451 | case dict(z): |
| 1452 | y = 0 |
| 1453 | self.assertEqual(x, {}) |
| 1454 | self.assertEqual(y, 0) |
| 1455 | self.assertIs(z, x) |
| 1456 | |
| 1457 | def test_patma_144(self): |
| 1458 | x = 0.0 |
| 1459 | match x: |
| 1460 | case float(z): |
| 1461 | y = 0 |
| 1462 | self.assertEqual(x, 0.0) |
| 1463 | self.assertEqual(y, 0) |
| 1464 | self.assertIs(z, x) |
| 1465 | |
| 1466 | def test_patma_145(self): |
| 1467 | x = frozenset() |
| 1468 | match x: |
| 1469 | case frozenset(z): |
| 1470 | y = 0 |
| 1471 | self.assertEqual(x, frozenset()) |
| 1472 | self.assertEqual(y, 0) |
| 1473 | self.assertIs(z, x) |
| 1474 | |
| 1475 | def test_patma_146(self): |
| 1476 | x = 0 |
| 1477 | match x: |
| 1478 | case int(z): |
| 1479 | y = 0 |
| 1480 | self.assertEqual(x, 0) |
| 1481 | self.assertEqual(y, 0) |
| 1482 | self.assertIs(z, x) |
| 1483 | |
| 1484 | def test_patma_147(self): |
| 1485 | x = [] |
| 1486 | match x: |
| 1487 | case list(z): |
| 1488 | y = 0 |
| 1489 | self.assertEqual(x, []) |
| 1490 | self.assertEqual(y, 0) |
| 1491 | self.assertIs(z, x) |
| 1492 | |
| 1493 | def test_patma_148(self): |
| 1494 | x = set() |
| 1495 | match x: |
| 1496 | case set(z): |
| 1497 | y = 0 |
| 1498 | self.assertEqual(x, set()) |
| 1499 | self.assertEqual(y, 0) |
| 1500 | self.assertIs(z, x) |
| 1501 | |
| 1502 | def test_patma_149(self): |
| 1503 | x = "" |
| 1504 | match x: |
| 1505 | case str(z): |
| 1506 | y = 0 |
| 1507 | self.assertEqual(x, "") |
| 1508 | self.assertEqual(y, 0) |
| 1509 | self.assertIs(z, x) |
| 1510 | |
| 1511 | def test_patma_150(self): |
| 1512 | x = () |
| 1513 | match x: |
| 1514 | case tuple(z): |
| 1515 | y = 0 |
| 1516 | self.assertEqual(x, ()) |
| 1517 | self.assertEqual(y, 0) |
| 1518 | self.assertIs(z, x) |
| 1519 | |
| 1520 | def test_patma_151(self): |
| 1521 | x = 0 |
| 1522 | match x,: |
| 1523 | case y,: |
| 1524 | z = 0 |
| 1525 | self.assertEqual(x, 0) |
| 1526 | self.assertIs(y, x) |
| 1527 | self.assertIs(z, 0) |
| 1528 | |
| 1529 | def test_patma_152(self): |
| 1530 | w = 0 |
| 1531 | x = 0 |
| 1532 | match w, x: |
| 1533 | case y, z: |
| 1534 | v = 0 |
| 1535 | self.assertEqual(w, 0) |
| 1536 | self.assertEqual(x, 0) |
| 1537 | self.assertIs(y, w) |
| 1538 | self.assertIs(z, x) |
| 1539 | self.assertEqual(v, 0) |
| 1540 | |
| 1541 | def test_patma_153(self): |
| 1542 | x = 0 |
| 1543 | match w := x,: |
| 1544 | case y as v,: |
| 1545 | z = 0 |
| 1546 | self.assertEqual(x, 0) |
| 1547 | self.assertIs(y, x) |
| 1548 | self.assertEqual(z, 0) |
| 1549 | self.assertIs(w, x) |
| 1550 | self.assertIs(v, y) |
| 1551 | |
| 1552 | def test_patma_154(self): |
| 1553 | x = 0 |
| 1554 | y = None |
| 1555 | match x: |
| 1556 | case 0 if x: |
| 1557 | y = 0 |
| 1558 | self.assertEqual(x, 0) |
| 1559 | self.assertIs(y, None) |
| 1560 | |
| 1561 | def test_patma_155(self): |
| 1562 | x = 0 |
| 1563 | y = None |
| 1564 | match x: |
| 1565 | case 1e1000: |
| 1566 | y = 0 |
| 1567 | self.assertEqual(x, 0) |
| 1568 | self.assertIs(y, None) |
| 1569 | |
| 1570 | def test_patma_156(self): |
| 1571 | x = 0 |
| 1572 | match x: |
| 1573 | case z: |
| 1574 | y = 0 |
| 1575 | self.assertEqual(x, 0) |
| 1576 | self.assertEqual(y, 0) |
| 1577 | self.assertIs(z, x) |
| 1578 | |
| 1579 | def test_patma_157(self): |
| 1580 | x = 0 |
| 1581 | y = None |
| 1582 | match x: |
| 1583 | case _ if x: |
| 1584 | y = 0 |
| 1585 | self.assertEqual(x, 0) |
| 1586 | self.assertIs(y, None) |
| 1587 | |
| 1588 | def test_patma_158(self): |
| 1589 | x = 0 |
| 1590 | match x: |
| 1591 | case -1e1000: |
| 1592 | y = 0 |
| 1593 | case 0: |
| 1594 | y = 1 |
| 1595 | self.assertEqual(x, 0) |
| 1596 | self.assertEqual(y, 1) |
| 1597 | |
| 1598 | def test_patma_159(self): |
| 1599 | x = 0 |
| 1600 | match x: |
| 1601 | case 0 if not x: |
| 1602 | y = 0 |
| 1603 | case 1: |
| 1604 | y = 1 |
| 1605 | self.assertEqual(x, 0) |
| 1606 | self.assertEqual(y, 0) |
| 1607 | |
| 1608 | def test_patma_160(self): |
| 1609 | x = 0 |
| 1610 | z = None |
| 1611 | match x: |
| 1612 | case 0: |
| 1613 | y = 0 |
| 1614 | case z if x: |
| 1615 | y = 1 |
| 1616 | self.assertEqual(x, 0) |
| 1617 | self.assertEqual(y, 0) |
| 1618 | self.assertIs(z, None) |
| 1619 | |
| 1620 | def test_patma_161(self): |
| 1621 | x = 0 |
| 1622 | match x: |
| 1623 | case 0: |
| 1624 | y = 0 |
| 1625 | case _: |
| 1626 | y = 1 |
| 1627 | self.assertEqual(x, 0) |
| 1628 | self.assertEqual(y, 0) |
| 1629 | |
| 1630 | def test_patma_162(self): |
| 1631 | x = 0 |
| 1632 | match x: |
| 1633 | case 1 if x: |
| 1634 | y = 0 |
| 1635 | case 0: |
| 1636 | y = 1 |
| 1637 | self.assertEqual(x, 0) |
| 1638 | self.assertEqual(y, 1) |
| 1639 | |
| 1640 | def test_patma_163(self): |
| 1641 | x = 0 |
| 1642 | y = None |
| 1643 | match x: |
| 1644 | case 1: |
| 1645 | y = 0 |
| 1646 | case 1 if not x: |
| 1647 | y = 1 |
| 1648 | self.assertEqual(x, 0) |
| 1649 | self.assertIs(y, None) |
| 1650 | |
| 1651 | def test_patma_164(self): |
| 1652 | x = 0 |
| 1653 | match x: |
| 1654 | case 1: |
| 1655 | y = 0 |
| 1656 | case z: |
| 1657 | y = 1 |
| 1658 | self.assertEqual(x, 0) |
| 1659 | self.assertEqual(y, 1) |
| 1660 | self.assertIs(z, x) |
| 1661 | |
| 1662 | def test_patma_165(self): |
| 1663 | x = 0 |
| 1664 | match x: |
| 1665 | case 1 if x: |
| 1666 | y = 0 |
| 1667 | case _: |
| 1668 | y = 1 |
| 1669 | self.assertEqual(x, 0) |
| 1670 | self.assertEqual(y, 1) |
| 1671 | |
| 1672 | def test_patma_166(self): |
| 1673 | x = 0 |
| 1674 | match x: |
| 1675 | case z if not z: |
| 1676 | y = 0 |
| 1677 | case 0 if x: |
| 1678 | y = 1 |
| 1679 | self.assertEqual(x, 0) |
| 1680 | self.assertEqual(y, 0) |
| 1681 | self.assertIs(z, x) |
| 1682 | |
| 1683 | def test_patma_167(self): |
| 1684 | x = 0 |
| 1685 | match x: |
| 1686 | case z if not z: |
| 1687 | y = 0 |
| 1688 | case 1: |
| 1689 | y = 1 |
| 1690 | self.assertEqual(x, 0) |
| 1691 | self.assertEqual(y, 0) |
| 1692 | self.assertIs(z, x) |
| 1693 | |
| 1694 | def test_patma_168(self): |
| 1695 | x = 0 |
| 1696 | match x: |
| 1697 | case z if not x: |
| 1698 | y = 0 |
| 1699 | case z: |
| 1700 | y = 1 |
| 1701 | self.assertEqual(x, 0) |
| 1702 | self.assertEqual(y, 0) |
| 1703 | self.assertIs(z, x) |
| 1704 | |
| 1705 | def test_patma_169(self): |
| 1706 | x = 0 |
| 1707 | match x: |
| 1708 | case z if not z: |
| 1709 | y = 0 |
| 1710 | case _ if x: |
| 1711 | y = 1 |
| 1712 | self.assertEqual(x, 0) |
| 1713 | self.assertEqual(y, 0) |
| 1714 | self.assertIs(z, x) |
| 1715 | |
| 1716 | def test_patma_170(self): |
| 1717 | x = 0 |
| 1718 | match x: |
| 1719 | case _ if not x: |
| 1720 | y = 0 |
| 1721 | case 0: |
| 1722 | y = 1 |
| 1723 | self.assertEqual(x, 0) |
| 1724 | self.assertEqual(y, 0) |
| 1725 | |
| 1726 | def test_patma_171(self): |
| 1727 | x = 0 |
| 1728 | y = None |
| 1729 | match x: |
| 1730 | case _ if x: |
| 1731 | y = 0 |
| 1732 | case 1: |
| 1733 | y = 1 |
| 1734 | self.assertEqual(x, 0) |
| 1735 | self.assertIs(y, None) |
| 1736 | |
| 1737 | def test_patma_172(self): |
| 1738 | x = 0 |
| 1739 | z = None |
| 1740 | match x: |
| 1741 | case _ if not x: |
| 1742 | y = 0 |
| 1743 | case z if not x: |
| 1744 | y = 1 |
| 1745 | self.assertEqual(x, 0) |
| 1746 | self.assertEqual(y, 0) |
| 1747 | self.assertIs(z, None) |
| 1748 | |
| 1749 | def test_patma_173(self): |
| 1750 | x = 0 |
| 1751 | match x: |
| 1752 | case _ if not x: |
| 1753 | y = 0 |
| 1754 | case _: |
| 1755 | y = 1 |
| 1756 | self.assertEqual(x, 0) |
| 1757 | self.assertEqual(y, 0) |
| 1758 | |
| 1759 | def test_patma_174(self): |
| 1760 | def http_error(status): |
| 1761 | match status: |
| 1762 | case 400: |
| 1763 | return "Bad request" |
| 1764 | case 401: |
| 1765 | return "Unauthorized" |
| 1766 | case 403: |
| 1767 | return "Forbidden" |
| 1768 | case 404: |
| 1769 | return "Not found" |
| 1770 | case 418: |
| 1771 | return "I'm a teapot" |
| 1772 | case _: |
| 1773 | return "Something else" |
| 1774 | self.assertEqual(http_error(400), "Bad request") |
| 1775 | self.assertEqual(http_error(401), "Unauthorized") |
| 1776 | self.assertEqual(http_error(403), "Forbidden") |
| 1777 | self.assertEqual(http_error(404), "Not found") |
| 1778 | self.assertEqual(http_error(418), "I'm a teapot") |
| 1779 | self.assertEqual(http_error(123), "Something else") |
| 1780 | self.assertEqual(http_error("400"), "Something else") |
| 1781 | self.assertEqual(http_error(401 | 403 | 404), "Something else") # 407 |
| 1782 | |
| 1783 | def test_patma_175(self): |
| 1784 | def http_error(status): |
| 1785 | match status: |
| 1786 | case 400: |
| 1787 | return "Bad request" |
| 1788 | case 401 | 403 | 404: |
| 1789 | return "Not allowed" |
| 1790 | case 418: |
| 1791 | return "I'm a teapot" |
| 1792 | self.assertEqual(http_error(400), "Bad request") |
| 1793 | self.assertEqual(http_error(401), "Not allowed") |
| 1794 | self.assertEqual(http_error(403), "Not allowed") |
| 1795 | self.assertEqual(http_error(404), "Not allowed") |
| 1796 | self.assertEqual(http_error(418), "I'm a teapot") |
| 1797 | self.assertIs(http_error(123), None) |
| 1798 | self.assertIs(http_error("400"), None) |
| 1799 | self.assertIs(http_error(401 | 403 | 404), None) # 407 |
| 1800 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1801 | def test_patma_176(self): |
| 1802 | def whereis(point): |
| 1803 | match point: |
| 1804 | case (0, 0): |
| 1805 | return "Origin" |
| 1806 | case (0, y): |
| 1807 | return f"Y={y}" |
| 1808 | case (x, 0): |
| 1809 | return f"X={x}" |
| 1810 | case (x, y): |
| 1811 | return f"X={x}, Y={y}" |
| 1812 | case _: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 1813 | return "Not a point" |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1814 | self.assertEqual(whereis((0, 0)), "Origin") |
| 1815 | self.assertEqual(whereis((0, -1.0)), "Y=-1.0") |
| 1816 | self.assertEqual(whereis(("X", 0)), "X=X") |
| 1817 | self.assertEqual(whereis((None, 1j)), "X=None, Y=1j") |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 1818 | self.assertEqual(whereis(42), "Not a point") |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1819 | |
| 1820 | def test_patma_177(self): |
| 1821 | def whereis(point): |
| 1822 | match point: |
| 1823 | case Point(0, 0): |
| 1824 | return "Origin" |
| 1825 | case Point(0, y): |
| 1826 | return f"Y={y}" |
| 1827 | case Point(x, 0): |
| 1828 | return f"X={x}" |
| 1829 | case Point(): |
| 1830 | return "Somewhere else" |
| 1831 | case _: |
| 1832 | return "Not a point" |
| 1833 | self.assertEqual(whereis(Point(1, 0)), "X=1") |
| 1834 | self.assertEqual(whereis(Point(0, 0)), "Origin") |
| 1835 | self.assertEqual(whereis(10), "Not a point") |
| 1836 | self.assertEqual(whereis(Point(False, False)), "Origin") |
| 1837 | self.assertEqual(whereis(Point(0, -1.0)), "Y=-1.0") |
| 1838 | self.assertEqual(whereis(Point("X", 0)), "X=X") |
| 1839 | self.assertEqual(whereis(Point(None, 1j)), "Somewhere else") |
| 1840 | self.assertEqual(whereis(Point), "Not a point") |
| 1841 | self.assertEqual(whereis(42), "Not a point") |
| 1842 | |
| 1843 | def test_patma_178(self): |
| 1844 | def whereis(point): |
| 1845 | match point: |
| 1846 | case Point(1, var): |
| 1847 | return var |
| 1848 | self.assertEqual(whereis(Point(1, 0)), 0) |
| 1849 | self.assertIs(whereis(Point(0, 0)), None) |
| 1850 | |
| 1851 | def test_patma_179(self): |
| 1852 | def whereis(point): |
| 1853 | match point: |
| 1854 | case Point(1, y=var): |
| 1855 | return var |
| 1856 | self.assertEqual(whereis(Point(1, 0)), 0) |
| 1857 | self.assertIs(whereis(Point(0, 0)), None) |
| 1858 | |
| 1859 | def test_patma_180(self): |
| 1860 | def whereis(point): |
| 1861 | match point: |
| 1862 | case Point(x=1, y=var): |
| 1863 | return var |
| 1864 | self.assertEqual(whereis(Point(1, 0)), 0) |
| 1865 | self.assertIs(whereis(Point(0, 0)), None) |
| 1866 | |
| 1867 | def test_patma_181(self): |
| 1868 | def whereis(point): |
| 1869 | match point: |
| 1870 | case Point(y=var, x=1): |
| 1871 | return var |
| 1872 | self.assertEqual(whereis(Point(1, 0)), 0) |
| 1873 | self.assertIs(whereis(Point(0, 0)), None) |
| 1874 | |
| 1875 | def test_patma_182(self): |
| 1876 | def whereis(points): |
| 1877 | match points: |
| 1878 | case []: |
| 1879 | return "No points" |
| 1880 | case [Point(0, 0)]: |
| 1881 | return "The origin" |
| 1882 | case [Point(x, y)]: |
| 1883 | return f"Single point {x}, {y}" |
| 1884 | case [Point(0, y1), Point(0, y2)]: |
| 1885 | return f"Two on the Y axis at {y1}, {y2}" |
| 1886 | case _: |
| 1887 | return "Something else" |
| 1888 | self.assertEqual(whereis([]), "No points") |
| 1889 | self.assertEqual(whereis([Point(0, 0)]), "The origin") |
| 1890 | self.assertEqual(whereis([Point(0, 1)]), "Single point 0, 1") |
| 1891 | self.assertEqual(whereis([Point(0, 0), Point(0, 0)]), "Two on the Y axis at 0, 0") |
| 1892 | self.assertEqual(whereis([Point(0, 1), Point(0, 1)]), "Two on the Y axis at 1, 1") |
| 1893 | self.assertEqual(whereis([Point(0, 0), Point(1, 0)]), "Something else") |
| 1894 | self.assertEqual(whereis([Point(0, 0), Point(0, 0), Point(0, 0)]), "Something else") |
| 1895 | self.assertEqual(whereis([Point(0, 1), Point(0, 1), Point(0, 1)]), "Something else") |
| 1896 | |
| 1897 | def test_patma_183(self): |
| 1898 | def whereis(point): |
| 1899 | match point: |
| 1900 | case Point(x, y) if x == y: |
| 1901 | return f"Y=X at {x}" |
| 1902 | case Point(x, y): |
| 1903 | return "Not on the diagonal" |
| 1904 | self.assertEqual(whereis(Point(0, 0)), "Y=X at 0") |
| 1905 | self.assertEqual(whereis(Point(0, False)), "Y=X at 0") |
| 1906 | self.assertEqual(whereis(Point(False, 0)), "Y=X at False") |
| 1907 | self.assertEqual(whereis(Point(-1 - 1j, -1 - 1j)), "Y=X at (-1-1j)") |
| 1908 | self.assertEqual(whereis(Point("X", "X")), "Y=X at X") |
| 1909 | self.assertEqual(whereis(Point("X", "x")), "Not on the diagonal") |
| 1910 | |
| 1911 | def test_patma_184(self): |
| 1912 | class Seq(collections.abc.Sequence): |
| 1913 | __getitem__ = None |
| 1914 | def __len__(self): |
| 1915 | return 0 |
| 1916 | match Seq(): |
| 1917 | case []: |
| 1918 | y = 0 |
| 1919 | self.assertEqual(y, 0) |
| 1920 | |
| 1921 | def test_patma_185(self): |
| 1922 | class Seq(collections.abc.Sequence): |
| 1923 | __getitem__ = None |
| 1924 | def __len__(self): |
| 1925 | return 42 |
| 1926 | match Seq(): |
| 1927 | case [*_]: |
| 1928 | y = 0 |
| 1929 | self.assertEqual(y, 0) |
| 1930 | |
| 1931 | def test_patma_186(self): |
| 1932 | class Seq(collections.abc.Sequence): |
| 1933 | def __getitem__(self, i): |
| 1934 | return i |
| 1935 | def __len__(self): |
| 1936 | return 42 |
| 1937 | match Seq(): |
| 1938 | case [x, *_, y]: |
| 1939 | z = 0 |
| 1940 | self.assertEqual(x, 0) |
| 1941 | self.assertEqual(y, 41) |
| 1942 | self.assertEqual(z, 0) |
| 1943 | |
| 1944 | def test_patma_187(self): |
| 1945 | w = range(10) |
| 1946 | match w: |
| 1947 | case [x, y, *rest]: |
| 1948 | z = 0 |
| 1949 | self.assertEqual(w, range(10)) |
| 1950 | self.assertEqual(x, 0) |
| 1951 | self.assertEqual(y, 1) |
| 1952 | self.assertEqual(z, 0) |
| 1953 | self.assertEqual(rest, list(range(2, 10))) |
| 1954 | |
| 1955 | def test_patma_188(self): |
| 1956 | w = range(100) |
| 1957 | match w: |
| 1958 | case (x, y, *rest): |
| 1959 | z = 0 |
| 1960 | self.assertEqual(w, range(100)) |
| 1961 | self.assertEqual(x, 0) |
| 1962 | self.assertEqual(y, 1) |
| 1963 | self.assertEqual(z, 0) |
| 1964 | self.assertEqual(rest, list(range(2, 100))) |
| 1965 | |
| 1966 | def test_patma_189(self): |
| 1967 | w = range(1000) |
| 1968 | match w: |
| 1969 | case x, y, *rest: |
| 1970 | z = 0 |
| 1971 | self.assertEqual(w, range(1000)) |
| 1972 | self.assertEqual(x, 0) |
| 1973 | self.assertEqual(y, 1) |
| 1974 | self.assertEqual(z, 0) |
| 1975 | self.assertEqual(rest, list(range(2, 1000))) |
| 1976 | |
| 1977 | def test_patma_190(self): |
| 1978 | w = range(1 << 10) |
| 1979 | match w: |
| 1980 | case [x, y, *_]: |
| 1981 | z = 0 |
| 1982 | self.assertEqual(w, range(1 << 10)) |
| 1983 | self.assertEqual(x, 0) |
| 1984 | self.assertEqual(y, 1) |
| 1985 | self.assertEqual(z, 0) |
| 1986 | |
| 1987 | def test_patma_191(self): |
| 1988 | w = range(1 << 20) |
| 1989 | match w: |
| 1990 | case (x, y, *_): |
| 1991 | z = 0 |
| 1992 | self.assertEqual(w, range(1 << 20)) |
| 1993 | self.assertEqual(x, 0) |
| 1994 | self.assertEqual(y, 1) |
| 1995 | self.assertEqual(z, 0) |
| 1996 | |
| 1997 | def test_patma_192(self): |
| 1998 | w = range(1 << 30) |
| 1999 | match w: |
| 2000 | case x, y, *_: |
| 2001 | z = 0 |
| 2002 | self.assertEqual(w, range(1 << 30)) |
| 2003 | self.assertEqual(x, 0) |
| 2004 | self.assertEqual(y, 1) |
| 2005 | self.assertEqual(z, 0) |
| 2006 | |
| 2007 | def test_patma_193(self): |
| 2008 | x = {"bandwidth": 0, "latency": 1} |
| 2009 | match x: |
| 2010 | case {"bandwidth": b, "latency": l}: |
| 2011 | y = 0 |
| 2012 | self.assertEqual(x, {"bandwidth": 0, "latency": 1}) |
| 2013 | self.assertIs(b, x["bandwidth"]) |
| 2014 | self.assertIs(l, x["latency"]) |
| 2015 | self.assertEqual(y, 0) |
| 2016 | |
| 2017 | def test_patma_194(self): |
| 2018 | x = {"bandwidth": 0, "latency": 1, "key": "value"} |
| 2019 | match x: |
| 2020 | case {"latency": l, "bandwidth": b}: |
| 2021 | y = 0 |
| 2022 | self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"}) |
| 2023 | self.assertIs(l, x["latency"]) |
| 2024 | self.assertIs(b, x["bandwidth"]) |
| 2025 | self.assertEqual(y, 0) |
| 2026 | |
| 2027 | def test_patma_195(self): |
| 2028 | x = {"bandwidth": 0, "latency": 1, "key": "value"} |
| 2029 | match x: |
| 2030 | case {"bandwidth": b, "latency": l, **rest}: |
| 2031 | y = 0 |
| 2032 | self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"}) |
| 2033 | self.assertIs(b, x["bandwidth"]) |
| 2034 | self.assertIs(l, x["latency"]) |
| 2035 | self.assertEqual(rest, {"key": "value"}) |
| 2036 | self.assertEqual(y, 0) |
| 2037 | |
| 2038 | def test_patma_196(self): |
| 2039 | x = {"bandwidth": 0, "latency": 1} |
| 2040 | match x: |
| 2041 | case {"latency": l, "bandwidth": b, **rest}: |
| 2042 | y = 0 |
| 2043 | self.assertEqual(x, {"bandwidth": 0, "latency": 1}) |
| 2044 | self.assertIs(l, x["latency"]) |
| 2045 | self.assertIs(b, x["bandwidth"]) |
| 2046 | self.assertEqual(rest, {}) |
| 2047 | self.assertEqual(y, 0) |
| 2048 | |
| 2049 | def test_patma_197(self): |
| 2050 | w = [Point(-1, 0), Point(1, 2)] |
| 2051 | match w: |
| 2052 | case (Point(x1, y1), Point(x2, y2) as p2): |
| 2053 | z = 0 |
| 2054 | self.assertEqual(w, [Point(-1, 0), Point(1, 2)]) |
| 2055 | self.assertIs(x1, w[0].x) |
| 2056 | self.assertIs(y1, w[0].y) |
| 2057 | self.assertIs(p2, w[1]) |
| 2058 | self.assertIs(x2, w[1].x) |
| 2059 | self.assertIs(y2, w[1].y) |
| 2060 | self.assertIs(z, 0) |
| 2061 | |
| 2062 | def test_patma_198(self): |
| 2063 | class Color(enum.Enum): |
| 2064 | RED = 0 |
| 2065 | GREEN = 1 |
| 2066 | BLUE = 2 |
| 2067 | def f(color): |
| 2068 | match color: |
| 2069 | case Color.RED: |
| 2070 | return "I see red!" |
| 2071 | case Color.GREEN: |
| 2072 | return "Grass is green" |
| 2073 | case Color.BLUE: |
| 2074 | return "I'm feeling the blues :(" |
| 2075 | self.assertEqual(f(Color.RED), "I see red!") |
| 2076 | self.assertEqual(f(Color.GREEN), "Grass is green") |
| 2077 | self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(") |
| 2078 | self.assertIs(f(Color), None) |
| 2079 | self.assertIs(f(0), None) |
| 2080 | self.assertIs(f(1), None) |
| 2081 | self.assertIs(f(2), None) |
| 2082 | self.assertIs(f(3), None) |
| 2083 | self.assertIs(f(False), None) |
| 2084 | self.assertIs(f(True), None) |
| 2085 | self.assertIs(f(2+0j), None) |
| 2086 | self.assertIs(f(3.0), None) |
| 2087 | |
| 2088 | def test_patma_199(self): |
| 2089 | class Color(int, enum.Enum): |
| 2090 | RED = 0 |
| 2091 | GREEN = 1 |
| 2092 | BLUE = 2 |
| 2093 | def f(color): |
| 2094 | match color: |
| 2095 | case Color.RED: |
| 2096 | return "I see red!" |
| 2097 | case Color.GREEN: |
| 2098 | return "Grass is green" |
| 2099 | case Color.BLUE: |
| 2100 | return "I'm feeling the blues :(" |
| 2101 | self.assertEqual(f(Color.RED), "I see red!") |
| 2102 | self.assertEqual(f(Color.GREEN), "Grass is green") |
| 2103 | self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(") |
| 2104 | self.assertIs(f(Color), None) |
| 2105 | self.assertEqual(f(0), "I see red!") |
| 2106 | self.assertEqual(f(1), "Grass is green") |
| 2107 | self.assertEqual(f(2), "I'm feeling the blues :(") |
| 2108 | self.assertIs(f(3), None) |
| 2109 | self.assertEqual(f(False), "I see red!") |
| 2110 | self.assertEqual(f(True), "Grass is green") |
| 2111 | self.assertEqual(f(2+0j), "I'm feeling the blues :(") |
| 2112 | self.assertIs(f(3.0), None) |
| 2113 | |
| 2114 | def test_patma_200(self): |
| 2115 | class Class: |
Brandt Bucher | f84d5a1 | 2021-04-05 19:17:08 -0700 | [diff] [blame] | 2116 | __match_args__ = ("a", "b") |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2117 | c = Class() |
| 2118 | c.a = 0 |
| 2119 | c.b = 1 |
| 2120 | match c: |
| 2121 | case Class(x, y): |
| 2122 | z = 0 |
| 2123 | self.assertIs(x, c.a) |
| 2124 | self.assertIs(y, c.b) |
| 2125 | self.assertEqual(z, 0) |
| 2126 | |
| 2127 | def test_patma_201(self): |
| 2128 | class Class: |
| 2129 | __match_args__ = ("a", "b") |
| 2130 | c = Class() |
| 2131 | c.a = 0 |
| 2132 | c.b = 1 |
| 2133 | match c: |
| 2134 | case Class(x, b=y): |
| 2135 | z = 0 |
| 2136 | self.assertIs(x, c.a) |
| 2137 | self.assertIs(y, c.b) |
| 2138 | self.assertEqual(z, 0) |
| 2139 | |
| 2140 | def test_patma_202(self): |
| 2141 | class Parent: |
| 2142 | __match_args__ = "a", "b" |
| 2143 | class Child(Parent): |
Brandt Bucher | f84d5a1 | 2021-04-05 19:17:08 -0700 | [diff] [blame] | 2144 | __match_args__ = ("c", "d") |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2145 | c = Child() |
| 2146 | c.a = 0 |
| 2147 | c.b = 1 |
| 2148 | match c: |
| 2149 | case Parent(x, y): |
| 2150 | z = 0 |
| 2151 | self.assertIs(x, c.a) |
| 2152 | self.assertIs(y, c.b) |
| 2153 | self.assertEqual(z, 0) |
| 2154 | |
| 2155 | def test_patma_203(self): |
| 2156 | class Parent: |
| 2157 | __match_args__ = ("a", "b") |
| 2158 | class Child(Parent): |
| 2159 | __match_args__ = "c", "d" |
| 2160 | c = Child() |
| 2161 | c.a = 0 |
| 2162 | c.b = 1 |
| 2163 | match c: |
| 2164 | case Parent(x, b=y): |
| 2165 | z = 0 |
| 2166 | self.assertIs(x, c.a) |
| 2167 | self.assertIs(y, c.b) |
| 2168 | self.assertEqual(z, 0) |
| 2169 | |
| 2170 | def test_patma_204(self): |
| 2171 | def f(w): |
| 2172 | match w: |
| 2173 | case 42: |
| 2174 | out = locals() |
| 2175 | del out["w"] |
| 2176 | return out |
| 2177 | self.assertEqual(f(42), {}) |
| 2178 | self.assertIs(f(0), None) |
| 2179 | self.assertEqual(f(42.0), {}) |
| 2180 | self.assertIs(f("42"), None) |
| 2181 | |
| 2182 | def test_patma_205(self): |
| 2183 | def f(w): |
| 2184 | match w: |
| 2185 | case 42.0: |
| 2186 | out = locals() |
| 2187 | del out["w"] |
| 2188 | return out |
| 2189 | self.assertEqual(f(42.0), {}) |
| 2190 | self.assertEqual(f(42), {}) |
| 2191 | self.assertIs(f(0.0), None) |
| 2192 | self.assertIs(f(0), None) |
| 2193 | |
| 2194 | def test_patma_206(self): |
| 2195 | def f(w): |
| 2196 | match w: |
| 2197 | case 1 | 2 | 3: |
| 2198 | out = locals() |
| 2199 | del out["w"] |
| 2200 | return out |
| 2201 | self.assertEqual(f(1), {}) |
| 2202 | self.assertEqual(f(2), {}) |
| 2203 | self.assertEqual(f(3), {}) |
| 2204 | self.assertEqual(f(3.0), {}) |
| 2205 | self.assertIs(f(0), None) |
| 2206 | self.assertIs(f(4), None) |
| 2207 | self.assertIs(f("1"), None) |
| 2208 | |
| 2209 | def test_patma_207(self): |
| 2210 | def f(w): |
| 2211 | match w: |
| 2212 | case [1, 2] | [3, 4]: |
| 2213 | out = locals() |
| 2214 | del out["w"] |
| 2215 | return out |
| 2216 | self.assertEqual(f([1, 2]), {}) |
| 2217 | self.assertEqual(f([3, 4]), {}) |
| 2218 | self.assertIs(f(42), None) |
| 2219 | self.assertIs(f([2, 3]), None) |
| 2220 | self.assertIs(f([1, 2, 3]), None) |
| 2221 | self.assertEqual(f([1, 2.0]), {}) |
| 2222 | |
| 2223 | def test_patma_208(self): |
| 2224 | def f(w): |
| 2225 | match w: |
| 2226 | case x: |
| 2227 | out = locals() |
| 2228 | del out["w"] |
| 2229 | return out |
| 2230 | self.assertEqual(f(42), {"x": 42}) |
| 2231 | self.assertEqual(f((1, 2)), {"x": (1, 2)}) |
| 2232 | self.assertEqual(f(None), {"x": None}) |
| 2233 | |
| 2234 | def test_patma_209(self): |
| 2235 | def f(w): |
| 2236 | match w: |
| 2237 | case _: |
| 2238 | out = locals() |
| 2239 | del out["w"] |
| 2240 | return out |
| 2241 | self.assertEqual(f(42), {}) |
| 2242 | self.assertEqual(f(None), {}) |
| 2243 | self.assertEqual(f((1, 2)), {}) |
| 2244 | |
| 2245 | def test_patma_210(self): |
| 2246 | def f(w): |
| 2247 | match w: |
| 2248 | case (x, y, z): |
| 2249 | out = locals() |
| 2250 | del out["w"] |
| 2251 | return out |
| 2252 | self.assertEqual(f((1, 2, 3)), {"x": 1, "y": 2, "z": 3}) |
| 2253 | self.assertIs(f((1, 2)), None) |
| 2254 | self.assertIs(f((1, 2, 3, 4)), None) |
| 2255 | self.assertIs(f(123), None) |
| 2256 | self.assertIs(f("abc"), None) |
| 2257 | self.assertIs(f(b"abc"), None) |
| 2258 | self.assertEqual(f(array.array("b", b"abc")), {'x': 97, 'y': 98, 'z': 99}) |
| 2259 | self.assertEqual(f(memoryview(b"abc")), {"x": 97, "y": 98, "z": 99}) |
| 2260 | self.assertIs(f(bytearray(b"abc")), None) |
| 2261 | |
| 2262 | def test_patma_211(self): |
| 2263 | def f(w): |
| 2264 | match w: |
| 2265 | case {"x": x, "y": "y", "z": z}: |
| 2266 | out = locals() |
| 2267 | del out["w"] |
| 2268 | return out |
| 2269 | self.assertEqual(f({"x": "x", "y": "y", "z": "z"}), {"x": "x", "z": "z"}) |
| 2270 | self.assertEqual(f({"x": "x", "y": "y", "z": "z", "a": "a"}), {"x": "x", "z": "z"}) |
| 2271 | self.assertIs(f(({"x": "x", "y": "yy", "z": "z", "a": "a"})), None) |
| 2272 | self.assertIs(f(({"x": "x", "y": "y"})), None) |
| 2273 | |
| 2274 | def test_patma_212(self): |
| 2275 | def f(w): |
| 2276 | match w: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2277 | case Point(int(xx), y="hello"): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2278 | out = locals() |
| 2279 | del out["w"] |
| 2280 | return out |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2281 | self.assertEqual(f(Point(42, "hello")), {"xx": 42}) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2282 | |
| 2283 | def test_patma_213(self): |
| 2284 | def f(w): |
| 2285 | match w: |
| 2286 | case (p, q) as x: |
| 2287 | out = locals() |
| 2288 | del out["w"] |
| 2289 | return out |
| 2290 | self.assertEqual(f((1, 2)), {"p": 1, "q": 2, "x": (1, 2)}) |
| 2291 | self.assertEqual(f([1, 2]), {"p": 1, "q": 2, "x": [1, 2]}) |
| 2292 | self.assertIs(f(12), None) |
| 2293 | self.assertIs(f((1, 2, 3)), None) |
| 2294 | |
| 2295 | def test_patma_214(self): |
| 2296 | def f(): |
| 2297 | match 42: |
| 2298 | case 42: |
| 2299 | return locals() |
| 2300 | self.assertEqual(set(f()), set()) |
| 2301 | |
| 2302 | def test_patma_215(self): |
| 2303 | def f(): |
| 2304 | match 1: |
| 2305 | case 1 | 2 | 3: |
| 2306 | return locals() |
| 2307 | self.assertEqual(set(f()), set()) |
| 2308 | |
| 2309 | def test_patma_216(self): |
| 2310 | def f(): |
| 2311 | match ...: |
| 2312 | case _: |
| 2313 | return locals() |
| 2314 | self.assertEqual(set(f()), set()) |
| 2315 | |
| 2316 | def test_patma_217(self): |
| 2317 | def f(): |
| 2318 | match ...: |
| 2319 | case abc: |
| 2320 | return locals() |
| 2321 | self.assertEqual(set(f()), {"abc"}) |
| 2322 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2323 | def test_patma_218(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2324 | def f(): |
| 2325 | match ..., ...: |
| 2326 | case a, b: |
| 2327 | return locals() |
| 2328 | self.assertEqual(set(f()), {"a", "b"}) |
| 2329 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2330 | def test_patma_219(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2331 | def f(): |
| 2332 | match {"k": ..., "l": ...}: |
| 2333 | case {"k": a, "l": b}: |
| 2334 | return locals() |
| 2335 | self.assertEqual(set(f()), {"a", "b"}) |
| 2336 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2337 | def test_patma_220(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2338 | def f(): |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2339 | match Point(..., ...): |
| 2340 | case Point(x, y=y): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2341 | return locals() |
| 2342 | self.assertEqual(set(f()), {"x", "y"}) |
| 2343 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2344 | def test_patma_221(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2345 | def f(): |
| 2346 | match ...: |
| 2347 | case b as a: |
| 2348 | return locals() |
| 2349 | self.assertEqual(set(f()), {"a", "b"}) |
| 2350 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2351 | def test_patma_222(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2352 | def f(x): |
| 2353 | match x: |
| 2354 | case _: |
| 2355 | return 0 |
| 2356 | self.assertEqual(f(0), 0) |
| 2357 | self.assertEqual(f(1), 0) |
| 2358 | self.assertEqual(f(2), 0) |
| 2359 | self.assertEqual(f(3), 0) |
| 2360 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2361 | def test_patma_223(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2362 | def f(x): |
| 2363 | match x: |
| 2364 | case 0: |
| 2365 | return 0 |
| 2366 | self.assertEqual(f(0), 0) |
| 2367 | self.assertIs(f(1), None) |
| 2368 | self.assertIs(f(2), None) |
| 2369 | self.assertIs(f(3), None) |
| 2370 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2371 | def test_patma_224(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2372 | def f(x): |
| 2373 | match x: |
| 2374 | case 0: |
| 2375 | return 0 |
| 2376 | case _: |
| 2377 | return 1 |
| 2378 | self.assertEqual(f(0), 0) |
| 2379 | self.assertEqual(f(1), 1) |
| 2380 | self.assertEqual(f(2), 1) |
| 2381 | self.assertEqual(f(3), 1) |
| 2382 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2383 | def test_patma_225(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2384 | def f(x): |
| 2385 | match x: |
| 2386 | case 0: |
| 2387 | return 0 |
| 2388 | case 1: |
| 2389 | return 1 |
| 2390 | self.assertEqual(f(0), 0) |
| 2391 | self.assertEqual(f(1), 1) |
| 2392 | self.assertIs(f(2), None) |
| 2393 | self.assertIs(f(3), None) |
| 2394 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2395 | def test_patma_226(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2396 | def f(x): |
| 2397 | match x: |
| 2398 | case 0: |
| 2399 | return 0 |
| 2400 | case 1: |
| 2401 | return 1 |
| 2402 | case _: |
| 2403 | return 2 |
| 2404 | self.assertEqual(f(0), 0) |
| 2405 | self.assertEqual(f(1), 1) |
| 2406 | self.assertEqual(f(2), 2) |
| 2407 | self.assertEqual(f(3), 2) |
| 2408 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2409 | def test_patma_227(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2410 | def f(x): |
| 2411 | match x: |
| 2412 | case 0: |
| 2413 | return 0 |
| 2414 | case 1: |
| 2415 | return 1 |
| 2416 | case 2: |
| 2417 | return 2 |
| 2418 | self.assertEqual(f(0), 0) |
| 2419 | self.assertEqual(f(1), 1) |
| 2420 | self.assertEqual(f(2), 2) |
| 2421 | self.assertIs(f(3), None) |
| 2422 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2423 | def test_patma_228(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2424 | match(): |
| 2425 | case(): |
| 2426 | x = 0 |
| 2427 | self.assertEqual(x, 0) |
| 2428 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2429 | def test_patma_229(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2430 | x = 0 |
| 2431 | match(x): |
| 2432 | case(x): |
| 2433 | y = 0 |
| 2434 | self.assertEqual(x, 0) |
| 2435 | self.assertEqual(y, 0) |
| 2436 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2437 | def test_patma_230(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2438 | x = 0 |
| 2439 | match x: |
| 2440 | case False: |
| 2441 | y = 0 |
| 2442 | case 0: |
| 2443 | y = 1 |
| 2444 | self.assertEqual(x, 0) |
| 2445 | self.assertEqual(y, 1) |
| 2446 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2447 | def test_patma_231(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2448 | x = 1 |
| 2449 | match x: |
| 2450 | case True: |
| 2451 | y = 0 |
| 2452 | case 1: |
| 2453 | y = 1 |
| 2454 | self.assertEqual(x, 1) |
| 2455 | self.assertEqual(y, 1) |
| 2456 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2457 | def test_patma_232(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2458 | class Eq: |
| 2459 | def __eq__(self, other): |
| 2460 | return True |
| 2461 | x = eq = Eq() |
| 2462 | y = None |
| 2463 | match x: |
| 2464 | case None: |
| 2465 | y = 0 |
| 2466 | self.assertIs(x, eq) |
| 2467 | self.assertEqual(y, None) |
| 2468 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2469 | def test_patma_233(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2470 | x = False |
| 2471 | match x: |
| 2472 | case False: |
| 2473 | y = 0 |
| 2474 | self.assertIs(x, False) |
| 2475 | self.assertEqual(y, 0) |
| 2476 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2477 | def test_patma_234(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2478 | x = True |
| 2479 | match x: |
| 2480 | case True: |
| 2481 | y = 0 |
| 2482 | self.assertIs(x, True) |
| 2483 | self.assertEqual(y, 0) |
| 2484 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2485 | def test_patma_235(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2486 | x = None |
| 2487 | match x: |
| 2488 | case None: |
| 2489 | y = 0 |
| 2490 | self.assertIs(x, None) |
| 2491 | self.assertEqual(y, 0) |
| 2492 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2493 | def test_patma_236(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2494 | x = 0 |
| 2495 | match x: |
| 2496 | case (0 as w) as z: |
| 2497 | y = 0 |
| 2498 | self.assertEqual(w, 0) |
| 2499 | self.assertEqual(x, 0) |
| 2500 | self.assertEqual(y, 0) |
| 2501 | self.assertEqual(z, 0) |
| 2502 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2503 | def test_patma_237(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2504 | x = 0 |
| 2505 | match x: |
| 2506 | case (0 as w) as z: |
| 2507 | y = 0 |
| 2508 | self.assertEqual(w, 0) |
| 2509 | self.assertEqual(x, 0) |
| 2510 | self.assertEqual(y, 0) |
| 2511 | self.assertEqual(z, 0) |
| 2512 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2513 | def test_patma_238(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2514 | x = ((0, 1), (2, 3)) |
| 2515 | match x: |
| 2516 | case ((a as b, c as d) as e) as w, ((f as g, h) as i) as z: |
| 2517 | y = 0 |
| 2518 | self.assertEqual(a, 0) |
| 2519 | self.assertEqual(b, 0) |
| 2520 | self.assertEqual(c, 1) |
| 2521 | self.assertEqual(d, 1) |
| 2522 | self.assertEqual(e, (0, 1)) |
| 2523 | self.assertEqual(f, 2) |
| 2524 | self.assertEqual(g, 2) |
| 2525 | self.assertEqual(h, 3) |
| 2526 | self.assertEqual(i, (2, 3)) |
| 2527 | self.assertEqual(w, (0, 1)) |
| 2528 | self.assertEqual(x, ((0, 1), (2, 3))) |
| 2529 | self.assertEqual(y, 0) |
| 2530 | self.assertEqual(z, (2, 3)) |
| 2531 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2532 | def test_patma_239(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2533 | x = collections.UserDict({0: 1, 2: 3}) |
| 2534 | match x: |
| 2535 | case {2: 3}: |
| 2536 | y = 0 |
| 2537 | self.assertEqual(x, {0: 1, 2: 3}) |
| 2538 | self.assertEqual(y, 0) |
| 2539 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2540 | def test_patma_240(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2541 | x = collections.UserDict({0: 1, 2: 3}) |
| 2542 | match x: |
| 2543 | case {2: 3, **z}: |
| 2544 | y = 0 |
| 2545 | self.assertEqual(x, {0: 1, 2: 3}) |
| 2546 | self.assertEqual(y, 0) |
| 2547 | self.assertEqual(z, {0: 1}) |
| 2548 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2549 | def test_patma_241(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2550 | x = [[{0: 0}]] |
| 2551 | match x: |
| 2552 | case list([({-0-0j: int(real=0+0j, imag=0-0j) | (1) as z},)]): |
| 2553 | y = 0 |
| 2554 | self.assertEqual(x, [[{0: 0}]]) |
| 2555 | self.assertEqual(y, 0) |
| 2556 | self.assertEqual(z, 0) |
| 2557 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2558 | def test_patma_242(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2559 | x = range(3) |
| 2560 | match x: |
| 2561 | case [y, *_, z]: |
| 2562 | w = 0 |
| 2563 | self.assertEqual(w, 0) |
| 2564 | self.assertEqual(x, range(3)) |
| 2565 | self.assertEqual(y, 0) |
| 2566 | self.assertEqual(z, 2) |
| 2567 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2568 | def test_patma_243(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2569 | x = range(3) |
| 2570 | match x: |
| 2571 | case [_, *_, y]: |
| 2572 | z = 0 |
| 2573 | self.assertEqual(x, range(3)) |
| 2574 | self.assertEqual(y, 2) |
| 2575 | self.assertEqual(z, 0) |
| 2576 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2577 | def test_patma_244(self): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2578 | x = range(3) |
| 2579 | match x: |
| 2580 | case [*_, y]: |
| 2581 | z = 0 |
| 2582 | self.assertEqual(x, range(3)) |
| 2583 | self.assertEqual(y, 2) |
| 2584 | self.assertEqual(z, 0) |
| 2585 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2586 | def test_patma_245(self): |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 2587 | x = {"y": 1} |
| 2588 | match x: |
| 2589 | case {"y": (0 as y) | (1 as y)}: |
| 2590 | z = 0 |
| 2591 | self.assertEqual(x, {"y": 1}) |
| 2592 | self.assertEqual(y, 1) |
| 2593 | self.assertEqual(z, 0) |
| 2594 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2595 | def test_patma_246(self): |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 2596 | def f(x): |
| 2597 | match x: |
| 2598 | case ((a, b, c, d, e, f, g, h, i, 9) | |
| 2599 | (h, g, i, a, b, d, e, c, f, 10) | |
| 2600 | (g, b, a, c, d, -5, e, h, i, f) | |
| 2601 | (-1, d, f, b, g, e, i, a, h, c)): |
| 2602 | w = 0 |
| 2603 | out = locals() |
| 2604 | del out["x"] |
| 2605 | return out |
| 2606 | alts = [ |
| 2607 | dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0), |
| 2608 | dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0), |
| 2609 | dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0), |
| 2610 | dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0), |
| 2611 | dict(), |
| 2612 | ] |
| 2613 | self.assertEqual(f(range(10)), alts[0]) |
| 2614 | self.assertEqual(f(range(1, 11)), alts[1]) |
| 2615 | self.assertEqual(f(range(0, -10, -1)), alts[2]) |
| 2616 | self.assertEqual(f(range(-1, -11, -1)), alts[3]) |
| 2617 | self.assertEqual(f(range(10, 20)), alts[4]) |
| 2618 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2619 | def test_patma_247(self): |
Brandt Bucher | 0ad1e03 | 2021-05-02 13:02:10 -0700 | [diff] [blame] | 2620 | def f(x): |
| 2621 | match x: |
| 2622 | case [y, (a, b, c, d, e, f, g, h, i, 9) | |
| 2623 | (h, g, i, a, b, d, e, c, f, 10) | |
| 2624 | (g, b, a, c, d, -5, e, h, i, f) | |
| 2625 | (-1, d, f, b, g, e, i, a, h, c), z]: |
| 2626 | w = 0 |
| 2627 | out = locals() |
| 2628 | del out["x"] |
| 2629 | return out |
| 2630 | alts = [ |
| 2631 | dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0, y=False, z=True), |
| 2632 | dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0, y=False, z=True), |
| 2633 | dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0, y=False, z=True), |
| 2634 | dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0, y=False, z=True), |
| 2635 | dict(), |
| 2636 | ] |
| 2637 | self.assertEqual(f((False, range(10), True)), alts[0]) |
| 2638 | self.assertEqual(f((False, range(1, 11), True)), alts[1]) |
| 2639 | self.assertEqual(f((False, range(0, -10, -1), True)), alts[2]) |
| 2640 | self.assertEqual(f((False, range(-1, -11, -1), True)), alts[3]) |
| 2641 | self.assertEqual(f((False, range(10, 20), True)), alts[4]) |
| 2642 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 2643 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2644 | class TestSyntaxErrors(unittest.TestCase): |
Mark Shannon | 33ec88a | 2021-05-03 00:38:22 +0100 | [diff] [blame] | 2645 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2646 | def assert_syntax_error(self, code: str): |
| 2647 | with self.assertRaises(SyntaxError): |
| 2648 | compile(inspect.cleandoc(code), "<test>", "exec") |
| 2649 | |
| 2650 | def test_alternative_patterns_bind_different_names_0(self): |
| 2651 | self.assert_syntax_error(""" |
| 2652 | match ...: |
| 2653 | case "a" | a: |
| 2654 | pass |
| 2655 | """) |
| 2656 | |
| 2657 | def test_alternative_patterns_bind_different_names_1(self): |
| 2658 | self.assert_syntax_error(""" |
| 2659 | match ...: |
| 2660 | case [a, [b] | [c] | [d]]: |
| 2661 | pass |
| 2662 | """) |
| 2663 | |
| 2664 | |
| 2665 | def test_attribute_name_repeated_in_class_pattern(self): |
| 2666 | self.assert_syntax_error(""" |
| 2667 | match ...: |
| 2668 | case Class(a=_, a=_): |
| 2669 | pass |
| 2670 | """) |
| 2671 | |
| 2672 | def test_imaginary_number_required_in_complex_literal_0(self): |
| 2673 | self.assert_syntax_error(""" |
| 2674 | match ...: |
| 2675 | case 0+0: |
| 2676 | pass |
| 2677 | """) |
| 2678 | |
| 2679 | def test_imaginary_number_required_in_complex_literal_1(self): |
| 2680 | self.assert_syntax_error(""" |
| 2681 | match ...: |
| 2682 | case {0+0: _}: |
| 2683 | pass |
| 2684 | """) |
| 2685 | |
| 2686 | def test_invalid_syntax_0(self): |
| 2687 | self.assert_syntax_error(""" |
| 2688 | match ...: |
| 2689 | case {**rest, "key": value}: |
| 2690 | pass |
| 2691 | """) |
| 2692 | |
| 2693 | def test_invalid_syntax_1(self): |
| 2694 | self.assert_syntax_error(""" |
| 2695 | match ...: |
| 2696 | case {"first": first, **rest, "last": last}: |
| 2697 | pass |
| 2698 | """) |
| 2699 | |
| 2700 | def test_invalid_syntax_2(self): |
| 2701 | self.assert_syntax_error(""" |
| 2702 | match ...: |
| 2703 | case {**_}: |
| 2704 | pass |
| 2705 | """) |
| 2706 | |
| 2707 | def test_invalid_syntax_3(self): |
| 2708 | self.assert_syntax_error(""" |
| 2709 | match ...: |
| 2710 | case 42 as _: |
| 2711 | pass |
| 2712 | """) |
| 2713 | |
| 2714 | def test_mapping_pattern_keys_may_only_match_literals_and_attribute_lookups(self): |
| 2715 | self.assert_syntax_error(""" |
| 2716 | match ...: |
| 2717 | case {f"": _}: |
| 2718 | pass |
| 2719 | """) |
| 2720 | |
| 2721 | def test_multiple_assignments_to_name_in_pattern_0(self): |
| 2722 | self.assert_syntax_error(""" |
| 2723 | match ...: |
| 2724 | case a, a: |
| 2725 | pass |
| 2726 | """) |
| 2727 | |
| 2728 | def test_multiple_assignments_to_name_in_pattern_1(self): |
| 2729 | self.assert_syntax_error(""" |
| 2730 | match ...: |
| 2731 | case {"k": a, "l": a}: |
| 2732 | pass |
| 2733 | """) |
| 2734 | |
| 2735 | def test_multiple_assignments_to_name_in_pattern_2(self): |
| 2736 | self.assert_syntax_error(""" |
| 2737 | match ...: |
| 2738 | case MyClass(x, x): |
| 2739 | pass |
| 2740 | """) |
| 2741 | |
| 2742 | def test_multiple_assignments_to_name_in_pattern_3(self): |
| 2743 | self.assert_syntax_error(""" |
| 2744 | match ...: |
| 2745 | case MyClass(x=x, y=x): |
| 2746 | pass |
| 2747 | """) |
| 2748 | |
| 2749 | def test_multiple_assignments_to_name_in_pattern_4(self): |
| 2750 | self.assert_syntax_error(""" |
| 2751 | match ...: |
| 2752 | case MyClass(x, y=x): |
| 2753 | pass |
| 2754 | """) |
| 2755 | |
| 2756 | def test_multiple_assignments_to_name_in_pattern_5(self): |
| 2757 | self.assert_syntax_error(""" |
| 2758 | match ...: |
| 2759 | case a as a: |
| 2760 | pass |
| 2761 | """) |
| 2762 | |
| 2763 | def test_multiple_starred_names_in_sequence_pattern_0(self): |
| 2764 | self.assert_syntax_error(""" |
| 2765 | match ...: |
| 2766 | case *a, b, *c, d, *e: |
| 2767 | pass |
| 2768 | """) |
| 2769 | |
| 2770 | def test_multiple_starred_names_in_sequence_pattern_1(self): |
| 2771 | self.assert_syntax_error(""" |
| 2772 | match ...: |
| 2773 | case a, *b, c, *d, e: |
| 2774 | pass |
| 2775 | """) |
| 2776 | |
| 2777 | def test_name_capture_makes_remaining_patterns_unreachable_0(self): |
| 2778 | self.assert_syntax_error(""" |
| 2779 | match ...: |
| 2780 | case a | "a": |
| 2781 | pass |
| 2782 | """) |
| 2783 | |
| 2784 | def test_name_capture_makes_remaining_patterns_unreachable_1(self): |
| 2785 | self.assert_syntax_error(""" |
| 2786 | match 42: |
| 2787 | case x: |
| 2788 | pass |
| 2789 | case y: |
| 2790 | pass |
| 2791 | """) |
| 2792 | |
| 2793 | def test_name_capture_makes_remaining_patterns_unreachable_2(self): |
| 2794 | self.assert_syntax_error(""" |
| 2795 | match ...: |
| 2796 | case x | [_ as x] if x: |
| 2797 | pass |
| 2798 | """) |
| 2799 | |
| 2800 | def test_name_capture_makes_remaining_patterns_unreachable_3(self): |
| 2801 | self.assert_syntax_error(""" |
| 2802 | match ...: |
| 2803 | case x: |
| 2804 | pass |
| 2805 | case [x] if x: |
| 2806 | pass |
| 2807 | """) |
| 2808 | |
| 2809 | def test_name_capture_makes_remaining_patterns_unreachable_4(self): |
| 2810 | self.assert_syntax_error(""" |
| 2811 | match ...: |
| 2812 | case x: |
| 2813 | pass |
| 2814 | case _: |
| 2815 | pass |
| 2816 | """) |
| 2817 | |
| 2818 | def test_patterns_may_only_match_literals_and_attribute_lookups_0(self): |
| 2819 | self.assert_syntax_error(""" |
| 2820 | match ...: |
| 2821 | case f"": |
| 2822 | pass |
| 2823 | """) |
| 2824 | |
| 2825 | def test_patterns_may_only_match_literals_and_attribute_lookups_1(self): |
| 2826 | self.assert_syntax_error(""" |
| 2827 | match ...: |
| 2828 | case f"{x}": |
| 2829 | pass |
| 2830 | """) |
| 2831 | |
| 2832 | def test_real_number_required_in_complex_literal_0(self): |
| 2833 | self.assert_syntax_error(""" |
| 2834 | match ...: |
| 2835 | case 0j+0: |
| 2836 | pass |
| 2837 | """) |
| 2838 | |
| 2839 | def test_real_number_required_in_complex_literal_1(self): |
| 2840 | self.assert_syntax_error(""" |
| 2841 | match ...: |
| 2842 | case 0j+0j: |
| 2843 | pass |
| 2844 | """) |
| 2845 | |
| 2846 | def test_real_number_required_in_complex_literal_2(self): |
| 2847 | self.assert_syntax_error(""" |
| 2848 | match ...: |
| 2849 | case {0j+0: _}: |
| 2850 | pass |
| 2851 | """) |
| 2852 | |
| 2853 | def test_real_number_required_in_complex_literal_3(self): |
| 2854 | self.assert_syntax_error(""" |
| 2855 | match ...: |
| 2856 | case {0j+0j: _}: |
| 2857 | pass |
| 2858 | """) |
| 2859 | |
| 2860 | def test_wildcard_makes_remaining_patterns_unreachable_0(self): |
| 2861 | self.assert_syntax_error(""" |
| 2862 | match ...: |
| 2863 | case _ | _: |
| 2864 | pass |
| 2865 | """) |
| 2866 | |
| 2867 | def test_wildcard_makes_remaining_patterns_unreachable_1(self): |
| 2868 | self.assert_syntax_error(""" |
| 2869 | match ...: |
| 2870 | case (_ as x) | [x]: |
| 2871 | pass |
| 2872 | """) |
| 2873 | |
| 2874 | def test_wildcard_makes_remaining_patterns_unreachable_2(self): |
| 2875 | self.assert_syntax_error(""" |
| 2876 | match ...: |
| 2877 | case _ | _ if condition(): |
| 2878 | pass |
| 2879 | """) |
| 2880 | |
| 2881 | def test_wildcard_makes_remaining_patterns_unreachable_3(self): |
| 2882 | self.assert_syntax_error(""" |
| 2883 | match ...: |
| 2884 | case _: |
| 2885 | pass |
| 2886 | case None: |
| 2887 | pass |
| 2888 | """) |
| 2889 | |
| 2890 | def test_wildcard_makes_remaining_patterns_unreachable_4(self): |
| 2891 | self.assert_syntax_error(""" |
| 2892 | match ...: |
| 2893 | case (None | _) | _: |
| 2894 | pass |
| 2895 | """) |
| 2896 | |
| 2897 | def test_wildcard_makes_remaining_patterns_unreachable_5(self): |
| 2898 | self.assert_syntax_error(""" |
| 2899 | match ...: |
| 2900 | case _ | (True | False): |
| 2901 | pass |
| 2902 | """) |
| 2903 | |
| 2904 | |
| 2905 | class TestTypeErrors(unittest.TestCase): |
| 2906 | |
| 2907 | def test_accepts_positional_subpatterns_0(self): |
| 2908 | class Class: |
| 2909 | __match_args__ = () |
| 2910 | x = Class() |
| 2911 | y = z = None |
| 2912 | with self.assertRaises(TypeError): |
Mark Shannon | 33ec88a | 2021-05-03 00:38:22 +0100 | [diff] [blame] | 2913 | match x: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2914 | case Class(y): |
| 2915 | z = 0 |
| 2916 | self.assertIs(y, None) |
| 2917 | self.assertIs(z, None) |
| 2918 | |
| 2919 | def test_accepts_positional_subpatterns_1(self): |
| 2920 | x = range(10) |
| 2921 | y = None |
| 2922 | with self.assertRaises(TypeError): |
Mark Shannon | 33ec88a | 2021-05-03 00:38:22 +0100 | [diff] [blame] | 2923 | match x: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 2924 | case range(10): |
| 2925 | y = 0 |
| 2926 | self.assertEqual(x, range(10)) |
| 2927 | self.assertIs(y, None) |
| 2928 | |
| 2929 | def test_got_multiple_subpatterns_for_attribute_0(self): |
| 2930 | class Class: |
| 2931 | __match_args__ = ("a", "a") |
| 2932 | a = None |
| 2933 | x = Class() |
| 2934 | w = y = z = None |
| 2935 | with self.assertRaises(TypeError): |
| 2936 | match x: |
| 2937 | case Class(y, z): |
| 2938 | w = 0 |
| 2939 | self.assertIs(w, None) |
| 2940 | self.assertIs(y, None) |
| 2941 | self.assertIs(z, None) |
| 2942 | |
| 2943 | def test_got_multiple_subpatterns_for_attribute_1(self): |
| 2944 | class Class: |
| 2945 | __match_args__ = ("a",) |
| 2946 | a = None |
| 2947 | x = Class() |
| 2948 | w = y = z = None |
| 2949 | with self.assertRaises(TypeError): |
| 2950 | match x: |
| 2951 | case Class(y, a=z): |
| 2952 | w = 0 |
| 2953 | self.assertIs(w, None) |
| 2954 | self.assertIs(y, None) |
| 2955 | self.assertIs(z, None) |
| 2956 | |
| 2957 | def test_match_args_elements_must_be_strings(self): |
| 2958 | class Class: |
| 2959 | __match_args__ = (None,) |
| 2960 | x = Class() |
| 2961 | y = z = None |
| 2962 | with self.assertRaises(TypeError): |
| 2963 | match x: |
| 2964 | case Class(y): |
| 2965 | z = 0 |
| 2966 | self.assertIs(y, None) |
| 2967 | self.assertIs(z, None) |
| 2968 | |
| 2969 | def test_match_args_must_be_a_tuple_0(self): |
| 2970 | class Class: |
| 2971 | __match_args__ = None |
| 2972 | x = Class() |
| 2973 | y = z = None |
| 2974 | with self.assertRaises(TypeError): |
| 2975 | match x: |
| 2976 | case Class(y): |
| 2977 | z = 0 |
| 2978 | self.assertIs(y, None) |
| 2979 | self.assertIs(z, None) |
| 2980 | |
| 2981 | def test_match_args_must_be_a_tuple_1(self): |
| 2982 | class Class: |
| 2983 | __match_args__ = "XYZ" |
| 2984 | x = Class() |
| 2985 | y = z = None |
| 2986 | with self.assertRaises(TypeError): |
| 2987 | match x: |
| 2988 | case Class(y): |
| 2989 | z = 0 |
| 2990 | self.assertIs(y, None) |
| 2991 | self.assertIs(z, None) |
| 2992 | |
| 2993 | def test_match_args_must_be_a_tuple_2(self): |
| 2994 | class Class: |
| 2995 | __match_args__ = ["spam", "eggs"] |
| 2996 | spam = 0 |
| 2997 | eggs = 1 |
| 2998 | x = Class() |
| 2999 | w = y = z = None |
| 3000 | with self.assertRaises(TypeError): |
| 3001 | match x: |
| 3002 | case Class(y, z): |
| 3003 | w = 0 |
| 3004 | self.assertIs(w, None) |
| 3005 | self.assertIs(y, None) |
| 3006 | self.assertIs(z, None) |
Mark Shannon | 33ec88a | 2021-05-03 00:38:22 +0100 | [diff] [blame] | 3007 | |
| 3008 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3009 | class TestValueErrors(unittest.TestCase): |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3010 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3011 | def test_mapping_pattern_checks_duplicate_key_0(self): |
| 3012 | x = {"a": 0, "b": 1} |
| 3013 | w = y = z = None |
| 3014 | with self.assertRaises(ValueError): |
| 3015 | match x: |
| 3016 | case {"a": y, "a": z}: |
| 3017 | w = 0 |
| 3018 | self.assertIs(w, None) |
| 3019 | self.assertIs(y, None) |
| 3020 | self.assertIs(z, None) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3021 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3022 | def test_mapping_pattern_checks_duplicate_key_1(self): |
| 3023 | class Keys: |
| 3024 | KEY = "a" |
| 3025 | x = {"a": 0, "b": 1} |
| 3026 | w = y = z = None |
| 3027 | with self.assertRaises(ValueError): |
| 3028 | match x: |
| 3029 | case {Keys.KEY: y, "a": z}: |
| 3030 | w = 0 |
| 3031 | self.assertIs(w, None) |
| 3032 | self.assertIs(y, None) |
| 3033 | self.assertIs(z, None) |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3034 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3035 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3036 | if __name__ == "__main__": |
| 3037 | """ |
| 3038 | # From inside environment using this Python, with pyperf installed: |
| 3039 | sudo $(which pyperf) system tune && \ |
| 3040 | $(which python) -m test.test_patma --rigorous; \ |
| 3041 | sudo $(which pyperf) system reset |
| 3042 | """ |
| 3043 | import pyperf |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3044 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3045 | |
| 3046 | class PerfPatma(TestPatma): |
| 3047 | |
| 3048 | def assertEqual(*_, **__): |
| 3049 | pass |
| 3050 | |
| 3051 | def assertIs(*_, **__): |
| 3052 | pass |
| 3053 | |
| 3054 | def assertRaises(*_, **__): |
| 3055 | assert False, "this test should be a method of a different class!" |
| 3056 | |
| 3057 | def run_perf(self, count): |
| 3058 | tests = [] |
| 3059 | for attr in vars(TestPatma): |
| 3060 | if attr.startswith("test_"): |
| 3061 | tests.append(getattr(self, attr)) |
| 3062 | tests *= count |
| 3063 | start = pyperf.perf_counter() |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3064 | for test in tests: |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3065 | test() |
| 3066 | return pyperf.perf_counter() - start |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 3067 | |
| 3068 | |
Miss Islington (bot) | bba7267 | 2021-06-18 10:48:07 -0700 | [diff] [blame] | 3069 | runner = pyperf.Runner() |
| 3070 | runner.bench_time_func("patma", PerfPatma().run_perf) |