MCPcopy Index your code
hub / github.com/python/cpython / test_simple_compare

Method test_simple_compare

Lib/test/test_dataclasses/__init__.py:409–461  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

407 self.assertGreaterEqual(C(1), C(1))
408
409 def test_simple_compare(self):
410 # Ensure that order=False is the default.
411 @dataclass
412 class C0:
413 x: int
414 y: int
415
416 @dataclass(order=False)
417 class C1:
418 x: int
419 y: int
420
421 for cls in [C0, C1]:
422 with self.subTest(cls=cls):
423 self.assertEqual(cls(0, 0), cls(0, 0))
424 self.assertEqual(cls(1, 2), cls(1, 2))
425 self.assertNotEqual(cls(1, 0), cls(0, 0))
426 self.assertNotEqual(cls(1, 0), cls(1, 1))
427 for idx, fn in enumerate([lambda a, b: a < b,
428 lambda a, b: a <= b,
429 lambda a, b: a > b,
430 lambda a, b: a >= b]):
431 with self.subTest(idx=idx):
432 with self.assertRaisesRegex(TypeError,
433 f"not supported between instances of '{cls.__name__}' and '{cls.__name__}'"):
434 fn(cls(0, 0), cls(0, 0))
435
436 @dataclass(order=True)
437 class C:
438 x: int
439 y: int
440
441 for idx, fn in enumerate([lambda a, b: a == b,
442 lambda a, b: a <= b,
443 lambda a, b: a >= b]):
444 with self.subTest(idx=idx):
445 self.assertTrue(fn(C(0, 0), C(0, 0)))
446
447 for idx, fn in enumerate([lambda a, b: a < b,
448 lambda a, b: a <= b,
449 lambda a, b: a != b]):
450 with self.subTest(idx=idx):
451 self.assertTrue(fn(C(0, 0), C(0, 1)))
452 self.assertTrue(fn(C(0, 1), C(1, 0)))
453 self.assertTrue(fn(C(1, 0), C(1, 1)))
454
455 for idx, fn in enumerate([lambda a, b: a > b,
456 lambda a, b: a >= b,
457 lambda a, b: a != b]):
458 with self.subTest(idx=idx):
459 self.assertTrue(fn(C(0, 1), C(0, 0)))
460 self.assertTrue(fn(C(1, 0), C(0, 1)))
461 self.assertTrue(fn(C(1, 1), C(1, 0)))
462
463 def test_compare_subclasses(self):
464 # Comparisons fail for subclasses, even if no fields

Callers

nothing calls this directly

Calls 9

subTestMethod · 0.95
assertEqualMethod · 0.95
assertNotEqualMethod · 0.95
assertRaisesRegexMethod · 0.95
assertTrueMethod · 0.95
enumerateFunction · 0.85
fnFunction · 0.85
CClass · 0.70
clsClass · 0.50

Tested by

no test coverage detected