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