Coverage for tests/unit/test_kappa.py: 100%

39 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2025-04-04 03:33 -0600

1from typing import Mapping 

2 

3import pytest 

4 

5from muutils.kappa import _BASE_DOC, Kappa 

6 

7 

8def test_Kappa_returns_Kappa_instance(): 

9 func = lambda x: x**2 # noqa: E731 

10 result = Kappa(func) 

11 assert isinstance(result, Mapping), "Kappa did not return a Mapping instance" 

12 

13 

14def test_Kappa_getitem_calls_func(): 

15 func = lambda x: x**2 # noqa: E731 

16 result = Kappa(func) 

17 assert result[2] == 4, "__getitem__ did not correctly call the input function" 

18 

19 

20def test_Kappa_doc_is_correctly_formatted(): 

21 func = lambda x: x**2 # noqa: E731 

22 result = Kappa(func) 

23 expected_doc = _BASE_DOC + "None" 

24 assert result.doc == expected_doc, "doc was not correctly formatted" 

25 

26 

27def test_Kappa_getitem_works_with_different_functions(): 

28 func = lambda x: x + 1 # noqa: E731 

29 result = Kappa(func) 

30 assert result[2] == 3, "__getitem__ did not correctly call the input function" 

31 

32 func = lambda x: str(x) # noqa: E731 

33 result = Kappa(func) 

34 assert result[2] == "2", "__getitem__ did not correctly call the input function" 

35 

36 

37def test_Kappa_iter_raises_NotImplementedError(): 

38 func = lambda x: x**2 # noqa: E731 

39 result = Kappa(func) 

40 with pytest.raises(NotImplementedError): 

41 iter(result) 

42 

43 

44def test_Kappa_len_raises_NotImplementedError(): 

45 func = lambda x: x**2 # noqa: E731 

46 result = Kappa(func) 

47 with pytest.raises(NotImplementedError): 

48 len(result) 

49 

50 

51def test_Kappa_doc_works_with_function_with_docstring(): 

52 func = lambda x: x**2 # noqa: E731 

53 func.__doc__ = "This is a test function" 

54 result = Kappa(func) 

55 expected_doc = _BASE_DOC + "This is a test function" 

56 assert ( 

57 result.doc == expected_doc 

58 ), "doc was not correctly formatted with function with docstring"