]> mj.ucw.cz Git - eval.git/blob - t/moe/conf.test.py
Many fixes to config parser, many (passed) tests.
[eval.git] / t / moe / conf.test.py
1 import conf
2 from confparser import *
3 import logging as log
4 import unittest
5
6 class TestConfig(unittest.TestCase):
7   def setUp(s):
8     s.t = conf.ConfigTree()    
9   def parse(s, string, level=0, fname='test'):
10     c=ConfigParser(string, s.t, fname, level)
11     ops = c.parse()
12     c.p_WS()
13     assert c.eof()
14     return ops
15   def val(s, varname):
16     return s.t.lookup(varname, create=False).value()
17   def eqparse(s, string, *args, **kwargs):
18     return [(i[0], i[1].operation) for i in s.parse(string, *args, **kwargs)]
19
20 class TestParser(TestConfig):
21   s1 = r"""a="1";z{b='2';w{S.Tr_an-g.e='""'}};c.d='\n';e="{a}{b}";e+='{c.d}';a+="\"\n\{\}";f+='Z{a.b}'"""
22   s2 = '\t\n \n ' + s1.replace('=', '= \n ').replace(';', '\t \n\t \n ').replace('+=',' \n += ') + '\n\n '
23   def test_noWS(s):
24     assert len(s.parse(s.s1)) == 8
25   def test_noWS_COMMENT(s):
26     assert s.eqparse(s.s1+'#COMMENT') == s.eqparse(s.s1+'#') == s.eqparse(s.s1+'#\n') == s.eqparse(s.s1+'\n#')
27   def test_manyWS(s):
28     assert s.eqparse(s.s2) == s.eqparse(s.s1)
29   def test_manyWS_COMMENT(s):
30     assert s.eqparse(s.s2.replace('\n',' #COMMENT \n')) == s.eqparse(s.s2.replace('\n','#\n')) == s.eqparse(s.s1)
31   def test_empty(s):
32     assert s.eqparse('') == s.eqparse('\n') == s.eqparse('') == s.eqparse('a{}') == \
33       s.eqparse('a.b.c{if ""==\'\' {d.e{\n\n#Nothing\n}} }') == []
34   def test_syntax_errors(s):
35     s.assertRaises(ConfigSyntaxError, s.parse, "a=#")
36     s.assertRaises(ConfigSyntaxError, s.parse, "a='\"")
37     s.assertRaises(ConfigSyntaxError, s.parse, 'a="{a@b}"')
38     s.assertRaises(ConfigSyntaxError, s.parse, 'a="A{A"')
39   def test_error_location(s):
40     try: s.parse('\t \n  \n  { \n \n ')
41     except ConfigSyntaxError, e:
42       assert e.line == 3 and e.column in range(2,4)
43   def test_quoting(s):
44     s.parse(' a="\\"\\{a$b\\}\'\n\n\'{z}" ')
45     assert s.t.lookup('z', create=False)
46     # No escaping in '-string 
47     s.assertRaises(ConfigSyntaxError, s.parse, " a='\"\\'\n\n' ")
48     # Variable should not be created
49     s.parse(" a='{z2}' ")
50     s.assertRaises(conf.ConfigError, s.t.lookup, 'z2', create=False)
51   def test_conditions(s):
52     s.assertRaises(ConfigSyntaxError, s.parse, "if '{a}'=='{b}' and ''!='' {}")
53     s.parse('if ((#C\n (\n (not not not""!="")\n#C\n)\t ) ) {}')
54     s.parse('if (""=="" and not (not not ""!="" or ""=="")){}')
55     s.parse('if(""==""){a{if(""==""){if(""==""){b{if(""==""){if(""==""){}}}}}}}')
56     s.assertRaises(ConfigSyntaxError, s.parse, "if notnot'{a}'=='{b}' {}")
57     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' not and ''!='') {}")
58     s.assertRaises(ConfigSyntaxError, s.parse, "if ('{a}'=='{b}' ornot ''!='') {}")
59
60 class TestConfigEval(TestConfig):
61   def test_ops(s):
62     s.parse('c+="-C_APP"', level=20)
63     s.parse('a="A"; b="{a}-B"; c="C1-{b}-C2"; a+="FOO"; a="AA"')
64     assert s.val('c') == 'C1-AA-B-C2-C_APP'
65     s.parse('b+="-A:\{{a}\}";a+="A"', level=10)
66     assert s.val('c') == 'C1-AAA-B-A:{AAA}-C2-C_APP'
67   def test_nested(s):
68     s.parse('a="0"; b{a="1"; b{a="2"; b{a="3"; b{a="4"; b{a="5"}}}}}')
69     assert s.val('b.b.b.a') == '3'
70     s.parse('b.b{b.b{b.a="5MOD"}}')
71     assert s.val('b.b.b.b.b.a') == '5MOD'
72   def test_escape_chars(s):
73     s.parse(r"""a='{a}\\\\#\n'; b="{a}'\"\{\}"; c='\'; c+="\{{b}\}";""")
74     assert s.val('c') == r"""\{{a}\\\\#\n'"{}}"""
75
76 # TODO: conditions, chaining conditions, undefined (incl +=), loops, removal, fixing, fail on 1st April
77 # TODO: coverage
78
79 if __name__ == '__main__':
80   log.getLogger().setLevel(log.WARN)
81 #  log.getLogger().setLevel(log.DEBUG)
82   unittest.main()
83
84 # TODO: log.info('maxdepth: %d', conf.debug_maxdepth)
85