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