1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
import dataclasses
from collections import Counter
from collections import OrderedDict
from collections import UserDict
from collections import UserList
from collections import defaultdict
from collections import namedtuple
from dataclasses import dataclass
from typing import NamedTuple
import pytest
from inline_snapshot import HasRepr
from inline_snapshot import snapshot
from inline_snapshot._code_repr import code_repr
from inline_snapshot._sentinels import undefined
from inline_snapshot.testing import Example
def test_enum(check_update):
assert (
check_update(
"""
from enum import Enum
class color(Enum):
val="val"
assert [color.val] == snapshot()
""",
flags="create",
)
== snapshot(
"""\
from enum import Enum
class color(Enum):
val="val"
assert [color.val] == snapshot([color.val])
"""
)
)
def test_snapshot_generates_hasrepr():
Example(
"""\
from inline_snapshot import snapshot
class Thing:
def __repr__(self):
return "<something>"
def __eq__(self,other):
if not isinstance(other,Thing):
return NotImplemented
return True
def test_thing():
assert Thing() == snapshot()
"""
).run_pytest(
["--inline-snapshot=create"],
returncode=snapshot(1),
changed_files=snapshot(
{
"test_something.py": """\
from inline_snapshot import snapshot
from inline_snapshot import HasRepr
class Thing:
def __repr__(self):
return "<something>"
def __eq__(self,other):
if not isinstance(other,Thing):
return NotImplemented
return True
def test_thing():
assert Thing() == snapshot(HasRepr(Thing, "<something>"))
\
"""
}
),
).run_pytest(
["--inline-snapshot=disable"], returncode=0
).run_pytest(
returncode=0
)
def test_hasrepr_type():
assert 5 == HasRepr(int, "5")
assert not "a" == HasRepr(int, "5")
assert not HasRepr(float, "nan") == HasRepr(str, "nan")
assert not HasRepr(str, "a") == HasRepr(str, "b")
def test_enum_in_dataclass(check_update):
assert (
check_update(
"""
from enum import Enum
from dataclasses import dataclass
class color(Enum):
red="red"
blue="blue"
@dataclass
class container:
bg: color=color.red
fg: color=color.blue
assert container(bg=color.red,fg=color.red) == snapshot()
""",
flags="create",
)
== snapshot(
"""\
from enum import Enum
from dataclasses import dataclass
class color(Enum):
red="red"
blue="blue"
@dataclass
class container:
bg: color=color.red
fg: color=color.blue
assert container(bg=color.red,fg=color.red) == snapshot(container(fg=color.red))
"""
)
)
def test_flag(check_update):
assert (
check_update(
"""
from enum import Flag, auto
class Color(Flag):
red = auto()
green = auto()
blue = auto()
assert Color.red | Color.blue == snapshot()
""",
flags="create",
)
== snapshot(
"""\
from enum import Flag, auto
class Color(Flag):
red = auto()
green = auto()
blue = auto()
assert Color.red | Color.blue == snapshot(Color.red | Color.blue)
"""
)
)
def test_type(check_update):
assert (
check_update(
"""\
class Color:
pass
assert [Color,int] == snapshot()
""",
flags="create",
)
== snapshot(
"""\
class Color:
pass
assert [Color,int] == snapshot([Color, int])
"""
)
)
def test_qualname():
Example(
"""\
from enum import Enum
from inline_snapshot import snapshot
class Namespace:
class Color(Enum):
red="red"
def test():
assert Namespace.Color.red == snapshot()
"""
).run_inline(
["--inline-snapshot=create"],
changed_files=snapshot(
{
"test_something.py": """\
from enum import Enum
from inline_snapshot import snapshot
class Namespace:
class Color(Enum):
red="red"
def test():
assert Namespace.Color.red == snapshot(Namespace.Color.red)
\
"""
}
),
).run_inline()
A = namedtuple("A", "a,b", defaults=[0])
B = namedtuple("B", "a,b", defaults=[0, 0])
class C(NamedTuple):
a: int
b: int = 0
c: int = 0
@dataclass
class Dataclass:
a: int
b: int = dataclasses.field(default=0)
c: list = dataclasses.field(default_factory=lambda: [])
default_dict = defaultdict(list)
default_dict[5].append(2)
default_dict[3].append(1)
@pytest.mark.parametrize(
"d",
[
frozenset(["a"]),
frozenset(),
{"a"},
set(),
list(),
["a"],
{},
{1: "1"},
(),
(1,),
(1, 2, 3),
A(1, 2),
A(1),
A(0, 0),
B(),
B(b=5),
C(1),
C(1, 2),
C(a=1, c=2),
Dataclass(a=0, b=0, c=[]),
Dataclass(a=1, b=2, c=[3]),
default_dict,
OrderedDict({1: 2, 3: 4}),
UserDict({1: 2}),
UserList([1, 2]),
undefined,
],
)
def test_datatypes(d):
code = code_repr(d)
print("repr: ", repr(d))
print("code_repr:", code)
assert d == eval(code)
def test_set():
assert code_repr({1, 2, 3, "a", True, "b"}) == snapshot("{'a', 'b', 1, 2, 3}")
assert code_repr({1j, 2j, 3j, "a", True, "b"}) == snapshot(
"{'a', 'b', 1j, 2j, 3j, True}"
)
assert code_repr({1, 2, 3, 10, 11, 20, 200}) == snapshot(
"{1, 2, 3, 10, 11, 20, 200}"
)
def test_datatypes_explicit():
assert code_repr(C(a=1, c=2)) == snapshot("C(a=1, c=2)")
assert code_repr(B(b=5)) == snapshot("B(b=5)")
assert code_repr(B(b=0)) == snapshot("B()")
assert code_repr(Dataclass(a=0, b=0, c=[])) == snapshot("Dataclass(a=0)")
assert code_repr(Dataclass(a=1, b=2, c=[3])) == snapshot(
"Dataclass(a=1, b=2, c=[3])"
)
assert code_repr(Counter([1, 1, 1, 2])) == snapshot("Counter({1: 3, 2: 1})")
assert code_repr(default_dict) == snapshot("defaultdict(list, {5: [2], 3: [1]})")
def test_tuple():
class FakeTuple(tuple):
def __init__(self):
self._fields = 5
def __repr__(self):
return "FakeTuple()"
assert code_repr(FakeTuple()) == snapshot("FakeTuple()")
def test_invalid_repr(check_update):
assert (
check_update(
"""\
class Thing:
def __repr__(self):
return "+++"
def __eq__(self,other):
if not isinstance(other,Thing):
return NotImplemented
return True
assert Thing() == snapshot()
""",
flags="create",
)
== snapshot(
"""\
class Thing:
def __repr__(self):
return "+++"
def __eq__(self,other):
if not isinstance(other,Thing):
return NotImplemented
return True
assert Thing() == snapshot(HasRepr(Thing, "+++"))
"""
)
)
|