better cache decorator

This commit is contained in:
2024-02-28 14:25:56 +01:00
parent 4ef82397bc
commit d84138f668
2 changed files with 47 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
import scgenerator.cache as scc
from scgenerator.cache import Cache
from scgenerator.cache import Cache, methodcache, normalize_path
def test_io():
@@ -60,8 +60,21 @@ def test_toml():
assert not cache3.dir.exists()
class Stuff:
call_count = 0
@classmethod
def from_other(cls, stuff: str):
cls.call_count += 1
return cls(stuff[:5])
def __init__(self, x):
self.x = x
def test_decorator():
cache = Cache("Test")
cache.compile_class(Stuff)
cache.delete()
call_count = 0
@@ -77,12 +90,38 @@ def test_decorator():
call_count += 1
return sum(some_list)
Stuff.from_other = cache(lambda _, *args: str(args))(Stuff.from_other)
assert func("hello") == "hellohello"
assert func2([0, 1, 2, 80]) == 83
assert func2([0, 1, 2, 80]) == 83
assert (scc.CACHE_DIR / "Test" / "test_decorator.<locals>.func hello").exists()
assert (scc.CACHE_DIR / "Test" / "test_decorator.<locals>.func2 0-80 4").exists()
assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func hello").exists()
assert (scc.CACHE_DIR / "Test" / "test_decorator._locals_.func2 0-80 4").exists()
assert Stuff.from_other("Bonjour").x == "Bonjo"
assert Stuff.from_other("Bonjour").x == "Bonjo"
assert Stuff.call_count == 1
assert call_count == 2
cache.delete()
def test_normalize():
assert normalize_path("abd") == "abd"
assert normalize_path("a/b") == "a_b"
assert normalize_path("bonjour\nsdfk") == "bonjour sdfk"
assert normalize_path("a\\b") == "a_b"
assert (
normalize_path(
"""sdfl asdfk
asd
"""
)
== "sdfl asdfk asd"
)