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