Issues (70)

tests/parsing/LineParserTest.py (1 issue)

Labels
Severity
1
import unittest
2
3
from coalib.parsing.LineParser import LineParser
4
5
6
class LineParserTest(unittest.TestCase):
7
8
    def setUp(self):
9
        self.uut = LineParser(comment_separators=('#', ';'))
10
11
    def test_empty_line(self):
12
        self.check_data_set("")
13
        self.check_data_set("\n \n \n")
14
15
    def test_comment_parsing(self):
16
        self.check_data_set("# comment only$§\n",
17
                            output_comment="# comment only$§")
18
        self.check_data_set("   ; comment only  \n",
19
                            output_comment="; comment only")
20
        self.check_data_set("   ; \\comment only  \n",
21
                            output_comment="; comment only")
22
        self.check_data_set("#", output_comment="#")
23
24
    def test_section_override(self):
25
        self.check_data_set(r"a.b, \a\.\b\ c=",
26
                            output_keys=[("a", "b"), ("", r"\a.\b c")])
27
28
    def test_escaping(self):
29
        self.check_data_set("hello = world\ # yes here's a space",
0 ignored issues
show
A suspicious escape sequence \ was found. Did you maybe forget to add an r prefix?

Escape sequences in Python are generally interpreted according to rules similar to standard C. Only if strings are prefixed with r or R are they interpreted as regular expressions.

The escape sequence that was used indicates that you might have intended to write a regular expression.

Learn more about the available escape sequences. in the Python documentation.

Loading history...
30
                            output_keys=[('', 'hello')],
31
                            output_value='world\\ ',
32
                            output_comment="# yes here's a space")
33
34
    def test_multi_value_parsing(self):
35
        self.check_data_set(
36
            "a, b\\ \\=, section.c= = :()&/ \\\\#heres a comment \n",
37
            output_section='',
38
            output_keys=[("", 'a'), ("", 'b ='), ("section", 'c')],
39
            output_value='= :()&/ \\\\',
40
            output_comment='#heres a comment')
41
42
    def test_multi_line_parsing(self):
43
        self.check_data_set(" a,b,d another value ",
44
                            output_value="a,b,d another value")
45
        self.check_data_set(" a,b,d\\= another value ",
46
                            output_value="a,b,d\\= another value")
47
48
    def test_section_name_parsing(self):
49
        self.check_data_set(" [   a section name   ]      # with comment   \n",
50
                            'a section name',
51
                            output_comment="# with comment")
52
        self.check_data_set(" [   a section name]   ]         \n",
53
                            'a section name]')
54
        self.check_data_set(" [   a section name\\]   ]         \n",
55
                            'a section name]')
56
        self.check_data_set(" [   a section name\\;   ]         \n",
57
                            'a section name;')
58
59
        self.uut.section_name_surroundings["Section:"] = ''
60
        self.check_data_set("[  sec]; thats a normal section",
61
                            output_section="sec",
62
                            output_comment="; thats a normal section")
63
        self.check_data_set("  Section:  sEc]\\\\; thats a new section",
64
                            output_section="sEc]\\",
65
                            output_comment="; thats a new section")
66
        self.check_data_set("  Section:  sec]\\\\\\\\; thats a new section",
67
                            output_section="sec]\\\\",
68
                            output_comment="; thats a new section")
69
        self.check_data_set("  Section:  sec]\\\\\\; thats a new section",
70
                            output_section="sec]\\; thats a new section")
71
72
    def check_data_set(self,
73
                       line,
74
                       output_section="",
75
                       output_keys=None,
76
                       output_value='',
77
                       output_comment=''):
78
        output_keys = output_keys or []
79
80
        section_name, keys, value, comment = self.uut.parse(line)
81
82
        self.assertEqual(section_name, output_section)
83
        self.assertEqual(keys, output_keys)
84
        self.assertEqual(value, output_value)
85
        self.assertEqual(comment, output_comment)
86