66
77import pytest
88
9- from docx .bookmark import Bookmarks , _DocumentBookmarkFinder , _PartBookmarkFinder
9+ from docx .bookmark import (
10+ _Bookmark ,
11+ Bookmarks ,
12+ _DocumentBookmarkFinder ,
13+ _PartBookmarkFinder ,
14+ )
1015from docx .opc .part import Part , XmlPart
1116from docx .parts .document import DocumentPart
1217
2429
2530class DescribeBookmarks (object ):
2631
27- def it_knows_how_many_bookmarks_the_document_contains (
28- self , _finder_prop_ , finder_
32+ def it_provides_access_to_bookmarks_by_index (
33+ self , _finder_prop_ , finder_ , _Bookmark_ , bookmark_
34+ ):
35+ bookmarkStarts = tuple (element ("w:bookmarkStart" ) for _ in range (3 ))
36+ bookmarkEnds = tuple (element ("w:bookmarkEnd" ) for _ in range (3 ))
37+ _finder_prop_ .return_value = finder_
38+ finder_ .bookmark_pairs = zip (bookmarkStarts , bookmarkEnds )
39+ _Bookmark_ .return_value = bookmark_
40+ bookmarks = Bookmarks (None )
41+
42+ bookmark = bookmarks [1 ]
43+
44+ _Bookmark_ .assert_called_once_with ((bookmarkStarts [1 ], bookmarkEnds [1 ]))
45+ assert bookmark == bookmark_
46+
47+ def it_provides_access_to_bookmarks_by_slice (
48+ self , _finder_prop_ , finder_ , _Bookmark_ , bookmark_
49+ ):
50+ bookmarkStarts = tuple (element ("w:bookmarkStart" ) for _ in range (4 ))
51+ bookmarkEnds = tuple (element ("w:bookmarkEnd" ) for _ in range (4 ))
52+ _finder_prop_ .return_value = finder_
53+ finder_ .bookmark_pairs = zip (bookmarkStarts , bookmarkEnds )
54+ _Bookmark_ .return_value = bookmark_
55+ bookmarks = Bookmarks (None )
56+
57+ bookmarks_slice = bookmarks [1 :3 ]
58+
59+ assert _Bookmark_ .call_args_list == [
60+ call ((bookmarkStarts [1 ], bookmarkEnds [1 ])),
61+ call ((bookmarkStarts [2 ], bookmarkEnds [2 ])),
62+ ]
63+ assert bookmarks_slice == [bookmark_ , bookmark_ ]
64+
65+ def it_can_iterate_its_bookmarks (
66+ self , _finder_prop_ , finder_ , _Bookmark_ , bookmark_
2967 ):
68+ bookmarkStarts = tuple (element ("w:bookmarkStart" ) for _ in range (3 ))
69+ bookmarkEnds = tuple (element ("w:bookmarkEnd" ) for _ in range (3 ))
70+ _finder_prop_ .return_value = finder_
71+ finder_ .bookmark_pairs = zip (bookmarkStarts , bookmarkEnds )
72+ _Bookmark_ .return_value = bookmark_
73+ bookmarks = Bookmarks (None )
74+
75+ _bookmarks = list (b for b in bookmarks )
76+
77+ assert _Bookmark_ .call_args_list == [
78+ call ((bookmarkStarts [0 ], bookmarkEnds [0 ])),
79+ call ((bookmarkStarts [1 ], bookmarkEnds [1 ])),
80+ call ((bookmarkStarts [2 ], bookmarkEnds [2 ])),
81+ ]
82+ assert _bookmarks == [bookmark_ , bookmark_ , bookmark_ ]
83+
84+ def it_knows_how_many_bookmarks_the_document_contains (self , _finder_prop_ , finder_ ):
3085 _finder_prop_ .return_value = finder_
3186 finder_ .bookmark_pairs = tuple ((1 , 2 ) for _ in range (42 ))
3287 bookmarks = Bookmarks (None )
@@ -48,6 +103,14 @@ def it_provides_access_to_its_bookmark_finder_to_help(
48103
49104 # fixture components ---------------------------------------------
50105
106+ @pytest .fixture
107+ def _Bookmark_ (self , request ):
108+ return class_mock (request , 'docx.bookmark._Bookmark' )
109+
110+ @pytest .fixture
111+ def bookmark_ (self , request ):
112+ return instance_mock (request , _Bookmark )
113+
51114 @pytest .fixture
52115 def _DocumentBookmarkFinder_ (self , request ):
53116 return class_mock (request , 'docx.bookmark._DocumentBookmarkFinder' )
0 commit comments