def test_read_several(self): """Read several stanzas from file""" tmpf = TemporaryFile() tmpf.write("""\ version_header: 1 name: foo val: 123 name: quoted address: "Willowglen" \t 42 Wallaby Way \t Sydney name: bar val: 129319 """) tmpf.seek(0) s = read_stanza(tmpf) self.assertEqual(s, Stanza(version_header='1')) s = read_stanza(tmpf) self.assertEqual(s, Stanza(name="foo", val='123')) s = read_stanza(tmpf) self.assertEqualDiff(s.get('name'), 'quoted') self.assertEqualDiff(s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney') s = read_stanza(tmpf) self.assertEqual(s, Stanza(name="bar", val='129319')) s = read_stanza(tmpf) self.assertEqual(s, None) self.check_rio_file(tmpf)
def test_read_several(self): """Read several stanzas from file""" tmpf = TemporaryFile() tmpf.write("""\ version_header: 1 name: foo val: 123 name: quoted address: "Willowglen" \t 42 Wallaby Way \t Sydney name: bar val: 129319 """) tmpf.seek(0) s = read_stanza(tmpf) self.assertEquals(s, Stanza(version_header='1')) s = read_stanza(tmpf) self.assertEquals(s, Stanza(name="foo", val='123')) s = read_stanza(tmpf) self.assertEqualDiff(s.get('name'), 'quoted') self.assertEqualDiff(s.get('address'), ' "Willowglen"\n 42 Wallaby Way\n Sydney') s = read_stanza(tmpf) self.assertEquals(s, Stanza(name="bar", val='129319')) s = read_stanza(tmpf) self.assertEquals(s, None) self.check_rio_file(tmpf)
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, ['foo: ' + uni_data.encode('utf-8') + '\n']) new_s = read_stanza(raw_lines) self.assertEqual(new_s.get('foo'), uni_data)
def test_rio_unicode(self): uni_data = u'\N{KATAKANA LETTER O}' s = Stanza(foo=uni_data) self.assertEquals(s.get('foo'), uni_data) raw_lines = s.to_lines() self.assertEquals(raw_lines, ['foo: ' + uni_data.encode('utf-8') + '\n']) new_s = read_stanza(raw_lines) self.assertEquals(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'), map(str, [10, 100, 1000])) self.assertEqual(s.get_all('b'), map(str, [20, 200, 2000]))
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.assertEquals(s, s2) self.assertEquals(s.get_all('a'), map(str, [10, 100, 1000])) self.assertEquals(s.get_all('b'), map(str, [20, 200, 2000]))
def test_tricky_quoted(self): tmpf = TemporaryFile() tmpf.write('''\ s: "one" s:\x20 \t"one" \t s: " s: "" s: """ s:\x20 \t s: \\ s:\x20 \t\\ \t\\\\ \t s: word\\ s: quote" s: backslashes\\\\\\ s: both\\\" ''') tmpf.seek(0) expected_vals = [ '"one"', '\n"one"\n', '"', '""', '"""', '\n', '\\', '\n\\\n\\\\\n', 'word\\', 'quote\"', 'backslashes\\\\\\', 'both\\\"', ] for expected in expected_vals: stanza = read_stanza(tmpf) self.rio_file_stanzas([stanza]) self.assertEqual(len(stanza), 1) self.assertEqualDiff(stanza.get('s'), expected)
def test_blank_line(self): s = Stanza(none='', one='\n', two='\n\n') self.assertEqualDiff(s.to_string(), """\ none:\x20 one:\x20 \t two:\x20 \t \t """) s2 = read_stanza(s.to_lines()) self.assertEquals(s, s2)
def test_whitespace_value(self): s = Stanza(space=' ', tabs='\t\t\t', combo='\n\t\t\n') self.assertEqualDiff(s.to_string(), """\ combo:\x20 \t\t\t \t space:\x20\x20 tabs: \t\t\t """) s2 = read_stanza(s.to_lines()) self.assertEquals(s, s2) self.rio_file_stanzas([s])
def test_tricky_quoted(self): tmpf = TemporaryFile() tmpf.write('''\ s: "one" s:\x20 \t"one" \t s: " s: "" s: """ s:\x20 \t s: \\ s:\x20 \t\\ \t\\\\ \t s: word\\ s: quote" s: backslashes\\\\\\ s: both\\\" ''') tmpf.seek(0) expected_vals = ['"one"', '\n"one"\n', '"', '""', '"""', '\n', '\\', '\n\\\n\\\\\n', 'word\\', 'quote\"', 'backslashes\\\\\\', 'both\\\"', ] for expected in expected_vals: stanza = read_stanza(tmpf) self.rio_file_stanzas([stanza]) self.assertEquals(len(stanza), 1) self.assertEqualDiff(stanza.get('s'), expected)
def test_blank_line(self): s = Stanza(none='', one='\n', two='\n\n') self.assertEqualDiff(s.to_string(), """\ 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.assertEqualDiff(s.to_string(), """\ combo: \t\t\t \t space: tabs: \t\t\t """) s2 = read_stanza(s.to_lines()) self.assertEquals(s, s2) self.rio_file_stanzas([s])
def test_multiline_string(self): tmpf = TemporaryFile() s = Stanza(motto="war is peace\nfreedom is slavery\nignorance is strength") s.write(tmpf) tmpf.seek(0) self.assertEqualDiff(tmpf.read(), '''\ motto: war is peace \tfreedom is slavery \tignorance is strength ''') tmpf.seek(0) s2 = read_stanza(tmpf) self.assertEquals(s, s2)
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(['child: foo: ' + uni_data.encode('utf-8') + '\n', '\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_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([ 'child: foo: ' + uni_data.encode('utf-8') + '\n', '\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_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.assertEquals(s, s2)
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_multiline_string(self): tmpf = TemporaryFile() s = Stanza( motto="war is peace\nfreedom is slavery\nignorance is strength") s.write(tmpf) tmpf.seek(0) self.assertEqualDiff( tmpf.read(), '''\ motto: war is peace \tfreedom is slavery \tignorance is strength ''') tmpf.seek(0) s2 = read_stanza(tmpf) self.assertEqual(s, s2)
def test_read_stanza(self): """Load stanza from string""" lines = """\ revision: [email protected] timestamp: 1130653962 timezone: 36000 committer: Martin Pool <*****@*****.**> """.splitlines(True) s = read_stanza(lines) self.assertTrue('revision' in s) self.assertEqualDiff(s.get('revision'), '[email protected]') self.assertEquals(list(s.iter_pairs()), [('revision', '[email protected]'), ('timestamp', '1130653962'), ('timezone', '36000'), ('committer', "Martin Pool <*****@*****.**>")]) self.assertEquals(len(s), 4)
def test_read_stanza(self): """Load stanza from string""" lines = """\ revision: [email protected] timestamp: 1130653962 timezone: 36000 committer: Martin Pool <*****@*****.**> """.splitlines(True) s = read_stanza(lines) self.assertTrue('revision' in s) self.assertEqualDiff(s.get('revision'), '[email protected]') self.assertEqual( list(s.iter_pairs()), [('revision', '[email protected]'), ('timestamp', '1130653962'), ('timezone', '36000'), ('committer', "Martin Pool <*****@*****.**>")]) self.assertEqual(len(s), 4)
def _parse_info(self, info_file): return read_stanza(info_file.readlines()).as_dict()
def test_backslash(self): s = Stanza(q='\\') t = s.to_string() self.assertEqualDiff(t, 'q: \\\n') s2 = read_stanza(s.to_lines()) self.assertEqual(s, s2)
def test_read_empty(self): """Detect end of rio file""" s = read_stanza([]) self.assertEqual(s, None) self.assertTrue(s is None)
def test_backslash(self): s = Stanza(q='\\') t = s.to_string() self.assertEqualDiff(t, 'q: \\\n') s2 = read_stanza(s.to_lines()) self.assertEquals(s, s2)