This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
||||
from slic.utils.debug import *
|
||||
from unittest.mock import patch
|
||||
|
||||
class A(Traceable):
|
||||
def __init__(self, a, b, c=None, d=None, **kwargs):
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.c = c
|
||||
self.d = d
|
||||
self.extra = kwargs
|
||||
|
||||
class CustomObj:
|
||||
def __init__(self, val):
|
||||
self.val = val
|
||||
def __repr__(self):
|
||||
return f"CustomObj({self.val})"
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"cls, entry, expected",
|
||||
[
|
||||
# A(10, 20)
|
||||
(A, ([10, 20], {}),
|
||||
"creating: A(10, 20)"),
|
||||
|
||||
# A(10, 20, e=100)
|
||||
(A, ([10, 20], {"e": 100}),
|
||||
"creating: A(10, 20, e=100)"),
|
||||
|
||||
# A('foo', [1,2,3], flag=True, data={'x': 9})
|
||||
(A, (['foo', [1,2,3]], {"flag": True, "data": {"x": 9}}),
|
||||
f"creating: A('foo', [1, 2, 3], flag={short_repr(True)}, data={short_repr({'x': 9})})"),
|
||||
|
||||
# A(CustomObj("big"), [0]*12, name="test", meta="y"*70)
|
||||
(A, ([CustomObj("big"), [0]*12], {"name": "test", "meta": "y"*70}),
|
||||
f"creating: A({short_repr(CustomObj('big'))}, {short_repr([0]*12)}, name={short_repr('test')}, meta={short_repr('y'*70)})"),
|
||||
|
||||
# A("A"*60, [0]*5) # long string, no kwargs
|
||||
(A, (["A"*60, [0]*5], {}),
|
||||
f"creating: A({short_repr('A'*60)}, {short_repr([0]*5)})"),
|
||||
]
|
||||
)
|
||||
def test_traceable(cls, entry, expected):
|
||||
args, kwargs = entry
|
||||
with patch("slic.utils.debug.log.trace") as mock_trace:
|
||||
cls(*args, **kwargs)
|
||||
actual = mock_trace.call_args[0][0]
|
||||
assert actual == expected
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"value, cutoff, expected",
|
||||
[
|
||||
("abc", 10, "'abc'"),
|
||||
("a" * 100, 10, "'aaaaaaaaaa..."),
|
||||
(12345, 10, "12345"),
|
||||
([0]*100, 15, str(repr([0]*100))[:15] + "..."),
|
||||
(None, 10, "None"),
|
||||
(type("Obj", (), {"__repr__": lambda self: "Obj(" + "x"*50 + ")"})(), 20, "Obj(xxxxxxxxxxxxxxxxx..."),
|
||||
]
|
||||
)
|
||||
|
||||
def test_short_repr(value, cutoff, expected):
|
||||
assert short_repr(value, cutoff) == expected
|
||||
Reference in New Issue
Block a user