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