예제 #1
0
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()
예제 #2
0
    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)
예제 #3
0
 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'
예제 #4
0
 def __add__(self, other):
     _bytecode_nameof(caller=2)
예제 #5
0
def test_original_nameof():
    x = 1
    assert original_nameof(x) == nameof(x) == _bytecode_nameof(x) == 'x'
예제 #6
0
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
예제 #7
0
 def test_original_nameof(self):
     x = 1
     self.assertEqual(nameof(x), 'x')
     self.assertEqual(nameof_both(x), 'x')
     self.assertEqual(_bytecode_nameof(x), 'x')