~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_rio.py

  • Committer: Marius Kruger
  • Date: 2010-07-10 21:28:56 UTC
  • mto: (5384.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 5385.
  • Revision ID: marius.kruger@enerweb.co.za-20100710212856-uq4ji3go0u5se7hx
* Update documentation
* add NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for rio serialization
18
18
 
24
24
 
25
25
import cStringIO
26
26
import os
 
27
import re
27
28
import sys
28
29
from tempfile import TemporaryFile
29
30
 
53
54
        # these aren't enforced at construction time
54
55
        ## self.assertRaises(ValueError,
55
56
        ##        Stanza, complex=42 + 3j)
56
 
        ## self.assertRaises(ValueError, 
 
57
        ## self.assertRaises(ValueError,
57
58
        ##        Stanza, several=range(10))
58
59
 
59
60
    def test_empty_value(self):
122
123
    def test_repeated_field(self):
123
124
        """Repeated field in rio"""
124
125
        s = Stanza()
125
 
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), 
 
126
        for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
126
127
                     ('a', '1000'), ('b', '2000')]:
127
128
            s.add(k, v)
128
129
        s2 = read_stanza(s.to_lines())
140
141
    def test_blank_line(self):
141
142
        s = Stanza(none='', one='\n', two='\n\n')
142
143
        self.assertEqualDiff(s.to_string(), """\
143
 
none: 
144
 
one: 
 
144
none:\x20
 
145
one:\x20
145
146
\t
146
 
two: 
 
147
two:\x20
147
148
\t
148
149
\t
149
150
""")
153
154
    def test_whitespace_value(self):
154
155
        s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
155
156
        self.assertEqualDiff(s.to_string(), """\
156
 
combo: 
 
157
combo:\x20
157
158
\t\t\t
158
159
\t
159
 
space:  
 
160
space:\x20\x20
160
161
tabs: \t\t\t
161
162
""")
162
163
        s2 = read_stanza(s.to_lines())
165
166
 
166
167
    def test_quoted(self):
167
168
        """rio quoted string cases"""
168
 
        s = Stanza(q1='"hello"', 
169
 
                   q2=' "for', 
 
169
        s = Stanza(q1='"hello"',
 
170
                   q2=' "for',
170
171
                   q3='\n\n"for"\n',
171
172
                   q4='for\n"\nfor',
172
173
                   q5='\n',
173
 
                   q6='"', 
 
174
                   q6='"',
174
175
                   q7='""',
175
176
                   q8='\\',
176
177
                   q9='\\"\\"',
186
187
        s = read_stanza([])
187
188
        self.assertEqual(s, None)
188
189
        self.assertTrue(s is None)
189
 
        
 
190
 
190
191
    def test_read_iter(self):
191
192
        """Read several stanzas from file"""
192
193
        tmpf = TemporaryFile()
203
204
        reader = read_stanzas(tmpf)
204
205
        read_iter = iter(reader)
205
206
        stuff = list(reader)
206
 
        self.assertEqual(stuff, 
 
207
        self.assertEqual(stuff,
207
208
                [ Stanza(version_header='1'),
208
209
                  Stanza(name="foo", val='123'),
209
210
                  Stanza(name="bar", val='129319'), ])
258
259
        tmpf.write('''\
259
260
s: "one"
260
261
 
261
 
s: 
 
262
s:\x20
262
263
\t"one"
263
264
\t
264
265
 
268
269
 
269
270
s: """
270
271
 
271
 
s: 
 
272
s:\x20
272
273
\t
273
274
 
274
275
s: \\
275
276
 
276
 
s: 
 
277
s:\x20
277
278
\t\\
278
279
\t\\\\
279
280
\t
353
354
        self.assertEqual(u'foo: %s\n' % uni_data, child_text)
354
355
        new_child = rio.read_stanza_unicode(child_text.splitlines(True))
355
356
        self.assertEqual(uni_data, new_child.get('foo'))
 
357
 
 
358
    def mail_munge(self, lines, dos_nl=True):
 
359
        new_lines = []
 
360
        for line in lines:
 
361
            line = re.sub(' *\n', '\n', line)
 
362
            if dos_nl:
 
363
                line = re.sub('([^\r])\n', '\\1\r\n', line)
 
364
            new_lines.append(line)
 
365
        return new_lines
 
366
 
 
367
    def test_patch_rio(self):
 
368
        stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
 
369
        lines = rio.to_patch_lines(stanza)
 
370
        for line in lines:
 
371
            self.assertContainsRe(line, '^# ')
 
372
            self.assertTrue(72 >= len(line))
 
373
        for line in rio.to_patch_lines(stanza, max_width=12):
 
374
            self.assertTrue(12 >= len(line))
 
375
        new_stanza = rio.read_patch_stanza(self.mail_munge(lines,
 
376
                                                           dos_nl=False))
 
377
        lines = self.mail_munge(lines)
 
378
        new_stanza = rio.read_patch_stanza(lines)
 
379
        self.assertEqual('#\n\r\\r ', new_stanza.get('data'))
 
380
        self.assertEqual(' '* 255, new_stanza.get('space'))
 
381
        self.assertEqual('#'* 255, new_stanza.get('hash'))
 
382
 
 
383
    def test_patch_rio_linebreaks(self):
 
384
        stanza = Stanza(breaktest='linebreak -/'*30)
 
385
        self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
 
386
                              'linebreak\\\\\n')
 
387
        stanza = Stanza(breaktest='linebreak-/'*30)
 
388
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
389
                              'linebreak-\\\\\n')
 
390
        stanza = Stanza(breaktest='linebreak/'*30)
 
391
        self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
 
392
                              'linebreak\\\\\n')