3
3
import pytest
4
4
import unittest
5
5
from varname .utils import bytecode_nameof as bytecode_nameof_cached
6
- from varname import *
6
+ from varname import nameof , varname , ImproperUseError , VarnameRetrievingError
7
+
7
8
# config.debug = True
8
9
10
+
9
11
def bytecode_nameof (frame ):
10
12
frame = sys ._getframe (frame )
11
13
return bytecode_nameof_cached (frame .f_code , frame .f_lasti )
12
14
15
+
13
16
def nameof_both (var , * more_vars ):
14
17
"""Test both implementations at the same time"""
15
18
result = nameof (var , * more_vars , frame = 2 )
@@ -18,58 +21,62 @@ def nameof_both(var, *more_vars):
18
21
assert result == bytecode_nameof (frame = 2 )
19
22
return result
20
23
24
+
21
25
class Weird :
22
26
def __add__ (self , other ):
23
27
bytecode_nameof (frame = 2 )
24
28
29
+
25
30
class TestNameof (unittest .TestCase ):
26
31
def test_original_nameof (self ):
27
32
x = 1
28
- self .assertEqual (nameof (x ), 'x' )
29
- self .assertEqual (nameof_both (x ), 'x' )
30
- self .assertEqual (bytecode_nameof (x ), 'x' )
33
+ self .assertEqual (nameof (x ), "x" )
34
+ self .assertEqual (nameof_both (x ), "x" )
35
+ self .assertEqual (bytecode_nameof (x ), "x" )
31
36
32
37
def test_bytecode_nameof_wrong_node (self ):
33
38
with pytest .raises (
34
- VarnameRetrievingError ,
35
- match = "Did you call 'nameof' in a weird way" ,
39
+ VarnameRetrievingError ,
40
+ match = "Did you call 'nameof' in a weird way" ,
36
41
):
37
42
Weird () + Weird ()
38
43
39
44
def test_bytecode_pytest_nameof_fail (self ):
40
45
with pytest .raises (
41
- VarnameRetrievingError ,
42
- match = ("Found the variable name '@py_assert2' "
43
- "which is obviously wrong." ),
46
+ VarnameRetrievingError ,
47
+ match = (
48
+ "Found the variable name '@py_assert2' " "which is obviously wrong."
49
+ ),
44
50
):
45
51
lam = lambda : 0
46
52
lam .a = 1
47
- assert bytecode_nameof (lam .a ) == 'a'
53
+ assert bytecode_nameof (lam .a ) == "a"
48
54
49
55
def test_nameof (self ):
50
56
a = 1
51
57
b = nameof_both (a )
52
- assert b == 'a'
58
+ assert b == "a"
53
59
nameof2 = nameof_both
54
60
c = nameof2 (a , b )
55
- assert b == 'a'
56
- assert c == ('a' , 'b' )
61
+ assert b == "a"
62
+ assert c == ("a" , "b" )
63
+
57
64
def func ():
58
- return varname () + ' abc'
65
+ return varname () + " abc"
59
66
60
67
f = func ()
61
- assert f == ' fabc'
68
+ assert f == " fabc"
62
69
63
- self .assertEqual (nameof_both (f ), 'f' )
64
- self .assertEqual ('f' , nameof_both (f ))
70
+ self .assertEqual (nameof_both (f ), "f" )
71
+ self .assertEqual ("f" , nameof_both (f ))
65
72
self .assertEqual (len (nameof_both (f )), 1 )
66
73
67
74
fname1 = fname = nameof_both (f )
68
- self .assertEqual (fname , 'f' )
69
- self .assertEqual (fname1 , 'f' )
75
+ self .assertEqual (fname , "f" )
76
+ self .assertEqual (fname1 , "f" )
70
77
71
78
with pytest .raises (ImproperUseError ):
72
- nameof_both (a == 1 )
79
+ nameof_both (a == 1 )
73
80
74
81
with pytest .raises (VarnameRetrievingError ):
75
82
bytecode_nameof (a == 1 )
@@ -79,30 +86,30 @@ def func():
79
86
# nameof_both()
80
87
81
88
def test_nameof_statements (self ):
82
- a = {' test' : 1 }
89
+ a = {" test" : 1 }
83
90
test = {}
84
91
del a [nameof_both (test )]
85
92
assert a == {}
86
93
87
94
def func ():
88
95
return nameof_both (test )
89
96
90
- assert func () == ' test'
97
+ assert func () == " test"
91
98
92
99
def func2 ():
93
100
yield nameof_both (test )
94
101
95
- assert list (func2 ()) == [' test' ]
102
+ assert list (func2 ()) == [" test" ]
96
103
97
104
def func3 ():
98
105
raise ValueError (nameof_both (test ))
99
106
100
107
with pytest .raises (ValueError ) as verr :
101
108
func3 ()
102
- assert str (verr .value ) == ' test'
109
+ assert str (verr .value ) == " test"
103
110
104
111
for i in [0 ]:
105
- self .assertEqual (nameof_both (test ), ' test' )
112
+ self .assertEqual (nameof_both (test ), " test" )
106
113
self .assertEqual (len (nameof_both (test )), 4 )
107
114
108
115
def test_nameof_expr (self ):
@@ -126,4 +133,4 @@ def test_nameof_expr(self):
126
133
self .assertEqual (nameof_both (lam .lam .lam .lam ), "lam" )
127
134
self .assertEqual (nameof_both (lams [0 ].lam ), "lam" )
128
135
self .assertEqual (nameof_both (lams [0 ].lam .a ), "a" )
129
- self .assertEqual (nameof_both ((lam () or lams [0 ]).lam .a ), "a" )
136
+ self .assertEqual (nameof_both ((lam () or lams [0 ]).lam .a ), "a" )
0 commit comments