66
77from docx .oxml .text import CT_P , CT_PPr , CT_R , CT_Text
88
9- from .unitdata .text import a_p , a_pPr , a_t , an_r
9+ from .unitdata .text import a_p , a_pPr , a_pStyle , a_t , an_r
1010
1111
1212class DescribeCT_P (object ):
1313
1414 def it_can_construct_a_new_p_element (self ):
1515 p = CT_P .new ()
16- expected_xml = a_p ().with_nsdecls ().xml
16+ expected_xml = a_p ().with_nsdecls ().xml ()
1717 assert p .xml == expected_xml
1818
1919 def it_has_a_sequence_of_the_runs_it_contains (self ):
20- p = a_p ().with_nsdecls ().with_r ( 3 ).element
21- assert len (p .r_lst ) == 3
20+ p = a_p ().with_nsdecls ().with_child ( an_r ()). with_child ( an_r () ).element
21+ assert len (p .r_lst ) == 2
2222 for r in p .r_lst :
2323 assert isinstance (r , CT_R )
2424
@@ -27,37 +27,35 @@ def it_can_add_an_r_to_itself(self):
2727 # exercise -----------------
2828 r = p .add_r ()
2929 # verify -------------------
30- assert p .xml == a_p ().with_nsdecls ().with_r () .xml
30+ assert p .xml == a_p ().with_nsdecls ().with_child ( an_r ()) .xml ()
3131 assert isinstance (r , CT_R )
3232
3333 def it_knows_its_paragraph_style (self ):
34- # effectively an integration test with CT_PPr
35- pPr = a_pPr ().with_style ('foobar' )
34+ pPr_bldr = a_pPr ().with_child (a_pStyle ().with_val ('foobar' ))
3635 cases = (
3736 (a_p (), None ),
38- (a_p ().with_pPr ( pPr ), 'foobar' ),
37+ (a_p ().with_child ( pPr_bldr ), 'foobar' ),
3938 )
4039 for builder , expected_value in cases :
41- print builder .with_nsdecls ().xml
40+ print builder .with_nsdecls ().xml ()
4241 p = builder .with_nsdecls ().element
4342 assert p .style == expected_value
4443
4544 def it_can_set_its_paragraph_style (self ):
46- # effectively an integration test with CT_PPr
47- pPr = a_pPr ().with_style ('foobar' )
48- pPr2 = a_pPr ().with_style ('barfoo' )
45+ pPr = a_pPr ().with_child (a_pStyle ().with_val ('foobar' ))
46+ pPr2 = a_pPr ().with_child (a_pStyle ().with_val ('barfoo' ))
4947 cases = (
50- (1 , a_p (), None , a_p ().with_pPr (a_pPr ())),
51- (2 , a_p (), 'foobar' , a_p ().with_pPr (pPr )),
52- (3 , a_p ().with_pPr (pPr ), None , a_p ().with_pPr (a_pPr ())),
53- (4 , a_p ().with_pPr (pPr ), 'barfoo' , a_p ().with_pPr (pPr2 )),
48+ (1 , a_p (), None , a_p ().with_child (a_pPr ())),
49+ (2 , a_p (), 'foobar' , a_p ().with_child (pPr )),
50+ (3 , a_p ().with_child (pPr ), None , a_p ().with_child (a_pPr ())),
51+ (4 , a_p ().with_child (pPr ), 'barfoo' , a_p ().with_child (pPr2 )),
5452 )
5553 for case_nmbr , before_bldr , new_style , after_bldr in cases :
56- print before_bldr .with_nsdecls ().xml
54+ print before_bldr .with_nsdecls ().xml ()
5755 print 'case_nmbr, new_style => %d, %s' % (case_nmbr , new_style )
5856 p = before_bldr .with_nsdecls ().element
5957 p .style = new_style
60- expected_xml = after_bldr .with_nsdecls ().xml
58+ expected_xml = after_bldr .with_nsdecls ().xml ()
6159 print expected_xml
6260 print p .xml
6361 assert p .xml == expected_xml
@@ -67,13 +65,13 @@ class DescribeCT_PPr(object):
6765
6866 def it_can_construct_a_new_pPr_element (self ):
6967 pPr = CT_PPr .new ()
70- expected_xml = a_pPr ().with_nsdecls ().xml
68+ expected_xml = a_pPr ().with_nsdecls ().xml ()
7169 assert pPr .xml == expected_xml
7270
7371 def it_knows_the_paragraph_style (self ):
7472 cases = (
7573 (a_pPr (), None ),
76- (a_pPr ().with_style ( 'foobar' ), 'foobar' ),
74+ (a_pPr ().with_child ( a_pStyle (). with_val ( 'foobar' ) ), 'foobar' ),
7775 )
7876 for builder , expected_value in cases :
7977 print builder .with_nsdecls ().xml
@@ -83,17 +81,19 @@ def it_knows_the_paragraph_style(self):
8381 def it_can_set_the_paragraph_style (self ):
8482 cases = (
8583 (1 , a_pPr (), None , a_pPr ()),
86- (2 , a_pPr (), 'foobar' , a_pPr ().with_style ('foobar' )),
87- (3 , a_pPr ().with_style ('foobar' ), None , a_pPr ()),
88- (4 , a_pPr ().with_style ('foobar' ), 'barfoo' ,
89- a_pPr ().with_style ('barfoo' )),
84+ (2 , a_pPr (), 'foobar' ,
85+ a_pPr ().with_child (a_pStyle ().with_val ('foobar' ))),
86+ (3 , a_pPr ().with_child (a_pStyle ().with_val ('foobar' )), None ,
87+ a_pPr ()),
88+ (4 , a_pPr ().with_child (a_pStyle ().with_val ('foobar' )), 'barfoo' ,
89+ a_pPr ().with_child (a_pStyle ().with_val ('barfoo' ))),
9090 )
9191 for case_nmbr , before_bldr , new_style , after_bldr in cases :
9292 print '====\n case %d: new_style => %s' % (case_nmbr , new_style )
93- print 'before:\n %s' % before_bldr .with_nsdecls ().xml
93+ print 'before:\n %s' % before_bldr .with_nsdecls ().xml ()
9494 pPr = before_bldr .with_nsdecls ().element
9595 pPr .style = new_style
96- expected_xml = after_bldr .with_nsdecls ().xml
96+ expected_xml = after_bldr .with_nsdecls ().xml ()
9797 print 'expected:\n %s' % expected_xml
9898 print 'actual:\n %s' % pPr .xml
9999 assert pPr .xml == expected_xml
@@ -103,22 +103,28 @@ class DescribeCT_R(object):
103103
104104 def it_can_construct_a_new_r_element (self ):
105105 r = CT_R .new ()
106- assert r .xml == an_r ().with_nsdecls ().xml
106+ assert r .xml == an_r ().with_nsdecls ().xml ()
107107
108108 def it_can_add_a_t_to_itself (self ):
109109 text = 'foobar'
110110 r = an_r ().with_nsdecls ().element
111111 # exercise -----------------
112112 t = r .add_t (text )
113113 # verify -------------------
114- assert r .xml == an_r ().with_nsdecls ().with_t (text ).xml
114+ assert (
115+ r .xml ==
116+ an_r ().with_nsdecls ().with_child (a_t ().with_text (text )).xml ()
117+ )
115118 assert isinstance (t , CT_Text )
116119
117120 def it_has_a_sequence_of_the_t_elms_it_contains (self ):
118121 cases = (
119122 (an_r ().with_nsdecls (), 0 ),
120- (an_r ().with_nsdecls ().with_t ('foo' ), 1 ),
121- (an_r ().with_nsdecls ().with_t ('foo' ).with_t ('bar' ), 2 ),
123+ (an_r ().with_nsdecls ().with_child (
124+ a_t ().with_text ('foo' )), 1 ),
125+ (an_r ().with_nsdecls ().with_child (
126+ a_t ().with_text ('foo' )).with_child (
127+ a_t ().with_text ('bar' )), 2 ),
122128 )
123129 for r_bldr , expected_len in cases :
124130 r = r_bldr .element
@@ -132,4 +138,4 @@ class DescribeCT_Text(object):
132138 def it_can_construct_a_new_t_element (self ):
133139 text = 'foobar'
134140 t = CT_Text .new (text )
135- assert t .xml == a_t (text ).with_nsdecls ().xml
141+ assert t .xml == a_t ().with_nsdecls ().with_text ( text ). xml ()
0 commit comments