def test_nameof(): a = 1 b = nameof(a) assert b == 'a' nameof2 = nameof c = nameof2(a, b) assert b == 'a' assert c == ('a', 'b') def func(): return varname() + 'abc' f = func() assert f == 'fabc' assert nameof(f) == 'f' assert 'f' == nameof(f) assert len(nameof(f)) == 1 fname1 = fname = nameof(f) assert fname1 == fname == 'f' with pytest.raises(VarnameRetrievingError): nameof(a == 1) with pytest.raises(VarnameRetrievingError): _bytecode_nameof(a == 1) with pytest.raises(VarnameRetrievingError): nameof()
def test_nameof(self): a = 1 b = nameof_both(a) assert b == 'a' nameof2 = nameof_both c = nameof2(a, b) assert b == 'a' assert c == ('a', 'b') def func(): return varname() + 'abc' f = func() assert f == 'fabc' self.assertEqual(nameof_both(f), 'f') self.assertEqual('f', nameof_both(f)) self.assertEqual(len(nameof_both(f)), 1) fname1 = fname = nameof_both(f) self.assertEqual(fname, 'f') self.assertEqual(fname1, 'f') with pytest.raises(VarnameRetrievingError): nameof_both(a == 1) with pytest.raises(VarnameRetrievingError): _bytecode_nameof(a == 1)
def test_bytecode_pytest_nameof_fail(self): with pytest.raises( VarnameRetrievingError, match=("Found the variable name '@py_assert2' " "which is obviously wrong."), ): lam = lambda: 0 lam.a = 1 assert _bytecode_nameof(lam.a) == 'a'
def __add__(self, other): _bytecode_nameof(caller=2)
def test_original_nameof(): x = 1 assert original_nameof(x) == nameof(x) == _bytecode_nameof(x) == 'x'
def nameof(*args): """Test both implementations at the same time""" result = original_nameof(*args, caller=2) if len(args) == 1: assert result == _bytecode_nameof(caller=2) return result
def test_original_nameof(self): x = 1 self.assertEqual(nameof(x), 'x') self.assertEqual(nameof_both(x), 'x') self.assertEqual(_bytecode_nameof(x), 'x')