|
1 | 1 | import sys |
| 2 | +import unittest |
| 3 | + |
2 | 4 | import pytest |
3 | 5 | from varname import (varname, |
4 | 6 | VarnameRetrievingWarning, |
@@ -26,12 +28,6 @@ def nameof(*args): |
26 | 28 | varname_module.nameof = nameof |
27 | 29 |
|
28 | 30 |
|
29 | | -def test_original_nameof(): |
30 | | - x = 1 |
31 | | - assert original_nameof(x) == nameof(x) == _bytecode_nameof(x) == 'x' |
32 | | - |
33 | | - |
34 | | - |
35 | 31 | @pytest.fixture |
36 | 32 | def no_getframe(): |
37 | 33 | """ |
@@ -258,74 +254,103 @@ def test_wrapper(): |
258 | 254 | assert str(val1) == 'True' |
259 | 255 | assert repr(val1) == "<Wrapper (name='val1', value=True)>" |
260 | 256 |
|
261 | | -def test_nameof(): |
262 | | - a = 1 |
263 | | - b = nameof(a) |
264 | | - assert b == 'a' |
265 | | - nameof2 = nameof |
266 | | - c = nameof2(a, b) |
267 | | - assert b == 'a' |
268 | | - assert c == ('a', 'b') |
269 | 257 |
|
270 | | - def func(): |
271 | | - return varname() + 'abc' |
| 258 | +class TestNameof(unittest.TestCase): |
| 259 | + def test_original_nameof(self): |
| 260 | + x = 1 |
| 261 | + self.assertEqual(original_nameof(x), 'x') |
| 262 | + self.assertEqual(nameof(x), 'x') |
| 263 | + self.assertEqual(_bytecode_nameof(x), 'x') |
272 | 264 |
|
273 | | - f = func() |
274 | | - assert f == 'fabc' |
| 265 | + def test_nameof(self): |
| 266 | + a = 1 |
| 267 | + b = nameof(a) |
| 268 | + assert b == 'a' |
| 269 | + nameof2 = nameof |
| 270 | + c = nameof2(a, b) |
| 271 | + assert b == 'a' |
| 272 | + assert c == ('a', 'b') |
275 | 273 |
|
276 | | - assert nameof(f) == 'f' |
277 | | - assert 'f' == nameof(f) |
278 | | - assert len(nameof(f)) == 1 |
| 274 | + def func(): |
| 275 | + return varname() + 'abc' |
279 | 276 |
|
280 | | - fname1 = fname = nameof(f) |
281 | | - assert fname1 == fname == 'f' |
| 277 | + f = func() |
| 278 | + assert f == 'fabc' |
282 | 279 |
|
283 | | - with pytest.raises(VarnameRetrievingError): |
284 | | - nameof(a==1) |
| 280 | + self.assertEqual(nameof(f), 'f') |
| 281 | + self.assertEqual('f', nameof(f)) |
| 282 | + self.assertEqual(len(nameof(f)), 1) |
285 | 283 |
|
286 | | - with pytest.raises(VarnameRetrievingError): |
287 | | - _bytecode_nameof(a == 1) |
| 284 | + fname1 = fname = nameof(f) |
| 285 | + self.assertEqual(fname, 'f') |
| 286 | + self.assertEqual(fname1, 'f') |
288 | 287 |
|
289 | | - with pytest.raises(VarnameRetrievingError): |
290 | | - nameof() |
| 288 | + with pytest.raises(VarnameRetrievingError): |
| 289 | + nameof(a==1) |
291 | 290 |
|
292 | | -def test_nameof_statements(): |
293 | | - a = {'test': 1} |
294 | | - test = {} |
295 | | - del a[nameof(test)] |
296 | | - assert a == {} |
| 291 | + with pytest.raises(VarnameRetrievingError): |
| 292 | + _bytecode_nameof(a == 1) |
297 | 293 |
|
298 | | - def func(): |
299 | | - return nameof(test) |
| 294 | + with pytest.raises(VarnameRetrievingError): |
| 295 | + nameof() |
300 | 296 |
|
301 | | - assert func() == 'test' |
| 297 | + def test_nameof_statements(self): |
| 298 | + a = {'test': 1} |
| 299 | + test = {} |
| 300 | + del a[nameof(test)] |
| 301 | + assert a == {} |
302 | 302 |
|
303 | | - def func2(): |
304 | | - yield nameof(test) |
| 303 | + def func(): |
| 304 | + return nameof(test) |
| 305 | + |
| 306 | + assert func() == 'test' |
| 307 | + |
| 308 | + def func2(): |
| 309 | + yield nameof(test) |
305 | 310 |
|
306 | | - assert list(func2()) == ['test'] |
| 311 | + assert list(func2()) == ['test'] |
307 | 312 |
|
308 | | - def func3(): |
309 | | - raise ValueError(nameof(test)) |
| 313 | + def func3(): |
| 314 | + raise ValueError(nameof(test)) |
310 | 315 |
|
311 | | - with pytest.raises(ValueError) as verr: |
312 | | - func3() |
313 | | - assert str(verr.value) == 'test' |
| 316 | + with pytest.raises(ValueError) as verr: |
| 317 | + func3() |
| 318 | + assert str(verr.value) == 'test' |
314 | 319 |
|
315 | | - for i in [0]: |
316 | | - assert nameof(test) == 'test' |
317 | | - assert len(nameof(test)) == 4 |
| 320 | + for i in [0]: |
| 321 | + self.assertEqual(nameof(test), 'test') |
| 322 | + self.assertEqual(len(nameof(test)), 4) |
318 | 323 |
|
319 | | -def test_nameof_expr(): |
320 | | - test = {} |
321 | | - assert len(varname_module.nameof(test)) == 4 |
| 324 | + def test_nameof_expr(self): |
| 325 | + test = {} |
| 326 | + self.assertEqual(len(varname_module.nameof(test)), 4) |
| 327 | + |
| 328 | + lam = lambda: 0 |
| 329 | + lam.a = 1 |
| 330 | + with pytest.raises(VarnameRetrievingError) as vrerr: |
| 331 | + varname_module.nameof(test, lam.a) |
| 332 | + assert str(vrerr.value) == ("Only variables should " |
| 333 | + "be passed to nameof.") |
| 334 | + |
| 335 | + |
| 336 | +def test_nameof_pytest_fail(): |
| 337 | + with pytest.raises( |
| 338 | + VarnameRetrievingError, |
| 339 | + match="Couldn't retrieve the call node. " |
| 340 | + "This may happen if you're using some other AST magic" |
| 341 | + ): |
| 342 | + assert nameof(nameof) == 'nameof' |
| 343 | + |
| 344 | + |
| 345 | +def test_bytecode_pytest_nameof_fail(): |
| 346 | + with pytest.raises( |
| 347 | + VarnameRetrievingError, |
| 348 | + match="Found the variable name '@py_assert2' which is obviously wrong.", |
| 349 | + ): |
| 350 | + lam = lambda: 0 |
| 351 | + lam.a = 1 |
| 352 | + assert _bytecode_nameof(lam.a) == 'a' |
322 | 353 |
|
323 | | - lam = lambda: 0 |
324 | | - lam.a = 1 |
325 | | - with pytest.raises(VarnameRetrievingError) as vrerr: |
326 | | - varname_module.nameof(test, lam.a) |
327 | | - assert str(vrerr.value) == ("Only variables should " |
328 | | - "be passed to nameof.") |
329 | 354 |
|
330 | 355 | def test_class_property(): |
331 | 356 | class C: |
@@ -382,7 +407,8 @@ def do(self): |
382 | 407 | return 'I will do something' |
383 | 408 |
|
384 | 409 | c = C() |
385 | | - assert c.iwill.do() == 'I will do something' |
| 410 | + result = c.iwill.do() |
| 411 | + assert result == 'I will do something' |
386 | 412 |
|
387 | 413 | def test_will_fail(): |
388 | 414 |
|
|
0 commit comments