test_istr.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import gc
  2. import sys
  3. from typing import Callable, Type
  4. import pytest
  5. IMPLEMENTATION = getattr(sys, "implementation") # to suppress mypy error
  6. def test_ctor(case_insensitive_str_class: Type[str]) -> None:
  7. s = case_insensitive_str_class()
  8. assert "" == s
  9. def test_ctor_str(case_insensitive_str_class: Type[str]) -> None:
  10. s = case_insensitive_str_class("aBcD")
  11. assert "aBcD" == s
  12. def test_ctor_istr(case_insensitive_str_class: Type[str]) -> None:
  13. s = case_insensitive_str_class("A")
  14. s2 = case_insensitive_str_class(s)
  15. assert "A" == s
  16. assert s == s2
  17. def test_ctor_buffer(case_insensitive_str_class: Type[str]) -> None:
  18. s = case_insensitive_str_class(b"aBc")
  19. assert "b'aBc'" == s
  20. def test_ctor_repr(case_insensitive_str_class: Type[str]) -> None:
  21. s = case_insensitive_str_class(None)
  22. assert "None" == s
  23. def test_str(case_insensitive_str_class: Type[str]) -> None:
  24. s = case_insensitive_str_class("aBcD")
  25. s1 = str(s)
  26. assert s1 == "aBcD"
  27. assert type(s1) is str
  28. def test_eq(case_insensitive_str_class: Type[str]) -> None:
  29. s1 = "Abc"
  30. s2 = case_insensitive_str_class(s1)
  31. assert s1 == s2
  32. @pytest.fixture
  33. def create_istrs(case_insensitive_str_class: Type[str]) -> Callable[[], None]:
  34. """Make a callable populating memory with a few ``istr`` objects."""
  35. def _create_strs() -> None:
  36. case_insensitive_str_class("foobarbaz")
  37. istr2 = case_insensitive_str_class()
  38. case_insensitive_str_class(istr2)
  39. return _create_strs
  40. @pytest.mark.skipif(
  41. IMPLEMENTATION.name != "cpython",
  42. reason="PyPy has different GC implementation",
  43. )
  44. def test_leak(create_istrs: Callable[[], None]) -> None:
  45. gc.collect()
  46. cnt = len(gc.get_objects())
  47. for _ in range(10000):
  48. create_istrs()
  49. gc.collect()
  50. cnt2 = len(gc.get_objects())
  51. assert abs(cnt - cnt2) < 10 # on PyPy these numbers are not equal