def test_rio_unicode(self): uni_data = u'\N{KATAKANA LETTER O}' s = Stanza(foo=uni_data) self.assertEqual(s.get('foo'), uni_data) raw_lines = s.to_lines() self.assertEqual(raw_lines, [b'foo: ' + uni_data.encode('utf-8') + b'\n']) new_s = read_stanza(raw_lines) self.assertEqual(new_s.get('foo'), uni_data)
def test_repeated_field(self): """Repeated field in rio""" s = Stanza() for k, v in [('a', '10'), ('b', '20'), ('a', '100'), ('b', '200'), ('a', '1000'), ('b', '2000')]: s.add(k, v) s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2) self.assertEqual(s.get_all('a'), ['10', '100', '1000']) self.assertEqual(s.get_all('b'), ['20', '200', '2000'])
def test_blank_line(self): s = Stanza(none='', one='\n', two='\n\n') self.assertEqual(s.to_string(), b"""\ none:\x20 one:\x20 \t two:\x20 \t \t """) s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2)
def test_whitespace_value(self): s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n') self.assertEqual(s.to_string(), b"""\ combo:\x20 \t\t\t \t space:\x20\x20 tabs: \t\t\t """) s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2) self.rio_file_stanzas([s])
def test_nested_rio_unicode(self): uni_data = u'\N{KATAKANA LETTER O}' s = Stanza(foo=uni_data) parent_stanza = Stanza(child=s.to_unicode()) raw_lines = parent_stanza.to_lines() self.assertEqual([ b'child: foo: ' + uni_data.encode('utf-8') + b'\n', b'\t\n', ], raw_lines) new_parent = read_stanza(raw_lines) child_text = new_parent.get('child') self.assertEqual(u'foo: %s\n' % uni_data, child_text) new_child = rio.read_stanza_unicode(child_text.splitlines(True)) self.assertEqual(uni_data, new_child.get('foo'))
def test_rio_surrogateescape(self): raw_bytes = b'\xcb' self.assertRaises(UnicodeDecodeError, raw_bytes.decode, 'utf-8') try: uni_data = raw_bytes.decode('utf-8', 'surrogateescape') except LookupError: self.skipTest('surrogateescape is not available on Python < 3') s = Stanza(foo=uni_data) self.assertEqual(s.get('foo'), uni_data) raw_lines = s.to_lines() self.assertEqual(raw_lines, [b'foo: ' + uni_data.encode('utf-8', 'surrogateescape') + b'\n']) new_s = read_stanza(raw_lines) self.assertEqual(new_s.get('foo'), uni_data)
def test_quoted(self): """rio quoted string cases""" s = Stanza(q1='"hello"', q2=' "for', q3='\n\n"for"\n', q4='for\n"\nfor', q5='\n', q6='"', q7='""', q8='\\', q9='\\"\\"', ) s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2)
def test_to_lines(self): """Write simple rio stanza to string""" s = Stanza(number='42', name='fred') self.assertEqual(list(s.to_lines()), [b'name: fred\n', b'number: 42\n'])
def test_backslash(self): s = Stanza(q='\\') t = s.to_string() self.assertEqual(t, b'q: \\\n') s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2)