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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
17
"""Tests for rio serialization
54
53
# these aren't enforced at construction time
55
54
## self.assertRaises(ValueError,
56
55
## Stanza, complex=42 + 3j)
57
## self.assertRaises(ValueError,
56
## self.assertRaises(ValueError,
58
57
## Stanza, several=range(10))
60
59
def test_empty_value(self):
123
122
def test_repeated_field(self):
124
123
"""Repeated field in rio"""
126
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
125
for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'),
127
126
('a', '1000'), ('b', '2000')]:
129
128
s2 = read_stanza(s.to_lines())
141
140
def test_blank_line(self):
142
141
s = Stanza(none='', one='\n', two='\n\n')
143
142
self.assertEqualDiff(s.to_string(), """\
154
153
def test_whitespace_value(self):
155
154
s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n')
156
155
self.assertEqualDiff(s.to_string(), """\
163
162
s2 = read_stanza(s.to_lines())
187
186
s = read_stanza([])
188
187
self.assertEqual(s, None)
189
188
self.assertTrue(s is None)
191
190
def test_read_iter(self):
192
191
"""Read several stanzas from file"""
193
192
tmpf = TemporaryFile()
204
203
reader = read_stanzas(tmpf)
205
204
read_iter = iter(reader)
206
205
stuff = list(reader)
207
self.assertEqual(stuff,
206
self.assertEqual(stuff,
208
207
[ Stanza(version_header='1'),
209
208
Stanza(name="foo", val='123'),
210
209
Stanza(name="bar", val='129319'), ])
354
353
self.assertEqual(u'foo: %s\n' % uni_data, child_text)
355
354
new_child = rio.read_stanza_unicode(child_text.splitlines(True))
356
355
self.assertEqual(uni_data, new_child.get('foo'))
358
def mail_munge(self, lines, dos_nl=True):
361
line = re.sub(' *\n', '\n', line)
363
line = re.sub('([^\r])\n', '\\1\r\n', line)
364
new_lines.append(line)
367
def test_patch_rio(self):
368
stanza = Stanza(data='#\n\r\\r ', space=' ' * 255, hash='#' * 255)
369
lines = rio.to_patch_lines(stanza)
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,
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'))
383
def test_patch_rio_linebreaks(self):
384
stanza = Stanza(breaktest='linebreak -/'*30)
385
self.assertContainsRe(rio.to_patch_lines(stanza, 71)[0],
387
stanza = Stanza(breaktest='linebreak-/'*30)
388
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],
390
stanza = Stanza(breaktest='linebreak/'*30)
391
self.assertContainsRe(rio.to_patch_lines(stanza, 70)[0],