|
4 | 4 | Test suite for the docx.oxml.text module. |
5 | 5 | """ |
6 | 6 |
|
7 | | -from docx.oxml.text import CT_R, CT_Text |
| 7 | +from __future__ import absolute_import, print_function, unicode_literals |
8 | 8 |
|
9 | | -from .unitdata.text import a_p, a_pPr, a_pStyle, a_t, an_r |
| 9 | +import pytest |
10 | 10 |
|
11 | | - |
12 | | -class DescribeCT_P(object): |
13 | | - |
14 | | - def it_has_a_sequence_of_the_runs_it_contains(self): |
15 | | - p = a_p().with_nsdecls().with_child(an_r()).with_child(an_r()).element |
16 | | - assert len(p.r_lst) == 2 |
17 | | - for r in p.r_lst: |
18 | | - assert isinstance(r, CT_R) |
19 | | - |
20 | | - def it_can_add_an_r_to_itself(self): |
21 | | - p = a_p().with_nsdecls().element |
22 | | - # exercise ----------------- |
23 | | - r = p.add_r() |
24 | | - # verify ------------------- |
25 | | - assert p.xml == a_p().with_nsdecls().with_child(an_r()).xml() |
26 | | - assert isinstance(r, CT_R) |
27 | | - |
28 | | - def it_knows_its_paragraph_style(self): |
29 | | - pPr_bldr = a_pPr().with_child(a_pStyle().with_val('foobar')) |
30 | | - cases = ( |
31 | | - (a_p(), None), |
32 | | - (a_p().with_child(pPr_bldr), 'foobar'), |
33 | | - ) |
34 | | - for builder, expected_value in cases: |
35 | | - p = builder.with_nsdecls().element |
36 | | - assert p.style == expected_value |
37 | | - |
38 | | - def it_can_set_its_paragraph_style(self): |
39 | | - pPr = a_pPr().with_child(a_pStyle().with_val('foobar')) |
40 | | - pPr2 = a_pPr().with_child(a_pStyle().with_val('barfoo')) |
41 | | - cases = ( |
42 | | - (1, a_p(), None, a_p().with_child(a_pPr())), |
43 | | - (2, a_p(), 'foobar', a_p().with_child(pPr)), |
44 | | - (3, a_p().with_child(pPr), None, a_p().with_child(a_pPr())), |
45 | | - (4, a_p().with_child(pPr), 'barfoo', a_p().with_child(pPr2)), |
46 | | - ) |
47 | | - for case_nmbr, before_bldr, new_style, after_bldr in cases: |
48 | | - p = before_bldr.with_nsdecls().element |
49 | | - p.style = new_style |
50 | | - expected_xml = after_bldr.with_nsdecls().xml() |
51 | | - assert p.xml == expected_xml |
52 | | - |
53 | | - |
54 | | -class DescribeCT_PPr(object): |
55 | | - |
56 | | - def it_knows_the_paragraph_style(self): |
57 | | - cases = ( |
58 | | - (a_pPr(), None), |
59 | | - (a_pPr().with_child(a_pStyle().with_val('foobar')), 'foobar'), |
60 | | - ) |
61 | | - for builder, expected_value in cases: |
62 | | - pPr = builder.with_nsdecls().element |
63 | | - assert pPr.style == expected_value |
64 | | - |
65 | | - def it_can_set_the_paragraph_style(self): |
66 | | - cases = ( |
67 | | - (1, a_pPr(), None, a_pPr()), |
68 | | - (2, a_pPr(), 'foobar', |
69 | | - a_pPr().with_child(a_pStyle().with_val('foobar'))), |
70 | | - (3, a_pPr().with_child(a_pStyle().with_val('foobar')), None, |
71 | | - a_pPr()), |
72 | | - (4, a_pPr().with_child(a_pStyle().with_val('foobar')), 'barfoo', |
73 | | - a_pPr().with_child(a_pStyle().with_val('barfoo'))), |
74 | | - ) |
75 | | - for case_nmbr, before_bldr, new_style, after_bldr in cases: |
76 | | - pPr = before_bldr.with_nsdecls().element |
77 | | - pPr.style = new_style |
78 | | - expected_xml = after_bldr.with_nsdecls().xml() |
79 | | - assert pPr.xml == expected_xml |
| 11 | +from ..unitutil.cxml import element, xml |
80 | 12 |
|
81 | 13 |
|
82 | 14 | class DescribeCT_R(object): |
83 | 15 |
|
84 | | - def it_can_add_a_t_to_itself(self): |
85 | | - text = 'foobar' |
86 | | - r = an_r().with_nsdecls().element |
87 | | - # exercise ----------------- |
88 | | - t = r.add_t(text) |
89 | | - # verify ------------------- |
90 | | - assert ( |
91 | | - r.xml == |
92 | | - an_r().with_nsdecls().with_child(a_t().with_text(text)).xml() |
93 | | - ) |
94 | | - assert isinstance(t, CT_Text) |
95 | | - |
96 | | - def it_has_a_sequence_of_the_t_elms_it_contains(self): |
97 | | - cases = ( |
98 | | - (an_r().with_nsdecls(), 0), |
99 | | - (an_r().with_nsdecls().with_child( |
100 | | - a_t().with_text('foo')), 1), |
101 | | - (an_r().with_nsdecls().with_child( |
102 | | - a_t().with_text('foo')).with_child( |
103 | | - a_t().with_text('bar')), 2), |
104 | | - ) |
105 | | - for r_bldr, expected_len in cases: |
106 | | - r = r_bldr.element |
107 | | - assert len(r.t_lst) == expected_len |
108 | | - for t in r.t_lst: |
109 | | - assert isinstance(t, CT_Text) |
| 16 | + def it_can_add_a_t_preserving_edge_whitespace(self, add_t_fixture): |
| 17 | + r, text, expected_xml = add_t_fixture |
| 18 | + r.add_t(text) |
| 19 | + assert r.xml == expected_xml |
| 20 | + |
| 21 | + # fixtures ------------------------------------------------------- |
| 22 | + |
| 23 | + @pytest.fixture(params=[ |
| 24 | + ('w:r', 'foobar', 'w:r/w:t"foobar"'), |
| 25 | + ('w:r', 'foobar ', 'w:r/w:t{xml:space=preserve}"foobar "'), |
| 26 | + ('w:r/(w:rPr/w:rStyle{w:val=emphasis}, w:cr)', 'foobar', |
| 27 | + 'w:r/(w:rPr/w:rStyle{w:val=emphasis}, w:cr, w:t"foobar")'), |
| 28 | + ]) |
| 29 | + def add_t_fixture(self, request): |
| 30 | + initial_cxml, text, expected_cxml = request.param |
| 31 | + r = element(initial_cxml) |
| 32 | + expected_xml = xml(expected_cxml) |
| 33 | + return r, text, expected_xml |
0 commit comments