Coverage for tests/test_main.py: 100%

33 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-12-05 11:36 +0000

1import pickle 1abcdefghijkl

2from typing import Annotated, get_type_hints 1abcdefghijkl

3 

4from annotated_doc import Doc 1abcdefghijkl

5 

6 

7def test_doc_basic() -> None: 1abcdefghijkl

8 doc = Doc("This is a test documentation.") 1mnopqrstuvwx

9 assert doc.documentation == "This is a test documentation." 1mnopqrstuvwx

10 assert repr(doc) == "Doc('This is a test documentation.')" 1mnopqrstuvwx

11 assert hash(doc) == hash("This is a test documentation.") 1mnopqrstuvwx

12 assert doc == Doc("This is a test documentation.") 1mnopqrstuvwx

13 assert doc != Doc("Different documentation.") 1mnopqrstuvwx

14 assert doc != "Not a Doc instance" 1mnopqrstuvwx

15 

16 

17def test_annotation(): 1abcdefghijkl

18 def hi(name: Annotated[str, Doc("Who to say hi to")]) -> None: # pragma: no cover 1yzABCDEFGHIJ

19 pass 

20 

21 hints = get_type_hints(hi, include_extras=True) 1yzABCDEFGHIJ

22 doc_info: Doc = hints["name"].__metadata__[0] 1yzABCDEFGHIJ

23 assert doc_info.documentation == "Who to say hi to" 1yzABCDEFGHIJ

24 assert isinstance(doc_info, Doc) 1yzABCDEFGHIJ

25 

26 

27def test_repr(): 1abcdefghijkl

28 doc_info = Doc("Who to say hi to") 1-./:;=?@[]^_

29 assert repr(doc_info) == "Doc('Who to say hi to')" 1-./:;=?@[]^_

30 

31 

32def test_hashability(): 1abcdefghijkl

33 doc_info = Doc("Who to say hi to") 189!#$%'()*+,

34 assert isinstance(hash(doc_info), int) 189!#$%'()*+,

35 assert hash(doc_info) != hash(Doc("Who not to say hi to")) 189!#$%'()*+,

36 

37 

38def test_equality(): 1abcdefghijkl

39 doc_info = Doc("Who to say hi to") 1KLMNOPQRSTUV

40 # Equal to itself 

41 assert doc_info == doc_info 1KLMNOPQRSTUV

42 # Equal to another instance with the same string 

43 assert doc_info == Doc("Who to say hi to") 1KLMNOPQRSTUV

44 # Not equal to another instance with a different string 

45 assert doc_info != Doc("Who not to say hi to") 1KLMNOPQRSTUV

46 

47 

48def test_pickle(): 1abcdefghijkl

49 doc_info = Doc("Who to say hi to") 1WXYZ01234567

50 for proto in range(pickle.HIGHEST_PROTOCOL + 1): 1WXYZ01234567

51 pickled = pickle.dumps(doc_info, protocol=proto) 1WXYZ01234567

52 assert doc_info == pickle.loads(pickled) 1WXYZ01234567