|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import unittest
|
| 4 | +from collections.abc import Iterator, Mapping |
4 | 5 | from datetime import datetime, timedelta
|
5 | 6 | from decimal import Decimal
|
6 | 7 | from fractions import Fraction
|
7 |
| -from typing_extensions import assert_type |
| 8 | +from typing_extensions import TypedDict, assert_type |
8 | 9 | from unittest.mock import MagicMock, Mock, patch
|
9 | 10 |
|
10 | 11 | case = unittest.TestCase()
|
@@ -89,6 +90,54 @@ def __gt__(self, other: Bacon) -> bool:
|
89 | 90 | case.assertGreater(Ham(), Bacon()) # type: ignore
|
90 | 91 | case.assertGreater(Bacon(), Ham()) # type: ignore
|
91 | 92 |
|
| 93 | + |
| 94 | +### |
| 95 | +# Tests for assertDictEqual |
| 96 | +### |
| 97 | + |
| 98 | + |
| 99 | +class TD1(TypedDict): |
| 100 | + x: int |
| 101 | + y: str |
| 102 | + |
| 103 | + |
| 104 | +class TD2(TypedDict): |
| 105 | + a: bool |
| 106 | + b: bool |
| 107 | + |
| 108 | + |
| 109 | +class MyMapping(Mapping[str, int]): |
| 110 | + def __getitem__(self, __key: str) -> int: |
| 111 | + return 42 |
| 112 | + |
| 113 | + def __iter__(self) -> Iterator[str]: |
| 114 | + return iter([]) |
| 115 | + |
| 116 | + def __len__(self) -> int: |
| 117 | + return 0 |
| 118 | + |
| 119 | + |
| 120 | +td1: TD1 = {"x": 1, "y": "foo"} |
| 121 | +td2: TD2 = {"a": True, "b": False} |
| 122 | +m = MyMapping() |
| 123 | + |
| 124 | +case.assertDictEqual({}, {}) |
| 125 | +case.assertDictEqual({"x": 1, "y": 2}, {"x": 1, "y": 2}) |
| 126 | +case.assertDictEqual({"x": 1, "y": "foo"}, {"y": "foo", "x": 1}) |
| 127 | +case.assertDictEqual({"x": 1}, {}) |
| 128 | +case.assertDictEqual({}, {"x": 1}) |
| 129 | +case.assertDictEqual({1: "x"}, {"y": 222}) |
| 130 | +case.assertDictEqual({1: "x"}, td1) |
| 131 | +case.assertDictEqual(td1, {1: "x"}) |
| 132 | +case.assertDictEqual(td1, td2) |
| 133 | + |
| 134 | +case.assertDictEqual(1, {}) # type: ignore |
| 135 | +case.assertDictEqual({}, 1) # type: ignore |
| 136 | + |
| 137 | +# These should fail, but don't due to TypedDict limitations: |
| 138 | +# case.assertDictEqual(m, {"": 0}) # xtype: ignore |
| 139 | +# case.assertDictEqual({"": 0}, m) # xtype: ignore |
| 140 | + |
92 | 141 | ###
|
93 | 142 | # Tests for mock.patch
|
94 | 143 | ###
|
|
0 commit comments